expand large expressions made of complex functions and variables

asked 2019-10-03 15:11:11 +0200

Rutger gravatar image

updated 2019-10-03 15:26:01 +0200

I'm trying to use Sagemath notebook environment without much prior knowledge of Sage. What I tried to do was define some variables (R, a, x, y, x, t...) and then some functions expressed in these variables. The expressions get quite large and complex this way as they are functions built of other functions. Last line of my code I'm trying to expand a large complex-valued symbolic expression using the variables of the functions (so not their values). I use expand() and collectterms(x,y,z,t) to get a result that is sorted by powers of x,y,z,t. When I used this with less complicated expressions it seemed to work OK giving me some reassurance that my syntax should be correct. But when the expressions get larger (see my code below) the outcome isn't correct anymore. I get a result without error messages, but the result is not correct. For example : the code below gives me a result without any imaginary numbers, also the real part is wrong. Does anyone know what is going on here? Is my syntax wrong? Or does Sage fail without error when expressions get large? And if so: are there ways to do this correctly? Thanks in advance!

clos(R,a)=((1/(2*R))+R/2)*cos(a)+I*((1/(2*R))-R/2)*sin(a)
slin(R,a)=((1/(2*R))+R/2)*sin(a)-I*((1/(2*R))-R/2)*cos(a)

a_1(R,a)=(1/5)*sqrt(5)
a_2(R,a)=-(1/20)*sqrt(5)-(1/4)+(1/2)*clos(R,a)
a_3(R,a)=-(1/20)*sqrt(5)+(1/4)+(1/2)*slin(R,a)
a_4(R,a)=-(1/20)*sqrt(5)-(1/4)-(1/2)*clos(R,a)
a_5(R,a)=-(1/20)*sqrt(5)+(1/4)-(1/2)*slin(R,a)

b_1(R,a)=(1/5)*sqrt(5)
b_2(R,a)=-(1/20)*sqrt(5)+(1/4)-(1/2)*slin(R,a)
b_3(R,a)=-(1/20)*sqrt(5)-(1/4)+(1/2)*clos(R,a)
b_4(R,a)=-(1/20)*sqrt(5)+(1/4)+(1/2)*slin(R,a)
b_5(R,a)=-(1/20)*sqrt(5)-(1/4)-(1/2)*clos(R,a)

cc_1(R,a)=(1/5)*sqrt(5)
cc_2(R,a)=-(1/20)*sqrt(5)-(1/4)-(1/2)*clos(R,a)
cc_3(R,a)=-(1/20)*sqrt(5)+(1/4)-(1/2)*slin(R,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,z,t)

(uu(i,j,x,y,z,t)).expand().maxima_methods().collectterms(x,y,z,t)
edit retag flag offensive close merge delete

Comments

To display blocks of code or error messages, skip a line above and below, and do one of the following (all give the same result):

  • indent all code lines with 4 spaces
  • select all code lines and click the "code" button (the icon with '101 010')
  • select all code lines and hit ctrl-K

For instance, typing

If we define `f` by

    def f(x, y, z):
        return x * y * z

then `f(2, 3, 5)` returns `30` but `f(2*3*5)` gives:

    TypeError: f() takes exactly 3 arguments (1 given)

produces:

If we define f by

def f(x, y, z):
    return x * y * z

then f(2, 3, 5) returns 30 but f(2*3*5) gives:

TypeError: f() takes exactly 3 arguments (1 given)

Please edit your question to do that.

slelievre gravatar imageslelievre ( 2019-10-03 15:13:51 +0200 )edit

@slelievre much better, thank you

Rutger gravatar imageRutger ( 2019-10-03 15:20:41 +0200 )edit