Bases: sage.categories.morphism.Morphism
A morphism from numpy types to the symbolic ring.
TESTS:
We check that trac ticket #8949 and trac ticket #9769 are fixed (see also trac ticket #18076):
sage: import numpy
sage: f(x) = x^2
sage: f(numpy.int8('2'))
4
sage: f(numpy.int32('3'))
9
Note that the answer is a Sage integer and not a numpy type:
sage: a = f(numpy.int8('2')).pyobject()
sage: type(a)
<type 'sage.rings.integer.Integer'>
This behavior also applies to standard functions:
sage: cos(numpy.int('2'))
cos(2)
sage: numpy.cos(numpy.int('2'))
-0.41614683654714241
Bases: sage.rings.ring.CommutativeRing
Symbolic Ring, parent object for all symbolic expressions.
Return the characteristic of the symbolic ring, which is 0.
OUTPUT:
EXAMPLES:
sage: c = SR.characteristic(); c
0
sage: type(c)
<type 'sage.rings.integer.Integer'>
Return False, because there are approximate elements in the symbolic ring.
EXAMPLES:
sage: SR.is_exact()
False
Here is an inexact element.
sage: SR(1.9393)
1.93930000000000
Returns True, since the symbolic expression ring is (for the most part) a field.
EXAMPLES:
sage: SR.is_field()
True
Return False, since the Symbolic Ring is infinite.
EXAMPLES:
sage: SR.is_finite()
False
EXAMPLES:
sage: SR.pi() is pi
True
EXAMPLES:
sage: t0 = SR.symbol("t0")
sage: t0.conjugate()
conjugate(t0)
sage: t1 = SR.symbol("t1", domain='real')
sage: t1.conjugate()
t1
sage: t0.abs()
abs(t0)
sage: t0_2 = SR.symbol("t0", domain='positive')
sage: t0_2.abs()
t0
sage: bool(t0_2 == t0)
True
sage: t0.conjugate()
t0
sage: SR.symbol() # temporary variable
symbol...
Return the symbolic variable defined by x as an element of the symbolic ring.
EXAMPLES:
sage: zz = SR.var('zz'); zz
zz
sage: type(zz)
<type 'sage.symbolic.expression.Expression'>
sage: t = SR.var('theta2'); t
theta2
TESTS:
sage: var(' x y z ')
(x, y, z)
sage: var(' x , y , z ')
(x, y, z)
sage: var(' ')
Traceback (most recent call last):
...
ValueError: You need to specify the name of the new variable.
var(['x', 'y ', ' z '])
(x, y, z)
var(['x,y'])
Traceback (most recent call last):
...
ValueError: The name "x,y" is not a valid Python identifier.
Check that trac ticket #17206 is fixed:
sage: var1 = var('var1', latex_name=r'\sigma^2_1'); latex(var1)
{\sigma^2_1}
Return the n-th wild-card for pattern matching and substitution.
INPUT:
OUTPUT:
EXAMPLES:
sage: x,y = var('x,y')
sage: w0 = SR.wild(0); w1 = SR.wild(1)
sage: pattern = sin(x)*w0*w1^2; pattern
$1^2*$0*sin(x)
sage: f = atan(sin(x)*3*x^2); f
arctan(3*x^2*sin(x))
sage: f.has(pattern)
True
sage: f.subs(pattern == x^2)
arctan(x^2)
TESTS:
Check that trac ticket #15047 is fixed:
sage: latex(SR.wild(0))
$0
Bases: sage.categories.morphism.Morphism
A Morphism which constructs Expressions from an arbitrary Python object by calling the _sage_() method on the object.
EXAMPLES:
sage: import sympy
sage: from sage.symbolic.ring import UnderscoreSageMorphism
sage: b = sympy.var('b')
sage: f = UnderscoreSageMorphism(type(b), SR)
sage: f(b)
b
sage: _.parent()
Symbolic Ring
Returns True if R is the symbolic expression ring.
EXAMPLES:
sage: from sage.symbolic.ring import is_SymbolicExpressionRing
sage: is_SymbolicExpressionRing(ZZ)
False
sage: is_SymbolicExpressionRing(SR)
True
Returns True if x is a variable.
EXAMPLES:
sage: from sage.symbolic.ring import is_SymbolicVariable
sage: is_SymbolicVariable(x)
True
sage: is_SymbolicVariable(x+2)
False
TESTS:
sage: ZZ['x']
Univariate Polynomial Ring in x over Integer Ring
Return whether x is a valid identifier.
When we switch to Python 3 this function can be replaced by the official Python function of the same name.
INPUT:
OUTPUT:
Boolean. Whether the string x can be used as a variable name.
EXAMPLES:
sage: from sage.symbolic.ring import isidentifier
sage: isidentifier('x')
True
sage: isidentifier(' x') # can't start with space
False
sage: isidentifier('ceci_n_est_pas_une_pipe')
True
sage: isidentifier('1 + x')
False
sage: isidentifier('2good')
False
sage: isidentifier('good2')
True
sage: isidentifier('lambda s:s+1')
False
Return the unique symbolic ring object.
(This is mainly used for unpickling.)
EXAMPLES:
sage: sage.symbolic.ring.the_SymbolicRing()
Symbolic Ring
sage: sage.symbolic.ring.the_SymbolicRing() is sage.symbolic.ring.the_SymbolicRing()
True
sage: sage.symbolic.ring.the_SymbolicRing() is SR
True
EXAMPLES:
sage: from sage.symbolic.ring import var
sage: var("x y z")
(x, y, z)
sage: var("x,y,z")
(x, y, z)
sage: var("x , y , z")
(x, y, z)
sage: var("z")
z
TESTS:
These examples test that variables can only be made from valid identifiers. See Trac 7496 (and 9724) for details:
sage: var(' ')
Traceback (most recent call last):
...
ValueError: You need to specify the name of the new variable.
sage: var('3')
Traceback (most recent call last):
...
ValueError: The name "3" is not a valid Python identifier.