The symbolic ring#

class sage.symbolic.ring.NumpyToSRMorphism[source]#

Bases: Morphism

A morphism from numpy types to the symbolic ring.

class sage.symbolic.ring.SymbolicRing[source]#

Bases: SymbolicRing

Symbolic Ring, parent object for all symbolic expressions.

I()[source]#

The imaginary unit, viewed as an element of the symbolic ring.

EXAMPLES:

sage: SR.I()^2
-1
sage: SR.I().parent()
Symbolic Ring
>>> from sage.all import *
>>> SR.I()**Integer(2)
-1
>>> SR.I().parent()
Symbolic Ring
characteristic()[source]#

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

OUTPUT:

  • a Sage integer

EXAMPLES:

sage: c = SR.characteristic(); c
0
sage: type(c)
<class 'sage.rings.integer.Integer'>
>>> from sage.all import *
>>> c = SR.characteristic(); c
0
>>> type(c)
<class 'sage.rings.integer.Integer'>
cleanup_var(symbol)[source]#

Cleans up a variable, removing assumptions about the variable and allowing for it to be garbage collected

INPUT:

  • symbol – a variable or a list of variables

is_exact()[source]#

Return False, because there are approximate elements in the symbolic ring.

EXAMPLES:

sage: SR.is_exact()
False
>>> from sage.all import *
>>> SR.is_exact()
False

Here is an inexact element.

sage: SR(1.9393)
1.93930000000000
>>> from sage.all import *
>>> SR(RealNumber('1.9393'))
1.93930000000000
is_field(proof=True)[source]#

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

EXAMPLES:

sage: SR.is_field()
True
>>> from sage.all import *
>>> SR.is_field()
True
is_finite()[source]#

Return False, since the Symbolic Ring is infinite.

EXAMPLES:

sage: SR.is_finite()
False
>>> from sage.all import *
>>> SR.is_finite()
False
pi()[source]#

EXAMPLES:

sage: SR.pi() is pi
True
>>> from sage.all import *
>>> SR.pi() is pi
True
subring(*args, **kwds)[source]#

Create a subring of this symbolic ring.

INPUT:

Choose one of the following keywords to create a subring.

  • accepting_variables (default: None) – a tuple or other iterable of variables. If specified, then a symbolic subring of expressions in only these variables is created.

  • rejecting_variables (default: None) – a tuple or other iterable of variables. If specified, then a symbolic subring of expressions in variables distinct to these variables is created.

  • no_variables (default: False) – a boolean. If set, then a symbolic subring of constant expressions (i.e., expressions without a variable) is created.

OUTPUT:

A ring.

EXAMPLES:

Let us create a couple of symbolic variables first:

sage: V = var('a, b, r, s, x, y')
>>> from sage.all import *
>>> V = var('a, b, r, s, x, y')

Now we create a symbolic subring only accepting expressions in the variables \(a\) and \(b\):

sage: A = SR.subring(accepting_variables=(a, b)); A
Symbolic Subring accepting the variables a, b
>>> from sage.all import *
>>> A = SR.subring(accepting_variables=(a, b)); A
Symbolic Subring accepting the variables a, b

An element is

sage: A.an_element()
a
>>> from sage.all import *
>>> A.an_element()
a

From our variables in \(V\) the following are valid in \(A\):

sage: tuple(v for v in V if v in A)
(a, b)
>>> from sage.all import *
>>> tuple(v for v in V if v in A)
(a, b)

Next, we create a symbolic subring rejecting expressions with given variables:

sage: R = SR.subring(rejecting_variables=(r, s)); R
Symbolic Subring rejecting the variables r, s
>>> from sage.all import *
>>> R = SR.subring(rejecting_variables=(r, s)); R
Symbolic Subring rejecting the variables r, s

An element is

sage: R.an_element()
some_variable
>>> from sage.all import *
>>> R.an_element()
some_variable

From our variables in \(V\) the following are valid in \(R\):

sage: tuple(v for v in V if v in R)
(a, b, x, y)
>>> from sage.all import *
>>> tuple(v for v in V if v in R)
(a, b, x, y)

We have a third kind of subring, namely the subring of symbolic constants:

