Solving Equations with Python and Sympy and getting numerical answers - python

I'm trying to use sympy to solve equations, but I would like to get a straight numerical answer. My script is like this:
from sympy import *
A,B,V=symbols('A,B,V')
eq1=Eq(630.26*(V-39.0)*V*(V+39)-A+B,0)
eq2=Eq(B,1.36*10**8*(V-39))
eq3=Eq(A,5.75*10**5*V*(V+39.0))
solve([eq1,eq2,eq3], [A,B,V], dict=True)
It gives me a long list of solutions that are in very expanded form. As an example,
[{V: 304.107299632956 - (-5162698.06009073 + 3004043.12120894*I)**(1/3)*(-0.5 + 0.866025403784439*I) - 32920.4469842867/((-5162698.06009073 + 3004043.12120894*I)**(1/3)*(-0.5 + 0.866025403784439*I)), B: 36054592750.082 - 1245.8292864816*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) + 8.46536389385714e+17/((-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)*(1.0 - 1.73205080756888*I)) + 719.279873914469*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3), A: 97854838797.9765 - 3957.60119254414*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) - 3.13901978017549e-5*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) - 0.000285202926135405*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) + 2925.78725273524*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)}, {V: 304.107299632956 - (-5162698.06009073 + 3004043.12120894*I)**(1/3) - 32920.4469842867/(-5162698.06009073 + 3004043.12120894*I)**(1/3), B: -1.05776452046245e-5*(4.0015351858068e+22 - 136000000.0*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)*(25062979.0 - (-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)))/(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3), A: 97854838797.9765 - 3936.45368131564*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) + 5.56956529342379e+24/(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) + 6.43347823930771e-5*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) - 1.15822484655024e+18/(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)}, {V: 304.107299632956 - 32920.4469842867/((-5162698.06009073 + 3004043.12120894*I)**(1/3)*(-0.5 - 0.866025403784439*I)) - (-5162698.06009073 + 3004043.12120894*I)**(1/3)*(-0.5 - 0.866025403784439*I), B: 36054592750.082 + 8.46536389385714e+17/((-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)*(1.0 + 1.73205080756888*I)) + 719.279873914469*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) + 1245.8292864816*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3), A: 97854838797.9765 + 2.31644969310047e+18/((-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)*(1.0 + 1.73205080756888*I)) - 3.21673911965385e-5*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) + 5.57155558993486e-5*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3) - 1.11391305868476e+25/((-4.36224183723014e+21 + 2.53827793755398e+21*I)**(2/3)*(1.0 - 1.73205080756888*I)) + 1968.22684065782*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3) + 3409.06888884012*I*(-4.36224183723014e+21 + 2.53827793755398e+21*I)**(1/3)}]
I can of course evaluate them with evalf but not all at once. I'm looking for a clean way receive the solutions of the equation in numerical form. I've made a workaround function for now. If there's a better way, I'd really like to know. My function to print answers is as follows:
def printeqsolve(input):
for i in input:
for j in i:
print "%r:" %j, i[j].evalf(chop=True)
print "---"
I'd also like to exclude non-real solutions, but when I restrict my symbols to Real no solutions are found.

You could also use nsolve but need to give a "good enough" guess and cannot pass Eq instances:
>>> nsolve([e.lhs - e.rhs for e in eq1,eq2,eq3], [A,B,V], [0,0,0])
matrix(
[['4442890172.68209'],
['4289299466.1432'],
['70.5389666628177']])
>>> nsolve([e.lhs - e.rhs for e in eq1,eq2,eq3], [A,B,V], [1e5,1e4,1e3])
matrix(
[['266367838273.086'],
['84646784928.5322'],
['661.402830356854']])

In Python 3.8.3, it should be written as follows; otherwise, an error will be reported:
nsolve([e.lhs - e.rhs for e in (eq1,eq2,eq3)], [A,B,V], [0,0,0])
(eq1,eq2,eq3)

Related

Passing a matrix through multiple functions

