HP Forums
[CAS:HP-PPL] Total control of flags to successfully run a program - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: [CAS:HP-PPL] Total control of flags to successfully run a program (/thread-7291.html)



[CAS:HP-PPL] Total control of flags to successfully run a program - compsystems - 11-22-2016 09:56 PM

Hello, the following code fails if the "increasing" flag is set

The evolution of the HP-PPL is going in a right way, but this should improve.
The handling of expression with PART cmd is affected by the cas flags, for this reason I should be able to source code to set them

Expr -> 9 * x ^ 2 + 8 * x (increasing off)
Part (expr, 1) -> 9 * x ^ 2

Expr -> 8 * x + 9 * x ^ 2 (increasing on)
Part (expr, 1) -> 8 * x

I can not tell the user to prefix manually n flags before running the program, the control of flag must be automatic and by commands,
with the currently HP-PPL, I can not determine the status of some flags and the programs return incorrect answers
My recommendation is to complete the support of the flag shown in CAS settings ...

quadraticFormulaStepByStep source code
https://en.wikipedia.org/wiki/Quadratic_formula
PHP Code:
export exprout// only test
#cas
    
quadraticFormulaStepByStep():=
    
BEGIN
    
// version 0.9
    
local expr0expr1expr2expr3coef
    
local leftExprleftExpr1leftExpr2rightExprrightExpr1rightExpr2;
    
local part1part2part3;
    
local degree2degree1;
    
part1:=1part2:=2part3:=3;
    
degree2:=2degree1:=1;
    
purge(a,b,c,x);

    print;
    print( 
"DETERMINACIÓN DE LA FÓRMULA CUADRÁTICA" ); //Ecuación de segundo grado
    
print( "Sea la función cuadrática en una variable:" );
    
expr0:= a*x^2+b*x+c;
    print( 
"f(x) = " expr0 );
    print( 
"" ); 
    print( 
"Soluciones:" );
    print( 
"x12 =-b/(2 a) ±√[b² -4 a c]/(2 a)" );
    print( 
"" ); 
    print( 
"Para hallar la fórmula cuadrática, aplicamos el método de completar cuadrados. Veamos:" );

    print( 
"" ); 
    print( 
"Step0: hacemos f(x)=0" );
    
expr1:= expr0 0;
    print( 
string(expr1) );

    print( 
"" ); 
    print( 
"Step1: llevamos el termino independiente al lado derecho (restamos c) a ambos lados:" );
    
leftExpr := part(expr1,part1) - c// parte1 - c
    
rightExpr := part(expr1,part2) - c// parte2 - c
    
expr1:= leftExpr rightExpr;
    print( 
string(expr1) );
    print( 
"" );
    print( 
"Step1.1: simplificando a ambos lados de la ecuación:" );
    
expr1:= simplifyexpr1 );
    print( 
string(expr1) ); 

    print( 
"" ); 
    print( 
"Step2: llevando el coeficiente del termino cuadrático a 1; para esto se divide toda la ecuación por 'a'." );
    
expr1:= expr1/a;
    print( 
string(expr1) );

    print( 
"" ); 
    print( 
"Step2.1: distribuyendo el denominador en el lado izquierdo de la ecuación:" );
    
expr1:= expandexpr1 );
    print( 
string(expr1) ); 

    print( 
"" );
    print( 
"Step2.2: reorganizando coeficientes luego variable:" );
    
//print( "Step2.3: simplificando el primer término del lado izquierdo de la ecuación:" );
    
leftExpr:= part(expr1,part1); // a/a -> 1 

    
leftExpr1:= coeffpart(leftExpr,part1), degree2)*x^2//print( leftExpr1 ); wait();
    
leftExpr2:= coeffpart(leftExpr,part2), degree1)*x//print( leftExpr2 ); wait();
    
rightExpr:= part(expr1,part2); 
    
expr1:= leftExpr1 leftExpr2 rightExpr
    print( 
string(expr1) );

    print( 
"" ); 
    print( 
"Step3: Se extrae el coeficiente del termino lineal para completar cuadrados:" );
    