sage: C = SR.subring(no_variables=True); C
Symbolic Constants Subring
>>> from sage.all import *
>>> C = SR.subring(no_variables=True); C
Symbolic Constants Subring

Note that this subring can be considered as a special accepting subring; one without any variables.

An element is

sage: C.an_element()
I*pi*e
>>> from sage.all import *
>>> C.an_element()
I*pi*e

None of our variables in \(V\) is valid in \(C\):

sage: tuple(v for v in V if v in C)
()
>>> from sage.all import *
>>> tuple(v for v in V if v in C)
()
symbol(name=None, latex_name=None, domain=None)[source]#

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...
>>> from sage.all import *
>>> t0 = SR.symbol("t0")
>>> t0.conjugate()
conjugate(t0)

>>> t1 = SR.symbol("t1", domain='real')
>>> t1.conjugate()
t1

>>> t0.abs()
abs(t0)

>>> t0_2 = SR.symbol("t0", domain='positive')
>>> t0_2.abs()
t0
>>> bool(t0_2 == t0)
True
>>> t0.conjugate()
t0

>>> SR.symbol() # temporary variable
symbol...

We propagate the domain to the assumptions database:

sage: n = var('n', domain='integer')
sage: solve([n^2 == 3],n)
[]
>>> from sage.all import *
>>> n = var('n', domain='integer')
>>> solve([n**Integer(2) == Integer(3)],n)
[]
symbols[source]#
temp_var(n=None, domain=None)[source]#

Return one or multiple new unique symbolic variables as an element of the symbolic ring. Use this instead of SR.var() if there is a possibility of name clashes occuring. Call SR.cleanup_var() once the variables are no longer needed or use a \(with SR.temp_var() as ...\) construct.

INPUT:

  • n – (optional) positive integer; number of symbolic variables

  • domain – (optional) specify the domain of the variable(s);

EXAMPLES:

Simple definition of a functional derivative:

sage: def functional_derivative(expr,f,x):
....:     with SR.temp_var() as a:
....:         return expr.subs({f(x):a}).diff(a).subs({a:f(x)})
sage: f = function('f')
sage: a = var('a')
sage: functional_derivative(f(a)^2+a,f,a)
2*f(a)
>>> from sage.all import *
>>> def functional_derivative(expr,f,x):
...     with SR.temp_var() as a:
...         return expr.subs({f(x):a}).diff(a).subs({a:f(x)})
>>> f = function('f')
>>> a = var('a')
>>> functional_derivative(f(a)**Integer(2)+a,f,a)
2*f(a)

Contrast this to a similar implementation using SR.var(), which gives a wrong result in our example:

sage: def functional_derivative(expr,f,x):
....:     a = SR.var('a')
....:     return expr.subs({f(x):a}).diff(a).subs({a:f(x)})
sage: f = function('f')
sage: a = var('a')
sage: functional_derivative(f(a)^2+a,f,a)
2*f(a) + 1
>>> from sage.all import *
>>> def functional_derivative(expr,f,x):
...     a = SR.var('a')
...     return expr.subs({f(x):a}).diff(a).subs({a:f(x)})
>>> f = function('f')
>>> a = var('a')
>>> functional_derivative(f(a)**Integer(2)+a,f,a)
2*f(a) + 1
var(name, latex_name=None, n=None, domain=None)[source]#

Return a symbolic variable as an element of the symbolic ring.

INPUT:

  • name – string or list of strings with the name(s) of the symbolic variable(s)

  • latex_name – (optional) string used when printing in latex mode, if not specified use 'name'

  • n – (optional) positive integer; number of symbolic variables, indexed from \(0\) to \(n-1\)

  • domain – (optional) specify the domain of the variable(s); it is None by default, and possible options are (non-exhaustive list, see note below): 'real', 'complex', 'positive', 'integer' and 'noninteger'

OUTPUT:

Symbolic expression or tuple of symbolic expressions.

See also

This function does not inject the variable(s) into the global namespace. For that purpose see var().

Note

For a comprehensive list of acceptable features type 'maxima('features')', and see also the documentation of Assumptions.

EXAMPLES:

Create a variable \(zz\):