A little complicated but I'll try to explain best I can, but I have values I am trying to calculate that are based on two other functions with multiple inputs. In the code below, my inputs are various theta values which then should create an array of m & n values. From the m & n arrays, I then need to calculate the various Q_bar terms which should output an array for each term as well.
theta = np.array([0, 25, -80, 90, 20])
m = math.cos(math.radians(theta)) #[deg]
n = math.sin(math.radians(theta)) #[deg]
Q_bar11 = Q11*(m**4) + 2*(Q12 + 2*Q66)*(n**2)*(m**2) + Q22*(n**4)
Q_bar12 = (Q11 + Q22 - 4*Q66)*(n**2)*(m**2) + Q12*(n**4 + m**4)
Q_bar16 = (Q11 - Q12 - 2*Q66)*n*(m**3) + (Q12 - Q22 + 2*Q66)*(n**3)*m
Q_bar22 = Q11*(n**4) + 2*(Q12 + 2*Q66)*(n**2)*(m**2) + Q22*(m**4)
Q_bar26 = (Q11 - Q12 - 2*Q66)*(n**3)*m + (Q12 - Q22 + 2*Q66)*n*(m**3)
Q_bar66 = (Q11 + Q22 - 2*Q12 - 2*Q66)*(n**2)*(m**2) + Q66*(n**4 + m**4)
I've seen similar posts about passing arrays through functions however I have not been successful in implementing them, any help would be much appreciated!
Instead of passing a list into function. pass each values differently might help!
deg = np.array([math.cos(math.radians(theta[i])) for i in range(5)])
news = pd.Series(theta,deg)
Sorry, I couldn't understand the q part exactly but if you explain it deeper than I'll try to help it too

Collect and substitute terms in very long and nested expressions with sympy

Short version:I want to collect and substitute some terms that I can clearly read in the expression but are not picked by sympy subs function.
I've done the symbolic computation in python, but in the end I will have to make these computation in C#. For this purpose, I'm trying to do some substitutions and partial numerical evaluations that I will hardcode in C#
As example this is one of the expressions (simple, I have to do this job on expressions ten times longer and with more levels of parenthesis):
from sympy import symbols
x,y,rho_0,v = symbols('x y rho_0 v')
expr = 4*x*(x**2 + y**2)*(7*(-1 + 2*(x**2 + y**2)/rho_0**2)**2 + 8 - 14*(x**2 + y**2)/rho_0**2)/rho_0**4 + (x**2 + y**2)**2*(56*x*(-1 + 2*(x**2 + y**2)/rho_0**2)/rho_0**2 - 28*x/rho_0**2)/rho_0**4
I don't know how to display equations in a better format here, sorry.
But the point is that I can clearly see that I can collect and substitute (x**2 + y**2)/rho_0**2 with very small manipulations
Using expr.subs((x**2 + y**2)/rho_0**2, v) has not given any result. I started using sympy last week so I don't know much yet, I think should try to navigate the expression from the innermost level of parenthesis, factorize and try to substitute, but I don't have any clue on how to do it.
subs has a hard time when a target contains an Add and is multiplied by a Rational. Targeting the Add first and continuing from there brings more success:
>>> expr
4*x*(x**2 + y**2)*(7*(-1 + (2*x**2 + 2*y**2)/rho_0**2)**2 + 8 - (14*x**2 +
14*y**2)/rho_0**2)/rho_0**4 + (x**2 + y**2)**2*(56*x*(-1 + (2*x**2 +
2*y**2)/rho_0**2)/rho_0**2 - 28*x/rho_0**2)/rho_0**4
Get the Rational separated from the Add
>>> factor_terms(expr)
4*x*(x**2 + y**2)*(7*(-1 + 2*(x**2 + y**2)/rho_0**2)**2 + 8 + 7*(-3 + 4*(x**2 +
y**2)/rho_0**2)*(x**2 + y**2)/rho_0**2 - 14*(x**2 + y**2)/rho_0**2)/rho_0**4
Do subs in two steps: make Add a Symbol and then Add/Pow the Symbol
>>> _.subs(x**2+y**2, v).subs(v/rho_0**2, v)
4*v*x*(7*v*(4*v - 3) - 14*v + 7*(2*v - 1)**2 + 8)/rho_0**2
Simplify if desired
>>> _.simplify()
4*v*x*(56*v**2 - 63*v + 15)/rho_0**2

Collecting like term of an expression in Sympy

