Mathematica (Mathematica)Show numbers instead of variables

  • Thread starter Thread starter hdmaniak
  • Start date Start date
  • Tags Tags
    Numbers Variables
AI Thread Summary
In Mathematica, obtaining an output that displays numbers instead of variables can be challenging due to its infinite evaluation system, which continuously processes inputs until a final result is reached. The Trace function can show intermediate steps, but for complex formulas, achieving the desired output format is difficult. One suggested method involves temporarily redefining the Plus operator to prevent further evaluation, allowing for a display of the expression with substituted values. Another approach is to convert formulas into strings and use string replacement for variable values, though this method has limitations in distinguishing between similar symbols. Overall, creating a function to achieve this output is complex and may require significant adjustments to Mathematica's evaluation process.
hdmaniak
Messages
1
Reaction score
0
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
 
Physics news on Phys.org
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.
 
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:
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.
 
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.
 

Similar threads

Replies
2
Views
2K
Replies
1
Views
2K
Replies
4
Views
3K
Replies
5
Views
3K
Replies
1
Views
1K
Replies
2
Views
2K
Back
Top