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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3);  L
Module of linear expressions in variables x, y, z over Rational Field
>>> x + Integer(2)*y + Integer(3)*z + Integer(4)
x + 2*y + 3*z + 4
>>> L(Integer(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
>>> from sage.all import *
>>> L([Integer(1), Integer(2), Integer(3)], Integer(4))
x + 2*y + 3*z + 4
>>> L([(Integer(1), Integer(2), Integer(3)), Integer(4)])
x + 2*y + 3*z + 4
>>> L([Integer(4), Integer(1), Integer(2), Integer(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
>>> from sage.all import *
>>> m = x + Integer(2)*y + Integer(3)*z + Integer(4)
>>> Integer(2)*m
2*x + 4*y + 6*z + 8
>>> m+m
2*x + 4*y + 6*z + 8
>>> m-m
0*x + 0*y + 0*z + 0
class sage.geometry.linear_expression.LinearExpression(parent, coefficients, constant, check=True)[source]#

Bases: ModuleElement

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3)
>>> m = L([Integer(1), Integer(2), Integer(3)], Integer(4)); m
x + 2*y + 3*z + 4
>>> m2 = L([(Integer(1), Integer(2), Integer(3)), Integer(4)]); m2
x + 2*y + 3*z + 4
>>> m3 = L([Integer(4), Integer(1), Integer(2), Integer(3)]); m3   # note: constant is first in single-tuple notation
x + 2*y + 3*z + 4
>>> m == m2
True
>>> m2 == m3
True
>>> L.zero()
0*x + 0*y + 0*z + 0
>>> a = L([Integer(12), Integer(2)/Integer(3), -Integer(1)], -Integer(2))
>>> a - m
11*x - 4/3*y - 4*z - 6
>>> LZ = LinearExpressionModule(ZZ, names=('x', 'y', 'z',)); (x, y, z,) = LZ._first_ngens(3)
>>> a - LZ([Integer(2), -Integer(1), Integer(3)], Integer(1))
10*x + 5/3*y - 4*z - 3
A()[source]#

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3)
>>> linear = L([Integer(1), Integer(2), Integer(3)], Integer(4));  linear
x + 2*y + 3*z + 4
>>> linear.A()
(1, 2, 3)
>>> linear.b()
4
b()[source]#

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3)
>>> linear = L([Integer(1), Integer(2), Integer(3)], Integer(4));  linear
x + 2*y + 3*z + 4
>>> linear.A()
(1, 2, 3)
>>> linear.b()
4
change_ring(base_ring)[source]#

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3)
>>> a = x + Integer(2)*y + Integer(3)*z + Integer(4);  a
x + 2*y + 3*z + 4
>>> a.change_ring(RDF)
1.0*x + 2.0*y + 3.0*z + 4.0
coefficients()[source]#

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]
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3)
>>> linear = L([Integer(1), Integer(2), Integer(3)], Integer(4));  linear
x + 2*y + 3*z + 4
>>> linear.coefficients()
[4, 1, 2, 3]
constant_term()[source]#

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3)
>>> linear = L([Integer(1), Integer(2), Integer(3)], Integer(4));  linear
x + 2*y + 3*z + 4
>>> linear.A()
(1, 2, 3)
>>> linear.b()
4
dense_coefficient_list()[source]#

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]
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3)
>>> linear = L([Integer(1), Integer(2), Integer(3)], Integer(4));  linear
x + 2*y + 3*z + 4
>>> linear.coefficients()
[4, 1, 2, 3]
evaluate(point)[source]#

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])                                                           # needs sage.symbolic
2*pi + 3*e + 4
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y',)); (x, y,) = L._first_ngens(2)
>>> ex = Integer(2)*x + Integer(3)* y + Integer(4)
>>> ex.evaluate([Integer(1),Integer(1)])
9
>>> ex([Integer(1),Integer(1)])    # syntactic sugar
9
>>> ex([pi, e])                                                           # needs sage.symbolic
2*pi + 3*e + 4
monomial_coefficients(copy=True)[source]#

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(), key=lambda x: str(x[0]))
[(0, 1), (1, 2), (2, 3), ('b', 4)]
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3)
>>> linear = L([Integer(1), Integer(2), Integer(3)], Integer(4))
>>> sorted(linear.monomial_coefficients().items(), key=lambda x: str(x[Integer(0)]))
[(0, 1), (1, 2), (2, 3), ('b', 4)]
class sage.geometry.linear_expression.LinearExpressionModule(base_ring, names=())[source]#

Bases: Parent, UniqueRepresentation

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, ('x', 'y', 'z'))
>>> L
Module of linear expressions in variables x, y, z over Rational Field
>>> L.an_element()
x + 0*y + 0*z + 0
Element[source]#

alias of LinearExpression

ambient_module()[source]#

Return the ambient module.

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, ('x', 'y', 'z'))
>>> L.ambient_module()
Vector space of dimension 3 over Rational Field
>>> M = LinearExpressionModule(ZZ, ('r', 's'))
>>> M.ambient_module()
Ambient free module of rank 2 over the principal ideal domain Integer Ring
>>> M.ambient_vector_space()
Vector space of dimension 2 over Rational Field
ambient_vector_space()[source]#

Return the ambient vector space.

See also

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, ('x', 'y', 'z'))
>>> L.ambient_vector_space()
Vector space of dimension 3 over Rational Field
>>> M = LinearExpressionModule(ZZ, ('r', 's'))
>>> M.ambient_module()
Ambient free module of rank 2 over the principal ideal domain Integer Ring
>>> M.ambient_vector_space()
Vector space of dimension 2 over Rational Field
basis()[source]#

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]
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, ('x', 'y', 'z'))
>>> 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]
change_ring(base_ring)[source]#

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> M = LinearExpressionModule(ZZ, names=('y',)); (y,) = M._first_ngens(1)
>>> L = M.change_ring(QQ);  L
Module of linear expressions in variable y over Rational Field
gen(i)[source]#

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, ('x', 'y', 'z'))
>>> L.gen(Integer(0))
x + 0*y + 0*z + 0
gens()[source]#

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)
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, ('x', 'y', 'z'))
>>> L.gens()
(x + 0*y + 0*z + 0, 0*x + y + 0*z + 0, 0*x + 0*y + z + 0)
ngens()[source]#

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
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, ('x', 'y', 'z'))
>>> L.ngens()
3
random_element()[source]#

Return a random element.

EXAMPLES:

sage: from sage.geometry.linear_expression import LinearExpressionModule
sage: L.<x,y,z> = LinearExpressionModule(QQ)
sage: L.random_element() in L
True
>>> from sage.all import *
>>> from sage.geometry.linear_expression import LinearExpressionModule
>>> L = LinearExpressionModule(QQ, names=('x', 'y', 'z',)); (x, y, z,) = L._first_ngens(3)
>>> L.random_element() in L
True