The symbolic ring

The symbolic ring

class sage.symbolic.ring.NumpyToSRMorphism

Bases: sage.categories.morphism.Morphism

A Morphism which constructs Expressions from NumPy floats and complexes by converting them to elements of either RDF or CDF.

EXAMPLES:

sage: import numpy
sage: from sage.symbolic.ring import NumpyToSRMorphism
sage: f = NumpyToSRMorphism(numpy.float64, SR)
sage: f(numpy.float64('2.0'))
2.0
sage: _.parent()
Symbolic Ring
class sage.symbolic.ring.SymbolicRing

Bases: sage.rings.ring.CommutativeRing

Symbolic Ring, parent object for all symbolic expressions.

characteristic()

Return the characteristic of the symbolic ring, which is 0.

OUTPUT:

  • a Sage integer

EXAMPLES:

sage: c = SR.characteristic(); c
0
sage: type(c)
<type 'sage.rings.integer.Integer'>
is_exact()

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
is_field(proof=True)

Returns True, since the symbolic expression ring is (for the most part) a field.

EXAMPLES:

sage: SR.is_field()
True
pi()

EXAMPLES:

sage: SR.pi() is pi
True
symbol(name=None, latex_name=None, domain=None)

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...
var(name, latex_name=None, domain=None)

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.
wild(n=0)

Return the n-th wild-card for pattern matching and substitution.

INPUT:

  • n - a nonnegative integer

OUTPUT:

  • \(n^{th}\) wildcard expression

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)
class sage.symbolic.ring.UnderscoreSageMorphism

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
sage.symbolic.ring.is_SymbolicExpressionRing(R)

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
sage.symbolic.ring.is_SymbolicVariable(x)

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
sage.symbolic.ring.isidentifier(x)

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:

  • x – a string.

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
sage.symbolic.ring.the_SymbolicRing()

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
sage.symbolic.ring.var(name, **kwds)

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.

Previous topic

Units of measurement

Next topic

Functional notation support for common calculus methods

This Page