How to avoid sym.symbols all the time - python

I am trying to use the symbolic libraries of the python Symp. I am getting lots of errors. How to overcome this. every time I don't want to type sym.symobls for defining some thing new.
from sympy import *
from math import *
W1, W2, W3,z1, z2, b, a,g,l = symbols('W1 W2 W3 z1 z2 b a g l')
l = b**2(g/sqrt(a));
#Beam Functions
W1 = simplify(c1*cos(b*x) + c2*sin(b*x) + c3*cosh(b*x) + c4*sinh(b*x));

The expression l = b**2(g/sqrt(a)) isn't valid. You need e.g. a * between the 2 and the opening bracket. Also note that after the assignment, l isn't a symbol anymore, but a symbolic expression.
You can avoid declaring many variables by using sympify(). That function gets a string as input and creates symbolic variables on the fly. With sympify a lot of standard functions also get their correct symbolic version.
As mentioned in the comments, from sympy import * can be problematic, especially if you also work with other libraries. For convenience, a lot of examples in the docs do use import *, but only for short programs which don't use other libraries.
Here is a simple standalone example which assigns a symbolic expression to l and shows the internal representation (note that the expression is placed into quotes):
from sympy import sympify, srepr
l = sympify("b**2*(g/sqrt(a))")
print(srepr(l))
W1 = sympify("simplify(c1*cos(b*x) + c2*sin(b*x) + c3*cosh(b*x) + c4*sinh(b*x))")
print(srepr(W1))
Output:
Mul(Pow(Symbol('a'), Rational(-1, 2)), Pow(Symbol('b'), Integer(2)), Symbol('g'))
Add(Mul(Symbol('c1'), cos(Mul(Symbol('b'), Symbol('x')))), Mul(Symbol('c2'), sin(Mul(Symbol('b'), Symbol('x')))), Mul(Symbol('c3'), cosh(Mul(Symbol('b'), Symbol('x')))), Mul(Symbol('c4'), sinh(Mul(Symbol('b'), Symbol('x')))))

Often, routines will allow string input so you can just refer to variables created automatically through sympifying an expression with a string:
>>> from sympy import S
>>> S('x')
x
>>> _.subs('x',42)
42
>>> S('2*x').coeff('x')
2
If all you are doing is working with single letter symbols and SymPy functions (and no other libraries) you can (and this is how I always start my sessions):
>>> from sympy.abc import *
>>> from sympy import *
I put the second import there so I get S as the shortcut to sympify rather than as symbol S.

Related

Define a function that is a derivative of a function

I was wondering if is there is a way to define a function that is a derivative of a function. I'm new to python so I don't no much, I tired looking up stuff that might be similar but nothing has worked so far. This is what I have for my code right now.
import sympy as sp
import math
x = sp.Symbol('x')
W = 15 #kN/m
E = 70 # Gpa
I = 52.9*10**(-6) #m**4
L = 3 #m
e = 0.01
xi = 1.8
y = 9
def f(x):
return ( ( y*3*(math.pi**4)*E*I/(W*L) ) - ( 48*(L**3)*math.cos(math.pi*x/(2*L)) ) + ( 48*(L**3) ) + ( (math.pi**3)*(x**3) ) )/(3*L*(math.pi**3))**(1/2)
def derv(f,x):
return sp.diff(f)
print (derv(f,x))
Also, I don't understand whatx = sp.Symbol('x') does, so if someone could explain that, that would be awesome.
Any help is appreciated.
You are conflating two different things: python functions like f and math functions, which you can express with sympy like y = π * x/3. f is a python function that returns a sympy expression. sympy lets you stay in the world of symbolic math functions by defining variables like x = sp.Symbol('x') So calling f() produces a symbolic math function like:
You can use sympy to find the derivative of the symbolic function returned by f() but you need to define it with the sympy versions of the cos() function (and sp.pi if you want to keep it symbolic).
For example:
import sympy as sp
x = sp.Symbol('x')
W = 15 #kN/m
E = 70 # Gpa
I = 52.9*10**(-6) #m**4
L = 3 #m
e = 0.01
xi = 1.8
y = 9
def f(x):
return ( ( y*3*(sp.pi**4)*E*I/(W*L) ) - ( 48*(L**3)*sp.cos(sp.pi*x/(2*L)) ) + ( 48*(L**3) ) + ( (sp.pi**3)*(x**3) ) )/(3*L*(sp.pi**3))**(1/2)
def derv(f,x):
return sp.diff(f(x)) # pass the result of f() which is a sympy function
derv(f,x)
You've programmed the function. it appears to be a simple function of two independent variables x and y.
Could be that x = sp.Symbol('x') is how SymPy defines the independent variable x. I don't know if you need one or another one for y.
You know enough about calculus to know that you need a derivative. Do you know how to differentiate a function of a single independent variable? It helps to know the answer before you start coding.
y*3*(math.pi**4)*E*I/(W*L) ) - ( 48*(L**3)*math.cos(math.pi*x/(2*L)) ) + ( 48*(L**3) ) + ( (math.pi**3)*(x**3) ) )/(3*L*(math.pi**3))**(1/2)
Looks simple.
There's only one term with y in it. The partial derivative w.r.t. y leaves you with 3*(math.pi**4)*E*I/(W*L) )
There's only one term with Cx**3 in it. That's easy to differentiate: 3C*x**2.
What's so hard? What's the problem?
In traditional programming, each function you write is translated to a series of commands that are then sent to the CPU and the result of the calculation is returned. Therefore, symbolic manipulation, like what we humans do with algebra and calculus, doesn't make any sense to the computer. Sympy gets around this by overriding Python's normal arithmetic operators, allowing you to do generate algebraic functions that can be manipulated similarly to how we humans do math. That's what sp.Symbols('x') is doing: providing you with a symbolic variable you can work with (you're also naming it in sympy).
If you want to evaluate your derivative, simply call evalf with the numerical value you want to assign to x.

