Ask Your Question
3

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

asked 2021-03-07 12:27:56 +0200

Emmanuel Charpentier gravatar image

updated 2021-03-08 02:02:03 +0200

tmonteil gravatar image

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 ?

Comments ?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2021-03-07 14:18:35 +0200

tmonteil gravatar image

updated 2021-03-07 14:38:17 +0200

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[0]^2                                                                                                                                                                                                 
a^2
edit flag offensive delete link more

Comments

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?

dsejas gravatar imagedsejas ( 2021-03-08 06:59:26 +0200 )edit

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)
tmonteil gravatar imagetmonteil ( 2021-03-08 11:19:01 +0200 )edit

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.

tmonteil gravatar imagetmonteil ( 2021-03-08 17:10:07 +0200 )edit

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.

dsejas gravatar imagedsejas ( 2021-03-09 05:07:47 +0200 )edit

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.

dsejas gravatar imagedsejas ( 2021-03-09 05:14:31 +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: 2021-03-07 12:27:56 +0200

Seen: 107 times

Last updated: Mar 07