Search code examples
maxima

How to display a variable by symbols


Suppose I have

b:2;
c:3;
a:b+c;

Is there a way to display the definition of a, that is to display b+c instead of 5?


Solution

  • As you have stated the problem, no, because after the assignment, a doesn't have any association with b + c (only with the numerical value which was assigned).

    However, you can rephrase the problem so that you can recover the right-hand side of the assignment. For example:

    kill (a, b, c); /* remove any existing values */
    b : 2;
    c : 3;
    a : '(b + c);
      => c + b
    ''a;
      => 5
    a;
      => c + b
    
    kill (a, b, c);
    a : b + c;
      => c + b
    b : 2;
    c : 3;
    ''a;
      => 5
    a;
      => c + b
    

    In both cases, b + c is not substituted by the numerical values when it is assigned to a, either by preventing evaluation (first example) or by assigning before b and c are given numerical values. Finally here's another approach which avoids assigning numerical values to b and c:

    kill (a, b, c);
    a : b + c;
      => c + b
    subst ([b = 2, c = 3], a);
      => 5
    a;
      => c + b
    

    I think this last method, which uses subst, is preferable, because the other approaches use the quote-quote operator '' which is a little too subtle and therefore has an effect which might be surprising.