Azzera filtri
Azzera filtri

How to store Numerical Expressions Without Evaluating

3 visualizzazioni (ultimi 30 giorni)
Im trying to write a piece of code that can convert any expression of many variables (which store a double data type in decimal form) into LaTex code, however im having difficulty because the expression c which stores symbolic variables seems to evaluate the expression numerically, which is undesirable.
Heres a current very basic version of my code
a = 293848383832882.29828582852 %Long numerical values are stored initially
b = 382929284.2828283
digitsOld = digits(2)
a_sym = sym(a,'d') %The values are converted to a decimal symbolic form (2 decimal places)
b_sym = sym(b,'d')
c = a_sym/b_sym % An equation relating the symbolic numbers is stored
L = latex(c)
Ideally, I would like the expression c to be stored as:
c = 2.9e+14/3.8e+8
So that the variable L could then store
L = '\frac{2.9e+14}{3.8e+8}'
However, I am currently receiving:
L = '7.6e+5'

Risposte (1)

Walter Roberson
Walter Roberson il 16 Ago 2020
There is no easy way to do this.
There are mechanisms inside the symbolic engine to postpone evaluation, so it is possible to internally form an unevaluated 1.1/2.2 (for example) . The internal mechanism for that is to call MuPAD's hold() function, passing in the expression whose evaluation is to be delayed. Each level of hold() delays evaluation once:
>> evalin(symengine, 'hold(1.1)/hold(2.2)')
ans =
0.5
>> evalin(symengine, 'hold(hold(1.2)/hold(2.2))')
ans =
hold(1.2)/hold(2.2)
However, as soon as you start trying to use it with expressions:
>> feval(symengine, 'hold', 1.1)
ans =
Warning: Unable to display symbolic object because 'symengine' was reset. Repeat commands to regenerate result.
> In sym/disp (line 44)
In sym/display>displayVariable (line 89)
In sym/display (line 51)
This is caused because MATLAB is unable to display the result of expressions that are wrapped with hold() and resets the symbolic engine. It is a display issue: you can store the result and process it further:
>> latex(feval(symengine, 'hold', 1.1))
ans =
'{_{\mathrm{symans\_}}}_{\left[2,2,64343\right]}'
But as you can see, the latex layer ends up using the internal representation of the objects used to interface between MATLAB and the symbolic engine...
>> latex(evalin(symengine, 'hold(hold(1.1/2.2))'))
ans =
'\mathrm{hold}\left(\frac{1.1}{2.2}\right)'
You have to be pretty careful:
>> latex(evalin(symengine, 'op(hold(hold(1.1/2.2)))'))
ans =
'\frac{1.1}{2.2}'
but to get that from expressions is not easy.
This shows that if you put in enough effort to construct character vectors of operations, then you can latex() them. It is not clear it is worth doing, though.
  3 Commenti
Walter Roberson
Walter Roberson il 16 Ago 2020
Modificato: Walter Roberson il 16 Ago 2020
Code the expression as a character vector with variables named in it. Then use text processing techniques to find the variable names and replace them with the text version of the contents of the variables. Wrap 'op(hold(hold( )))' around that. Evaluate with evalin(symengine) and latex() the result.
This will have challenges if some of the elements are non-scalar. It could also have challenges if some of the variables store symbolic expressions that you want expanded.
The technique will also have other challenges:
x = sym('y') + 1;
y = sym('x') + 1;
turn_it_into_latex('z = x + y')
should it produce 'z = x + 1 + y + 1' ? Should it produce 'z = x + y + 2' ? Should it infinite loop because each expansion of variables leaves another variable that should possibly be expanded?
Oliver Wan
Oliver Wan il 16 Ago 2020
Ok, thank you for your answers. It is much appreciated. I will try it out.

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by