Passing a matrix through multiple functions - python

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

Related

How to extract common factors of trigonometric functions using sympy?

I am new to using sympy. I checked the tutorial on the official website. There is only the "sym.collect" function for extracting common factors, but it does not support the common factors of trigonometric functions.
Here is my code:
F0,w = symbols('F0,w')
m1,m2,c,k1,k2,t = symbols('m1,m2,c,k1,k2,t')
c1, c2, d1, d2 = symbols('c1,c2,d1,d2')
res = -F0*sin(t*w) + c*c1*w*cos(t*w) - c*c2*w*sin(t*w) - c*d1*w*cos(t*w) + \
c*d2*w*sin(t*w) + c1*k1*sin(t*w) + c1*k2*sin(t*w) - \
c1*m1*w**2*sin(t*w) + c2*k1*cos(t*w) + c2*k2*cos(t*w) - c2 * \
m1*w**2*cos(t*w) - d1*k2*sin(t*w) - d2*k2*cos(t*w)
sym.collect(res,sin)
turn out:
ValueError: keyfunc gave non-binary output
The result I want to get is:
sin(wt)*(-F0- c*c2*w + c*d2*w + ....) + cos(wt)*(c*c1*w + ...)
So how can I fix it?
The collection can be done by passing one or more expressions, too:
collect(mid, (sin(t*w), cos(t*w)))
I have solved it.
q = symbols('q')
mid = sym.collect(res1.subs(cos(t*w), q), q).subs(q, cos(t*w))

Filtering out/saving a term from an equation by using its factor (in multiplication)

I have the following equation/term:
1 + (a2*b2*c1 - a2*b2 + a2*c2 + 2)/λ + (a2*c2 + 1)/λ**2
I would like to save: (a2*c2 + 1) as a
and (a2*b2*c1 - a2*b2 + a2*c2 + 2) as b.
However, a and b vary each time. Hence, I need an algorithm which can separate a and b from the above equation using the "indicators" λ**2 and λ respectively. My idea is something along the lines: if multiplication contains λ**2 or λ then ...

Why is .subs not working in JupyterLab Python

