Matrix with expressions as elements

64 views
Skip to first unread message

Carlos Bouthelier Madre

unread,
Apr 29, 2017, 9:19:31 AM4/29/17
to sympy
Hi, I have just started using sympy and I don't really know how to make this work. I've got functions that should return a matrix (I don't know if that is possible) whose elements are expressions of symbolic variables.  
This is my code, I've made a logical analysis of it (in pseudo-code) and it should do what I want to, the issue is that I am neither used to sympy variables nor to python syntaxis, so I would really appreciate your help. 

from sympy import *
from sympy import cos
from sympy.tensor.array import Array
from sympy import I, Matrix, symbols
from sympy import init_printing
from sympy import init_session


from IPython.display import display, Math, Latex


var('Tita1 Tita2 Tita3 Tita4 Tita5 Tita6 Beta eps', real=True)
def delta(n):
    f='Tita'+str(n)
    return sqrt(cos(f)*cos(f)*eps*eps+1)

def EF(i):
    return Matrix([[0,0],[0,2*delta(i)]])

ID=Matrix([[1,0],[0,1]])

def ExtDim(X,n):
    if n==1:
        return X
    else:
        return kron(ExtDim(X,n-1),ID)

def n_energy(n):
    if n==1:
        return EF(n)
    else:
        return kron(ID,n_energy(n-1))+ExtDim(EF(n),n) #This should be a matrix


Thanks in advance!

Aaron Meurer

unread,
Apr 29, 2017, 9:16:11 PM4/29/17
to sy...@googlegroups.com
Matrices can have expressions as elements. What does the code do when you run it? It doesn't run for me because kron is not defined.

Aaron Meurer

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/ea805a03-ad40-44e9-8b44-f9957536e192%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carlos Bouthelier Madre

unread,
May 2, 2017, 6:16:00 PM5/2/17
to sympy
Thanks for your message, Aaron!
I restarted my computer and it worked, not having changed a thing, so.... problem solved!

I have another problem though,  I have a function f(x) which I know has no primitive, but I want to define its integral from 0 to 2*pi, would python know what integrate(f(x),(x,0,2*pi)) means? Or, conversely, would it keep trying to get a primitive expression "indefinitely"? (I think this is what is happening) If so, does anybody know if there is another way to do this?

In fact , what I have is f(x1,x2,...xi,xi+1,....xn,Beta) and I want to define the integral of that function in the xi variables from i=1 to n iteratively (n is a value, like 10) from i=1 to N, obtaining an expression exclusively on Beta (I say this just in case it makes harder the use of an hypothetic method that could solve my problem )

Thanks in advance!!
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.

Aaron Meurer

unread,
May 2, 2017, 6:17:41 PM5/2/17
to sy...@googlegroups.com
If you use Integral() instead of integrate() it will keep the integral unevaluated. Otherwise, it will try to compute it, and if it cannot compute a closed-form expression, it will return an Integral object.

Aaron Meurer

To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.

To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.

Carlos Bouthelier Madre

unread,
May 3, 2017, 1:12:12 PM5/3/17
to sympy
All right! Thanks again.

If later I would like to plot that integral(f(xi,Beta)*dxi) with Beta in the X axis, would it work?

Message has been deleted

Aaron Meurer

unread,
May 3, 2017, 6:55:03 PM5/3/17
to sy...@googlegroups.com
It should work, yes. plot() will evaluate the integral numerically.

Aaron Meurer

On Wed, May 3, 2017 at 4:58 PM, Carlos Bouthelier Madre <carlosbo...@gmail.com> wrote:
All right! Thanks again.

If later I would like to plot that integral(f(xi,Beta)*dxi) with Beta in the X axis, would it work?


El miércoles, 3 de mayo de 2017, 0:17:41 (UTC+2), Aaron Meurer escribió:

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
0 new messages