Sympy substitutes

I am just a new user for SymPy. I am self learning this library for my undergrauate research. But in the middle of the process I am stuck with one code.
I have defined a function with a subscript.
U_n= x^n + 1/x^n
When I consider (U_1)^3 I get (substitute n=1)
(U_1)^3 = (x+1/x)^3
Then after simplifying this I get
(U_1)^3 = (x^3 + 1/x^3) + 3(x+ 1/x)
But one can see this answer as
(U_1)^3 = U_3 + 3U_1
How to get the output in terms of U_n 's ?
from sympy import *
from sympy import sympify
import sympy
x=symbols('x')
def u(n):
return x**n+1/x**n
def unu(eq1):
c = (eq1.subs(x, exp(x))).simplify()/2
return c.subs(cosh, Function('u')).subs(x,1)
from sympy import *
from sympy import sympify
import sympy
x=symbols('x')
def v(n):
return x**n-1/x**n
def vnu(eq2):
c = (eq2.subs(x, exp(x))).simplify()/2
return c.subs(sinh, Function('v')).subs(x,1)
This is my current code.I have built it to 2 separate U_n and V_n.But I cannot combine them.
Can someone please give an idea how to build this code using SymPy. It would be a very big help for my research.
Thank you very much.

How to add "*" inside an equation?

I'm new to python.
I want to make a calculator and I am facing a problem right now.
Here's a simplified code I am trying to make:
from math import *
input = "(2)(3)e(sqrt(49))pi" #This is an example of equation
equation = "(2)*(3)*e*(sqrt(49))*pi" #The output
How can I add " * " between every ")(", ")e", "e(", and others based on the equation so that I can eval (equation) without having to put "*" manually, just like real life math?
I have tried to do it by making a code like this:
from math import *
input = "(2)(3)e(sqrt(49))pi"
input = input.replace(")(", ")*(")
input = input.replace(")e", ")*e")
input = input.replace("e(", "e*(")
input = input.replace(")pi", ")*pi")
#^^^I can loop this using for loop^^^
equation = input
print(eval(equation))
This definitely only works in this equation. I can loop the replacing method but that would be very inefficient. I don't want to have 49 iterations to just check if 7 different symbols need "*" between it or not.
The issue you will encounter here is that "e(" should be transformed to "e*(" but "sqrt(" should stay. As comments have suggested, the best or "cleanest" solution would be to write a proper parser for your equation. You could put "calculator parser" into your favorite search engine for a quick solution, or if you are interested in over-engineering but learning a lot, you could have a look at parser generators such as ANTLr.
If, for some reason, neither of those are an option, a quick-and-dirty solution could be this:
import re
def add_multiplication_symbols(equation: str) -> str:
constants = ['e', 'pi']
constants_re = '|'.join(f'(?:{re.escape(c)})' for c in constants)
equation = re.sub(r'(\))(\(|\w+)', r'\1*\2', equation)
equation = re.sub(f'({constants_re})' + r'(\()', r'\1*\2', equation)
return equation
Then print(add_multiplication_symbols("(2)(3)e(sqrt(49))pi")) results in (2)*(3)*e*(sqrt(49))*pi.
The function makes use of the re module (regular expressions) to group the cases for all constants together. It tries to work around the issue I described above by defining a set of constant variables (e.g. "e" and "pi") by hand.