I have a very long symbolic expression in JupyterLab which I am trying to substitute values in. The expression is very long so I won't elaborate on what it is unless it is necessary, but I have a simplified symbolic expression which outputs (this is using SymPy). I am trying to use the .subs command as seen below, but this is not replacing any of the variables I have and rather just outputting the same symbolic notation. I have used .subs before, but in a very limited capacity so I am not sure if there is a type issue or something like that. All of these variables represent those in the symbolic notation.
replacements=[(t1,np.pi/2), (t1d,1), (t1dd,1), (l1,1), (l2,1), (m1,1), (m2,1), (m3,1), (g,-10)]
Lagrangian = Lagrangian.subs(replacements)
display(Lagrangian)
These variables are defined in the following way using the SymPy command symbols and then are manipulated to get the following expression. Not all of these symbols defined are in the final expression and are intermediary symbols.
l1, l2= sp.symbols('l_1, l_2')
y1, y2, y3, y1d, y2d, y3d = sp.symbols('y_1, y_2, y_3, ydot_1, ydot_2, ydot_3')
t1, t2, t1d, t2d= sp.symbols('theta_1, theta_1, thetadot_1, thetadot_2')
t1dd, t2dd = sp.symbols('thetaddot_1, thetaddot_2')
m1, m2, m3 = sp.symbols('m_1, m_2, m_3')
I1, I2 = sp.symbols('I_1, I_2')
g = sp.symbols('g')
When it comes to Lagrangian, it is just a manipulatioon of alll of these variables. I will show some of the following manipulations down below, but I do not think that the are not very necessary in causing the issue.
I1 = 1/3*m1*l1**2
I2 = 1/3*m2*l2**2
t2 = sp.acos(l1/l2*sp.sin(t1))
t2d = sp.diff(t2, t1) * t1d
y1 = 0.5*l1*sp.cos(t1)
y1d = sp.diff(y1,t1) * t1d
y2 = l1*sp.cos(t1) + 0.5*l2*sp.cos(t2)
y2d = 2*y1d - 0.5*l2*sp.sin(t2)*t2d
y3 = l1*sp.cos(t1) + l2*sp.cos(t2)
y3d = 2*y1d + 2*y2d
T = 0.5*m1*y1d**2 + 0.5*m2*y2d**2 + 0.5*m3*y3d**2 + 0.5*I1*t1d**2 + 0.5*I2*t2d**2
U = m1*g*y1 + m2*g*y2 + m3*g*y3
print('T=')
display(sp.simplify(T))
print('U=')
display(sp.simplify(U))
dTdTheta_dot = sp.diff(T,t1d)
dTdTheta = sp.diff(T,t1)
dUdTheta = sp.diff(U,t1)
#d_dt =
print('dT/dtheta_dot=')
display(sp.simplify(dTdTheta_dot))
print('dT/dTheta=')
display(sp.simplify(dTdTheta))
print('dU/dTheta=')
display(sp.simplify(dUdTheta))
A = (1/3)*l1**4*l2**2*m2*t1d**2*sp.sin(t1)*sp.cos(t1)**3
dA = (1/3)*l1**4*l2**4*m2*(2*t1d*t1dd*sp.sin(t1)*sp.cos(t1)**3+t1d**2*sp.cos(t1)*t1d*sp.cos(t1)**3+
t1d**2*sp.sin(t1)*3*sp.cos(t1)**2*(-sp.sin(t1)*t1d))
B = l1**2*((1/3)*l2**2*m2*l1**2*t1d*sp.sin(t1)**3*sp.cos(t1)-(1/3)*l2**4*m2*t1d*sp.cos(t1)*sp.cos(t1))
dB = l1**2*(((1/3)*l1**2*l2**2*m2*t1dd*sp.sin(t1)**3*sp.cos(t1) - (1/3)*l1**2*l2**2*m2*t1d*3*sp.sin(t1)**2*sp.cos(t1)*t1d*sp.cos(t1) +
(1/3)*l1**2/l2**2*m2*t1d*sp.sin(t1)**3*(-sp.sin(t1))*t1d) - ((1/3)*l1**4*m2*t1dd*sp.sin(t1)*sp.cos(t1)+
(1/3)*l2**4*m2*t1d*sp.cos(t1)*t1d*sp.cos(t1) + (1/3)*l2**4*m2*t1d*sp.sin(t1)*(-sp.sin(t1))*t1d))
C = l1**2*(l1**4*t1d*sp.sin(t1)**4 - 2*l1**2*l2**2*t1d*sp.sin(t1)**2 + l2**4*t1d)*((1/8)*m1*sp.sin(2*t1) + (3/8)*m2*sp.sin(2*t1) - (1/2)*m2)
dC = l1**2 * ((l1**4*(t1dd*sp.sin(t1)**4 + t1d*4*sp.sin(t1)**3*sp.cos(t1)*t1d) - 2*l1**2*l2**2*(t1dd*sp.sin(t1)**2 + t1d*2*sp.sin(t1)*sp.cos(t1)*t1d)+l2**4*t1dd) *
((1/8)*m1*sp.sin(2*t1) + (3/8)*m2*sp.sin(2*t1) - (1/2)*m2*sp.cos(2*t1)) + (l1**4*t1d*sp.sin(t1)**4 - 2*l1**2*l2**2*t1d*sp.sin(t1) + l2**4*t1d) *
((1/8)*m1*sp.cos(2*t1)*2*t1d + (3/8)*m2*sp.cos(2*t1)*2*t1d - (1/2)*m2*(-sp.sin(2*t1)*2*t1d)))
D = (4*m2*sp.sin(2*t1)-3*m3*sp.cos(2*t1)) * (l1**2*sp.sin(t1)**2-l2**2)**2
dD = (4*m2*sp.cos(2*t1)*2*t1d-3*m2*(-sp.sin(2*t1))*2*t1d)*((l1**2*sp.sin(t1)**2-l2**2)**2) + (4*m2*sp.sin(2*t1)-3*m3*sp.cos(2*t1))*(2*(l1**2*sp.sin(t1)-l2**2)*(2*l1**2*sp.sin(t1)*sp.cos(t1)*t1d))
E = (l1**2*sp.sin(t1)**2-l2**2)**-2
dE = -2*(l1**2*sp.sin(t1)-l2**2)**-3*(2*l1**2*sp.sin(t1)*sp.cos(t1)*t1d)
F = (dA+dB+dC+dD)*E + (A+B+C+D)*dE
display(sp.simplify(F))
Lagrangian = F - dTdTheta + dUdTheta
display(sp.simplify(Lagrangian))
Does anyone know why .subs doesn't work? Any help would be much appreciated. Thanks in advance!

sympy: simplify a larger expression with Binomial formula and quadratic complement

