The symbolic ring

class sage.symbolic.ring.NumpyToSRMorphism

Bases: sage.categories.morphism.Morphism

A morphism from numpy types to the symbolic ring.

class sage.symbolic.ring.SymbolicRing

Bases: sage.rings.ring.CommutativeRing

Symbolic Ring, parent object for all symbolic expressions.

I()

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

EXAMPLES:

sage: SR.I()^2
-1
sage: SR.I().parent()
Symbolic Ring
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'>
cleanup_var(symbol)

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()

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
is_finite()

Return False, since the Symbolic Ring is infinite.

EXAMPLES:

sage: SR.is_finite()
False
pi()

EXAMPLES:

sage: SR.pi() is pi
True
subring(*args, **kwds)

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')

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

An element is

sage: 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)

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

An element is

sage: 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)

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

sage: 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

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

sage: tuple(v for v in V if v in C)
()
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...

We propagate the domain to the assumptions database:

sage: n = var('n', domain='integer')
sage: solve([n^2 == 3],n)
[]
symbols
temp_var(n=None, domain=None)

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)

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

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 the complex plane 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\) (complex by default):

sage: zz = SR.var('zz'); zz
zz

The return type is a symbolic expression:

sage: type(zz)
<type 'sage.symbolic.expression.Expression'>

We can specify the domain as well:

sage: zz = SR.var('zz', domain='real')
sage: zz.is_real()
True

The real domain is also set with the integer domain:

sage: 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

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
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.TemporaryVariables

Bases: tuple

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

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)

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

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
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