Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

(Mathematica)Show numbers instead of variables

  1. Nov 3, 2011 #1
    Hello, I am doing some project and I need a little help from you.
    I am using variables, for example (very simple):
    input:
    a=6
    b=10
    c=2
    a+b+c
    Output: 18
    .. so how do I force mathematica to use otput like this:
    Output:6+10+2

    Because I am using comlicated formulas and I need to include formulas both with variables and numbers, thanks in advance
     
  2. jcsd
  3. Nov 3, 2011 #2
    Mathematica is an "infinite evaluation system." That means it keeps performing operations on the input you give it until the result stops changing. That also means it is sometimes very difficult to change Mathematica's whole philosophy about what it should do with your input.

    Trace might possibly be of use to you. For your very simple example the next to last item in the list of things returned from Trace is the 6+10+2 that you desire. Trace[a+b+c][[-2]] can show you that. But for complicated formulas the result will almost certainly not be in the form you are looking for.

    I suspect what you are looking for is a function that would take an expression, look at all the currently defined variables, substitute the values for each of those variables into the expression while not allowing Mathematica to make any other changes at all. I am not aware of anyone having written a function like that and I suspect that would not be a task for the ordinary Mathematica expert to tackle.
     
  4. Nov 3, 2011 #3
    What Bill says is correct - it's very hard to modify the evaluation sequence of mathematica so it stops after a certain number of steps. Basically you need to rewrite the evaluator: https://groups.google.com/forum/#!msg/comp.soft-sys.math.mathematica/6edVElPTkDE/7o9LHRERh1oJ

    However, you could temporarily disable Plus, e.g.

    Code (Text):
    In[1]:= a = 6; b = 10; c = 2;

    In[2]:= Block[{Plus = CirclePlus}, a + b + c]
            % /. CirclePlus -> Plus

    Out[2]= 6 ⊕ 10 ⊕ 2

    Out[3]= 18
     
    See the discussion at http://stackoverflow.com/q/4252789/421225 for more info.
     
  5. Nov 4, 2011 #4
    I really hesitate to get into this but, for the "complicated formulas" that you are looking at, I'm wondering if redefining all the operators and possibly even some of the functions you are using is going to become more and more of a mess.

    A different, but likely just as bad, method might be to turn the complicated formulas into strings and then do string substitution for your variable values.

    Your simple example might look like this

    In[1]:= a=6;b=10;c=2;
    StringReplace[ToString[HoldForm[a+b+c]],{ToString[HoldForm[a]]->ToString[a],
    ToString[HoldForm]->ToString,ToString[HoldForm[c]]->ToString[c]}
    ]

    Out[2]= 6 + 10 + 2

    The difficulty with this approach is that StringReplace can't tell the difference between i in Sin[] and the i in i^2.

    But perhaps you can find something of use in this idea.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: (Mathematica)Show numbers instead of variables
Loading...