= to ==, why? [SOLVED] - compsystems - 12-09-2016 05:53 PM
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;
RE: = to ==, why? - Han - 12-09-2016 07:58 PM
From a programming point of view, there must be a way of distinguishing the operation that tests whether two things are equal in value (==) and the operation that equates two objects (=) in a symbolic manner. So setting the variables x equal to y (i.e. making the two variables interchangeable) is different from testing if the quantity represented by x is equal to the quantity represented by y. This is really only meaningful in the CAS view. In Home view (non-CAS), there is generally no symbolic manipulation so that "=" and "==" are treated the same (i.e. both stand for testing if two values are equal).
Your CAS program uses non-CAS commands which forces "translation" since non-CAS commands generally do not know how to handle symbolic input; hence the translation to the more common programmatic representation for testing equality (i.e. ==). You also declared equ from the non-CAS side when you really wanted equ to be a CAS variable.
Why not just (within the CAS program) do
eq1:= a*x^2+b*x+c = 0;
eq1:= eq1*4*a;
print(eq1)
rather than passing things back and forth between your main CAS program and non-CAS subroutines? Here's a partially edited program which you can modify to fit your needs (and also to complete based on what is already there).
PHP Code:
export lineByLineFlag := 0; export equ; #cas deductionQuadFormula5():= begin local ansStr;
print; // Clear Terminal Window print( "***** Deduction Quadratic Formula *****" ); // Title freeze; wait(); choose_cas(); freeze; assume(a>0); print( "Quadratic Equation" ); eq1:= a*x^2+b*x+c=0; print( ">"+eq ); pause(); eq1 := eq1 * 4*a; print(eq1); pause(); eq1 := expand(eq1); print(eq1); pause(); eq1 := eq1 + b^2 - 4*a*c eq1 := simplify(eq1); print(eq1); pause(); eq1 := factor(eq1); print(eq1); pause(); eq1 := sqrt(eq1); eq1 := subst(eq1, abs(2*a*x+b)=2*a*x+b); print(eq1); pause(); end; #end
#cas str2expr( str ):= begin local step1; print( ">"+str ); step1:= replace( str, "answer", "(" + equ + ")" ); print( ">"+step1 ) ; equ := expr( step1 ); print( "" ); print( ""+ equ ); end; #end
export pause() begin if lineByLineFlag == 1 then print( " [PAUSE]" ); wait( ); else print( "" ); end; end;
export choose_cas() begin local keyPressedOnMenu := 0; local currentPos := 2; keyPressedOnMenu := choose(currentPos,"Pause every step", { "No", "Yes" }); if keyPressedOnMenu then if currentPos == 2 then print( "Any key to continue after [PAUSE]" ); lineByLineFlag := 1; else print( "Use cursor keys ↑↓ to move the output screen" ); lineByLineFlag:= 0; end; print( "" ); else kill; end; end;
RE: = to ==, why? - compsystems - 12-09-2016 10:15 PM
Han Wrote:... You also declared equ from the non-CAS side when you really wanted equ to be a CAS variable.
I also think that the problem may be in the global variable, but some statements work fine, it fails when it reaches SQRT.
It is possible to define global variables symbolic, as it does, matLab?
Quote:rather than passing things back and forth between your main CAS program and non-CAS subroutines?
A principle of programming is to split an algorithm into sub-functions, my code should work
Quote:Why not just (within the CAS program) do
eq1:= a*x^2+b*x+c = 0;
eq1:= eq1*4*a;
print(eq1)
The first version of my code is in, it works fine.
http://www.hpmuseum.org/forum/thread-7318.html
RE: = to ==, why? - Han - 12-10-2016 05:46 AM
(12-09-2016 10:15 PM)compsystems Wrote: Quote:rather than passing things back and forth between your main CAS program and non-CAS subroutines?
A principle of programming is to split an algorithm into sub-functions, my code should work
Quote:Why not just (within the CAS program) do
eq1:= a*x^2+b*x+c = 0;
eq1:= eq1*4*a;
print(eq1)
The first version of my code is in, it works fine.
http://www.hpmuseum.org/forum/thread-7318.html
Your first version worked fine because everything was kept on the CAS side. The problem is not that you used sub-functions. The problem is that you are using non-CAS commands (I erred and wrote 'subroutines' in my first response; but I meant commands) and therefore there is a lot of conversion/translation between CAS and non-CAS environments that are resulting in unwanted outputs. The problem is your equ:=expr(...) statement in your str2expr() subroutine. My modification of your code also uses sqrt() and does not exhibit the issue that you have.
EDIT: I still do not understand why you are doing all your calculations using strings when you have direct access to symbolic calculations by the fact that your program is a CAS program. It is as if you had an HP48 installed with an emulator, and the emulator is emulating the HP48. What is the reason for the seemingly unnecessary "extra layer" ?
RE: = to ==, why? - compsystems - 12-10-2016 07:07 PM
(12-10-2016 05:46 AM)Han Wrote: I still do not understand why you are doing all your calculations using strings when you have direct access to symbolic calculations by the fact that your program is a CAS program. It is as if you had an HP48 installed with an emulator, and the emulator is emulating the HP48. What is the reason for the seemingly unnecessary "extra layer" ?
The firt version "deductionQuadFormula_1()" (works fine), operates directly on symbolic expressions
The second version "deductionQuadFormula_2()" (works fine), to show intermediate steps, is required to convert it to string.
An alternative code may exist
The third version "deductionQuadFormula_3()", same as the previous one but using a subfunction to not repeat code (it does not work).
An alternative code may exist, Who can code it?
Codes updated in the first post
Comparison between the outputs of each version (1 & 2)
RE: = to ==, why? - Han - 12-10-2016 09:39 PM
In your third example, just comment out the export equ line (so that equ is created as a CAS variable) for a quick fix.
(12-10-2016 07:07 PM)compsystems Wrote: The second version "deductionQuadFormula_2()" (works fine), to show intermediate steps, is required to convert it to string. An alternative code may exist
Why are you converting the CAS operations into strings? You can simply print the strings that show what steps you are doing, and then evaluate the actual operations using the actual CAS commands rather than using expr(...).
Code:
print("> factor(answer)");
equ:=factor(equ);
print("> " + equ);
Quote:The third version "deductionQuadFormula_3()", same as the previous one but using a subfunction to not repeat code (it does not work). An alternative code may exist, Who can code it?
Unless the images provide information that your code itself cannot generate, there is really no need to include them because it makes your post that much more difficult to read. Also, please don't edit your original post in that manner. Instead, insert "EDIT:" and add more to your post. Otherwise, when existing responses to your original text it may no longer make sense since you have changed your original post.
Anyway, here is an incomplete example (run as quad(0) or quad(1) for no pause or with pause).
PHP Code:
#cas quad(p):= BEGIN local eq;
print("Quadratic Formula");
dostep('assume(a>0)',p); dostep('equ1:=a*x^2+b*x+c=0',p); dostep('equ1:=equ1*4*a',p); dostep('equ1:=expand(equ1)',p); dostep('equ1:=equ1+b^2-4*a*c',p); dostep('equ1:=simplify(equ1)',p); dostep('equ1:=factor(equ1)',p); // more steps here END;
dostep(cas,p):= begin print(cas); print(eval(cas)); // or use print("> " + eval(cas)) if p then print(" [PAUSE]"); wait(); else print(""); end; end; #end
Here's an alternative that prints something different from what is actually being computed.
PHP Code:
#cas quad(p):= BEGIN local eq;
print("Quadratic Formula");
dostep("assume(a>0)",'assume(a>0)',p); dostep("equ1:=a*ax^2+b*x+c=0",'equ1:=a*x^2+b*x+c=0',p); dostep("answer * 4*a",'equ1:=equ1*4*a',p); dostep("expand(answer)",'equ1:=expand(equ1)',p); dostep("answer + b^2-4*a*c",'equ1:=equ1+b^2-4*a*c',p); dostep("simpify(answer)",'equ1:=simplify(equ1)',p); dostep("factor(answer)",'equ1:=factor(equ1)',p); // more steps here END;
dostep(s,cas,p):= begin print("> "+s); print(eval(cas)); if p then print(" [PAUSE]"); wait(); else print(""); end; end; #end
RE: = to ==, why? - compsystems - 12-11-2016 03:08 PM
Thanks Han your codes work well, I already discovered the problem.
The problem is that when exporting variables they are operating in HOME mode.
My code works if you delete or comment the line
//export equ;
There should be a command to export symbolic variables
export symbolic equ;
|