Ask Your Question
0

Is there any difference between the two different ways of declaring variables?

asked 2020-10-22 04:11:30 +0100

cybervigilante gravatar image

I sometimes see variables declared as y,z,t = var("y z t") and others simply as var("y z t") Both seem to work. Is there any actual difference between them?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-10-22 21:59:23 +0100

nbruin gravatar image

The top-level var has this "binding injection behaviour" to avoid having to write the print name of the symbolic variable "x" and the python symbol you bind it to separately, since generally you'd want them to agree. The non-toplevel SR.var does not have that behaviour, so SR.var("x,y,z") returns the right symbols, but doesn't bind them. You have to assign them, giving you the option of naming them differently, like

symbol_x, symbol_y = SR.var("x,y")

(with the top-level var this would result in symbol_x as well as x being bound to the same symbol x). Explicit assignment can sometimes be useful to avoid name clashes.

You can invoke darker magic to get the result of the top-level var. For instance, once you've done var('symbols') you can use

_(x,y,z)=symbols

which works because of sage's preparser:

sage: preparse("_(x,y,z)=symbols")
'__tmp__=var("x,y,z"); _ = symbolic_expression(symbols).function(x,y,z)'

so it calls var for you.

Alternatively, with the following definition:

class symbols:
    def __init__(self, names):
        self._symbols = map(SR.symbol, names)
    def _first_ngens(self,n):
        return self._symbols[:n]

you'd be able to leverage another preparser feature to write

_.<x,y,z>=symbols()

because it preparses as:

sage: preparse("_.<x,y,z>=symbols()")
"_ = symbols(names=('x', 'y', 'z',)); (x, y, z,) = _._first_ngens(3)"
edit flag offensive delete link more
0

answered 2020-10-22 16:31:23 +0100

Emmanuel Charpentier gravatar image

updated 2020-10-22 16:39:54 +0100

var("x,y,z") :

  • creates the Sage objects representing the symbolic variables called $x$, $y$ an,d $z$,

  • attachs them to the Python variables x, y and z, and

  • returns them (as a tuple).

The difference is that x, y, z = var("x, y, z") re-affects the elements of the tuple to the Python variables x, y and z, which is pretty unuseful but forbids the Python interpreter to print them.

The homonymy between symbolic variable names and the names pf the Python variables pointing to them is often quite useful. A great way to confuse oneself (read : to shoot oneself in the foot) is to break it ; for example, it would be legal to write y, z, x=var("x, y, z") ; this can happen in successive "improvements" to a portion of code (don't ask me how I know it... ;-).

A potentially more useful use would be to affect the tuple to another Python variable representing them collectively. See my answer to this question for an (useful) example.

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

Stats

Asked: 2020-10-22 04:11:30 +0100

Seen: 376 times

Last updated: Oct 22 '20