# Difference(s) between SR.var and SR.symbol ?

Almost all is in the title.

The online documentation of SR.symbol is made only of examples ; it deserves at least a skimpy justification of the existence of SR.symbol...

Reading the source code suggests that the actual work of creating a variable is done by SR.symbol, and that SR.var is a wrapper handling the processing of name(s), domain, LaTeX names, etc...

If so, what would be the point of using SR.symbol directly instead of SR.var ?

edit retag close merge delete

Sort by » oldest newest most voted

Here is a personal comment: there is a real pedagogical benefit in using symbol instead of variable, as variablehas too many meanings, and causes a lot of confusions, as a consequence we do not count the number of examples provided on ask.sagemath.org with the things like:

sage: var("t")
....: t = 2
....: # do something with t


As if varwas used to delcare a variable. Instead, i prefer to use separate denominations for distincts concepts:

• t is a Python name when we write t=2
• t is a symbol when we write expr = exp(t) + sqrt(2)
• t is an indeterminate when we write R.<t> = PolynomialRing(QQ)

Then, conistently, i tend to use SR.symbol instead of SR.var, not to mention the ugly var, which both creates a Python name and returns a symbol.

I would vote +1 if there were a SR.symbols doing the same wrapping as SR.var. Note that SR.symbol and SR.var do not have the same semantics:

sage: a = SR.symbol('a,z')
sage: a
a,z
sage: a + a
2*a,z
sage: a = SR.var('a,z')
sage: a
(a, z)
sage: a^2
a^2

more

SR.symbol() seems to be lower-level than SR.var(). Am I correct? I just checked the code of both functions, and the former seems to make the hard work for the latter. In simple terms, SR.var() seems to be a wrapper for SR.symbol() with additional functionality, like defining multiple symbols.

I really like the idea of an SR.symbols(). Could Sage define it just as SR.symbols = SR.var?

I do not get your timings:

sage: %timeit a = SR.symbol('a')
743 ns ± 12.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
sage: %timeit a = SR.var('a')
956 ns ± 23.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)


I was going to open a ticket, but then i noticed that SR.symbols already means something:

sage: SR.symbols
{'a': a,
'b': b,
'c': c,
'd': d,
'f': f,
'g': g,
'h': h,
'j': j,
'k': k,
'l': l,
...


I do not know the exact use of that, in particular if it could be replaced with a hidden dict somehow.

Hello, @tmonteil! I am sorry, but I didn't understand your comment about timings. On the other hand, concerning SR.symbols, you are right; I actually forgot about is already exists. However, I can contribute with some insight: SR.symbols is a dictionary where all predefined and user-defined symbols are stored. For example, if you do something like var('foo') or SR.var('bar') or even SR.symbol('foo'), then you will see that SR.symbols['foo'] contains the variable foo. My appreciation is that this is not intended to direct user access, so perhaps it should be renamed SR._symbols_ or something like that.

What I cannot answer is why SR.symbols stores a, b, etc. as predefined variables. Perhaps it is a matter of speed? (not totally convinced of that.) For example, when you run SR.var('a') or var('a'), this calls SR.symbol('a'), which in turn checks if SR.symbols['a'] is already defined.

Let me do a "grep" on the complete Sage code in order to determine if this can indeed can be renamed as SR._symbols_. Not only that would be convenient, but also Python-de-facto-compliant if indeed it is not intended for direct user access.