(Mathematica)Show numbers instead of variables

  • Context: Mathematica 
  • Thread starter Thread starter hdmaniak
  • Start date Start date
  • Tags Tags
    Numbers Variables
Click For Summary

Discussion Overview

The discussion revolves around how to force Mathematica to display expressions with their variable values instead of evaluating them to a numerical result. Participants explore various methods to achieve this, particularly in the context of complicated formulas.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant notes that Mathematica is an "infinite evaluation system" and suggests using the Trace function to see intermediate steps, but warns that for complicated formulas, the desired output may not be achievable.
  • Another participant proposes temporarily disabling the Plus operator to prevent evaluation, suggesting a method using CirclePlus to achieve the desired output format.
  • A different approach is suggested involving converting complicated formulas into strings and using string substitution for variable values, although this method has limitations regarding variable identification.
  • Concerns are raised about the complexity and potential messiness of redefining operators and functions in Mathematica to achieve the desired output.

Areas of Agreement / Disagreement

Participants generally agree that modifying Mathematica's evaluation behavior is challenging, but they propose different methods to address the issue. There is no consensus on a single effective solution, and the discussion remains unresolved regarding the best approach.

Contextual Notes

Participants acknowledge limitations in their proposed methods, such as the inability of string replacement to distinguish between different uses of variables and the complexity of redefining operators in Mathematica.

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 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K