I get some eigenvalues of the matrix
import sys
import mpmath
from sympy import *
X,Y,Z = symbols("X,Y,Z")
Rxy,Rxz, Ry,Ryx,Ryz, Rz,Rzy,Rzz = symbols("Rxy,Rxz, Ry,Ryx,Ryz, Rz,Rzy,Rzz")
J = Matrix([
[ -1, 0, 0],
[ 0, -Ry*Y, Ry*Rzy*Y],
[Rxz*Rz*Z, Ryz*Rz*Z, -Rz*Z]])
which are the following:
{-Ry*Y/2 - Rz*Z/2 + sqrt(Ry**2*Y**2 + 4*Ry*Ryz*Rz*Rzy*Y*Z - 2*Ry*Rz*Y*Z + Rz**2*Z**2)/2: 1,
-Ry*Y/2 - Rz*Z/2 - sqrt(Ry**2*Y**2 + 4*Ry*Ryz*Rz*Rzy*Y*Z - 2*Ry*Rz*Y*Z + Rz**2*Z**2)/2: 1,
-1: 1}
lets just look at eigenvalue one:
In [25]: J.eigenvals().keys()[0]
Out[25]: -Ry*Y/2 - Rz*Z/2 + sqrt(Ry**2*Y**2 + 4*Ry*Ryz*Rz*Rzy*Y*Z - 2*Ry*Rz*Y*Z + Rz**2*Z**2)/2
I want to simplify this term the following: factor out 1/2 and (this is important) the radicant.
I can transform the radicant as following by adding the quadratic complement
Ry**2*Y**2 + 4*Ry*Ryz*Rz*Rzy*Y*Z - 2*Ry*Rz*Y*Z + Rz**2*Z**2 | + 4*Ry*Rz*Y*Z -4*Ry*Rz*Y*Z
which leads to
Ry**2*Y**2 + Rz**2*Z**2 + 2*Ry*Rz*Y*Z - 4*Ry*Rz*Y*Z + 4*Ry*Ryz*Rz*Rzy*Y*Z
which can be factorized to
(Ry*Y + Rz*Z)**2 - 4*Ry*Rz*Y*Z*(1 - Ryz*Rzy)
with these evaluations the complete eigenvalue should look like this
-1/2*(Ry*Y + Rz*Z - sqrt((Ry*Y + Rz*Z)**2 - 4*Ry*Rz*Y*Z*(1 - Ryz*Rzy)))
This calculation is really important for me because I have to evaluate if the eigenvalue is <0. Which is much easier in the last form.
Let me show you what I did until now.
In [24]: J.eigenvals().keys()[0]
Out[24]: -Ry*Y/2 - Rz*Z/2 + sqrt(Ry**2*Y**2 + 4*Ry*Ryz*Rz*Rzy*Y*Z - 2*Ry*Rz*Y*Z + Rz**2*Z**2)/2
In [25]: J.eigenvals().keys()[0].factor()
Out[25]: -(Ry*Y + Rz*Z - sqrt(Ry**2*Y**2 + 4*Ry*Ryz*Rz*Rzy*Y*Z - 2*Ry*Rz*Y*Z + Rz**2*Z**2))/2
In [26]: J.eigenvals().keys()[0].simplify()
Out[26]: -Ry*Y/2 - Rz*Z/2 + sqrt(Ry**2*Y**2 + 4*Ry*Ryz*Rz*Rzy*Y*Z - 2*Ry*Rz*Y*Z + Rz**2*Z**2)/2
So .simplify() doesnt changes the result at all.
.factor() just factors out the -1/2.
If I remember correct, I can pass an argument to .factor() like Y or Z, which variable should be factorized. But I get a lot of slightly different eigenvalues as output and I don't want to specify each argument of factor() by hand (if this solution even works).
I also tried to calculate the eigenvalues by myself by calculating the determinant and solve determinat==0...
I also used determinat.factor() and solved it afterwards but the best result of this approach was the same as J.eigenvals().keys()[0].factor().
Do you have any idea how to solve this problem?
Thank you in advance
Alex
This sort of thing is asked for a lot (see also, for instance, this question: Expression simplification in SymPy), but there isn't really a good way in SymPy to do it. The problem is that such "partial" factorizations are not unique (there may be multiple ways to convert a polynomial into a sum of products).
I opened this issue about it in the SymPy issue tracker. I showed there a way that you can get close (here a is the term under the square root)
In [92]: collect(expand(a.subs(Ry*Y, x - Rz*Z)), x, func=factor).subs(x, Ry*Y + Rz*Z)
Out[92]:
2 2 2
- 4⋅Rz ⋅Z ⋅(Ryz⋅Rzy - 1) + 4⋅Rz⋅Z⋅(Ry⋅Y + Rz⋅Z)⋅(Ryz⋅Rzy - 1) + (Ry⋅Y + Rz⋅Z)
Here I am temporarily replacing Ry*Y + Rz*Z with a variable x so that I can get the squared term that you want.
I couldn't figure out a way to get closer to exactly what you want (i.e., factor the Ryz*Rzy - 1 out of the remaining terms).

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.

Categories

Resources