ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 29 Nov 2020 05:13:42 -0600simplifying an expectation with the normal lawhttps://ask.sagemath.org/question/54440/simplifying-an-expectation-with-the-normal-law/ Just for the fun (since I can find the good result), why am I obliged to help so much Sagemath toĆ§ gpo through the last steps of this integration for so elementary evidence ?
var("a x s m")
f(x) = - exp(- a*x)
p(x) = (1/ (sqrt(2*pi)*s))*exp(- (1/2)*((x-m)/s)^2)
g(x) = f(x)*p(x)
show((integrate(g(x),(x,-oo,oo)).full_simplify()))
show(integrate(g(x),(x,-oo,oo)).full_simplify().subs({sqrt(s^2):s}).subs({sqrt(s^2):s}))CyrilleSun, 29 Nov 2020 05:13:42 -0600https://ask.sagemath.org/question/54440/Simplification Methods Surveyhttps://ask.sagemath.org/question/54047/simplification-methods-survey/I noted that SageMath has many ways to simplify expressions, including simplify_full(), full_simplify and canonicalize_radical.
Is there a survey nd usage guide available for these?
In particular, I am trying to simplify an expression as in
constants=var('a1,a2,b,c')
((a1*b)^c/(a2*b)^c).simplify_full()
such that I get `(a1/a2)^c`, but without success.IngoTue, 27 Oct 2020 13:32:18 -0500https://ask.sagemath.org/question/54047/Sage fails to recognize `1 + (1/2 + i*sqrt(3)/2)^3` is zerohttps://ask.sagemath.org/question/52863/sage-fails-to-recognize-1-12-isqrt323-is-zero/The following code:
print( 1 + ( 1/2 + i*sqrt(3)/2 )^3 )
print( simplify( 1 + ( 1/2 + i*sqrt(3)/2 )^3) )
print( N(1 + ( 1/2 + i*sqrt(3)/2 )^3) )
Results in the output:
(1/2*I*sqrt(3) + 1/2)^3 + 1
(1/2*I*sqrt(3) + 1/2)^3 + 1
2.22044604925031e-16 + 1.11022302462516e-16*I
but I'd expect the middle one, and perhaps even the first one, to be just "0." I think this means that there is something which I don't understand about how these expressions are handled.
Gregory BardTue, 04 Aug 2020 18:09:56 -0500https://ask.sagemath.org/question/52863/Reduction to a rational expressionhttps://ask.sagemath.org/question/52656/reduction-to-a-rational-expression/I have the the following expression:
$ tan(m \cdot actan(x) + n \cdot arctan(y))$
For what values of $n,m \in \mathbb{N}$ is this expression reducible to a rational expression in $R[x,y]$, where $R$ is a generic ring?
Is there some rule $lhs => rhs $ that is needed in order to get a rational expression form the expression above?salFri, 24 Jul 2020 12:12:44 -0500https://ask.sagemath.org/question/52656/Simplifying expression by known termshttps://ask.sagemath.org/question/52281/simplifying-expression-by-known-terms/I have expression that need to be simplified to the known terms. Below are the expression:
var('b0 a1 a2 h v_0 v_1 v_2 u_0 u_1 u_2');
expr = b0 == (16*h^2*v_0^2+(38*h*u_0-38*h*u_1)*v_0+25*u_1^2-50*u_0*u_1+25*u_0^2)/3;
expr.substitute(-h*v_0+2*u_1-2*u_0==a1,-2*(h*v_0-u_1+u_0)==a2)
The expected result is
b0 == c1*(a1)^q1 + c2*(a2)^q2
where c1, c2, q1, q2 are constants.
The output from Sage is
b0 == 16/3*h^2*v_0^2 + 25/3*u_0^2 - 50/3*u_0*u_1 + 25/3*u_1^2 + 38/3*(h*u_0 - h*u_1)*v_0Le MaburWed, 01 Jul 2020 04:37:52 -0500https://ask.sagemath.org/question/52281/Simplifying symbolic complex normhttps://ask.sagemath.org/question/52265/simplifying-symbolic-complex-norm/Sage's simplifier seems to have trouble expanding the square-absolute value of complex numbers:
sage: x,y = var('x,y', domain=RR)
sage: (x^2 + y^2 - abs(x + i*y)^2).simplify_full ()
x^2 + y^2 - abs(x + I*y)^2
How can I ensure sage expands the square-absolute value and simplify this down to zero? I'm aware that using `(x + i*y).norm()` instead of `abs(x + i*y)^2` helps in this particular example, but that solution doesn't generalize. For instance, when I stick expressions involving `x`, `y` into vectors and compute the vector norm, the vector norm is expressed in terms of absolute values, so I still need a way to deal with the `abs`.JunTue, 30 Jun 2020 01:57:16 -0500https://ask.sagemath.org/question/52265/How to rewrite multivariate polynomial as polynomial on one variable?https://ask.sagemath.org/question/49878/how-to-rewrite-multivariate-polynomial-as-polynomial-on-one-variable/ Suppose i have declared many varibles and a polynomial using them
x, y, z = var("x y z")
poly = x^3*y*z + x^2*y^2 + 3*x^3 + 2*x^2 + x*y + x*z + 1
How can i simplify the expression is such a way that it is written as a polynomial over x? I mean something like
(...)*x^3 + (...)*x^2 + (...)*x +...JGCTue, 11 Feb 2020 17:00:22 -0600https://ask.sagemath.org/question/49878/How to find a short form of recursive defined sequences?https://ask.sagemath.org/question/48820/how-to-find-a-short-form-of-recursive-defined-sequences/Hi, I'm new to sagemath.
Is there any way to so calculate/solve/find a short version of a recursive defined sequence?
E.g. I have a sequence like: (Fibonacci)
def f(n):
if n == 0:
return 0
if n == 1:
return 1
if n == 2:
return 1
else:
return f(n-1)+f(n-2)
How can I compute a short form of $f_n$?
-----
In this example case $f_n$ would be:
$f_n=\frac{1}{\sqrt{5}} (\frac{1+\sqrt{5}}{2})^n - \frac{1}{\sqrt{5}} (\frac{1-\sqrt{5}}{2})^n$
-----
Edit:
Thanks to Emmanuel I found how to solve those equations in pdf:
from sympy import Function,rsolve
from sympy.abc import n
u = Function('u')
f = u(n-1)+u(n-2)-u(n)
rsolve(f, u(n), {u(0):0,u(1):1})
-sqrt(5)*(1/2 - sqrt(5)/2)**n/5 + sqrt(5)*(1/2 + sqrt(5)/2)**n/5maanWed, 20 Nov 2019 10:33:55 -0600https://ask.sagemath.org/question/48820/Finitely presented group simplificationhttps://ask.sagemath.org/question/47636/finitely-presented-group-simplification/Hi! I have a bunch of finitely presented groups, with many generators and relations. I know that all of these are in fact cyclic groups, but many times using the "simplified()" function, I get a simpler presentation with 2 generators, rather than only one. The following is one example:
G.<x0,x5> = FreeGroup()
H =G/[(x5^-1*x0)^2*x5^-3, (x0^-1*x5^-1)^2*x0^-2*x5*x0^-1]
H is in fact just Z/27Z. Is there another way to simplify these presentations, in order to get a minimal one?
The problem here is that I do not only need to identify the specific group, but also to recover the image of the previous generators in the simplified one (as done by the simplification_isomorphism() function).danieleCWed, 28 Aug 2019 05:25:45 -0500https://ask.sagemath.org/question/47636/Solving an ODE and simplifying the resulthttps://ask.sagemath.org/question/46517/solving-an-ode-and-simplifying-the-result/ I'm interested in solving the differential equation $$3 h' + 3 h^2 = c_1,$$ where $c_1$ is a positive real number.
var('t')
var('c1', latex_name=r'c_1')
h = function('h')(t)
eq = -3*h^2 + c1 - 3*diff(h, t)
eq_sol = desolve(eq, h, ivar=t, contrib_ode=True)
The above code works, but it's not solved explicitly for $h$, so
h_sol = solve(eq_sol, h)
h_sol = h_sol[0]
h_sol
This gives something like $$h\left(t\right) = \frac{\sqrt{3} \sqrt{c_{1}} {\left(e^{\left(\frac{2}{3} \, \sqrt{3} C \sqrt{c_{1}} + \frac{2}{3} \, \sqrt{3} \sqrt{c_{1}} t\right)} + 1\right)}}{3 \, {\left(e^{\left(\frac{2}{3} \, \sqrt{3} C \sqrt{c_{1}} + \frac{2}{3} \, \sqrt{3} \sqrt{c_{1}} t\right)} - 1\right)}},$$
in sage notation (non-LaTeX) it starts like
h(t) == 1/3*sqrt(3)*sqrt(c1)* ...
**Question 1:** Is there a way to allocate to the solution (i.e. `h_sol`) the RHS of the above? without the `h(t) == ` part.
I had to set by hand (it is ease, but it would be nice to automatize the allocation)
var('C') # the integration constant introduced above
h_sol = 1/3*sqrt(3)*sqrt(c1)* ...
Then, by simply looking at the solution it is clear that it can be simplified. I tried things like
h_sol = h_sol.canonicalize_radical()
h_sol = h_sol.collect_common_factors()
h_sol = h_sol.simplify_rectform(complexity_measure = None)
but none of them returns the expected result, which could be obtained from Mathematica's kernel
mathematica("DSolve[3*h'[t] + 3*h[t]^2 == C[1], h[t], t]//FullSimplify")
$$ \sqrt{\frac{c_1}{3}} \tanh\left( \sqrt{\frac{c_1}{3}} (t - 3 c_2) \right) $$
**Question 2:** How could the expression `h_sol` be manipulated to obtain the hyperbolic tangent?DoxTue, 14 May 2019 02:41:24 -0500https://ask.sagemath.org/question/46517/How to get latex expression in exponential notation?https://ask.sagemath.org/question/35753/how-to-get-latex-expression-in-exponential-notation/I need a latex representation of equations in exponential notation, e.g. units of J/K should be represented as:
J K^{-1}
Is there a way to force sage to return such a result? Here is an example:
var('J K ')
unit1 = J/K
latex(unit1)
returns:
\frac{J}{K}
I tried expand(), but it does not seem to expand fractions to their exponential representation.stanSun, 27 Nov 2016 06:47:22 -0600https://ask.sagemath.org/question/35753/Constant coefficient of Laurent Polynomialshttps://ask.sagemath.org/question/35746/constant-coefficient-of-laurent-polynomials/I am looking for the constant coefficient of a Laurent polynomial, the issue I am having is that sage is not simplifying the polynomial.
An example:
a = var(",".join( "a%i" %i for i in range(0, 6)))
f = x*y + 1.00000000000000*a6*x + 1.00000000000000*a4*y + x*y^-1 + x^-1*y + 1.00000000000000*a3*y^-1 + 1.00000000000000*a1*x^-1 + x^-1*y^-1
Then I ask
f/(x^1*y^0) # (The powers have to be in this way, just from the context of the work I am doing)
and it outputs:
1.00000000000000/x*x*y + 1.00000000000000*a6/x*x + 1.00000000000000*a4/x*y + 1.00000000000000/x*x*y^-1 + 1.00000000000000/x*x^-1*y + 1.00000000000000*a3/x*y^-1 + 1.00000000000000*a1/x*x^-1 + 1.00000000000000/x*x^-1*y^-1
Now when I ask for the constant coefficient of this LP it tells me its 0 when it is a6.
How can I fix this.
Thanks in advanceEd CalSat, 26 Nov 2016 17:35:11 -0600https://ask.sagemath.org/question/35746/Factor out rootshttps://ask.sagemath.org/question/26737/factor-out-roots/
sage: var('a, b, c, d')
sage: trm = 3*(a+b*sqrt(2))+(1+2*sqrt(2))*(c + d*sqrt(2))
sage: trm.expand()
3*sqrt(2)*b + 2*sqrt(2)*c + sqrt(2)*d + 3*a + c + 4*d
What could I do if I wanted a result like:
(3*b + 2*c + *d)*sqrt(2) + 3*a + c + 4*d
And similar, if there were other roots like `sqrt(5)`, `7^(1/3)` etc.
Thank you!OderynWed, 06 May 2015 08:43:40 -0500https://ask.sagemath.org/question/26737/working on part of an expressionhttps://ask.sagemath.org/question/26397/working-on-part-of-an-expression/ I came across a complicated expression containing cos(4*t). I wanted to change it to 1-sin(2*t)^2. The tools are
trig_reduce, trig_expand, and trig_simplify. One of them expresses everything in trig functions of t and the other
two are useless. And all three will work on the entire expression, not just on one part. So this is a very general
question: how can I get sage to do something to just one part of an expression, rather than the whole expression?
(I suppose I could extract the part, work on it, and then substitute the result back in, but that would be difficult!)
And even on a standalone basis, I don't know how to change cos(4*t) to 1-sin(2*t)^2.beesonTue, 31 Mar 2015 10:12:14 -0500https://ask.sagemath.org/question/26397/simplifying expressions in GF(2)https://ask.sagemath.org/question/25838/simplifying-expressions-in-gf2/ Hi guys,
I know that for a variable $x$ in $GF(2)$, $x^2 = x$, and $2x=0$.
How do I simplify a polynomial expression in $GF(2)$ in the Sage interface?
For example, I should obtain
$$(a+b+1)^2=a^2+b^2+1+2a+2b+2ab=a+b+1$$
freako89Sun, 15 Feb 2015 20:45:32 -0600https://ask.sagemath.org/question/25838/Declaring variable to be in a particular field/ring/grouphttps://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/Is it possible to have Sage symbolically simplify expressions involving variables subject to the assumption that the variables take values in a defined domain (field/ring/group/etc)?
The closest I've gotten is to declare a dummy polynomial ring over my domain of interest so that its variable has some notion of the domain, e.g.:
<pre>Z3=Integers(3)
Dummy.<x> = PolynomialRing(Z3)
3*x</pre>
evaluates to "0" as I'd expect, but sage fails to simplify "x^3" to "x", which ISTM should be doable if it really understood that x is a variable in Z/3Z.
Related things I've found in my searches that haven't panned out:<br/>
1. var('x', domain=*foo*) -- apparently *foo* can only be one of real/complex/positive (where I'd like to be able to say 'Z3' in the example above)<br/>
2. assume('x is Z3') - doesn't seem to have any effect.a1846583Fri, 28 Nov 2014 10:44:44 -0600https://ask.sagemath.org/question/25037/Auto-substitute complex term to ease numeric evaluationhttps://ask.sagemath.org/question/10427/auto-substitute-complex-term-to-ease-numeric-evaluation/I have a very large symbolic term (mainly composed of sums, products and divisions) and, using substitutions, I want to reformulate it in a way that avoids repetitions and thus can be evaluated using less arithmetic operations.
For example, if I have a term like
X = (d*(a-b)/(1 + a-b))^2 + d*(a-b)/(1 + a-b) + exp(d*(a-b)/(1 + a-b)) + a-b,
I want a function that returns something like:
S1 = a-b
S2 = d*S1/(1+S1)
X = S2^2 + S2 + exp(S2) + S1
Giving me instructions how to more efficiently calculate X: First calculate S1, then S2 using S1 and finally X using S1 and S2.
My actual problem is much nastier and I do not need optimal or near-optimal results, but any way of automatising such substitutions.WrzlprmftWed, 07 Aug 2013 05:55:00 -0500https://ask.sagemath.org/question/10427/Very basic simplification questionhttps://ask.sagemath.org/question/10135/very-basic-simplification-question/I know what the answer is, however I'm trying to work through the process of this basic question. The equation is below:
(3 + x)/x^2
To simplify can't you simplify it to by simplifying the exponents:
3/x
However if you were to plug a constant into the original variable such as 2, you would get a different result than if you simplified it.
1. x = 2 | (3 + x)/x^2 = 5/4
2. x = 2 | 3/2
It's been a while since I went through algebra, so I'm sure there's an easy answer, I'm just wanting to know where I'm not thinking it through properly.
Thanks!jordanhudgensSat, 18 May 2013 07:27:05 -0500https://ask.sagemath.org/question/10135/simplify equationshttps://ask.sagemath.org/question/9690/simplify-equations/Hello,
i'm trying to simplify equations "eq" (http://pastie.org/5624251) under two assumptions. The result is supposed to be (1+a*x+b*y+c*z) / 2
sage: var('x,y,z,a,b,c')
(x, y, z, a, b, c)
sage: (x*x+y*y+z*z == 1).assume()
sage: (a*a+b*b+c*c == 1).assume()
sage: eq = abs( sqrt((1+c)*(1+z)) / 2 + (1-c)*(1-z)*sqrt((1+c)*(1+z)) / (2*(a-i*b)*(x+i*y)) ) ^ 2
sage: eq.simplify_full()
(I*((-8.0*I*a*b - 8.0*b^2)*c - 8.0*I*a*b - 8.0*b^2)*x*y*z + 4*(c^3 - c^2 - c + 1)*z^3 + 8.0*I*b*c^2*x + 8.0*I*b*x*z^2 + I*((-8.0*I*a*b - 8.0*b^2)*c - 8.0*I*a*b - 8.0*b^2)*x*y + (-(4.0*a^2 - 8.0*I*a*b - 4.0*b^2)*c - 4*a^2 + 8.0*I*a*b + 4.0*b^2)*y^2 + ((4.0*a^2 - 8.0*I*a*b - 4.0*b^2)*c + 4*a^2 - 8*I*a*b - 4.0*b^2)*x^2 - (4*c^3 - ((8*a - 8*I*b)*c^2 - 8*a)*x - ((8*I*a + 8.0*b)*c^2 - 8*I*a - 8.0*b)*y - 4*c^2 - 4*c + 4)*z^2 + 4*c^3 + (-8.0*a*c^2 + 8*a - 8*I*b)*x + ((-8*I*a - 8.0*b)*c^2 + ((8*I*a^2 + 8.0*a*b)*c + 8*I*a^2 + 8.0*a*b)*x + 8*I*a + 8.0*b)*y + (((8*I*a^2 + 8.0*a*b)*c + 8*I*a^2 + 8.0*a*b)*x*y + (-(4.0*a^2 - 8.0*I*a*b - 4.0*b^2)*c - 4*a^2 + 8.0*I*a*b + 4.0*b^2)*y^2 + ((4.0*a^2 - 8.0*I*a*b - 4.0*b^2)*c + 4*a^2 - 8*I*a*b - 4.0*b^2)*x^2 - 4*c^3 + 4*c^2 + 4*c - 4)*z - 4*c^2 - 4*c + 4)/(-32.0*I*a*b*x^2 + 32.0*I*a*b*y^2 - 32.0*I*b^2*x*y + 16*(a^2 - b^2)*x^2 + (32*I*a^2 + 64.0*a*b)*x*y - 16*(a^2 - b^2)*y^2)
What am I doing wrong?rdmadss1Fri, 04 Jan 2013 13:06:15 -0600https://ask.sagemath.org/question/9690/