Ask Your Question

Why can you expand with one unknown but not more?

asked 2017-04-03 23:31:07 -0500

cybervigilante gravatar image

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 flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-04-04 08:58:49 -0500

tmonteil gravatar image

updated 2017-04-04 09:00:07 -0500

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
sage: I^2
sage: I.parent()
Symbolic Ring
sage: x
sage: 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
sage: 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
edit flag offensive delete link more


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

kcrisman gravatar imagekcrisman ( 2017-04-04 22:22:15 -0500 )edit

answered 2017-04-04 06:50:17 -0500

kcrisman gravatar image

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:



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)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-04-03 23:31:07 -0500

Seen: 31 times

Last updated: Apr 04