Sympy: Symbolic Output L^p norm - sympy

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!

Related

Taylor series in 2 or more variables (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
.

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)

Parsing series of formulas from a string using sympy

I have a pandas df that contains many string formulas that I would like to be able to parse and eventually solve. I came across parse_expr and initially seemed like it would work for my problem but now I'm not so sure.
An example string formula might look like this:
A = B + C; D = A*.2;
parse_expr would seem to work well if i had a system of equations and I may not be using this correctly. As it stands, parse_expr throws an "invalid syntax" error I believe because of the equal sign. Can anyone tell if its possible to solve this problem using parse_expr or if there is another approach I should try?
SymPy cannot parse a bunch of semicolon-separated formulas at once, so the string needs to be split first. It will need to be split again at =, assuming all formulas have = in them. After parsing each side of =, you can combine them with Eq, which is SymPy's equation object; or use them somehow else.
from sympy import S, Eq
str = "A = B + C; D = A*.2;"
result = [Eq(*map(S, f.split("="))) for f in str.split(";")[:-1]]
The result is [Eq(A, B + C), Eq(D, 0.2*A)]
I use S, short for sympify; parse_expr could be used similarly, and it has a few options that are not needed here.
parse_expr is based on the Python tokenizer, but it has several extensions. These extensions take the form of functions that take a list of tokens, a locals dictionary, and a globals dictionary, and return a modified list of tokens. These are passed as a tuple to parse_expr, like parse_expr(expression, transformations=(transformation1, transformation2, ...)).
It's probably easiest to just take a look at the source of the sympy.parsing.sympy_parser submodule to see the existing transformations and how they work. Some of the transformations that are there will probably be useful to you. In this case, you would want a transformation that transforms the = token into something else (actually there's already a transformation function convert_equals_sign in the sympy_parser submodule that does this). You assumedly also want to handle *. somehow.
I've also written a guide on Python tokenization which may be helpful here: https://www.asmeurer.com/brown-water-python
If your syntax is too far off from Python's then it will be challenging to use parse_expr, since it only works with Python's tokenizer. In that case, you'd need to generate your own grammar and parser (e.g., using antlr) for your DSL and parse it into something that can then be transformed into a SymPy expression.

SymPy does not evaluate Latex2SymPy conversion for sin(-x) or sin(pi)

I have been using Latex2SymPy for a while successfully to handle all sorts of LaTeX inputs, but I have been running into troubles with a few functions. For instance:
from latex2sympy.process_latex import process_sympy
from sympy import *
inputLatex = '\\sin{-x}\\sin{-x}'
trigsimp(process_sympy(inputLatex))
sin(x)**2
That works great: trigsimp handled the simplification well. Now, if I try:
inputLatex = '\\sin{-x}'
trigsimp(process_sympy(inputLatex))
sin(-x)
Even though this is obviously correct, I expected trigsimp() to give me -sin(x) as an answer. In fact, if I run trigsimp straight from a sympy expression:
trigsimp(sin(-x))
-sin(x)
This is what I expect. Even running sin(-x) without the trigsimp() command returns me -sin(x). I checked the object type of my process_sympy('\\sin{-x}') call, and it is 'sin'.
I thought this might be something related with the way x is transformed into a Symbol type, but I then tried putting pi in the sin function.
inputLatex = '\\sin{\\pi}'
trigsimp(process_sympy(inputLatex))
sin(pi)
If I run straight sin(pi), I get 0 as an answer, with or without trigsimp().
Can any of you shed a light on this?
Although it is probably too late, you can solve that issue by calling the doit() method in the sympy expression obtained from process_sympy(inputLatex).
When calling to process_sympy, it generates an unevaluated sympy expression, not a string. This expression cannot be evaluated just by calling sympify, since it has an attribute telling not to evaluate it.
You solved the problem by converting that expression to a string and then sympifying it, but a more direct way of doing it is just by using the doit() method in the expression:
inputLatex = '\\sin{-x}'
expr = process_sympy(inputLatex)
trigsimp(expr.doit())
would do the job.
I haven't solved the whole mystery yet, but I found a dirty workaround, which is to use sympify(). So if anyone else faces the same issue, here is what I did to get it to work:
inputLatex = '\\sin{-x}'
sympify(str(process_sympy(inputLatex)))
The answer now is -sin(x), what I wanted.
As for the sin(pi) thing, the issue is that process_sympy() cannot distinguish the pi as a symbol from pi as a number. I did the test here, and type(process_sympy('\pi')) returns Symbol type, and not a number. The same solution applies here.

Alglib library(C++)

I need help concerning libraries Alglib namely FFT. As I understand it, the input is a string, and the output is the same.
complex_1d_array z = "[1i,1i,1i,1i]"; - input
Can I make the input as an array? Thanks in advance.