I am currently dealing with functions of more than one variable and need to collect like terms in an attempt to simplify an expression.
Say the expression is written as follows:
x = sympy.Symbol('x')
y = sympy.Symbol('y')
k = sympy.Symbol('k')
a = sympy.Symbol('a')
z = k*(y**2*(a + x) + (a + x)**3/3) - k((2*k*y*(a + x)*(n - 1)*(-k*(y**2*(-a + x) + (-a + x)**3/3) + k*(y**2*(a + x) + (a + x)**3/3)) + y)**2*(-a + k*(n - 1)*(y**2 + (a + x)**2)*(-k*(y**2*(-a + x)))))
zEx = z.expand()
print type(z)
print type(zEx)
EDIT: Formatting to add clarity and changed the expression z to make the problem easier to understand.
Say z contains so many terms, that sifting through them by eye. and selecting the appropriate terms, would take an unsatisfactory amount of time.
I want to collect all of the terms which are ONLY a multiple of a**1. I do not care for quadratic or higher powers of a, and I do not care for terms which do not contain a.
The type of z and zEx return the following:
print type(z)
print type(zEx)
>>>
<class 'sympy.core.add.Add'>
<class 'sympy.core.mul.Mul'>
Does anyone know how I can collect the terms which are a multiple of a , not a^0 or a^2?
tl'dr
Where z(x,y) with constants a and k described by z and zEx and their type(): How can one remove all non-a terms from z AND remove all quadratic or higher terms of a from the expression? Such that what is left is only the terms which contain a unity power of a.
In addition to the other answers given, you can also use collect as a dictionary.
print(collect(zEx,a,evaluate=False)[a])
yields the expression
k*x**2 + k*y**2
In the end it is just an one-liner. #asmeurer brought me on the right track (check the comments below this post). Here is the code; explanations can be found below:
from sympy import *
from sympy.parsing.sympy_parser import parse_expr
import sys
x, y, k, a = symbols('x y k a')
# modified string: I added a few terms
z = x*(k*a**9) + (k**1)*x**2 - k*a**8 + y*x*(k**2) + y*(x**2)*k**3 + x*(k*a**1) - k*a**3 + y*a**5
zmod = Add(*[argi for argi in z.args if argi.has(a)])
Then zmod is
a**9*k*x - a**8*k + a**5*y - a**3*k + a*k*x
So let's look at this more carefully:
z.args
is just a collection of all individual terms in your expression (please note, that also the sign is parsed which makes things easier):
(k*x**2, a**5*y, -a**3*k, -a**8*k, a*k*x, a**9*k*x, k**2*x*y, k**3*x**2*y)
In the list comprehension you then select all the terms that contain an a using the function has. All these terms can then be glued back together using Add which gives you the desired output.
EDIT
The above returns all all the expressions that contain an a. If you only want to filter out the expressions that contain a with unity power, you can use collect and Mul:
from sympy import *
from sympy.parsing.sympy_parser import parse_expr
import sys
x, y, k, a = symbols('x y k a')
z2 = x**2*(k*a**1) + (k**1)*x**2 - k*a**8 + y*x*(k**2) + y*(x**2)*k**3 + x*k*a - k*a**3 + y*a**1
zc = collect(z2, a, evaluate=False)
zmod2 = Mul(zc[a], a)
then zmod2 is
a*(k*x**2 + k*x + y)
and zmod2.expand()
a*k*x**2 + a*k*x + a*y
which is correct.
With the updated z you provide I run:
z3 = k*(y**2*(a + x) + (a + x)**3/3) - k((2*k*y*(a + x)*(n - 1)*(-k*(y**2*(-a + x) + (-a + x)**3/3) + k*(y**2*(a + x) + (a + x)**3/3)) + y)**2*(-a + k*(n - 1)*(y**2 + (a + x)**2)*(-k*(y**2*(-a + x)))))
zc3 = collect(z3.expand(), a, evaluate=False)
zmod3 = Mul(zc3[a], a)
and then obtain for zmod3.expand():
a*k*x**2 + a*k*y**2
Is this the result you were looking for?
PS: Thanks to #asmeurer for all these helpful comments!
To iterate over the terms of an expression use expr.args.
I'm unclear what a is supposed to be, but the collect function may do what you want.

Python: pass a function call's list index as its own argument

