(Mathematica)Show numbers instead of variables

In summary, the conversation discusses the difficulty of changing Mathematica's evaluation sequence and suggests alternative methods such as using Trace or temporarily disabling the Plus operator. Another suggestion is to turn complicated formulas into strings and perform string substitution for variable values. However, this approach may have limitations when dealing with functions and variables with similar names.
  • #1
hdmaniak
1
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
  • #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.
 
  • #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:
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.
 
  • #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.
 
  • #5


I understand the importance of using clear and concise notation in scientific projects. In Mathematica, you can use the "HoldForm" function to display numbers instead of variables in your output. This will prevent Mathematica from simplifying the expression and display it exactly as it is inputted.

For example, using your given input, you can write:

HoldForm[a+b+c]

This will output 6+10+2, as desired.

Additionally, you can use the "TraditionalForm" function to display the output in a more traditional mathematical notation. For your example, you can write:

TraditionalForm[a+b+c]

This will output a+b+c, which is equivalent to 6+10+2.

I hope this helps with your project. Good luck!
 

FAQ: (Mathematica)Show numbers instead of variables

What is the purpose of showing numbers instead of variables in Mathematica?

The purpose of showing numbers instead of variables in Mathematica is to simplify mathematical expressions and make them easier to understand and manipulate. It also allows for easier substitution and evaluation of numerical values.

How do I display numbers instead of variables in Mathematica?

To display numbers instead of variables in Mathematica, use the function N[]. This function converts any symbolic expression into a numerical one, allowing for the display of numbers instead of variables.

Can I switch back to displaying variables instead of numbers in Mathematica?

Yes, you can switch back to displaying variables instead of numbers in Mathematica by using the function Defer[]. This function prevents the evaluation of an expression and displays it in its original form.

Will using numbers instead of variables affect the accuracy of my calculations in Mathematica?

Using numbers instead of variables in Mathematica will not affect the accuracy of your calculations as long as you use the appropriate precision and accuracy settings. It is important to use the correct data types and set the precision and accuracy levels to ensure accurate results.

Are there any advantages to using numbers instead of variables in Mathematica?

Yes, there are several advantages to using numbers instead of variables in Mathematica. It allows for easier computation and manipulation of expressions, and can also improve the performance of your calculations. It can also make your code more readable and understandable to others.

Similar threads

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