# Why can you expand with one unknown but not more?

If I say

expand((x+y)*(x-y)) it says y is not defined, so I define y as 5 and get x^2 - 25

But I never defined x. If it works symbolically for undefined x, why not for y? The expansion would then obviously be x^2 - y^2.

edit retag close merge delete

Sort by ยป oldest newest most voted

If you look at whatever documentation you found expand in, you'll note that somewhere along the line Sage needs to have y defined as a variable, like so:

var('y')
expand((x+y)*(x-y))


output:

x^2 - y^2


This was a compromise long ago between having all lower (or even upper) case variable names defined, and having none defined. It has worked reasonably well so far, especially since Sage's magic makes y defined automatically if you do something like

f(x,y) = (x+y)*(x-y)
expand(f)

more

To complement @kcrisman answer, tt startup, Sage imports some things into the namespace. For example, the python name I points to the symbolic imaginary number. Here, the python name x points to the symbol "x":

sage: I
I
sage: I^2
-1
sage: I.parent()
Symbolic Ring
sage: x
x
sage: x^2
x^2
sage: x.parent()
Symbolic Ring


But the python variable is not defined. You can define it as:

sage: y = SR.var('y')
sage: y
y
sage: y^2
y^2


If you want to have this defined at Sage startup, you can add y = SR.var('y') in your ~/.sageinit.sage.

If you want Sage to do this by default, then the question will be "where to stop ?", should we also define z,s,t?

Note the following trick, which only works on the Sage notebook (soon deprecated), will define the undefined Python variables on the fly to point to the symbol with the same name:

sage: automatic_names(True)
sage: x + y + z
x + y + z

more

You know, I never thought of perhaps trying to import automatic_names to the Jupyter...

( 2017-04-05 05:22:15 +0200 )edit