Can `sympy.codegen.ast` be used with `sympy.utilities.autowrap`?

I'm working on producing compiled functions using sympy.code.autowrap, specifically the make_routine, autowrap, and ufuncify functions of that module. I would like to be able to use sympy.codegen.ast to generate code to be wrapped. For instance:
import sympy as sy
from sympy.codegen.ast import (Assignment, CodeBlock, Return, Element,
Pointer, Declaration, float64,
FunctionPrototype, FunctionDefinition)
zlist = sy.symarray('z', (5,), real=True)
d1expz = sy.apply_finite_diff(1, x_list=range(-2,3), y_list=zlist)
d2expz = sy.apply_finite_diff(2, x_list=range(-2,3), y_list=zlist)
d1p = Pointer('d1p', float64)
d2p = Pointer('d2p', float64)
d1p0 = Element('d1p', '0')
d2p0 = Element('d2p', '0')
d12fpa = FunctionPrototype(float64, 'd12funca', [d1p, d2p] + list(zlist))
body12a = [
CodeBlock(
Assignment(d1p0, d1expz),
Assignment(d2p0, d2expz),
).cse(),
]
d12funca = FunctionDefinition.from_FunctionPrototype(
d12fpa, body12a
)
print(sy.ccode(d12funca))
This results in the following C code, which looks good:
double d12funca(double * d1p, double * d2p, double z_0, double z_1, double z_2, double z_3, double z_4){
x0 = (1.0/12.0)*z_0;
x1 = -1.0/12.0*z_4;
d1p[0] = x0 + x1 - 2.0/3.0*z_1 + (2.0/3.0)*z_3;
d2p[0] = -x0 + x1 + (4.0/3.0)*z_1 - 5.0/2.0*z_2 + (4.0/3.0)*z_3;
}
sympy.utilities.codegen.codegen also understands this, e.g. the following produces just the kind of results you'd expect.
[(c_name, c_code), (h_name, c_header)] = codegen(
('d12func',
[
sy.Eq(sy.Symbol('d1'), d1expz),
sy.Eq(sy.Symbol('d2'), d2expz)
]),
language='c'
)
I can also produce a nicely wrapped function that does essentially the same thing as follows:
from sympy.utilities.autowrap import autowrap
autowrap_dir = <some path>
derivs = sy.Matrix(
[[
d1expz,
d2expz
]]
)
dvraw = autowrap(
expr=derivs,
args=list(zlist),
backend='Cython',
tempdir=autowrap_dir,
verbose=True
)
But is there any way to use the ast module to specify functions to be wrapped with autowrap? It appears that autowrap, make_routine, and ufuncify cannot accept arbitrary code. The expressions they take can be either lists of straightforward sympy expressions, or lists of sympy.Eq expressions (in which case the wrapped function assigns the RHS of each equality to its LHS and returns each LHS as an output argument).
The problem is that I would like to be able to wrap more involved functions including such things as for loops and branching. The ast module provides the tools to write such functions. But as far as I can tell, autowrap and ast don't play with each other at all.
Any thoughts on how to accomplish this would be appreciated. Thanks.

Getting mathematical function as user's input

I need to know how transfer string input into executable function.
For example - user write string 'x*Sin(x**2)' and then programm takes it as function, can calculate a value for given x, can plot derivation of this function etc. I've read that there is module called scitools.stringfunction, but as far as I know this module is not callable in python-3.
Any ideas how to make it?
For Python 2.X
f = lambda x: input() # the user inputs: x**2 + 1
y = f(3)
print y # outputs: 10
For Python 3.X
f = lambda x: eval(input())
y = f(5)
print y
Just make sure to import the required mathematical functions. And make sure the user inputs a valid Python arithmetic expression.
using sympy you could do something like this:
from sympy import var
from sympy import sympify
x = var('x') # the possible variable names must be known beforehand...
user_input = 'x * sin(x**2)'
expr = sympify(user_input)
res = expr.subs(x, 3.14)
print(res) # -1.322...
if you want to turn the user input into a function you can call you could to this:
from sympy.utilities.lambdify import lambdify
f = lambdify(x, expr)
# f(3.14) -> -1.322...
sympy can do sybolic calculations (including derivatives); if you want to make plots i strongly suggest matplotlib.
the advantage of using a math library opposed to eval is that you do not need to sanitize the user input (against malicious code).
(deleted this thanks to a comment from ejm).

Categories

Resources