How to shorten this symbolic expression? - matlab

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?

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

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.

Related

What is the meaning of mod in quadray of Pari software

I need a polynomial that defines the narrow Hilbert class field of the real quadratic field x^2-505. How quadray could do this? quadray(x^2-505,-1)? What is the meaning of Mod in output?
x^4 + Mod(-19*y - 207, y^2 - y - 126)*x^3 + Mod(305*y + 3277, y^2 - y - 126)*x^2 + Mod(-1523*y - 16351, y^2 - y - 126)*x + Mod(21732 - y - 126)
Mod(y, y^2 - y - 126) means "a root of y^2 - y - 126". What you obtained is an equation with coefficients in the quadratic field Q[y]/(y^2 - y - 126) which is isomorphic to Q(sqrt(505)) since the discriinant of y^2 - y - 126 is 505, i.e., a relative extension of degree 4 over that quadratic field.
However, this is not the answer you're looking for: quadray(505,-1) just computes the ordinary Hilbert class field, not the narrow one. (The -1 is the conductor ideal and (-1) = (1).) Indeed, bnfnarrow(bnfinit(y^2-505)) tells you that you're looking for a degree 8 extension, not 4 ! The quadray function is not suitable, use
bnrclassfield(bnrinit(bnfinit(y^2-505), [1,[1,1]]))
which yields
x^8 + 2*x^7 + (-y + 2)*x^6 + (3*y + 77)*x^5 + (55/2*y + 1317/2)*x^4 + (16*y + 380)*x^3 + (-255/2*y - 5685/2)*x^2 + (95*y + 2125)*x + (650*y + 14625)
This is again a relative extension of degree 8 over Q(sqrt(505)), except this time y = sqrt(505), since I specified the base by the explicit polynomial y^2 - 505.
What [1, [1,1]] means is : a modulus (in the sense of class field theory), where we allow ramification at no finite place (the initial 1), but at both places at infinity (the [1,1]).

How to print symfunc in Octave so it looks human-typed as opposed of human-written?

I'm trying to see a laplace transform applied to a symbolic function, but it doesn't look like I would want to.
; file.m
syms t
f(t) = (3*t - 2) * cos(t);
laplace(f)
outputs
ans = (sym)
2 / 2 \
3*s - 2*s*\s + 1/ - 3
-----------------------
2
/ 2 \
\s + 1/
and I want
ans = (sym) (3*2^2 - 2*s*(s^2 + 1) - 3)/(s^2 + 1)^2
sympref display flat
See here: https://octave.sourceforge.io/symbolic/function/sympref.html
Also possibly useful: https://octave.sourceforge.io/symbolic/function/#sym/pretty.html

How to convert a symbolic expression to a Octave function from the Symbolic Package?

How to convert a symbolic expression to a Octave function from the Symbolic Package?
After installing the symbolic package on octave with pkg install -forge symbolic. Using the symbolic package on octave I may write this:
octave> pkg load symbolic;
octave> a = sym( "a" );
octave> int ( a^2 + csc(a) )
which will result in:
ans = (sym)
3
a log(cos(a) - 1) log(cos(a) + 1)
-- + --------------- - ---------------
3 2 2
But how to do this Integral (int(1)) symbolic result just above to became a valuable function like this below?
function x = f( x )
x = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2
end
f(3)
# Which evaluates to: 11.6463 + 1.5708i
I want to take the symbolic result from int ( a^2 + csc(a) ), and call result(3), to compute it at 3, i.e., return the numeric value 11.6463 + 1.5708i, from the symbolic expression integral a^2 + csc(a). Basically, how to use the symbolic expression as numerically evaluable expressions? It is the as this other question for Matlab.
References:
http://octave.sourceforge.net/symbolic/index.html
How do I declare a symbolic matrix in Octave?
Octave symbolic expression
Julia: how do I convert a symbolic expression to a function?
What is symbolic computation?
You can use pretty.
syms x;
x = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2;
pretty(x)
which gives this:
3
log(cos(x) - 1) log(cos(x) + 1) x
--------------- - --------------- + --
2 2 3
Update (Since the question is edited):
Make this function:
function x = f(y)
syms a;
f(a) = int ( a^2 + csc(a) );
x = double(f(y));
end
Now when you call it using f(3), it gives:
ans =
11.6463 + 1.5708i
it seems like you answered your own question by linking to the other question about Matlab.
Octave has an implementation of matlabFunction which is a wrapper for function_handle in the symbolic toolbox.
>> pkg load symbolic;
>> syms x;
>> y = x^3/3 + log( cos(x) - 1 )/2 - log( cos(x) + 1 )/2
y = (sym)
3
x log(cos(x) - 1) log(cos(x) + 1)
-- + --------------- - ---------------
3 2 2
>> testfun = matlabFunction(y)
testfun =
#(x) x .^ 3 / 3 + log (cos (x) - 1) / 2 - log (cos (x) + 1) / 2
testfun(3)
>> testfun(3)
ans = 11.6463 + 1.5708i
>> testfun([3:1:5]')
ans =
11.646 + 1.571i
22.115 + 1.571i
41.375 + 1.571i
>> testfun2 = matlabFunction(int ( x^2 + csc(x) ))
testfun2 =
#(x) x .^ 3 / 3 + log (cos (x) - 1) / 2 - log (cos (x) + 1) / 2
>> testfun2(3)
ans = 11.6463 + 1.5708i
>> testfun2([3:1:5]')
ans =
11.646 + 1.571i
22.115 + 1.571i
41.375 + 1.571i
I'm sure there are other ways you could implement this, but it may allow you to avoid hardcoding your equation in a function.

How to solve an equation with 3 unknown coefficients in Matlab?

I have this cubic spline function:
s(x) = 4 + k1*x + 2x^2 - (1/6)*x^3 for x in [0,1]
s(x) = 1 - (4/3)*(x-1) + k2*(x-1)^2 - (1/6) * (x-1)^3 for x in [1,2]
s(x) = 1 + k3*(x-2) + (x-2)^2 - (1/6) * (x-2)^3 for x in [2,3]
I would like to implement a simple function, that giving this function, it determines the coefficients k1, k2 and k3, but I couldn't do that..
Has anyone an idea?
Denoting the 3 functions as s0(x), s1(x) and s2(x), you can expand s1(x) and s2(x) and collect the coefficients for x^3, x^2, x and constant terms. Then, you get
s1(x)= 4 +k1*x+2*x^2-(1/6)*x^3 s2(x)=
(5/2+k2)+(-11/6-2k2)*x+(k2+1/2)*x^2-(1/6)*x^3 s3(x)= ..... (left our
for your own practice)
For these 3 functions to be all cubic polynomials, they should be identical. So, you can have
4 = 5/2 + k2
k1 = -11/6 - 2k2
2 = k2+1/2
from which you can get k1=-29/6, k2=3/2.
Compare the equations s0(x) and s2(x), you can also get k3 = 7/6.

Is it possible to visualise a code?

Is there any possibility to visualize(as written) codes that you put which contain mathematical formulas? I have some codes that I will use for some modelings but I can realise what she did since it's too long and complicated.
The closest to what you want I can think of is pretty and latex, that operate on symbolic expressions.
Example:
>> syms x
>> y = x^3/3 + 2*x^2 + x - 5
y =
x^3/3 + 2*x^2 + x - 5
>> pretty(y)
3
x 2
-- + 2 x + x - 5
3
>> latex(y)
ans =
\frac{x^3}{3} + 2\, x^2 + x - 5
The latter, pasted on an online latex interpreter, produces