ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 28 Dec 2014 08:26:38 -0600Variables in Sagehttps://ask.sagemath.org/question/25354/variables-in-sage/ I started using sage today and I encountered sime strange errors:
I declared variables a,b,c,d using var('a,b,c,d'). When I tried to expand ($a^2+b^2)(c^2+d^2)$
(expand($(a^2+b^2)(c^2+d^2$)) I got the following result:
$c^4+2c^2d^2+d^4+b^2$,
which is obviously not the correct result. Does anyone know why I got this incorrect andwer?
Sun, 28 Dec 2014 05:09:21 -0600https://ask.sagemath.org/question/25354/variables-in-sage/Answer by Luca for <p>I started using sage today and I encountered sime strange errors:</p>
<p>I declared variables a,b,c,d using var('a,b,c,d'). When I tried to expand ($a^2+b^2)(c^2+d^2)$</p>
<p>(expand($(a^2+b^2)(c^2+d^2$)) I got the following result:</p>
<p>$c^4+2c^2d^2+d^4+b^2$,</p>
<p>which is obviously not the correct result. Does anyone know why I got this incorrect andwer?</p>
https://ask.sagemath.org/question/25354/variables-in-sage/?answer=25356#post-id-25356You must have forgotten the multiplication sign `*`. Quite obviously, $(c^2+d^2)$ was substituted for $a$ in the expression, thus you got $(c^2+d^2)^2 + b^2$. If you are using a recent version of Sage, you must have gotten a warning when evaluating the expression, saying that the correct form for expression substitution is
(a^2 + b^2)(a=(c^2 + d^2))
In Sage 6.4.1
sage: var('a,b')
(a, b)
sage: (a+b)(a+b)
/home/dfl/sage/local/lib/python2.7/site-packages/IPython/core/interactiveshell.py:2883: DeprecationWarning:
Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future
release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
exec(code_obj, self.user_global_ns, self.user_ns)
a + 2*b
sage: (a+b)*(a+b)
(a + b)^2
Sun, 28 Dec 2014 08:26:38 -0600https://ask.sagemath.org/question/25354/variables-in-sage/?answer=25356#post-id-25356Answer by FrédéricC for <p>I started using sage today and I encountered sime strange errors:</p>
<p>I declared variables a,b,c,d using var('a,b,c,d'). When I tried to expand ($a^2+b^2)(c^2+d^2)$</p>
<p>(expand($(a^2+b^2)(c^2+d^2$)) I got the following result:</p>
<p>$c^4+2c^2d^2+d^4+b^2$,</p>
<p>which is obviously not the correct result. Does anyone know why I got this incorrect andwer?</p>
https://ask.sagemath.org/question/25354/variables-in-sage/?answer=25355#post-id-25355This works perfectly:
sage: var('a,b,c,d')
(a, b, c, d)
sage: t = (a**2+b**2)*(c**2+d**2)
sage: t
(a^2 + b^2)*(c^2 + d^2)
sage: expand(t)
a^2*c^2 + b^2*c^2 + a^2*d^2 + b^2*d^2
Maybe you made a mistake in the parentheses ?Sun, 28 Dec 2014 06:24:47 -0600https://ask.sagemath.org/question/25354/variables-in-sage/?answer=25355#post-id-25355