The symbolic ring¶
-
class
sage.symbolic.ring.
NumpyToSRMorphism
¶ 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
-
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
-
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) ()
See also
-
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) []
TESTS:
Test that the parent is set correctly (inheritance):
sage: from sage.symbolic.ring import SymbolicRing sage: class MySymbolicRing(SymbolicRing): ....: def _repr_(self): ....: return 'My Symbolic Ring' sage: MySR = MySymbolicRing() sage: MySR.symbol('x').parent() My Symbolic Ring sage: MySR.var('x').parent() # indirect doctest My Symbolic Ring sage: MySR.var('blub').parent() # indirect doctest My Symbolic Ring sage: MySR.an_element().parent() My Symbolic Ring
-
symbols
¶
-
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.
Check that trac ticket #17206 is fixed:
sage: var1 = var('var1', latex_name=r'\sigma^2_1'); latex(var1) {\sigma^2_1}
-
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)
TESTS:
Check that trac ticket #15047 is fixed:
sage: latex(SR.wild(0)) $0
-
-
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 ticket #7496 (and trac ticket #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.