I've got a rather sizable (3.9 kB) script that's designed to align some text based on some parameters, centering, etc.
Here are the blocks I'm looking to improve: (Apologies for the somewhat-codegolf but the point is not what the functions do but their structure: they work fine as it is in theory but I ache to make them shorter)
#manage + control the margin in spaces between the body text and right vertical rule
def calcMgn(lnNum): return toEven(bwinner-(len(LnOpn[lnNum])+len(LnCtn[lnNum])),-1)//2
def calcRMgn(lnNum): return (0-(1-(toEven(((bwinner-(len(LnOpn[lnNum])+len(LnCtn[lnNum])))//2),-1))))
def calcLenOf(lnNum): return len(LnMgn[lnNum])+len(LnOpn[lnNum])+len(LnCtn[lnNum])+calcRMgn(lnNum)
def calcRDiff(lnNum): return LnMgnR[lnNum] - (lenOf[lnNum] - bwinner)
def calcRMgnSpa(lnNum): return ((LnMgnRAdjust[lnNum])-adjust)
#there absolutely must be a better way to call a function based on its position in a list than the following:
LnMgn=[calcMgn(0)*spa,calcMgn(1)*spa,calcMgn(2)*spa,calcMgn(3)*spa]
LnMgnR=[calcRMgn(0),calcRMgn(1),calcRMgn(2),calcRMgn(3)]
lenOf=[calcLenOf(0),calcLenOf(1),calcLenOf(2),calcLenOf(3)]
LnMgnRAdjust=[calcRDiff(0),calcRDiff(1),calcRDiff(2),calcRDiff(3)]
LnMgnR_spa=[calcRMgnSpa(0)*spa,calcRMgnSpa(1)*spa,calcRMgnSpa(2)*spa,calcRMgnSpa(3)*spa,]
#take the lengths for a test drive to see if they break any rules
testLen=[LnMgn[0] + LnOpn[0] + spa + LnCtn[0] + LnMgnR_spa[0],\
LnMgn[1] + LnOpn[1] + spa + LnCtn[1] + LnMgnR_spa[1],\
LnMgn[2] + LnOpn[2] + spa + LnCtn[2] + LnMgnR_spa[2],\
LnMgn[3] + LnOpn[3] + spa + LnCtn[3] + LnMgnR_spa[3]] #instead of this, I want something like a for statement or ???
for i in range(0,3):
if len(testLen[i]) > bwinner:
LnMgnR_spa[i] = int((toEven(LnMgnRAdjust[i])-adjust)-(len(testLen[i])-bwinner))*str(spa)
#concatenate strings
addLine=[idt + vl + LnMgn[0] + LnOpn[0] + spa + LnCtn[0] + LnMgnR_spa[0] + vr + nl,\
idt + vl + LnMgn[1] + LnOpn[1] + spa + LnCtn[1] + LnMgnR_spa[1] + vr + nl,\
idt + vl + LnMgn[2] + LnOpn[2] + spa + LnCtn[2] + LnMgnR_spa[2] + vr + nl,\
idt + vl + LnMgn[3] + LnOpn[3] + spa + LnCtn[3] + LnMgnR_spa[3] + vr + nl]
I know SO's policy of "we won't write your code for you" and I definitely don't intend to ask for such favours, just guidance: is there a way (and I have googled, and googled) to make LnMgn and its similar arrays more efficient and less huge by its contents dynamically iteravely calling and self-defining based on their array index?
You can also create a list of functions:
func_list = [f1, f2, f3, f4]
and call them, for example, like:
[func_list[i](i) for i in range(4)]
This example would return:
[f1(0), f2(1), f3(2), f4(3)]
Just use a list comprehension. One example:
LnMgn = [calcMgn(index)*spa for index in range(4)]
You can do the same for all the others.

Choosing between different expression factorizations in SymPy

Say I have an expression as follows:
a*b*c + b*c + a*d
One could factorize it as:
b*(a*c + c) + (a*d)
or as
c*(a*b + b) + (a*d)
or as
a*d + b*c*(a + 1)
among other possibilities.
For other expressions, the # of possibilities can be much larger.
My question is, does SymPy have any utility that allows the user to choose which of them to display? Is there a way to specify the common factor/s to use when factorizing / grouping terms in an expression?
EDIT: As #user772649 points out below, I can use collect for this. However, collect seems to give different outputs depending on the initial factorization of the mathematical expression e.g.:
a,b,c,d = symbols("a,b,c,d")
# These two equations are mathematically equivalent:
eq1 = a*b*c + b*c + a*d
eq2 = a*d + b*c*(a + 1)
print collect(eq1, a)
print collect(eq2, a)
prints:
a*(b*c + d) + b*c
a*d + b*c*(a + 1)
The equations eq1 and eq2 are mathematically equivalent, but collect outputs a different factorization for each of them, despite of the fact that the call to the collect command was the same for both. This brings me to the following two questions:
Is there a way to "expand" an expression before calling collect?
Is there a way of "collecting" (factoring an expression) in a way that is invariant to the initial factorization without having to expand the expression first?
use collect():
from sympy import *
a,b,c,d = symbols("a,b,c,d")
eq = a * b * c + b * c + a * d
print collect(eq, b)
print collect(eq, c)
print collect(eq, b*c)
the output is:
a*d + b*(c + a*c)
a*d + c*(b + a*b)
a*d + b*c*(1 + a)
One thing that might be nice is if collect would collect on previously grouped sub-expressions if more than one symbol is given. But either giving a product to collect on (as #HYRY showed) or something like the following is possible:
def separatevars_additively(expr, symbols=[]):
from sympy import factor_terms
free = set(symbols) or expr.free_symbols
d = {}
while free:
f = free.pop()
expr, dep = expr.as_independent(f, as_Add=True)
if dep.has(*free):
return None
d[f] = factor_terms(dep)
if expr:
d[0] = expr
return d
var('a:d')
eq = a*b*c + b*c + a*d
def do(i):
return sum(separatevars_additively(eq,[i]).values())
for i in eq.free_symbols:
print('%s: %s' % (i, do(i)))
gives
b: a*d + b*c*(a + 1)
a: a*(b*c + d) + b*c
c: a*d + b*c*(a + 1)
d: a*b*c + a*d + b*c

Categories

Resources