Module: sage.modules.free_module_element
Elements of free modules
Author Log:
TODO: Change to use a get_unsafe / set_unsafe, etc., structure exactly like with matrices, since we'll have to define a bunch of special purpose implementations of vectors easily and systematically.
We create a vector space over
and a subspace of this space.
sage: V = QQ^5 sage: W = V.span([V.1, V.2])
Arithmetic operations always return something in the ambient space,
since there is a canonical map from
to
but not from
to
.
sage: parent(W.0 + V.1) Vector space of dimension 5 over Rational Field sage: parent(V.1 + W.0) Vector space of dimension 5 over Rational Field sage: W.0 + V.1 (0, 2, 0, 0, 0) sage: W.0 - V.0 (-1, 1, 0, 0, 0)
Next we define modules over
and a finite field.
sage: K = ZZ^5 sage: M = GF(7)^5
Arithmetic between the
and
modules is defined, and
the result is always over
, since there is a canonical coercion
map to
.
sage: K.0 + V.1 (1, 1, 0, 0, 0) sage: parent(K.0 + V.1) Vector space of dimension 5 over Rational Field
Since there is no canonical coercion map to the finite field from
the following arithmetic is not defined:
sage: V.0 + M.0 Traceback (most recent call last): ... TypeError: unsupported operand parent(s) for '+': 'Vector space of dimension 5 over Rational Field' and 'Vector space of dimension 5 over Finite Field of size 7'
However, there is a map from
to the finite field, so the
following is defined, and the result is in the finite field.
sage: w = K.0 + M.0; w (2, 0, 0, 0, 0) sage: parent(w) Vector space of dimension 5 over Finite Field of size 7 sage: parent(M.0 + K.0) Vector space of dimension 5 over Finite Field of size 7
Matrix vector multiply:
sage: MS = MatrixSpace(QQ,3) sage: A = MS([0,1,0,1,0,0,0,0,1]) sage: V = QQ^3 sage: v = V([1,2,3]) sage: v * A (2, 1, 3)
TESTS:
sage: D = 46341 sage: u = 7 sage: R = Integers(D) sage: p = matrix(R,[[84, 97, 55, 58, 51]]) sage: 2*p.row(0) (168, 194, 110, 116, 102)
Module-level Functions
) |
Return a vector over R with given entries.
CALL FORMATS: 1. vector(object) 2. vector(ring, object) 3. vector(object, ring) 4. vector(numpy_array)
In each case, give sparse=[True|False] as an option.
Input:
sage: v = vector([1,2,3]); v (1, 2, 3) sage: v.parent() Ambient free module of rank 3 over the principal ideal domain Integer Ring sage: v = vector([1,2,3/5]); v (1, 2, 3/5) sage: v.parent() Vector space of dimension 3 over Rational Field
All entries must canonically coerce to some common ring:
sage: v = vector([17, GF(11)(5), 19/3]); v Traceback (most recent call last): ... TypeError: unable to find a common ring for all elements
sage: v = vector([17, GF(11)(5), 19]); v (6, 5, 8) sage: v.parent() Vector space of dimension 3 over Finite Field of size 11 sage: v = vector([17, GF(11)(5), 19], QQ); v (17, 5, 19) sage: v.parent() Vector space of dimension 3 over Rational Field sage: v = vector((1,2,3), QQ); v (1, 2, 3) sage: v.parent() Vector space of dimension 3 over Rational Field sage: v = vector(QQ, (1,2,3)); v (1, 2, 3) sage: v.parent() Vector space of dimension 3 over Rational Field sage: v = vector(vector([1,2,3])); v (1, 2, 3) sage: v.parent() Ambient free module of rank 3 over the principal ideal domain Integer Ring
You can also use free_module_element
, which is the same as vector
.
sage: free_module_element([1/3, -4/5]) (1/3, -4/5)
Make a vector mod 3 out of a vector over ZZ:
sage: vector(vector([1,2,3]), GF(3)) (1, 2, 0)
Any 1 dimensional numpy array of type float or complex may be passed to vector. The result will vector in the appropriate dimensional vector space over the real double field or the complex double field. The data in the array must be contiguous so columnwise slices of numpy matrices will rase an exception.
sage: import numpy sage: x=numpy.random.randn(10) sage: y=vector(x) sage: v=numpy.random.randn(10)*numpy.complex(0,1) sage: w=vector(v)
) |
) |
) |
) |
) |
) |
) |
) |
Return a vector over R with given entries.
CALL FORMATS: 1. vector(object) 2. vector(ring, object) 3. vector(object, ring) 4. vector(numpy_array)
In each case, give sparse=[True|False] as an option.
Input:
sage: v = vector([1,2,3]); v (1, 2, 3) sage: v.parent() Ambient free module of rank 3 over the principal ideal domain Integer Ring sage: v = vector([1,2,3/5]); v (1, 2, 3/5) sage: v.parent() Vector space of dimension 3 over Rational Field
All entries must canonically coerce to some common ring:
sage: v = vector([17, GF(11)(5), 19/3]); v Traceback (most recent call last): ... TypeError: unable to find a common ring for all elements
sage: v = vector([17, GF(11)(5), 19]); v (6, 5, 8) sage: v.parent() Vector space of dimension 3 over Finite Field of size 11 sage: v = vector([17, GF(11)(5), 19], QQ); v (17, 5, 19) sage: v.parent() Vector space of dimension 3 over Rational Field sage: v = vector((1,2,3), QQ); v (1, 2, 3) sage: v.parent() Vector space of dimension 3 over Rational Field sage: v = vector(QQ, (1,2,3)); v (1, 2, 3) sage: v.parent() Vector space of dimension 3 over Rational Field sage: v = vector(vector([1,2,3])); v (1, 2, 3) sage: v.parent() Ambient free module of rank 3 over the principal ideal domain Integer Ring
You can also use free_module_element
, which is the same as vector
.
sage: free_module_element([1/3, -4/5]) (1/3, -4/5)
Make a vector mod 3 out of a vector over ZZ:
sage: vector(vector([1,2,3]), GF(3)) (1, 2, 0)
Any 1 dimensional numpy array of type float or complex may be passed to vector. The result will vector in the appropriate dimensional vector space over the real double field or the complex double field. The data in the array must be contiguous so columnwise slices of numpy matrices will rase an exception.
sage: import numpy sage: x=numpy.random.randn(10) sage: y=vector(x) sage: v=numpy.random.randn(10)*numpy.complex(0,1) sage: w=vector(v)
Class: FreeModuleElement
Functions: additive_order,
change_ring,
copy,
cross_product,
degree,
denominator,
dense_vector,
dict,
dot_product,
element,
get,
inner_product,
is_dense,
is_immutable,
is_mutable,
is_sparse,
is_vector,
iteritems,
lift,
list,
list_from_positions,
Mod,
nonzero_positions,
norm,
normalize,
pairwise_product,
plot,
plot_step,
set,
set_immutable,
sparse_vector,
support,
transpose
) |
Return the additive order of self.
sage: v = vector(Integers(4), [1,2]) sage: v.additive_order() 4
sage: v = vector([1,2,3]) sage: v.additive_order() +Infinity
sage: v = vector(Integers(30), [6, 15]); v (6, 15) sage: v.additive_order() 10 sage: 10*v (0, 0)
) |
Change the base ring of this vector, by coercing each element of this vector into R.
sage: v = vector(QQ['x,y'], [1..5]); v.change_ring(GF(3)) (1, 2, 0, 1, 2)
) |
Make a copy of this vector.
sage: v = vector([1..5]); v (1, 2, 3, 4, 5) sage: w = v.copy() sage: v == w True sage: v is w False
sage: v = vector([1..5], sparse=True); v (1, 2, 3, 4, 5) sage: v.copy() (1, 2, 3, 4, 5)
) |
Return the cross product of self and right, which is only defined for vectors of length 3.
This product is performed under the assumption that the basis vectors are orthonormal.
sage: v = vector([1,2,3]); w = vector([0,5,-9]) sage: v.cross_product(v) (0, 0, 0) sage: u = v.cross_product(w); u (-33, 9, 5) sage: u.dot_product(v) 0 sage: u.dot_product(w) 0
) |
Return the dot product of self and right, which is the sum of the product of the corresponding entries.
Input:
sage: V = FreeModule(ZZ, 3) sage: v = V([1,2,3]) sage: w = V([4,5,6]) sage: v.dot_product(w) 32
sage: W = VectorSpace(GF(3),3) sage: w = W([0,1,2]) sage: w.dot_product(v) 2 sage: w.dot_product(v).parent() Finite Field of size 3
Implicit coercion is well defined (irregardless of order), so we get 2 even if we do the dot product in the other order.
sage: v.dot_product(w) 2
) |
get is meant to be more efficient than getitem, because it does not do any error checking.
) |
Returns the inner product of self and other, with respect to the inner product defined on the parent of self.
sage: I = matrix(ZZ,3,[2,0,-1,0,2,0,-1,0,6]) sage: M = FreeModule(ZZ, 3, inner_product_matrix = I) sage: (M.0).inner_product(M.0) 2 sage: K = M.span_of_basis([[0/2,-1/2,-1/2], [0,1/2,-1/2],[2,0,0]]) sage: (K.0).inner_product(K.0) 2
) |
Return True if this vector is immutable, i.e., the entries cannot be changed.
sage: v = vector(QQ['x,y'], [1..5]); v.is_immutable() False sage: v.set_immutable() sage: v.is_immutable() True
) |
Return True if this vector is mutable, i.e., the entries can be changed.
sage: v = vector(QQ['x,y'], [1..5]); v.is_mutable() True sage: v.set_immutable() sage: v.is_mutable() False
) |
sage: V = vector(Integers(7), [5, 9, 13, 15]) ; V (5, 2, 6, 1) sage: V.lift() (5, 2, 6, 1) sage: parent(V.lift()) Ambient free module of rank 4 over the principal ideal domain Integer Ring
) |
sage: V = vector(ZZ, [5, 9, 13, 15]) sage: V.Mod(7) (5, 2, 6, 1) sage: parent(V.Mod(7)) Vector space of dimension 4 over Ring of integers modulo 7
) |
Return the sorted list of integers i such that self[i] != 0.
) |
Return the p-norm of this vector, where p can be a real number >= 1, Infinity, or a symbolic expression. If p=2 (default), this is the usual Euclidean norm; if p=Infinity, this is the maximum norm; if p=1, this is the taxicab (Manhattan) norm.
sage: v = vector([1,2,-3]) sage: v.norm(5) 276^(1/5)
The default is the usual Euclidean norm:
sage: v.norm() sqrt(14) sage: v.norm(2) sqrt(14)
The infinity norm is the maximum size of any entry:
sage: v.norm(Infinity) 3
Any real or symbolic value works:
sage: v=vector(RDF,[1,2,3]) sage: v.norm(5) 3.07738488539 sage: v.norm(pi/2) 4.2165958647 sage: var('a b c d p') (a, b, c, d, p) sage: v=vector([a, b, c, d]) sage: v.norm(p) (abs(d)^p + abs(c)^p + abs(b)^p + abs(a)^p)^(1/p)
) |
Return this vector divided through by the first nonzero entry of this vector.
sage: v = vector(QQ,[0,4/3,5,1,2]) sage: v.normalize() (0, 1, 15/4, 3/4, 3/2)
) |
Return the dot product of self and right, which is a vector of of the product of the corresponding entries.
Input:
sage: V = FreeModule(ZZ, 3) sage: v = V([1,2,3]) sage: w = V([4,5,6]) sage: v.pairwise_product(w) (4, 10, 18) sage: sum(v.pairwise_product(w)) == v.dot_product(w) True
sage: W = VectorSpace(GF(3),3) sage: w = W([0,1,2]) sage: w.pairwise_product(v) (0, 2, 0) sage: w.pairwise_product(v).parent() Vector space of dimension 3 over Finite Field of size 3
Implicit coercion is well defined (irregardless of order), so we get 2 even if we do the dot product in the other order.
sage: v.pairwise_product(w).parent() Vector space of dimension 3 over Finite Field of size 3
TESTS:
sage: x, y = var('x, y')
sage: parent(vector(ZZ,[1,2]).pairwise_product(vector(ZZ,[1,2]))) Ambient free module of rank 2 over the principal ideal domain Integer Ring sage: parent(vector(ZZ,[1,2]).pairwise_product(vector(QQ,[1,2]))) Vector space of dimension 2 over Rational Field sage: parent(vector(QQ,[1,2]).pairwise_product(vector(ZZ,[1,2]))) Vector space of dimension 2 over Rational Field sage: parent(vector(QQ,[1,2]).pairwise_product(vector(QQ,[1,2]))) Vector space of dimension 2 over Rational Field
sage: parent(vector(QQ,[1,2,3,4]).pairwise_product(vector(ZZ[x],[1,2,3,4]))) Ambient free module of rank 4 over the principal ideal domain Univariate Polynomial Ring in x over Rational Field sage: parent(vector(ZZ[x],[1,2,3,4]).pairwise_product(vector(QQ,[1,2,3,4]))) Ambient free module of rank 4 over the principal ideal domain Univariate Polynomial Ring in x over Rational Field
sage: parent(vector(QQ,[1,2,3,4]).pairwise_product(vector(ZZ[x][y],[1,2,3,4]))) Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field sage: parent(vector(ZZ[x][y],[1,2,3,4]).pairwise_product(vector(QQ,[1,2,3,4]))) Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(vector(QQ[x],[1,2,3,4]).pairwise_product(vector(ZZ[x][y],[1,2,3,4]))) Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field sage: parent(vector(ZZ[x][y],[1,2,3,4]).pairwise_product(vector(QQ[x],[1,2,3,4]))) Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(vector(QQ[y],[1,2,3,4]).pairwise_product(vector(ZZ[x][y],[1,2,3,4]))) Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field sage: parent(vector(ZZ[x][y],[1,2,3,4]).pairwise_product(vector(QQ[y],[1,2,3,4]))) Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in y over Univariate Polynomial Ring in x over Rational Field
sage: parent(vector(ZZ[x],[1,2,3,4]).pairwise_product(vector(ZZ[y],[1,2,3,4]))) Traceback (most recent call last): ... TypeError: no common canonical parent for objects with parents: 'Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in x over Integer Ring' and 'Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in y over Integer Ring' sage: parent(vector(ZZ[x],[1,2,3,4]).pairwise_product(vector(QQ[y],[1,2,3,4]))) Traceback (most recent call last): ... TypeError: no common canonical parent for objects with parents: 'Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in x over Integer Ring' and 'Ambient free module of rank 4 over the principal ideal domain Univariate Polynomial Ring in y over Rational Field' sage: parent(vector(QQ[x],[1,2,3,4]).pairwise_product(vector(ZZ[y],[1,2,3,4]))) Traceback (most recent call last): ... TypeError: no common canonical parent for objects with parents: 'Ambient free module of rank 4 over the principal ideal domain Univariate Polynomial Ring in x over Rational Field' and 'Ambient free module of rank 4 over the integral domain Univariate Polynomial Ring in y over Integer Ring' sage: parent(vector(QQ[x],[1,2,3,4]).pairwise_product(vector(QQ[y],[1,2,3,4]))) Traceback (most recent call last): ... TypeError: no common canonical parent for objects with parents: 'Ambient free module of rank 4 over the principal ideal domain Univariate Polynomial Ring in x over Rational Field' and 'Ambient free module of rank 4 over the principal ideal domain Univariate Polynomial Ring in y over Rational Field'
) |
Input:
plot_type - (default: 'arrow' if v has 3 or fewer components, otherwise 'step') type of plot. Options are 'arrow' to draw an arrow; 'point' to draw a point at the coordinates specified by the vector; 'step' to draw a step function representing the coordinates of the vector. Both 'arrow' and 'point' raise exceptions if the vector has more than 3 dimensions.
sage: v = vector(RDF, (1,2)) sage: eps = 0.1 sage: plot(v, plot_type='arrow') sage: plot(v, plot_type='point') sage: plot(v, plot_type='step') # calls v.plot_step() sage: plot(v, plot_type='step', eps=eps, xmax=5, hue=0) sage: v = vector(RDF, (1,2,1)) sage: plot(v) # defaults to an arrow plot sage: plot(v, plot_type='arrow') sage: from sage.plot.plot3d.shapes2 import frame3d sage: plot(v, plot_type='point')+frame3d((0,0,0), v.list()) sage: plot(v, plot_type='step') # calls v.plot_step() sage: plot(v, plot_type='step', eps=eps, xmax=5, hue=0) sage: v = vector(RDF, (1,2,3,4)) sage: plot(v) # defaults to a step plot
) |
Input:
sage: eps=0.1 sage: v = vector(RDF, [sin(n*eps) for n in range(100)]) sage: v.plot_step(eps=eps, xmax=5, hue=0)
) |
set is meant to be more efficient than setitem, because it does not do any error checking or coercion. Use with care.
) |
Make this vector immutable. This operation can't be undone.
sage: v = vector([1..5]); v (1, 2, 3, 4, 5) sage: v[1] = 10 sage: v.set_immutable() sage: v[1] = 10 Traceback (most recent call last): ... ValueError: vector is immutable; please change a copy instead (use self.copy())
) |
Return the integers i such that self[i] != 0.
This is the same as the nonzero_positions
function.
) |
sage: v = vector(ZZ, [2, 12, 22]) sage: transpose(vector(v)) [ 2] [12] [22]
sage: transpose(vector(GF(7), v)) [2] [5] [1]
sage: transpose(vector(v, ZZ['x', 'y'])) [ 2] [12] [22]
Special Functions: __abs__,
__copy__,
__delitem__,
__delslice__,
__eq__,
__ge__,
__getitem__,
__getslice__,
__gt__,
__init__,
__invert__,
__le__,
__len__,
__lt__,
__mod__,
__ne__,
__pos__,
__pow__,
__rmod__,
__rpow__,
__setitem__,
__setslice__,
_hash,
_latex_,
_maple_init_,
_mathematica_init_,
_matrix_,
_repr_,
_vector_
) |
Return the square root of the sum of the squares of the entries of this vector.
sage: v = vector([1..5]); abs(v) sqrt(55) sage: v = vector(RDF, [1..5]); abs(v) 7.4161984871
) |
) |
) |
) |
sage: V = vector(ZZ, [5, 9, 13, 15]) sage: V % 7 (5, 2, 6, 1) sage: parent(V % 7) Ambient free module of rank 4 over the principal ideal domain Integer Ring
) |
) |
) |
Return a latex representation of self. For example, if self is the free module element (1,2,3,4), then following latex is generated: "(1,2,3,4)" (without the quotes).
) |
sage: v = vector(ZZ, 4, range(4)) #optional sage: maple(v) #optional Vector[row](4, [0,1,2,3])
sage: v = vector(QQ, 3, [2/3, 0, 5/4]) #optional sage: maple(v) #optional Vector[row](3, [2/3,0,5/4])
sage: P.<x> = ZZ[] #optional sage: v = vector(P, 3, [x^2 + 2, 2*x + 1, -2*x^2 + 4*x]) #optional sage: maple(v) #optional Vector[row](3, [x^2+2,2*x+1,-2*x^2+4*x])
) |
Returns string representation of this vector as a Mathematica list.
sage: vector((1,2,3), QQ)._mathematica_init_() '{1/1, 2/1, 3/1}' sage: mathematica(vector((1,2,3), QQ)) #optional -- requires mathematica {1, 2, 3} sage: a = vector(SR, 5, [1, x, x^2, sin(x), pi]); a (1, x, x^2, sin(x), pi) sage: a._mathematica_init_() '{1, x, (x) ^ (2), Sin[x], Pi}'
) |
sage: v = vector(ZZ, [2, 12, 22]) sage: vector(v) (2, 12, 22) sage: vector(GF(7), v) (2, 5, 1) sage: vector(v, ZZ['x', 'y']) (2, 12, 22)
) |
String representation of a vector.
sage: vector(QQ, [])._repr_() '()' sage: vector(QQ, range(5))._repr_() '(0, 1, 2, 3, 4)'
Symbolic are not displayed using ASCII art.
sage: x = var('x') sage: v = vector([x/(2*x)+sqrt(2)+var('theta')^3,x/(2*x)]); v (theta^3 + sqrt(2) + 1/2, 1/2) sage: v._repr_() '(theta^3 + sqrt(2) + 1/2, 1/2)'
) |
sage: v = vector(ZZ, [2, 12, 22]) sage: vector(v) (2, 12, 22) sage: vector(GF(7), v) (2, 5, 1) sage: vector(v, ZZ['x', 'y']) (2, 12, 22)
Class: FreeModuleElement_generic_dense
Functions: list,
n
) |
Returns a numerical approximation of self by calling the n() method on all of its entries.
sage: v = vector(RQDF, [1,2,3]) sage: v.n() (1.00000000000000, 2.00000000000000, 3.00000000000000) sage: _.parent() Vector space of dimension 3 over Real Field with 53 bits of precision sage: v.n(prec=75) (1.000000000000000000000, 2.000000000000000000000, 3.000000000000000000000) sage: _.parent() Vector space of dimension 3 over Real Field with 75 bits of precision
Special Functions: __copy__,
__delitem__,
__delslice__,
__getitem__,
__init__,
__reduce__,
__setitem__,
__setslice__,
_hash
) |
) |
sage: v = vector([RR(1), RR(2)]); v (1.00000000000000, 2.00000000000000) sage: v[0] 1.00000000000000 sage: v[-1] 2.00000000000000 sage: v[4] Traceback (most recent call last): ... IndexError: index must be between -2 and 1 sage: v[-4] Traceback (most recent call last): ... IndexError: index must be between -2 and 1
) |
) |
Class: FreeModuleElement_generic_sparse
Pickling works:
sage: v = FreeModule(ZZ, 3, sparse=True).0 sage: loads(dumps(v)) == v True sage: v = FreeModule(Integers(8)['x,y'], 5, sparse=True).1 sage: loads(dumps(v)) - v (0, 0, 0, 0, 0)
sage: a = vector([-1,0,1/1],sparse=True); b = vector([-1/1,0,0],sparse=True) sage: a.parent() Sparse vector space of dimension 3 over Rational Field sage: b - a (0, 0, -1)
Functions: denominator,
dict,
get,
iteritems,
list,
n,
nonzero_positions,
set
) |
Like __getitem__ but with no type or bounds checking. Returns 0 if access is out of bounds.
) |
Returns a numerical approximation of self by calling the n() method on all of its entries.
sage: v = vector(RQDF, [1,2,3], sparse=True) sage: v.n() (1.00000000000000, 2.00000000000000, 3.00000000000000) sage: _.parent() Sparse vector space of dimension 3 over Real Field with 53 bits of precision sage: v.n(prec=75) (1.000000000000000000000, 2.000000000000000000000, 3.000000000000000000000) sage: _.parent() Sparse vector space of dimension 3 over Real Field with 75 bits of precision
) |
Returns the set of pairs (i,j) such that self[i,j] != 0.
) |
Like __setitem__ but with no type or bounds checking.
Special Functions: __copy__,
__delitem__,
__getitem__,
__init__,
__reduce__,
__setitem__
) |
) |
sage: v = vector([RR(1), RR(2)], sparse=True); v (1.00000000000000, 2.00000000000000) sage: v[0] 1.00000000000000 sage: v[-1] 2.00000000000000 sage: v[5] Traceback (most recent call last): ... IndexError: index must be between -2 and 1 sage: v[-3] Traceback (most recent call last): ... IndexError: index must be between -2 and 1
) |
See About this document... for information on suggesting changes.