ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 03 Oct 2019 08:11:11 -0500expand large expressions made of complex functions and variableshttp://ask.sagemath.org/question/48165/expand-large-expressions-made-of-complex-functions-and-variables/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)=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z,t)*r_4(R,a,x,y,z,t)*r_5(R,a,x,y,z,t) + r_2(R,a,x,y,z,t)*r_3(R,a,x,y,z,t)*r_4(R,a,x,y,z,t)*r_5(R,a,x,y,z,t)
(uu(i,j,x,y,z,t)).expand().maxima_methods().collectterms(x,y,z,t)RutgerThu, 03 Oct 2019 08:11:11 -0500http://ask.sagemath.org/question/48165/How to expand the result of derivative()?http://ask.sagemath.org/question/47484/how-to-expand-the-result-of-derivative/I ran:
```
derivative(acosh(x), x)
```
and the result was:
```
1/(sqrt(x + 1)*sqrt(x - 1))
```
I personally would prefer `1/sqrt(x^2-1)` as it's an expanded form and I'm wondering how I can get SageMath to show this. I've tried adding `.expand()` afterwards and that made no difference to the result. Fusion809Wed, 14 Aug 2019 11:15:47 -0500http://ask.sagemath.org/question/47484/Quicker expansion of multivariate polynomialshttp://ask.sagemath.org/question/46098/quicker-expansion-of-multivariate-polynomials/ Hi everyone,
I'm working on a project that involves first generating polynomials, then checking to see if certain terms exist within them. Essentially, I have a function `build_polynomial` that builds polynomials in un-expanded form according to a certain set of specifications. The polynomials produced by this function usually look something like this:
(x1 - x2 - x3 + y1 - y2 + y3 - y4)*(x1 - x2)*(x1 - x3)*(x1 + y1 - y2)*(x2 + x3 - y1 + y2 - y3 + y4)*(x2 + x3 + y2 - y3)*(x2 + x3 - y3 + y4)*(x2 + x3)*(x2 - x3)*(x2 + y2 - y3)*(y1 - y2)*(y1 - y3)*(y1 - y4)*(y2 - y3)*(y2 - y4)*(y3 - y4)
This is a particularly small example, the polynomials expand quite rapidly as the problem grows in size, but this example should give an idea of generally what they look like.
Next, my program must check too see if specific terms exist in the expanded form of the polynomial. Currently, I do this by first expanding the polynomial with the Sage `expand` function, then using the `coefficient` function with the specific terms as arguments.
My issue is that as the polynomial grows, the completion time of the `expand` function also grows quite rapidly. For example, at medium-to-large complexity, the polynomial can take days to be expanded.
Is there any way around this? Possibly a more efficient function that anybody knows of? I know that Sage is not really the best option for large computations like this, but I'm looking for a way to at least slightly improve performance while I research a longer-term solution.
Thanks!trenzafeedsThu, 11 Apr 2019 12:41:03 -0500http://ask.sagemath.org/question/46098/Expand not workinghttp://ask.sagemath.org/question/42577/expand-not-working/ ![image description](https://i.stack.imgur.com/3Plyz.png)
I multiplied some matrices, as seen in the picture to get a 1 by 1 matrix. I tried to expand the contents, but it didn't work. I had to copy and paste the contents. Why didn't it work?ionsmeTue, 12 Jun 2018 15:53:06 -0500http://ask.sagemath.org/question/42577/Should I always use .expand() before using .coefficient()?http://ask.sagemath.org/question/41437/should-i-always-use-expand-before-using-coefficient/I used the following code in SageMath (Windows binary, version 8.1):
q,x,t,bb_1_1_0,bb_1_1_1,aa_1_1_1,cc_1,eps=var('q x t bb_1_1_0 bb_1_1_1 aa_1_1_1 cc_1 eps')
const1=-(bb_1_1_1*eps*cos(t)*cos(x) + bb_1_1_0*eps*cos(x) + q)*aa_1_1_1*eps*cos(x)*sin(t) - bb_1_1_1*cc_1*eps*cos(x)*sin(t)
Obtaining the coefficients of polynomial with respect to eps: `const1.coefficient(eps,1)` gives
-(bb_1_1_1*cos(t)*cos(x) + bb_1_1_0*cos(x))*aa_1_1_1*cos(x)*sin(t) - bb_1_1_1*cc_1*cos(x)*sin(t)
but using `const1.expand().coefficient(eps,1)` gives
-bb_1_1_1*cc_1*cos(x)*sin(t) - aa_1_1_1*q*cos(x)*sin(t)
Clearly the second one is correct. Does this mean that I should always use `.expand()` before using `.coefficient()`?
In another instance I was forced to use `simplify_full()` before using `coefficient()`.DanialBaghThu, 08 Mar 2018 06:49:38 -0600http://ask.sagemath.org/question/41437/simplifying expressionhttp://ask.sagemath.org/question/40347/simplifying-expression/I have been trying to simplify this expression but with no luck. I checked using PARI, it indeed gives 0, means the change of variable `p` and `v` do satisfy the curves given. Can someone drop me a hint on how to work this out.
> p,x,v,y= var('p x v y')
> p=(-72*x-264+36*y)/(9*x^2+30*x-119)
> v=(-162*x^4+540*x^3-648*x^2*y+13176*x^2-4752*x*y+62340*x-16488*y+153994)/(81*x^4+540*x^3-1242*x^2-7140*x+14161)
> eq1=expand(v^2-(p^4-2*p^3+5*p^2+8*p+4))
> eq=eq1.subs({y:sqrt(-x^3+(121/3)*x+(1690/27))}).subs({sqrt(-x^3+(121/3)*x+(1690/27)):y})
> eq2=eq.simplify_full();eq2
The output gave :
3456*(27*(-x^3 + 121/3*x + 1690/27)^(3/2)*(9*x^2 + 174*x + 409) + (243*x^5 + 4698*x^4 + 1242*x^3 - 204696*x^2 - 739461*x - 691210)*y)/(6561*x^8 + 87480*x^7 + 90396*x^6 - 2498040*x^5 - 3874554*x^4
+ 33029640*x^3 + 15803676*x^2 - 202219080*x + 200533921)
It is suppose to be zero (I have checked). Problem lies at the expression `3456*(27*(-x^3 + 121/3*x + 1690/27)^(3/2)`. This term should be simplified into somewhat `y^2*y=(-x^3 + 121/3*x + 1690/27)*y`. It will then simplify and give `0`. But how to work that out.ShaFri, 29 Dec 2017 00:19:19 -0600http://ask.sagemath.org/question/40347/Changing order of factors affects sympy expansionhttp://ask.sagemath.org/question/34204/changing-order-of-factors-affects-sympy-expansion/there is a strange behaviour when using sympy inside sage:
from sympy import Symbol, exp, I
import sympy
x = Symbol("x", real=True)
then, asking for ```exp(I*8*x).expand(complex=True)``` I get
I*sin(8*x) + cos(8*x)
that's ok. however, asking for ```exp(8*I*x).expand(complex=True)```, I get
I*exp(-8*im(x))*sin(8*re(x)) + exp(-8*im(x))*cos(8*re(x))
Why in the second case it forgets that $x$ is real? Is this behaviour expected?
this was causing me real trouble in another context, since I was calling functions from a package that itself uses sympy, and they didn't work unless some special ordering of the arguments was given.
I'm on SMC with sagemath kernel 6.10. I've checked in [Live SymPy](http://live.sympy.org/), the 1st result is obtained in both cases.
mforetsSun, 24 Jul 2016 09:06:51 -0500http://ask.sagemath.org/question/34204/conversion from polynomial to symbolichttp://ask.sagemath.org/question/23818/conversion-from-polynomial-to-symbolic/I want to convert a huge polynomial from ZZ[] into SR but with the following code I get an expression that is not expanded:
sage: R.<x> = ZZ[]
sage: var('n')
n
sage: p=16*x^5 - 20*x^3 + 5*x
sage: p.subs(x=n)
(4*(4*n^2 - 5)*n^2 + 5)*n
sage: SR(p)
(4*(4*x^2 - 5)*x^2 + 5)*x
Is there a way to get the result in expanded form without using `expand()`? Presumably the unnecessary grouping and subsequent expansion can take some time with huge polynomials, so I would like to prevent this from the start.rwsMon, 18 Aug 2014 09:39:16 -0500http://ask.sagemath.org/question/23818/expand hyperbolic trig functionshttp://ask.sagemath.org/question/23546/expand-hyperbolic-trig-functions/ Part of a symbolic expression is
ex = 2*e^(1/2*x)*sinh(1/2*x)
How to substitute sinh(x) with its expansion (e^x-e^(-x))/2 and simplify ex to e^x-1? I have tried all expand and simplify functions.rwsWed, 23 Jul 2014 08:48:08 -0500http://ask.sagemath.org/question/23546/numerator_denominator()http://ask.sagemath.org/question/10615/numerator_denominator/I have a very big rational function and i want to obtain the numerator and denominator.
with .numerator_denominator() it takes forever
with .numerator_denominator(False) not every term is expanded.
with .expand() and .combine() it takes forever and the result is not a single fraction.
I need a single fraction N(x)/D(x) where N and D are polynomials in x, it is not important how big !alessandroWed, 16 Oct 2013 11:58:48 -0500http://ask.sagemath.org/question/10615/How to expand an symbolic expression.http://ask.sagemath.org/question/8666/how-to-expand-an-symbolic-expression/With Maple I can write
T := proc(f,n,x)
local v;
add(binomial(n,v)*f(v+1)*(x)^v,v=0..n)
end:
and then
T(f,4,x)
will give me the answer
f(1)+4*f(2)*x+6*f(3)*x^2+4*f(4)*x^3+f(5)*x^4.
If I write the same with Sage
def T(f,n,x):
return add(binomial(n,v)*f(v+1)*(x)^v for v in (0..n))
var('f')
T(f,4,x)
then I will get the answer
5*x^4 + 16*x^3 + 18*x^2 + 8*x + 1.
How do I have to proceed to get Maple's answer?
petropolisMon, 23 Jan 2012 11:14:39 -0600http://ask.sagemath.org/question/8666/Simplification of expression with exponentials.http://ask.sagemath.org/question/8344/simplification-of-expression-with-exponentials/Hello,
I am looking at expanding the product:
(a+b\*g+c\*g^2+d\*g^3)\*(a+b\*g^2+c\*g^4+d\*g)\*(a+b\*g^3+c\*g+d\*g^4)\*(a+b\*g^4+c\*g^3+d\*g^2)
where $g=e^{2\pi i /5}$ is a fifth root of unity. The problem is, after expanding, and telling sage the value of $g$, it does not collect terms according to the rule $1+g+g^2+g^3+g^4=0$ so there are over 5 times too many terms.
Is there any way to make this simplification automatic? For degrees higher then $5$ going through manually will not be an option. (It is almost faster to multiply everything myself then figure out the collection of the terms)
Thank you very much for any help on this, I have been stuck for the last whileEric NaslundMon, 26 Sep 2011 07:31:17 -0500http://ask.sagemath.org/question/8344/