coef:= coeff(leftExpr2,1);
    print( 
"coeficiente: " coef ); 
    
coef:= simplify(coef/2); // b/a/2 -> b/(2*a)

    
print( "Se completa cuadrados en la ecuación:" );
    
leftExpr := leftExpr1 leftExpr2 coef^2;
    
rightExpr1:= coef^2;
    
rightExpr2:= rightExpr
    
rightExpr:= rightExpr1 rightExpr2;
    
expr1:= leftExpr rightExpr;
    print( 
string(expr1) );
    print( 
"" ); 
    print( 
"Step4: llevando el lado izquierdo como un trinomio cuadrado perfecto" );
    print( 
"y distribuyendo el cuadrado el lado derecho:" );
    
//factor
    
rightExpr:= simplify(rightExpr1) + rightExpr2;

    
expr2:= (x+coef); 
    
expr1:= expr2^rightExpr;
    print( 
string(expr1) );

    print( 
"" ); 
    print( 
"simplificando el lado derecho:" );
    
leftExpr:= part(expr1,part1);
    
rightExpr:= reorder(simplify(rightExpr),[b]); 
    
expr1:= leftExpr rightExpr;
    print( 
string(expr1) );

    print( 
"" );
    print( 
"Step5: extrayendo raíz cuadrada a la ecuación:" );
    
//expr1:= √(leftExpr) = √(rightExpr);
    
print( "√(" leftExpr ") =");
    print( 
"√(" rightExpr ")" );

    print( 
"" ); 
    print( 
"Step5.1: simplificando el lado izquierdo y distribuyendo √ lado derecho:" );
    
expr2:= part(rightExpr,part1); //print( "numerator " + expr2 );

    
expr3:= part (partrightExpr,part2), 1); //print( "denominator " + expr3 );

    
print( string(x+coef) + " = √[" expr2 "]/" "√[" expr3 "]" );

    print( 
"" ); 
    print( 
"Step5.2: simplificando lado derecho:" );
    
assume(a>0);
    
expr3:=simplify((expr3));
    print( 
string(x+coef) + " = √[" expr2 "]/" expr3 );

    print( 
"" ); 
    print( 
"Step5.3: Despejando x:" );
    print( 
"llevamos el termino independiente al lado derecho (restamos b/(2a)) a ambos lados:" );
    print( 
string(x+coef) + "-" coef " =" );
    print( 
string(-coef) + " + √[" expr2 "]/" expr3 );

    print( 
"" ); 
    print( 
"Step6: Simplificando" );
    print( 
string(x) + " = " + -coef " ± √[" expr2 "]/" expr3 );


    print( 
"" ); 
    print( 
"Step6.1: Agrupando" );
        
//

        
purge(a);
    
//exprout := expr1;
    
return "done";
    
//return exprout;
    
END;
#end 



[Image: quadraticFormulaStepByStep_image00.png]

Think if the output of the program previous, would be observed in pretty-print

[Image: quadraticFormulaStepByStep_image01.png]

Other problems with the previous code


History view

(b^2-4*a*c)/(4*a^2); [enter] -> (b^2-4*a*c)/(4*a^2) // ok // part 1 -> b^2-4*a*c , part 2 -> 4*a^2

part(Ans,1); [enter] -> b^2-4*a*c // ok

-----------------------------------
(b^2-4*a*c)/(4*a^2); [enter] -> (b^2-4*a*c)/(4*a^2) // ok

part(Ans,2); [enter] -> 1/(4*a^2) // Why? I expected 4*a^2, Does not agree with the show history view, with the calculated:

part(Ans,1); [enter] -> 4*a^2 Why? The above expression says 1/(4*a^2), // part 1 -> 1 part 2 -> 4*a^2

-----------------------------------

(b^2-4*a*c)/(4*a^2); [enter] -> (b^2-4*a*c)/(4*a^2) // ok

part(Ans,2); [enter] -> 1/(4*a^2) // part 1 -> 1 , part 2 -> 4*a^2

part(Ans,2); [enter] -> "Error: Bad Argument Value"