lihaitao
lihaitao

Reputation: 155

How to shorten this symbolic expression?

The symbolic expression below is the answer of some problem:

syms x y;
F = (6006059164170857*x^4)/36028797018963968 ...
    - (3741993627723215*x^3*y)/144115188075855872 ...
    - (3786059161694655*x^3)/576460752303423488 ...
    + (2057823154876729*x^2*y^2)/9007199254740992 ...
    + (7804706423002791*x^2*y)/36028797018963968 ...
    - (1579656551431947*x^2)/4503599627370496 ...
    - (5176864966130107*x*y^3)/576460752303423488 ...
    - (3350671128443929*x*y^2)/288230376151711744 ...
    - (2340405747630269*x*y)/72057594037927936 ...
    - (3122104315900301*x)/1152921504606846976 ...
    + (1757149312773205*y^4)/36028797018963968 ...
    - (5692299995057083*y^3)/576460752303423488 ...
    + (4054023049400589*y^2)/144115188075855872 ...
    - (434917661837037*y)/2251799813685248 ...
    - 2254148116991025/18014398509481984;

As you can see, it's too long to read, how could I shorten it to read easily?

Upvotes: 2

Views: 1007

Answers (2)

horchler
horchler

Reputation: 18484

You may have ended up with long integer like thin in the first place because you didn't create your symbolic equation in the best way. Compare the output of

sym(exp(pi))

to

exp(sym(pi))

Generally if you have any numeric constants in your symbolic equation that get transformed in complex ways (e.g., taking the exponential of them), you'll want to define them explicitly. If the constant is multiplied or added to a symbolic variable before being passed to the function then this may not be needed.

Additionally, you can use the simple and simplify functions to try to nicer versions of expressions. In your case:

G = simple(F)

returns

(192193893253467424*x^4 - 29935949021785720*x^3*y ...
- 7572118323389310*x^3 + 263401363824221312*x^2*y^2 ...
+ 249750605536089312*x^2*y - 404392077166578432*x^2 ...
- 10353729932260214*x*y^3 - 13402684513775716*x*y^2 ...
- 37446491962084304*x*y - 3122104315900301*x ...
+ 56228778008742560*y^4 - 11384599990114166*y^3 ...
+ 32432184395204712*y^2 - 222677842860562944*y ...
- 144265479487425600)/1152921504606846976

which is slightly shorter (it may be a lot nice if you do what I suggest above). You can then go from there to @pm89's excellent suggestions if needed.

Upvotes: 1

p8me
p8me

Reputation: 1860

vpa will do the numeric calculations as far as possible and returns the result with the precision defined by digits.

See also latex for latex representation of you symbolic expression,

digits(2) % Two digits precision
latex(vpa(F))

0.17\, x^4 - 0.026\, x^3\, y - \left(6.6\cdot 10^{-3}\right)\, x^3 + 0.23\, x^2\, y^2 + 0.22\, x^2\, y - 0.35\, x^2 - \left(9.0\cdot 10^{-3}\right)\, x\, y^3 - 0.012\, x\, y^2 - 0.032\, x\, y - \left(2.7\cdot 10^{-3}\right)\, x + 0.049\, y^4 - \left(9.9\cdot 10^{-3}\right)\, y^3 + 0.028\, y^2 - 0.19\, y - 0.13

and pretty for nicer presentation in command window.

pretty(vpa(F))
                              3                                             3                                                    3 
        4          3     6.6 x          2  2         2           2   9.0 x y             2               2.7 x          4   9.9 y           2 
  0.17 x  - 0.026 x  y - ------ + 0.23 x  y  + 0.22 x  y - 0.35 x  - -------- - 0.012 x y  - 0.032 x y - ----- + 0.049 y  - ------ + 0.028 y  - 0.19 y - 0.13 
                             3                                            3                                 3                   3 
                           10                                           10                                10                  10

Upvotes: 3

Related Questions