Taylor series in 2 or more variables (sympy) - sympy

I'm sure I've seen it somewhere...
How do I use the series function in sympy to find the Taylor series for some function like
f(x,y) = x^3+xy-2y^2.
Thank you
.

Related

How do I use FileTemplate as a substitute for Splice to export Mathematica expressions into Fortran?

I found the Splice functionality in Mathematica quite useful in the past. I am trying to insert mathematica expressions, formatted for Fortran, into Fotran code.
Does anyone have a small working example they would be willing to share? Thanks.
I could write Mathematica code
y=x^3
and construct a file test.fm with
program test
real x,y
x=1.0
y=
- <* y *>
write(6,*) "y",y
end
and the mathematica line
Splice["test.fm"]
would give a file test.f with
program test
real x,y
x=1.0
y=
- x**3
write(6,*) "y",y
end
Apparently this use of Splice is removed in recent Mathematica releases, and I get an error message
The function Splice with filename inputs is now obsolete and has been
superseded by FileTemplate.
I tried
FileTemplate["test.mf"]
but it returns something that apparently needs further output. I then tried
TemplateApply[FileTemplate["my.fm"]]
but this didn't work either.
FileTemplate yields a TemplateObject expression that you need to apply. Here is an example application using your code and StringTemplate, which similarly yields a TemplateObject expression.
Clear[x]
y = x^3
StringTemplate[" program test
real x,y
x=1.0
y=
- <* y *>
write(6,*) \"y\",y
end", InsertionFunction -> ToString#*FortranForm][<|"y" -> y|>]

Sympy: Symbolic Output L^p norm

I am trying to define a Sympy function $F(p,f)=|f|{L^p}$ symbolically. So it take Symbol("p") and Function f and output is in latex form of $|f|{L^p}$. Any idea of how to do it?
Thanks a lot!

Sympy simplify Euler's formula not working

I am doing my physics homework and tried to simplify an expression using the Euler formula. The minimal not-working example looks like this.
from sympy import *
x, phi = symbols("x varphi", real=True)
simplify(x * (E**(I*phi) + E**(-I*phi)))
My Jupiter notebook outputs the exact same thing back
While the desired expression using the Euler formula is
However, sympy actually knows how to use the Euler formula to represent the cosine function, because it outputs the simplified expression nicely when the x is removed:
simplify(E**(I*phi) + E**(-I*phi))
gives
Since the distributive property of multiplication apply to complex numbers, I don't see why sympy can't figure out the desired simplification of the first expression.
May be it is by design. As a workaround you can do
expr=x* (E**(I*phi) + E**(-I*phi))
expr.rewrite(cos)
which gives
2*x*cos(varphi)

How does the comma in line 2 translate to Fortran

lat2: =ASIN(SIN(lat1)*COS(d/ER) + COS(lat1)*SIN(d/ER)*COS(brng))
lon2: =lon1 + ATAN2(COS(d/ER)-SIN(lat1)*SIN(lat2), SIN(brng)*SIN(d/ER)*COS(lat1))
The above code is part of the code to start with Lat1 and Long1, travel azimuth and distance to arrive at Lat2 and Long2.
I am trying to convert the equations to Fortran, but I do not understand what to do with the comma. My current model is working for most test cases but is not correct when the distance crosses the 0 or 360 deg longitude line. The Long2 error is in stead of say + 10 deg E I get 350 deg E. I hope your model using the above equations handles the quadrant problem better.
ATAN2 is a Fortran function of two arguments. It is a common function that exists in several other programming languages as well, probably also in the language you are copying your lines from. You should have told us which language is that!
The function "computes the principal value of the argument function of the complex number X + i Y". The comma simply divides the first and the other argument.
Check, whether the language you are translating from uses the ATAN2 function in the same order for x and y as Fortran does. If not, switch the two arguments. Then simply call the Fortran function.
I do not understand this remark "hope your model using the above equations handles the quadrant problem better." Is it just some left-over from your private communication?

How to rewrite `sin(x)^2` to cos(2*x) form in Sympy

It is easy to obtain such rewrite in other CAS like Mathematica.
TrigReduce[Sin[x]^2]
(*1/2 (1 - Cos[2 x])*)
However, in Sympy, trigsimp with all methods tested returns sin(x)**2
trigsimp(sin(x)*sin(x),method='fu')
While dealing with a similar issue, reducing the order of sin(x)**6, I notice that sympy can reduce the order of sin(x)**n with n=2,3,4,5,... by using, rewrite, expand, and then rewrite, followed by simplify, as shown here:
expr = sin(x)**6
expr.rewrite(sin, exp).expand().rewrite(exp, sin).simplify()
this returns:
-15*cos(2*x)/32 + 3*cos(4*x)/16 - cos(6*x)/32 + 5/16
That works for every power similarly to what Mathematica will do.
On the other hand if you want to reduce sin(x)**2*cos(x) a similar strategy works. In that case you have to rewrite the cos and sin to exp and as before expand rewrite and simplify again as:
(sin(x)**2*cos(x)).rewrite(sin, exp).rewrite(cos, exp).expand().rewrite(exp, sin).simplify()
that returns:
cos(x)/4 - cos(3*x)/4
The full "fu" method tries many different combinations of transformations to find "the best" result.
The individual transforms used in the Fu-routines can be used to do targeted transformations. You will have to read the documentation to learn what the different functions do, but just running through the functions of the FU dictionary identifies TR8 as your workhorse here:
>>> for f in FU.keys():
... print("{}: {}".format(f, FU[f](sin(var('x'))**2)))
...
8<---
TR8 -cos(2*x)/2 + 1/2
TR1 sin(x)**2
8<---
Here is a silly way to get this job done.
trigsimp((sin(x)**2).rewrite(tan))
returns:
-cos(2*x)/2 + 1/2
also works for
trigsimp((sin(x)**3).rewrite(tan))
returns
3*sin(x)/4 - sin(3*x)/4
but not works for
trigsimp((sin(x)**2*cos(x)).rewrite(tan))
retruns
4*(-tan(x/2)**2 + 1)*cos(x/2)**6*tan(x/2)**2