= to ==, why? [SOLVED]
Hello
The following code is a program that shows step by step, the deduction of the quadratic formula
version 1 without intermediate steps.
PHP Code:
//The following code is a program that shows step by step, the deduction of the quadratic formula // version 1 without intermediate steps. export lineByLineFlag := 0; //global symbolics output; #cas deductionQuadFormula_1():= begin local ansStr, equStr0, equStr1; local equ;
print; // Clear Terminal Window print( "***** Deduction Quadratic Formula *****" ); // Title print( "version 1 without intermediate steps" ); wait(); choose_cas(); assume(a>0); print( "Quadratic Equation" ); equ := ((a*x^2+b*x+c) = 0); output:={ equ }; print( ">"+equ ); output:= append( output, "→ "+equ); print( "assume(a>0)" );
pause();
ansStr := "answer * 4*a"; // ((a*x^2+b*x+c)*4*a) = 0 print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := equ * 4*a; print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause(); ansStr := "expand( answer )"; // (4*a^2*x^2 +4*a*b*x +4*a*c) = 0 print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := expand( equ ); print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause();
ansStr := "answer + b²"; // (4*a^2*x^2 +4*a*b*x + 4*a*c+b^2 ) = (b^2) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := equ+b^2; print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause(); ansStr := "answer - 4*a*c"; // (4*a^2*x^2 +4*a*b*x +4*a*c+b^2 -4*a*c) = (b^2 -4*a*c) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := equ-4*a*c; print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause(); ansStr := "simplify( answer )"; // (4*a^2*x^2 +4*a*b*x +b^2) = (-4*a*c +b^2) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := simplify(equ); print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause(); ansStr := "factor( answer )"; // (2*a*x+b)^2) = (-4*a*c+b^2) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := factor(equ); print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause(); ansStr := "√(answer)"; // (abs(2*a*x+b)) = (√(-4*a*c+b^2)) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := √(equ); print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause(); equ := expr( replace( string( equ ), "abs", "" ) ); // (2*a*x+b) = (√(-4*a*c+b^2)) print( string( equ ) ); pause(); ansStr := "answer - b" ; // (2*a*x+b-b) = (√(-4*a*c+b^2)-b) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := equ-b; print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause();
ansStr := "simplify( answer )"; // (2*a*x-b) = (√(-4*a*c+b^2)-2*b) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := simplify(equ); print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause();
ansStr := "answer/( 2*a )"; // (2*a*x/(2*a)) = (-b+√(-4*a*c+b^2))/(2*a) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := equ/(2*a); print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ); pause();
ansStr := "simplify( answer )"; print( ">"+ansStr ); output:= append( output, "> "+ansStr); equ := simplify(equ); print( "" ); print( ""+ equ ); output:= append( output, "→ "+equ);
pause(); ansStr := "replace( answer, \"b+\", \"b±\" )"; // x1 = (-b±√[b^2-4*a*c])/(2*a) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "string(" + equ + ")" ); equStr1 := expr( equStr0 ); print( "" ); print( ""+ equStr1 ); pause(); purge(a); return "done"; end; #end
// Not CAS prg export pause() begin if lineByLineFlag == 1 then //print( "____________________________[PAUSE]" ); wait( ); print( " [PAUSE]" ); wait( ); else print( "" ); end; end;
// Not CAS prg export choose_cas() begin local ok := 1; local cancel := 0; local keyPressedOnMenu := 0; local currentPos := 2; keyPressedOnMenu := choose(currentPos,"Pause every step", { "No", "Yes" }); if keyPressedOnMenu >= ok then if currentPos == 2 then print( "Any key to continue after [PAUSE]" ); print( "" ); lineByLineFlag := 1; else print( "Use cursor keys ↑↓ to move the output screen" ); print( "" ); lineByLineFlag:= 0; end;
else kill; end; end;
output
Quote:(a*x^2+b*x+c) = 0
> answer * 4*a
→ ((a*x^2+b*x+c)*4*a) = 0
> expand( answer )
→ (4*a^2*x^2+4*a*b*x+4*a*c) = 0
> answer + b ²
→ (4*a^2*x^2+4*a*b*x+4*a*c+b^2) = (b^2)
> answer - 4*a*c
→ (4*a^2*x^2+4*a*b*x+4*a*c+b^2-4*a*c) = (b^2-4*a*c)
> simplify( answer )
→ (4*a^2*x^2+4*a*b*x+b^2) = (-4*a*c+b^2)
> factor( answer )
→ ((2*a*x+b)^2) = (-4*a*c+b^2)
> √ (answer)
→ (abs(2*a*x+b)) = (√(-4*a*c+b^2))
> answer - b
→ (2*a*x+b-b) = (√(-4*a*c+b^2)-b)
> simplify( answer )
→ (2*a*x) = (-b+√(-4*a*c+b^2))
> answer/( 2*a )
→ (2*a*x/(2*a)) = ((-b+√(-4*a*c+b^2))/(2*a))
> simplify( answer )
→ x = ((-b+√(-4*a*c+b^2))/(2*a))
version 2 with intermediate steps.
PHP Code:
//The following code is a program that shows step by step, the deduction of the quadratic formula // version 2 with intermediate steps. export lineByLineFlag := 0; //global symbolics output; #cas deductionQuadFormula_2():= begin local ansStr, equStr0, equStr1; local equ;
print; // Clear Terminal Window print( "***** Deduction Quadratic Formula *****" ); // Title print( "version 2 with intermediate steps" ); wait(); choose_cas(); assume(a>0); print( "Quadratic Equation" ); equ := ((a*x^2+b*x+c) = 0); output:={ equ }; print( ">"+equ ); print( "assume(a>0)" );
pause();
ansStr := "answer * 4*a"; // ((a*x^2+b*x+c)*4*a) = 0 print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause(); ansStr := "expand( answer )"; // (4*a^2*x^2 +4*a*b*x +4*a*c) = 0 print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause();
ansStr := "answer + b²"; // (4*a^2*x^2 +4*a*b*x + 4*a*c+b^2 ) = (b^2) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause(); ansStr := "answer - 4*a*c"; // (4*a^2*x^2 +4*a*b*x +4*a*c+b^2 -4*a*c) = (b^2 -4*a*c) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause(); ansStr := "simplify( answer )"; // (4*a^2*x^2 +4*a*b*x +b^2) = (-4*a*c +b^2) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause(); ansStr := "factor( answer )"; // (2*a*x+b)^2) = (-4*a*c+b^2) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause(); ansStr := "√(answer)"; // (abs(2*a*x+b)) = (√(-4*a*c+b^2)) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause(); equ := expr( replace( string( equ ), "abs", "" ) ); // (2*a*x+b) = (√(-4*a*c+b^2)) print( string( equ ) ); pause(); ansStr := "answer - b" ; // (2*a*x+b-b) = (√(-4*a*c+b^2)-b) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause();
ansStr := "simplify( answer )"; // (2*a*x-b) = (√(-4*a*c+b^2)-2*b) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause();
ansStr := "answer/( 2*a )"; // (2*a*x/(2*a)) = (-b+√(-4*a*c+b^2))/(2*a) print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); pause();
ansStr := "simplify( answer )"; print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ );
pause(); ansStr := "replace( answer, \"b+\", \"b±\" )"; // x1 = (-b±√[b^2-4*a*c])/(2*a) print( ">"+ansStr ); equStr0:= replace( ansStr, "answer", "string(" + equ + ")" ); print( ">"+equStr0 ); equStr1 := expr( equStr0 ); print( "" ); print( ""+ equStr1 );
pause();
// ansStr := "expr( replace( string( answer ), \"x\", \"x1\" ))"; // x1 = (-b+√(-4*a*c+b^2))/(2*a) // print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); // pause();
// ansStr := "expr( replace( string( answer ), \"b+\", \"b-\" ))"; // x1 = (-b-√[b^2-4*a*c])/(2*a) // print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); // pause();
// ansStr := "expr( replace( string( answer ), \"x1\", \"x2\" ))"; // x2 = (-b-√(-4*a*c+b^2))/(2*a) // print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); // pause(); purge(a); return "done"; end; #end
// Not CAS prg export pause() begin if lineByLineFlag == 1 then //print( "____________________________[PAUSE]" ); wait( ); print( " [PAUSE]" ); wait( ); else print( "" ); end; end;
// Not CAS prg export choose_cas() begin local ok := 1; local cancel := 0; local keyPressedOnMenu := 0; local currentPos := 2; keyPressedOnMenu := choose(currentPos,"Pause every step", { "No", "Yes" }); if keyPressedOnMenu >= ok then if currentPos == 2 then print( "Any key to continue after [PAUSE]" ); print( "" ); lineByLineFlag := 1; else print( "Use cursor keys ↑↓ to move the output screen" ); print( "" ); lineByLineFlag:= 0; end;
else kill; end; end;
output
Quote:(a*x^2+b*x+c) = 0
> answer * 4*a
> ((a*x^2+b*x+c) = 0) * 4*a
→ ((a*x^2+b*x+c)*4*a) = 0
> expand( answer )
> expand( (((a*x^2+b*x+c)*4*a) = 0) )
→ (4*a^2*x^2+4*a*b*x+4*a*c) = 0
> answer + b ²
> ((4*a^2*x^2+4*a*b*x+4*a*c) = 0) + b ²
→ (4*a^2*x^2+4*a*b*x+4*a*c+b^2) = (b^2)
> answer - 4*a*c
> ((4*a^2*x^2+4*a*b*x+4*a*c+b^2) = (b^2)) - 4*a*c
→ (4*a^2*x^2+4*a*b*x+4*a*c+b^2-4*a*c) = (b^2-4*a*c)
> simplify( answer )
> simplify( ((4*a^2*x^2+4*a*b*x+4*a*c+b^2-4*a*c) = (b^2-4*a*c)) )
→ (4*a^2*x^2+4*a*b*x+b^2) = (-4*a*c+b^2)
> factor( answer )
> factor( ((4*a^2*x^2+4*a*b*x+b^2) = (-4*a*c+b^2)) )
→ ((2*a*x+b)^2) = (-4*a*c+b^2)
> √ (answer)
> √ ((((2*a*x+b)^2) = (-4*a*c+b^2)))
→ (abs(2*a*x+b)) = (√(-4*a*c+b^2))
> answer - b
> ((2*a*x+b) = (√(-4*a*c+b^2))) - b
→ (2*a*x+b-b) = (√(-4*a*c+b^2)-b)
> simplify( answer )
> simplify( ((2*a*x+b-b) = (√(-4*a*c+b^2)-b)) )
→ (2*a*x) = (-b+√(-4*a*c+b^2))
> answer/( 2*a )
> ((2*a*x) = (-b+√(-4*a*c+b^2)))/( 2*a )
→ (2*a*x/(2*a)) = ((-b+√(-4*a*c+b^2))/(2*a))
> simplify( answer )
> simplify( ((2*a*x/(2*a)) = ((-b+√(-4*a*c+b^2))/(2*a))) )
→ x = ((-b+√(-4*a*c+b^2))/(2*a))
version 3 with intermediate steps & SUBROUTINES (DOES NOT WORK)
SAME AS THE PREVIOUS VERSION, simply that the repeated code places it inside a subroutine.
The problem arises because the equation is rewritten a=b => a==b, And in this case the equation becomes a test. a==b -> false (0)
PHP Code:
//The following code is a program that shows step by step, the deduction of the quadratic formula // VERSION 3 WITH INTERMEDIATE STEPS & SUBROUTINES export lineByLineFlag := 0; //export equ; //global symbolics equ, output; #cas deductionQuadFormula_3():= begin local ansStr, equstr0, equStr1;
print; // Clear Terminal Window print( "***** Deduction Quadratic Formula *****" ); // Title print( "version 3 with intermediate steps & subroutines" ); wait(); choose_cas(); assume(a>0); print( "Quadratic Equation" ); equ := ((a*x^2+b*x+c) = 0); output:={ equ }; print( ">"+equ ); print( "assume(a>0)" );
pause();
ansStr := "answer * 4*a"; // ((a*x^2+b*x+c)*4*a) = 0 str2expr( ansStr ); pause(); ansStr := "expand( answer )"; // (4*a^2*x^2 +4*a*b*x +4*a*c) = 0 str2expr( ansStr ); pause();
ansStr := "answer + b²"; // (4*a^2*x^2 +4*a*b*x + 4*a*c+b^2 ) = (b^2) str2expr( ansStr ); pause(); ansStr := "answer - 4*a*c"; // (4*a^2*x^2 +4*a*b*x +4*a*c+b^2 -4*a*c) = (b^2 -4*a*c) str2expr( ansStr ); pause(); ansStr := "simplify( answer )"; // (4*a^2*x^2 +4*a*b*x +b^2) = (-4*a*c +b^2) str2expr( ansStr ); pause(); ansStr := "factor( answer )"; // (2*a*x+b)^2) = (-4*a*c+b^2) str2expr( ansStr ); pause(); ansStr := "√(answer)"; // (abs(2*a*x+b)) = (√(-4*a*c+b^2)) str2expr( ansStr ); pause(); equ := expr( replace( string( equ ), "abs", "" ) ); // (2*a*x+b) = (√(-4*a*c+b^2)) print( string( equ ) ); pause(); ansStr := "answer - b" ; // (2*a*x+b-b) = (√(-4*a*c+b^2)-b) str2expr( ansStr ); pause();
ansStr := "simplify( answer )"; // (2*a*x-b) = (√(-4*a*c+b^2)-2*b) str2expr( ansStr ); pause();
ansStr := "answer/( 2*a )"; // (2*a*x/(2*a)) = (-b+√(-4*a*c+b^2))/(2*a) str2expr( ansStr ); pause();
ansStr := "simplify( answer )"; str2expr( ansStr );
pause(); ansStr := "replace( answer, \"b+\", \"b±\" )"; // x1 = (-b±√[b^2-4*a*c])/(2*a) print( ">"+ansStr ); equStr0:= replace( ansStr, "answer", "string(" + equ + ")" ); print( ">"+equStr0 ); equStr1 := expr( equStr0 ); print( "" ); print( ""+ equStr1 );
pause();
// ansStr := "expr( replace( string( answer ), \"x\", \"x1\" ))"; // x1 = (-b+√(-4*a*c+b^2))/(2*a) // str2expr( ansStr ); // pause();
// ansStr := "expr( replace( string( answer ), \"b+\", \"b-\" ))"; // x1 = (-b-√[b^2-4*a*c])/(2*a) // str2expr( ansStr ); // pause();
// ansStr := "expr( replace( string( answer ), \"x1\", \"x2\" ))"; // x2 = (-b-√(-4*a*c+b^2))/(2*a) // str2expr( ansStr ); // pause(); purge(a); return "done"; end; // str2expr( ansStr ):= begin local equStr0; print( ">"+ansStr ); output:= append( output, "> "+ansStr); equStr0:= replace( ansStr, "answer", "(" + equ + ")" ); output:= append( output, "> "+equStr0); print( ">"+equStr0 ); equ := expr( equStr0 ); output:= append( output, "→ "+equ); print( "" ); print( ""+ equ ); end; #end
// Not CAS prg export pause() begin if lineByLineFlag == 1 then //print( "____________________________[PAUSE]" ); wait( ); print( " [PAUSE]" ); wait( ); else print( "" ); end; end;
// Not CAS prg export choose_cas() begin local ok := 1; local cancel := 0; local keyPressedOnMenu := 0; local currentPos := 2; keyPressedOnMenu := choose(currentPos,"Pause every step", { "No", "Yes" }); if keyPressedOnMenu >= ok then if currentPos == 2 then print( "Any key to continue after [PAUSE]" ); print( "" ); lineByLineFlag := 1; else print( "Use cursor keys ↑↓ to move the output screen" ); print( "" ); lineByLineFlag:= 0; end;
else kill; end; end;
|