I will have data which uses degrees for the trigonometric functions.
I will use sympy and since it uses radians for the trigonometric functions, I want to be able to convert and use degrees.
But, I will not know which symbol variable is been used as input to the trigonometric function.
So , I will have something like:
import sympy
from sympy import mpmath
a = 2
b = 30
a,b = sympy.symbols('a b')
expr = 'sqrt(a + cos(b))'
expres = sympy.sympify(expr)
print(expres.atoms(sympy.cos))
The b is 30 and it is in degrees (so, in the cos(b) computations it must be degrees, not radians as sympy expects).
But, as I said, another time the program runs, it will use another symbol variable for trigonometric.Or, more than one trigonometric functions.
I thought if using atoms in order to find if in the expression I have trigonometric functions.And then , somehow , use degrees instead of radians wherever the corresponding symbol variables is been used.
Is there a way to do this?
Arguably the most clean way to avoid this problem is to tell sympify to use custom definitions for selected symbols/strings, e.g., like this:
from sympy.abc import *
from sympy import sin, pi, Lambda, sympify
sin_degree = Lambda(x, sin(x*pi/180))
degree_trigs = {"sin": sin_degree}
expr_string = "sin(a) + sin(b**d + exp(c))"
expr = sympify(expr_string, locals=degree_trigs)
print(expr)
This returns:
sin(pi*a/180) + sin(pi*(b**d/180 + exp(c)/180))
You can use subs with functions, e.g., like this:
from sympy.abc import *
from sympy import sin, exp, pi, Lambda
sin_degree = Lambda(x, sin(x*pi/180))
expr = sin(a) + sin(b**d + exp(c))
print( expr.subs(sin,sin_degree) )
This returns:
sin(pi*a/180) + sin(pi*(b**d/180 + exp(c)/180))
Related
I don't understand why Sympy won't return to me the expression below simplified (not sure its a bug in my code or a feature of Sympy).
import sympy as sp
a = sp.Symbol('a',finite = True, real = True)
b = sp.Symbol('b',finite = True, real = True)
sp.assumptions.assume.global_assumptions.add(sp.Q.positive(b))
sp.assumptions.assume.global_assumptions.add(sp.Q.negative(a))
sp.simplify(sp.Max(a-b,a+b))
I would expect the output to be $a+b$, but Sympy still gives me $Max(a-b,a+b)$.
Thanks; as you can see I am a beginner in Sympy so any hints/help are appreciated.
Surely the result should be a + b...
You can do this by setting the assumptions on the symbol as in:
In [2]: a = Symbol('a', negative=True)
In [3]: b = Symbol('b', positive=True)
In [4]: Max(a - b, a + b)
Out[4]: a + b
You are trying to use the new assumptions system but that system is still experimental and is not widely used within sympy. The new assumptions are not used in core evaluation so e.g. the Max function has no idea that you have declared global assumptions on a and b unless those assumptions are declared on the symbols as I show above.
I'm working with Python Sympy, solving a quadratic, and wanting to print the result using LaTex. For example, if the result is x = (1 + sqrt(3))/2, I would like it to print via LaTex as \frac{1 + \sqrt{3}}{2}. However, Python Sympy either splits this into two fractions, as in \frac{1}{2} + \frac{\sqrt{3}}{2}, OR factors out the half, as in \frac{1}{2}(1 + \sqrt{3}). I have tried to return the numerator via sympy.fraction(expr) and have viewed other articles (Sympy - fraction manipulation and others), but none were able to produce the result.
Check out how to override the default printers.
import sympy
from sympy.printing.latex import LatexPrinter # necessary because latex is both a function and a module
class CustomLatexPrinter(LatexPrinter):
def _print_Add(self, expr):
n, d = expr.as_numer_denom()
if d == sympy.S.One:
# defer to the default printing mechanism
super()._print_Add(expr)
return
return r'\frac{%s}{%s}' % (sympy.latex(n), sympy.latex(d))
# doing this should override the default latex printer globally
# adopted from "Examples of overloading StrPrinter" in the sympy documentation
sympy.printing.latex = lambda self: CustomLatexPrinter().doprint(self)
print(sympy.printing.latex((1 + sympy.sqrt(3)) / 2)) # \frac{1 + \sqrt{3}}{2}
Im trying to compile an expression that contains an UndefinedFunction which has an implementation provided. (Alternatively: an expression which contains a Symbol which represents a call to an external numerical function)
Is there a way to do this? Either with autowrap or codegen or perhaps with some manual editing of the generated files?
The following naive example does not work:
import sympy as sp
import numpy as np
from sympy.abc import *
from sympy.utilities.lambdify import implemented_function
from sympy.utilities.autowrap import autowrap, ufuncify
def g_implementation(a):
"""represents some numerical function"""
return a*5
# sympy wrapper around the above function
g = implemented_function('g', g_implementation)
# some random expression using the above function
e = (x+g(a))**2+100
# try to compile said expression
f = autowrap(e, backend='cython')
# fails with "undefined reference to `g'"
EDIT:
I have several large Sympy expressions
The expressions are generated automatically (via differentiation and such)
The expressions contain some "implemented UndefinedFunctions" that call some numerical functions (i.e. NOT Sympy expressions)
The final script/program that has to evaluate the expressions for some input will be called quite often. That means that evaluating the expression in Sympy (via evalf) is definitely not feasible. Even compiling just in time (lambdify, autowrap, ufuncify, numba.jit) produces too much of an overhead.
Basically I want to create a binary python extension for those expressions without having to implement them by hand in C, which I consider too error prone.
OS is Windows 7 64bit
You may want to take a look at this answer about serialization of SymPy lambdas (generated by lambdify).
It's not exactly what you asked but may alleviate your problem with startup performance. Then lambdify-ied functions will mostly count only execution time.
You may also take a look at Theano. It has nice integration with SymPy.
Ok, this could do the trick, I hope, if not let me know and I'll try again.
I compare a compiled version of an expression using Cython against the lambdified expression.
from sympy.utilities.autowrap import autowrap
from sympy import symbols, lambdify
def wraping(expression):
return autowrap(expression, backend='cython')
def lamFunc(expression, x, y):
return lambdify([x,y], expr)
x, y = symbols('x y')
expr = ((x - y)**(25)).expand()
print expr
binary_callable = wraping(expr)
print binary_callable(1, 2)
lamb = lamFunc(expr, x, y)
print lamb(1,2)
which outputs:
x**25 - 25*x**24*y + 300*x**23*y**2 - 2300*x**22*y**3 + 12650*x**21*y**4 - 53130*x**20*y**5 + 177100*x**19*y**6 - 480700*x**18*y**7 + 1081575*x**17*y**8 - 2042975*x**16*y**9 + 3268760*x**15*y**10 - 4457400*x**14*y**11 + 5200300*x**13*y**12 - 5200300*x**12*y**13 + 4457400*x**11*y**14 - 3268760*x**10*y**15 + 2042975*x**9*y**16 - 1081575*x**8*y**17 + 480700*x**7*y**18 - 177100*x**6*y**19 + 53130*x**5*y**20 - 12650*x**4*y**21 + 2300*x**3*y**22 - 300*x**2*y**23 + 25*x*y**24 - y**25
-1.0
-1
If I time the execution times the autowraped function is 10x faster (depending on the problem I have also observed cases where the factor was as little as two):
%timeit binary_callable(12, 21)
100000 loops, best of 3: 2.87 µs per loop
%timeit lamb(12, 21)
100000 loops, best of 3: 28.7 µs per loop
So here wraping(expr) wraps your expression expr and returns a wrapped object binary_callable. This you can use at any time to do numerical evaluation.
EDIT: I have done this on Linux/Ubuntu and Windows OS, both seem to work fine!
I have a little question about sympy.
I did load the library with :
from sympy import *
At some point of my program I would like to evaluate a function.
x=Symbol('x', real=True)
sqrt(1-x).subs(x, 9).evalf()
>>> 2.82842712474619*I
Sympy answer me complex value but i would like an error as in basic python :
sqrt(-1)
>>> ValueError: math domain error
Someone know how to do that using sympy?
I may be wrong, but I don't think you can make it yell that way, because that's a scientific library so it is made for supporting imaginary numbers, but you can change it a bit:
x=Symbol('x', real=True)
v = sqrt(1-x).subs(x, 9).evalf()
if not v.is_real:
raise ValueError, "math domain error"
or you can create a function:
def assert_real(v):
if not v.is_real:
raise ValueError, "math domain error"
return v
so you can do:
x = Symbol('x', real=True)
v = assert_real(sqrt(1-x).subs(x, 9).evalf())
Using numpy, how can I do the following:
ln(x)
Is it equivalent to:
np.log(x)
I apologise for such a seemingly trivial question, but my understanding of the difference between log and ln is that ln is logspace e?
np.log is ln, whereas np.log10 is your standard base 10 log.
Correct, np.log(x) is the Natural Log (base e log) of x.
For other bases, remember this law of logs: log-b(x) = log-k(x) / log-k(b) where log-b is the log in some arbitrary base b, and log-k is the log in base k, e.g.
here k = e
l = np.log(x) / np.log(100)
and l is the log-base-100 of x
I usually do like this:
from numpy import log as ln
Perhaps this can make you more comfortable.
Numpy seems to take a cue from MATLAB/Octave and uses log to be "log base e" or ln. Also like MATLAB/Octave, Numpy does not offer a logarithmic function for an arbitrary base.
If you find log confusing you can create your own object ln that refers to the numpy.log function:
>>> import numpy as np
>>> from math import e
>>> ln = np.log # assign the numpy log function to a new function called ln
>>> ln(e)
1.0
from numpy.lib.scimath import logn
from math import e
#using: x - var
logn(e, x)
You could simple just do the reverse by making the base of log to e.
import math
e = 2.718281
math.log(e, 10) = 2.302585093
ln(10) = 2.30258093