Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Linear Expressions
A linear expression is just a linear polynomial in some (fixed) variables (allowing a nonzero constant term). This class only implements linear expressions for others to use.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ); L Module of linear expressions in variables x, y, z over Rational Field sage: x + 2*y + 3*z + 4 x + 2*y + 3*z + 4 sage: L(4) 0*x + 0*y + 0*z + 4
You can also pass coefficients and a constant term to construct linear expressions::
sage: L([1, 2, 3], 4) x + 2*y + 3*z + 4 sage: L([(1, 2, 3), 4]) x + 2*y + 3*z + 4 sage: L([4, 1, 2, 3]) # note: constant is first in single-tuple notation x + 2*y + 3*z + 4
The linear expressions are a module over the base ring, so you can add them and multiply them with scalars::
sage: m = x + 2*y + 3*z + 4 sage: 2*m 2*x + 4*y + 6*z + 8 sage: m+m 2*x + 4*y + 6*z + 8 sage: m-m 0*x + 0*y + 0*z + 0 """
""" A linear expression.
A linear expression is just a linear polynomial in some (fixed) variables.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: m = L([1, 2, 3], 4); m x + 2*y + 3*z + 4 sage: m2 = L([(1, 2, 3), 4]); m2 x + 2*y + 3*z + 4 sage: m3 = L([4, 1, 2, 3]); m3 # note: constant is first in single-tuple notation x + 2*y + 3*z + 4 sage: m == m2 True sage: m2 == m3 True sage: L.zero() 0*x + 0*y + 0*z + 0 sage: a = L([12, 2/3, -1], -2) sage: a - m 11*x - 4/3*y - 4*z - 6 sage: LZ.<x,y,z> = LinearExpressionModule(ZZ) sage: a - LZ([2, -1, 3], 1) 10*x + 5/3*y - 4*z - 3 """ """ Initialize ``self``.
TESTS::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: linear = L([1, 2, 3], 4) # indirect doctest sage: linear.parent() is L True
sage: TestSuite(linear).run() """ raise ValueError("coefficients are not in the ambient module") raise ValueError("coefficients are not immutable") raise ValueError("the constant is not in the base ring")
""" Return the coefficient vector.
OUTPUT:
The coefficient vector of the linear expression.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: linear = L([1, 2, 3], 4); linear x + 2*y + 3*z + 4 sage: linear.A() (1, 2, 3) sage: linear.b() 4 """
""" Return the constant term.
OUTPUT:
The constant term of the linear expression.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: linear = L([1, 2, 3], 4); linear x + 2*y + 3*z + 4 sage: linear.A() (1, 2, 3) sage: linear.b() 4 """
""" Return all coefficients.
OUTPUT:
The constant (as first entry) and coefficients of the linear terms (as subsequent entries) in a list.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: linear = L([1, 2, 3], 4); linear x + 2*y + 3*z + 4 sage: linear.coefficients() [4, 1, 2, 3] """
""" Return a dictionary whose keys are indices of basis elements in the support of ``self`` and whose values are the corresponding coefficients.
INPUT:
- ``copy`` -- ignored
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: linear = L([1, 2, 3], 4) sage: sorted(linear.monomial_coefficients().items()) [(0, 1), (1, 2), (2, 3), ('b', 4)] """
""" Return a string representation.
INPUT:
- ``variable`` -- string; the name of the variable vector
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: L([1, 2, 3], 4)._repr_vector() '(1, 2, 3) x + 4 = 0' sage: L([-1, -2, -3], -4)._repr_vector('u') '(-1, -2, -3) u - 4 = 0' """ constant = '({0})'.format(constant)
""" Return a representation as a linear polynomial.
INPUT:
- ``include_zero`` -- whether to include terms with zero coefficient
- ``include_constant`` -- whether to include the constant term
- ``multiplication`` -- string (optional, default: ``*``); the multiplication symbol to use
OUTPUT:
A string.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: L([1, 2, 3], 4)._repr_linear() 'x + 2*y + 3*z + 4' sage: L([-1, -2, -3], -4)._repr_linear() '-x - 2*y - 3*z - 4' sage: L([0, 0, 0], 1)._repr_linear() '0*x + 0*y + 0*z + 1' sage: L([0, 0, 0], 0)._repr_linear() '0*x + 0*y + 0*z + 0'
sage: R.<u,v> = QQ[] sage: L.<x,y,z> = LinearExpressionModule(R) sage: L([-u+v+1, -3*u-2, 3], -4*u+v)._repr_linear() '(-u + v + 1)*x + (-3*u - 2)*y + 3*z - 4*u + v'
sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: L([1, 0, 3], 0)._repr_linear() 'x + 0*y + 3*z + 0' sage: L([1, 0, 3], 0)._repr_linear(include_zero=False) 'x + 3*z' sage: L([1, 0, 3], 1)._repr_linear(include_constant=False, multiplication='.') 'x + 0.y + 3.z' sage: L([1, 0, 3], 1)._repr_linear(include_zero=False, include_constant=False) 'x + 3*z' sage: L([0, 0, 0], 0)._repr_linear(include_zero=False) '0' """
""" Add two linear expressions.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: a = L([1, 2, 3], 4) sage: b = L([-1, 3, -3], 0) sage: a + b 0*x + 5*y + 0*z + 4 sage: a - b 2*x - y + 6*z + 4 """
""" Multiply a linear expression by a scalar.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: a = L([1, 2, 3], 4); a x + 2*y + 3*z + 4 sage: 2 * a 2*x + 4*y + 6*z + 8 sage: a * 2 2*x + 4*y + 6*z + 8 sage: -a -x - 2*y - 3*z - 4 sage: RDF(1) * a 1.0*x + 2.0*y + 3.0*z + 4.0
TESTS::
sage: a._lmul_(2) 2*x + 4*y + 6*z + 8 """
""" Action by scalars that do not live in the base ring.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: a = x + 2*y + 3*z + 4 sage: a * RDF(3) 3.0*x + 6.0*y + 9.0*z + 12.0 """
""" Change the base ring of this linear expression.
INPUT:
- ``base_ring`` -- a ring; the new base ring
OUTPUT:
A new linear expression over the new base ring.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: a = x + 2*y + 3*z + 4; a x + 2*y + 3*z + 4 sage: a.change_ring(RDF) 1.0*x + 2.0*y + 3.0*z + 4.0 """ return self
r""" TESTS::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x> = LinearExpressionModule(QQ) sage: hash(L([0,1])) 3430019387558 # 64-bit -1659481946 # 32-bit """
""" Compare two linear expressions.
INPUT:
- ``other`` -- another linear expression (will be enforced by the coercion framework)
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x> = LinearExpressionModule(QQ) sage: x == L([0, 1]) True sage: x == x + 1 False
sage: M.<x> = LinearExpressionModule(ZZ) sage: L.gen(0) == M.gen(0) # because there is a conversion True sage: L.gen(0) == L(M.gen(0)) # this is the conversion True
sage: x == 'test' False """ (other._coeffs, other._const), op)
""" Evaluate the linear expression.
INPUT:
- ``point`` -- list/tuple/iterable of coordinates; the coordinates of a point
OUTPUT:
The linear expression `Ax + b` evaluated at the point `x`.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y> = LinearExpressionModule(QQ) sage: ex = 2*x + 3* y + 4 sage: ex.evaluate([1,1]) 9 sage: ex([1,1]) # syntactic sugar 9 sage: ex([pi, e]) 2*pi + 3*e + 4 """
""" The module of linear expressions.
This is the module of linear polynomials which is the parent for linear expressions.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) sage: L Module of linear expressions in variables x, y, z over Rational Field sage: L.an_element() x + 0*y + 0*z + 0 """
""" Initialize ``self``.
TESTS::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) sage: type(L) <class 'sage.geometry.linear_expression.LinearExpressionModule_with_category'> sage: L.base_ring() Rational Field
sage: TestSuite(L).run()
sage: L = LinearExpressionModule(QQ) sage: TestSuite(L).run() """
def basis(self): """ Return a basis of ``self``.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) sage: list(L.basis()) [x + 0*y + 0*z + 0, 0*x + y + 0*z + 0, 0*x + 0*y + z + 0, 0*x + 0*y + 0*z + 1] """ self.base_ring().one())
def ngens(self): """ Return the number of linear variables.
OUTPUT:
An integer.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) sage: L.ngens() 3 """
def gens(self): """ Return the generators of ``self``.
OUTPUT:
A tuple of linear expressions, one for each linear variable.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) sage: L.gens() (x + 0*y + 0*z + 0, 0*x + y + 0*z + 0, 0*x + 0*y + z + 0) """
""" Return the `i`-th generator.
INPUT:
- ``i`` -- integer
OUTPUT:
A linear expression.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) sage: L.gen(0) x + 0*y + 0*z + 0 """
""" The element constructor.
This is part of the Sage parent/element framework.
TESTS::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z'))
Construct from coefficients and constant term::
sage: L._element_constructor_([1, 2, 3], 4) x + 2*y + 3*z + 4 sage: L._element_constructor_(vector(ZZ, [1, 2, 3]), 4) x + 2*y + 3*z + 4
Construct constant linear expression term::
sage: L._element_constructor_(4) 0*x + 0*y + 0*z + 4
Construct from list/tuple/iterable::
sage: L._element_constructor_(vector([4, 1, 2, 3])) x + 2*y + 3*z + 4
Construct from a pair ``(coefficients, constant)``::
sage: L([(1, 2, 3), 4]) x + 2*y + 3*z + 4
Construct from linear expression::
sage: M = LinearExpressionModule(ZZ, ('u', 'v', 'w')) sage: m = M([1, 2, 3], 4) sage: L._element_constructor_(m) x + 2*y + 3*z + 4 """ # Construct from linear expression # Construct from pair else: # Construct from list/tuple/iterable:: else: # arg1 is not None, construct from coefficients and constant term
""" Return a random element.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x,y,z> = LinearExpressionModule(QQ) sage: L.random_element() -1/2*x - 1/95*y + 1/2*z - 12 """
def ambient_module(self): """ Return the ambient module.
.. SEEALSO::
:meth:`ambient_vector_space`
OUTPUT:
The domain of the linear expressions as a free module over the base ring.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) sage: L.ambient_module() Vector space of dimension 3 over Rational Field sage: M = LinearExpressionModule(ZZ, ('r', 's')) sage: M.ambient_module() Ambient free module of rank 2 over the principal ideal domain Integer Ring sage: M.ambient_vector_space() Vector space of dimension 2 over Rational Field """
def ambient_vector_space(self): """ Return the ambient vector space.
.. SEEALSO::
:meth:`ambient_module`
OUTPUT:
The vector space (over the fraction field of the base ring) where the linear expressions live.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L = LinearExpressionModule(QQ, ('x', 'y', 'z')) sage: L.ambient_vector_space() Vector space of dimension 3 over Rational Field sage: M = LinearExpressionModule(ZZ, ('r', 's')) sage: M.ambient_module() Ambient free module of rank 2 over the principal ideal domain Integer Ring sage: M.ambient_vector_space() Vector space of dimension 2 over Rational Field """
""" Return whether there is a coercion.
TESTS::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x> = LinearExpressionModule(QQ) sage: M.<y> = LinearExpressionModule(ZZ) sage: L.coerce_map_from(M) Coercion map: From: Module of linear expressions in variable y over Integer Ring To: Module of linear expressions in variable x over Rational Field sage: M.coerce_map_from(L)
sage: M.coerce_map_from(ZZ) Coercion map: From: Integer Ring To: Module of linear expressions in variable y over Integer Ring sage: M.coerce_map_from(QQ) """ self.base().has_coerce_map_from(P.base())
""" Return a string representation.
OUTPUT:
A string.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: L.<x> = LinearExpressionModule(QQ); L Module of linear expressions in variable x over Rational Field """ ', '.join(self._names), self.base_ring(), 's' if self.ngens() > 1 else '')
""" Return a new module with a changed base ring.
INPUT:
- ``base_ring`` -- a ring; the new base ring
OUTPUT:
A new linear expression over the new base ring.
EXAMPLES::
sage: from sage.geometry.linear_expression import LinearExpressionModule sage: M.<y> = LinearExpressionModule(ZZ) sage: L = M.change_ring(QQ); L Module of linear expressions in variable y over Rational Field
TESTS::
sage: L.change_ring(QQ) is L True """ |