non-commutative generators when using Sympy - python

%reset -f
from sympy import *
import sympy as sym
from sympy.solvers import solve
b1=sym.Array([[Rational(-1/2),sqrt(3)/2,0],[-sqrt(3)/2,Rational(-1/2),0],[0,0,1]])
Sigma=[]
List=[]
x, y, z = symbols('\sigma_{11} \sigma_{12} \sigma_{13}',commutative=False)
k, m, n = symbols('\sigma_{21} \sigma_{22} \sigma_{23}',commutative=False)
f, g, h = symbols('\sigma_{31} \sigma_{32} \sigma_{33}',commutative=False)
a1 = sym.Matrix([[x, y, z], [k,m,n], [f, g,h]])
for i in range(0,3):
for j in range(0,3):
args = []
lists=[]
for k in range(0,3):
for l in range(0,3):
x= symbols(('\sigma_{%d%d}')%(k+1,l+1),commutative=False)
M=sym.Array([x])
Sigmatotal_tmp=tensorproduct(b1[i][k],b1[j][l],M)
args.append(Sigmatotal_tmp[0])
Sigma.append(Add(*args))
When trying to execute solve(Sigma) I get the error
non-commutative generators: (\sigma_{11}, \sigma_{12},
\sigma_{13}, \sigma_{21}, \sigma_{22}, \sigma_{23}, \sigma_{31},
\sigma_{32}, \sigma_{33})
I tried several ways, they all give either error or wrong answer. Any help?

In your question here you say that you don't want x + y = y + x. This is not what SymPy means when it says something is commutative or not. If x and y are not commutative then x*y != y*x. I am not aware of anything in SymPy that will deal with the property that you are dealing with.
The solution to your equations (consistent with your hand solution indicated in comments) is the answer obtained in the original question.

Related

Evaluating the solution of sympy dsolve numerically

I would like to evaluate the solution of a differential equation against some x_test
array
from sympy import *
init_printing()
from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
# Constants
C, R, u_rest = symbols('C R u_rest')
f, g, h = symbols('f g h', cls=Function)
solution = dsolve(C*Derivative(f(x), x) + (1/R)*(f(x) - u_rest ),f(x))
x_text = np.array(range(0,100))
but I fail
# 1. attempt with evalf(Not working)
solution.args[1].evalf(subs={x: 3.14})
# 2. attempt with lambdify(Not working)
lambdify(x,solution.args[1])(3.14)
What is the right way to do it?
If you look at your solution.args[1] value you will see that it is an expression of several variables, including x. It will not evaluate to a number until you supply values for all variables. Your first attempt doesn't fail, but you don't explain why it is not giving you what you hoped for:
>>> solution.args[1].evalf(subs={x: 3.14})
u_rest*(1.0 - exp((C1 - 3.14/R)/C))

Getting the derivative of a function as a function with sympy (for later evaluation and substitution)

I want to work with generic functions as long as possible, and only substitute functions at the end.
I'd like to define a function as the derivative of another one, define a generic expression with the function and its derivative, and substitute the function at the end.
Right now my attempts is as follows, but I get the error 'Derivative' object is not callable:
from sympy import Function
x, y, z = symbols('x y z')
f = Function('f')
df = f(x).diff(x) # <<< I'd like this to be a function of dummy variable x
expr = f(x) * df(z) + df(y) + df(0) # df is unfortunately not callable
# At the end, substitute with example function
expr.replace(f, Lambda(X, cos(X))) # should return: -cos(x)*sin(z) - sin(y) - sin(0)
I think I got it to work with integrals as follows:
I= Lambda( x, integrate( f(y), (y, 0, x))) but that won't work for derivatives.
If that helps, I'm fine restricting myself to functions of a single variable for now.
As a bonus, I'd like to get this to work with any combination (products, derivatives, integrals) of the original function.
It's pretty disappointing that f.diff(x) doesn't work, as you say. Maybe someone will create support it sometime in the future. In the mean time, there are 2 ways to go about it: either substitute x for your y, z, ... OR lambdify df.
I think the first option will work more consistently in the long run (for example, if you decide to extend to multivariate calculus). But the expr in second option is far more natural.
Using substitution:
from sympy import *
x, y, z = symbols('x y z')
X = Symbol('X')
f = Function('f')
df = f(x).diff(x)
expr = f(x) * df.subs(x, z) + df.subs(x, y) + df.subs(x, 0)
print(expr.replace(f, Lambda(X, cos(X))).doit())
Lambdifying df:
from sympy import *
x, y, z = symbols('x y z')
X = Symbol('X')
f = Function('f')
df = lambda t: f(t).diff(t) if isinstance(t, Symbol) else f(X).diff(X).subs(X, t)
expr = f(x) * df(z) + df(y) + df(0)
print(expr.replace(f, Lambda(X, cos(X))).doit())
Both give the desired output.

indexing in lambda function to optimize multiple variables of a sum

