ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 09 Jan 2023 08:26:29 +0100Matrix symbolic definition for algebrahttps://ask.sagemath.org/question/65838/matrix-symbolic-definition-for-algebra/Is there a way to define variables as matrices and then perform simplification? For instance, instead of A=var('A') being a scalar variable, can A be defined as a matrix?PickleMon, 09 Jan 2023 08:26:29 +0100https://ask.sagemath.org/question/65838/Simplify_full() and collect() togetherhttps://ask.sagemath.org/question/65355/simplify_full-and-collect-together/Dear all,
I am a novice with sagemath, and I use only a very small subset of its functionalities. I need to simplify the following expression, without using definitions:
var('c,f')
p_star_1 = ( (c+1)*f^2 - (2*c +1)*f - c - f ) / 2*(f^2-*2*f-1)
Here, I report the output of ``` p_star_1```, which in the real program is ```p_star_1 = something.simplify_full() ```.
I would like to express this as:
p_star_1 = c/2 + (2-f)*(1+ f)/(2*(1+2*f-f^2))
But using ``` collect.(c) ``` is not sufficient! How can I do this?
EDIT: I corrected the expression again, sorry.Boyko_BuThu, 15 Dec 2022 11:06:06 +0100https://ask.sagemath.org/question/65355/Simplifying expressionhttps://ask.sagemath.org/question/64637/simplifying-expression/Hello,
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_0MosesxiThu, 27 Oct 2022 11:30:36 +0200https://ask.sagemath.org/question/64637/generating powers of g, an irreducible polynomial in an extension fieldhttps://ask.sagemath.org/question/64555/generating-powers-of-g-an-irreducible-polynomial-in-an-extension-field/In an extension field, how do you to iteratively multiply an irreducible polynomial to obtain its powers, simplifying and printing the results in sagemath?
For example:
1) Within `GF(59^2)['x']`, the polynomial `x^2 + 2x + 13` is irreducible.
2) Say we use the label `'g'`, we can use this irreducible polynomial to express g squared.
`g^2 = -2g - 13
= 57g + 46
3) Subsequent powers of g can be obtained by iteratively multiplying the previous expression by g.
for example:
g^2 = 57g+46
g^3 = g(g^2) = g(57g+46) = 57g^2 + 46g = 57 (57g + 46) + 46g (mod 59)
= 50g + 26,
g^4 = g(g^3) = g(50g + 26) = 50g^2 + 26g = 50(57g+46) + 26g = (50*57)g + (50 * 46) + 26 g (mod 59)
= 44g + 58,
g^5 = g(g^4) = g(44g + 58) = 44g^2 + 58g = 44(57g+46) + 58g = (44*57)g + (44 * 46) + 58g = 30g + 18+ 58g
=29g + 18
(...)
and so on, up to cycle, or g^59.
The question is:
What would the sagemath code look like to do the following a) initialize g squared, subsequently multiply by g, and simplify mod 59 to print the following output?
Sample output:
g^2 = 57*g + 46
g^3 = 50*g + 26
g^4 = 46*g + 58
g^5 = 25*g + 51
(....)
g^59 = ....
Possible pseudocode:
#initialize the ring
F = GF(59^2)['x']
#initialize the starting point to our g^2
g_current = F('57g + 46')
#loop over an appropriate range
for i in range (1,57):
#accumulate the current power, multiplying the previous by g, and then simplifying
g_current = (g * g_current).simplify()
#print an appropriate line summary
print("g^" + str(i+2) + "=" + str(g_current))
Your help is appreciated.user5362Sat, 22 Oct 2022 06:55:17 +0200https://ask.sagemath.org/question/64555/Simplifying a simple rational expression with indeterminate exponenthttps://ask.sagemath.org/question/64001/simplifying-a-simple-rational-expression-with-indeterminate-exponent/`((sqrt(x)/(x+1))^n * (x+1)^n).simplify_full ()` returns the given expression as-is, but evidently it should be simplified to `sqrt(x)^n`. Sage indeed does this if the `x+1` is replaced by `x` or `n` by a concrete number like `10` (illustrating that sage isn't worried about division by zero, but it's something else). Is there a way to have sage simplify this?JunWed, 14 Sep 2022 03:00:15 +0200https://ask.sagemath.org/question/64001/How to reduce the number of terms in an expression by collecting terms / factoringhttps://ask.sagemath.org/question/62800/how-to-reduce-the-number-of-terms-in-an-expression-by-collecting-terms-factoring/Hello there, I would like some help trying to simplify the result I get by taking the gradient of a real-valued vector field in 3D Euclidean space. The vector field $\mathbf{u}$ is defined by:
$\mathbf{u}=\displaystyle\frac{\mathbf{\Gamma}\times\mathbf{r}}{\mathbf{r}\cdot\mathbf{r}}$
where
$\mathbf{\Gamma} = \mathbf{\Gamma}(\mathbf{y})$
and
$\mathbf{r} =\mathbf{r}(\mathbf{x},\mathbf{y})= \mathbf{x} - \mathbf{y}$
The gradient I am interested in is:
$\nabla\mathbf{u}=\displaystyle\frac{\partial u_i}{\partial x_j}$
The code I wrote for computing the gradient is:
E.<x1, x2, x3> = EuclideanSpace()
grad = E.metric().connection()
y1, y2, y3, Gamma1, Gamma2, Gamma3 = var('y1 y2 y3 Gamma1 Gamma2 Gamma3')
r = E.vector_field([x1 - y1, x2 - y2, x3 - y3])
Gamma = E.vector_field([Gamma1, Gamma2, Gamma3])
u = Gamma.cross(r) / r.dot(r)
u_gradient = grad(u)
The result I get for `u_gradient` is quite unwieldy:
u_gradient[:]
[ 2*(Gamma3*x2 - Gamma2*x3 - Gamma3*y2 + Gamma2*y3)*(x1 - y1)/(x1^2 + x2^2 + x3^2 - 2*x1*y1 + y1^2 - 2*x2*y2 + y2^2 - 2*x3*y3 + y3^2)^2 -(Gamma3*x1^2 - Gamma3*x2^2 + 2*Gamma2*x2*x3 + Gamma3*x3^2 - 2*Gamma3*x1*y1 + Gamma3*y1^2 - Gamma3*y2^2 + Gamma3*y3^2 + 2*(Gamma3*x2 - Gamma2*x3)*y2 - 2*(Gamma2*x2 + Gamma3*x3 - Gamma2*y2)*y3)/(x1^4 + 2*x1^2*x2^2 + x2^4 + x3^4 - 4*x1*y1^3 + y1^4 - 4*x2*y2^3 + y2^4 - 4*x3*y3^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3^2 - 4*(x1^3 + x1*x2^2 + x1*x3^2)*y1 - 4*(x1^2*x2 + x2^3 + x2*x3^2 - 2*x1*x2*y1 + x2*y1^2)*y2 - 4*(x3^3 - 2*x1*x3*y1 + x3*y1^2 - 2*x2*x3*y2 + x3*y2^2 + (x1^2 + x2^2)*x3)*y3)]
Focusing on the first component:
u_gradient[1, 1]
2*(Gamma3*x2 - Gamma2*x3 - Gamma3*y2 + Gamma2*y3)*(x1 - y1)/(x1^2 + x2^2 + x3^2 - 2*x1*y1 + y1^2 - 2*x2*y2 + y2^2 - 2*x3*y3 + y3^2)^2
I would rather see it expressed as:
u_gradient_simplified[1, 1]
2*(Gamma3*(x2 - y2) - Gamma2*(x3 - y3))*(x1 - y1)/((x1 - y1)^2 + (x2 - y2)^2 + (x3 - y3)^2)^2
I tried using `simplify` but it did not change anything, unfortunately. Are there any clever tricks I could apply to rewrite the expression into something shorter?mtgoncalvesSun, 12 Jun 2022 19:23:47 +0200https://ask.sagemath.org/question/62800/Simplify rational expression to polynomialhttps://ask.sagemath.org/question/62495/simplify-rational-expression-to-polynomial/ I'd like to check my computations for rational polynomial equations with sage. Here is an equation that gives the general form of a parabola with respect to a directrix and focus:
(ax+by+c)^2/(a^2+b^2) = (x-f1)^2 + (y-f2)^2
I'd like to obtain from this rational expression a polynomial expression in x,y. Is there a way to do this kind of computation automatically in Sage?Rodrigo RayaWed, 18 May 2022 14:53:38 +0200https://ask.sagemath.org/question/62495/Sage cannot simplify expressions with radical functions?https://ask.sagemath.org/question/60870/sage-cannot-simplify-expressions-with-radical-functions/Sage cannot seem to simplify symbolic expressions containing radical functions, even if I specify the domain and use assumptions:
var('x', domain='positive')
assume(x>1)
bool(sqrt(x-1)>0) # True
bool(sqrt((x-1)^3)-(x-1)*sqrt(x-1)==0) #True
# not true even with x>1 assumption?
bool(sqrt(x^3-x^2)-x*sqrt(x-1)==0) #False
I know there is the `simplify_chain_real()` function that can simplify `sqrt(x^3-x^2)` to `sqrt(x-1)*x`, but shouldn't the `bool()` function force a simplification? And why the behavior is different (and correct) for `bool(sqrt((x-1)^3)-(x-1)*sqrt(x-1)==0)`?
Thanks
sgiaSun, 30 Jan 2022 02:42:05 +0100https://ask.sagemath.org/question/60870/max_symbolic, polynomial expressions, assumptionshttps://ask.sagemath.org/question/56120/max_symbolic-polynomial-expressions-assumptions/ The following works as expected:
var('b')
with assuming(b>1):
v = max_symbolic(7*b-5,0).full_simplify()
print(v)
yields 7*b - 5
The following does not (maybe my expectations are unrealistic)
with assuming(b>1):
v = max_symbolic(b^2-b+1,0).full_simplify()
print(v)
yields max(0, b^2 - b + 1), even though the latter expression is always > 3/4
I have a bunch of expressions such as the one below, that I want to analyze, to get a piecewise-defined function:
print(myexpr,"\n\n")
print(myexpr.full_simplify(),"\n\n")
with assuming(a>2):
print(myexpr.full_simplify(),"\n\n")
max(max(3, a + 1) + 2*max(2*max(3, a + 1) + 1, a + max(3, a + 1)), a*max(3, a + 1) + max(2*max(3, a + 1) + 1, a + max(3, a + 1)))
max(max(3, a + 1) + 2*max(2*max(3, a + 1) + 1, a + max(3, a + 1)), a*max(3, a + 1) + max(2*max(3, a + 1) + 1, a + max(3, a + 1)))
max(5*a + 7, a^2 + 3*a + 3)
This is good, however:
with assuming(a>2000):
print(myexpr.full_simplify())
yields max(5*a + 7, a^2 + 3*a + 3)
Can such expressions, as above, in max_symbolic be converted into piecewise defined functions? (I would also be interested in having
more than one parameter, and having SAGE partition the parameter space appropriately and automatically).
JTSThu, 11 Mar 2021 13:39:33 +0100https://ask.sagemath.org/question/56120/simplifying 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 12:13:42 +0100https://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 19:32:18 +0100https://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 BardWed, 05 Aug 2020 01:09:56 +0200https://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 19:12:44 +0200https://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 11:37:52 +0200https://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 08:57:16 +0200https://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 +...JGCWed, 12 Feb 2020 00:00:22 +0100https://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 17:33:55 +0100https://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 12:25:45 +0200https://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 09:41:24 +0200https://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 13:47:22 +0100https://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 CalSun, 27 Nov 2016 00:35:11 +0100https://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 15:43:40 +0200https://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 17:12:14 +0200https://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$$
freako89Mon, 16 Feb 2015 03:45:32 +0100https://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 17:44:44 +0100https://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 12:55:00 +0200https://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 14:27:05 +0200https://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 20:06:15 +0100https://ask.sagemath.org/question/9690/