Opened 22 months ago

Last modified 18 months ago

#25178 new defect

SR('I') is not consistent

Reported by: vdelecroix Owned by:
Priority: major Milestone: sage-8.4
Component: symbolics Keywords:
Cc: rws, slelievre Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by vdelecroix)

This behavior is confusing

sage: SR('I') ** 2   # complex imaginary
-1
sage: SR.var('I')
I
sage: SR('I') ** 2   # now a variable!
I^2

So far different solutions are under consideration

  1. Let SR('I') always return the imaginary unit (and use SR.var(string) to create variables)
  2. Add SR.I() as a method and let SR(string) always return a variable
  3. Create methods SR.e(), SR.I(), SR.pi() for named constants and let SR(string) always create variables.
  4. Forbid the names e, I, pi, etc as variable names.

Change History (14)

comment:1 Changed 22 months ago by moritz

I am not sure if this is really so bad. It is the same for other constants:

sage: SR('e').n()   # Euler's constant
2.71828182845905
sage: SR.var('e')   
e
sage: SR('e').n()   # now a variable
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

TypeError: cannot evaluate symbolic expression numerically

But I can imagine that it would be very useful to use e sometimes as a symbolic variable (e.g. when using a, b, c and d before..)

comment:2 Changed 22 months ago by jdemeyer

I agree that this is not a bug. I is just a variable with a default value of sqrt(-1), but that value can be changed.

comment:3 Changed 22 months ago by vdelecroix

My complaint is about the way SR(whatever) is interpreting its argument. When I do SR.var('my_name') I clearly want a new variable name. But the behavior of SR(whatever) depends on the variables created at a given time. It is easy to imagine

def f():
    a,b,c,d,e = SR.var('a,b,c,d,e')
    # now using the symbolic variables

m = f()
e = SR('e')  # this is not anymore exp(1)!!

comment:4 Changed 22 months ago by moritz

What do you propose to do? What behavior do you want to have? Should SR.var('a,b,c,d,e') raise an error?

I think a line like a,b,c,d,e = SR.var('a,b,c,d,e') or `a,b,c,d,e = var('a,b,c,d,e')' is very natural. In fact:

sage: search_src("var\('a, b, c, d, e")
tensor/coordinate_patch.py:230:            sage: a, b, c, d, e = var('a, b, c, d, e')
tests/french_book/nonlinear_doctest.py:64:    sage: a, b, c, d, e, f, x = var('a, b, c, d, e, f, x')
matrix/special.py:3625:        sage: matrix.hankel(SR.var('a, b, c, d, e'))
matrix/special.py:3634:        sage: matrix.hankel(SR.var('a, b, c, d, e'), SR.var('f, g, h, i'))

comment:5 Changed 22 months ago by vdelecroix

  • option 1: keep SR.var('e') (variable creation) and make SR('e') always return exp(1) for consistency
  • option 2: forbid standard variable names (e, pi, cos, etc)

comment:6 Changed 22 months ago by vdelecroix

  • Summary changed from I should not be allowed as a symbolic variable name to SR('I') is not consistent

comment:7 follow-up: Changed 22 months ago by tmonteil

One option could be to have SR.I(), SR.e(), etc working the same way as the existing SR.pi(), and let SR(string) return/inject a symbolic variable.

comment:8 Changed 22 months ago by vdelecroix

  • Description modified (diff)

comment:9 follow-up: Changed 22 months ago by slelievre

  • Cc slelievre added

I don't see how

sage: SR('I') ** 2   # complex imaginary
-1
sage: SR.var('I')
I
sage: SR('I') ** 2   # now a variable!
I^2

is any more confusing than

sage: exp(1)         # exponential
e
sage: exp = log
sage: exp(1)         # now logarithm!
0

comment:10 in reply to: ↑ 9 Changed 22 months ago by vdelecroix

Replying to slelievre:

I don't see how

sage: SR('I') ** 2   # complex imaginary
-1
sage: SR.var('I')
I
sage: SR('I') ** 2   # now a variable!
I^2

is any more confusing than

sage: exp(1)         # exponential
e
sage: exp = log
sage: exp(1)         # now logarithm!
0

See 3.

comment:11 Changed 22 months ago by vdelecroix

More precisely

def f():
    exp = log
    SR.var('e')
f()
exp(x)  # still exp(x)
SR('e') # e is a variable here

comment:12 Changed 22 months ago by slelievre

I get it now. The changed behaviour would be expected after

sage: I = SR.var('I')

but not after

sage: SR.var('I')

comment:13 in reply to: ↑ 7 Changed 21 months ago by vdelecroix

  • Description modified (diff)
  • Milestone changed from sage-8.2 to sage-8.3

Replying to tmonteil:

One option could be to have SR.I(), SR.e(), etc working the same way as the existing SR.pi(), and let SR(string) return/inject a symbolic variable.

Indeed.

comment:14 Changed 18 months ago by vdelecroix

  • Milestone changed from sage-8.3 to sage-8.4

update milestone 8.3 -> 8.4

Note: See TracTickets for help on using tickets.