Search code examples
maxima

How to output equation without evaluation, but with variables replaced to their values?


I need to make tons of simple computations and present each step in my report with predefined manner: (for ex i got B=2, C=3):

A=B+12-6/C^2; A=2+12-6/3^2=13.333;

I can get 1st block and answer like this:

B:2$ C:3$
A:'(B+12-6/C^2)$
print("A=",A,"; ","A= ??? =",ev(A, numer) );

and get:

      6
A= (- --) + B + 12 ;  A= ??? = 13.33333333333333 
       2
      C

What i need instead of '???' to get desired output?


Solution

  • Maxima distinguishes two parts of figuring out a result: evaluation and simplification. Evaluation = substituting one thing (the value) for another thing (a variable or a function). Simplification = applying mathematical identities to get a "simpler", equivalent result.

    In your problem, it appears you want to postpone simplification. You can say simp: false to do that. Here's one possible approach. I'll disable simplification, substitute values into the expression, print the substituted expression, and then re-enable simplification to get the final result.

    (%i2) expr: A=B+12-6/C^2;
                                  6
    (%o2)                  A = (- --) + B + 12
                                   2
                                  C
    (%i3) simp: false $
    
    (%i4) subst ([B = 2, C = 3], expr);
                                             - 2
    (%o4)                A = 12 + 2 + (- 6) 3
    (%i5) simp: true $
    
    (%i6) %o4;
                                     40
    (%o6)                        A = --
                                     3
    

    Note that many operations in Maxima happen by simplification (e.g. adding numbers together), so in general, Maxima will act noticeably different when simp is false. But in this case that's what you want.

    EDIT: OP points out that the result after substitution is displayed in a somewhat different from. The reason for this has to do with some obscure implementation details of Maxima. Be that as it may, it's possible to work around that behavior by using the Lisp substitution function SUBST (referenced in Maxima as ?subst) instead of Maxima subst. SUBST is a little different than Maxima subst; the syntax is ?subst(new_thing, old_thing, some_expression). After substituting via SUBST, it's necessary to resimplify explicitly; one way to do that is to say expand(..., 0, 0) (which doesn't expand anything, the only effect is to resimplify).

    (%i2) expr: A=B+12-6/C^2;
                                  6
    (%o2)                  A = (- --) + B + 12
                                   2
                                  C
    (%i3) simp: false $                                  
    
    (%i4) ?subst (3, C, ?subst (2, B, expr));
                                  6
    (%o4)                  A = (- --) + 2 + 12
                                   2
                                  3
    (%i5) simp: true $
    
    (%i6) expand (%o4, 0, 0);
                                     40
    (%o6)                        A = --
                                     3
    

    Since SUBST is has a different effect on the internal representation, it is possible you could create an invalid expression, for some choices of new_thing, old_thing, and some_expression. I won't try to sort that out here.