sage: zz = SR.var('zz'); zz
zz
>>> from sage.all import *
>>> zz = SR.var('zz'); zz
zz

The return type is a symbolic expression:

sage: type(zz)
<class 'sage.symbolic.expression.Expression'>
>>> from sage.all import *
>>> type(zz)
<class 'sage.symbolic.expression.Expression'>

We can specify the domain as well:

sage: zz = SR.var('zz', domain='real')
sage: zz.is_real()
True
>>> from sage.all import *
>>> zz = SR.var('zz', domain='real')
>>> zz.is_real()
True

The real domain is also set with the integer domain:

sage: SR.var('x', domain='integer').is_real()
True
>>> from sage.all import *
>>> SR.var('x', domain='integer').is_real()
True

The name argument does not have to match the left-hand side variable:

sage: t = SR.var('theta2'); t
theta2
>>> from sage.all import *
>>> t = SR.var('theta2'); t
theta2

Automatic indexing is available as well:

sage: x = SR.var('x', 4)
sage: x[0], x[3]
(x0, x3)
sage: sum(x)
x0 + x1 + x2 + x3
>>> from sage.all import *
>>> x = SR.var('x', Integer(4))
>>> x[Integer(0)], x[Integer(3)]
(x0, x3)
>>> sum(x)
x0 + x1 + x2 + x3
wild(n=0)[source]#

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)
>>> from sage.all import *
>>> x,y = var('x,y')
>>> w0 = SR.wild(Integer(0)); w1 = SR.wild(Integer(1))
>>> pattern = sin(x)*w0*w1**Integer(2); pattern
$1^2*$0*sin(x)
>>> f = atan(sin(x)*Integer(3)*x**Integer(2)); f
arctan(3*x^2*sin(x))
>>> f.has(pattern)
True
>>> f.subs(pattern == x**Integer(2))
arctan(x^2)
class sage.symbolic.ring.TemporaryVariables(iterable=(), /)[source]#

Bases: tuple

Instances of this class can be used with Python \(with\) to automatically clean up after themselves.

class sage.symbolic.ring.UnderscoreSageMorphism[source]#

Bases: Morphism

A Morphism which constructs Expressions from an arbitrary Python object by calling the _sage_() method on the object.

EXAMPLES:

sage: # needs sympy
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
>>> from sage.all import *
>>> # needs sympy
>>> import sympy
>>> from sage.symbolic.ring import UnderscoreSageMorphism
>>> b = sympy.var('b')
>>> f = UnderscoreSageMorphism(type(b), SR)
>>> f(b)
b
>>> _.parent()
Symbolic Ring
sage.symbolic.ring.isidentifier(x)[source]#

Return whether x is a valid identifier.

INPUT:

  • x – a string

OUTPUT:

Boolean. Whether the string x can be used as a variable name.

This function should return False for keywords, so we can not just use the isidentifier method of strings, because, for example, it returns True for “def” and for “None”.

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: isidentifier('None')
False
sage: isidentifier('lambda')
False
sage: isidentifier('def')
False
>>> from sage.all import *
>>> from sage.symbolic.ring import isidentifier
>>> isidentifier('x')
True
>>> isidentifier(' x')   # can't start with space
False
>>> isidentifier('ceci_n_est_pas_une_pipe')
True
>>> isidentifier('1 + x')
False
>>> isidentifier('2good')
False
>>> isidentifier('good2')
True
>>> isidentifier('lambda s:s+1')
False
>>> isidentifier('None')
False
>>> isidentifier('lambda')
False
>>> isidentifier('def')
False
sage.symbolic.ring.the_SymbolicRing()[source]#

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
>>> from sage.all import *
>>> sage.symbolic.ring.the_SymbolicRing()
Symbolic Ring
>>> sage.symbolic.ring.the_SymbolicRing() is sage.symbolic.ring.the_SymbolicRing()
True
>>> sage.symbolic.ring.the_SymbolicRing() is SR
True
sage.symbolic.ring.var(name, **kwds)[source]#

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
>>> from sage.all import *
>>> from sage.symbolic.ring import var
>>> var("x y z")
(x, y, z)
>>> var("x,y,z")
(x, y, z)
>>> var("x , y , z")
(x, y, z)
>>> var("z")
z