I want to solve an optimization problem as proposed in this thread. Now, I not only want to solve for the x[1]...x[n], but also for the variable y. It looks like something is wrong with the indexing.
from sympy import Sum, symbols, Indexed, lambdify
from scipy.optimize import minimize
import numpy as np
def _eqn(y, variables, periods, sign=-1.0):
x, i = symbols("x i")
n = periods-1
s = Sum(Indexed('x', i)/(1+0.06)**i, (i, 1, n))
f = lambdify(x, s, modules=['sympy'])
return float(sign*(y + f(variables)))
z = 3
results = minimize(lambda xy: _eqn(xy[0], xy[1:z], z),np.zeros(z))
print(results.x)
From the error message it seems there is an issue in your indexing. The summation runs from 1 to n but by default indexing of list type objects in Python goes from 0 to n-1. If I change this in your code it seems to work. Check it out.
import sympy as sp
from scipy.optimize import minimize
import numpy as np
sp.init_printing()
def _eqn(y, variables, periods, sign=-1.0):
x, i = sp.symbols("x i")
n = periods-1
s = sp.Sum(sp.Indexed('x', i)/(1+0.06)**(i+1), (i, 0, n-1)).doit()
f = sp.lambdify(x, s, modules=['sympy'])
return float(sign*(y + f(variables)))
z = 3
results = minimize(lambda xy: _eqn(xy[0], xy[1:z], z),np.zeros(z))
print(results.x)
If all you need is variable number of arguments for minimization then does the following code work for you?
from sympy import var
from scipy.optimize import minimize
import numpy as np
def _eqn(y, variables, periods, sign=-1.0):
f = 0
for i,x in enumerate(variables):
f += x/(1+0.06)**(i+1)
return float(sign*(y + f))
z = 3
results = minimize(lambda xy: _eqn(xy[0], xy[1:z], z),np.zeros(z))
print(results.x)

Partial symbolic derivative in Python

I need to partially derivate my equation and form a matrix out of the derivatives. My equation is:
While this conditions must be met:
For doing this I've used the sympy module and its diff() function. My code so far is:
from sympy import*
import numpy as np
init_printing() #delete if you dont have LaTeX installed
logt_r, logt_a, T, T_a, a_0, a_1, a_2, logS, Taa_0, Taa_1, Taa_2 = symbols('logt_r, logt_a, T, T_a, a_0, a_1, a_2, logS, Taa_0, Taa_1, Taa_2')
A = (logt_r - logt_a - (T - T_a) * (a_0 + a_1 * logS + a_2 * logS**2) )**2
parametri = [logt_a, a_0, Taa_0, a_1, Taa_1, a_2, Taa_2]
M = expand(A)
M = M.subs(T_a*a_0, Taa_0)
M = M.subs(T_a*a_1, Taa_1)
M = M.subs(T_a*a_2, Taa_2)
K = zeros(len(parametri), len(parametri))
O = []
def odv(par):
for j in range(len(par)):
for i in range(len(par)):
P = diff(M, par[i])/2
B = P.coeff(par[j])
K[i,j] = B
return K
odv(parametri)
My result:
My problem
The problem that I'm having is in the partial derivatives of products (T_aa_0, T_aa_1 and T_a*a_2), because by using the diff() function, you cannot derivate a function with a product (obviously), else you get an error:
ValueError:
Can't calculate 1-th derivative wrt T_a*a_0.
To solve this I substitued this products with coefficients, like:
M = M.subs(T_a*a_0, Taa_0)
M = M.subs(T_a*a_1, Taa_1)
M = M.subs(T_a*a_2, Taa_2)
But as you can see in the final result, this works only in some cases. I would like to know if there is a better way of doing this where I wouldn't need to substitude the products and that it would work in all cases.
ADDITIONAL INFORMATION
Let me rephrase my question. Is it possible to symbolically derive an equation with a function by using python or in that matter, to use the sympy module?
So I've managed to solve my problem on my own. The main question was how to symbolically derive a function or equation with another function. As I've gone again slowly over the sympy documentation, I saw a little detail, that I've missed before.
In order to derive a function with a function you need to change the settings of the function, that will be used to derive. For example:
x, y, z = symbols('x, y, z')
A = x*y*z
B = x*y
# This is the detail:
type(B)._diff_wrt = True
diff(A, B)
Or in my case, the code looks like:
koef = [logt_a, a_0, T_a*a_0, a_1, T_a*a_1, a_2, T_a*a_2]
M = expand(A)
K = zeros(len(koef), len(koef))
def odvod_mat(par):
for j in range(len(par)):
for i in range(len(par)):
type(par[i])._diff_wrt = True
P = diff(M, par[i])/2
B = P.coeff(par[j])
K[i,j] = B
#Removal of T_a
K[i,j] = K[i,j].subs(T_a, 0)
return K
odvod_mat(koef)
Thanks again to all that were taking their time to read this. I hope this helps to anyone, who will have the same problem as I did.

passing a symbolic matrix to sympy.lambdify

I want to create a function of a symbolic matrix in sympy and then lambdify it for evaluation in numpy. Let's try the simple function that returns exactly what I feed it f(x) = x, but eventually Id like to do something like take the trace of the matrix, or something fancier.
It's fine when x is a sympy.Symbol:
import sympy as sy
from sympy import Matrix, MatrixSymbol
x = sy.Symbol('x')
f = sy.lambdify(x, x, 'numpy')
f(Matrix((3,))) # returns Matrix([[3]])
But not when x is a sympy.MatrixSymbol:
import sympy as sy
from sympy import Matrix, MatrixSymbol
x = sy.MatrixSymbol('x', 2, 2)
f = sy.lambdify(x, x, 'numpy')
f(Matrix((3,))) # should return Matrix((3,)), right?
It tells me:
lambda() takes no arguments (1 given)
Thanks for any ideas or guidance!
Here's a cludge hack. mySymbol takes a string (keep it short), and n and m are the desired matrix dimensions.
def makeMatrix(mySymbol,n,m): #makes a lambdafiable matrix
myMatrix = zeros(n,m)
for i in numpy.arange(n):
for j in numpy.arange(m):
myStr = mySymbol+"_"+str(i)+"_"+str(j)
myMatrix[i,j]=sympify(myStr)
return myMatrix

Categories