Ask Your Question
1

Why can you expand with one unknown but not more?

asked 2017-04-04 06:31:07 +0200

cybervigilante gravatar image

updated 2017-05-07 18:04:29 +0200

tmonteil 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
2

answered 2017-04-04 13:50:17 +0200

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:

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

answered 2017-04-04 15:58:49 +0200

tmonteil gravatar image

updated 2017-04-04 16:00:07 +0200

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

Comments

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

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

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

Stats

Asked: 2017-04-04 06:31:07 +0200

Seen: 312 times

Last updated: Apr 04 '17