Elements of free modules

AUTHORS:

  • William Stein
  • Josh Kantor

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.

EXAMPLES: We create a vector space over \QQ 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 W to V but not from V to W.

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 \ZZ and a finite field.

sage: K = ZZ^5
sage: M = GF(7)^5

Arithmetic between the \QQ and \ZZ modules is defined, and the result is always over \QQ, since there is a canonical coercion map to \QQ.

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 \QQ the following arithmetic is not defined:

sage: V.0 + M.0
...
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 \ZZ 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)
class sage.modules.free_module_element.FreeModuleElement

An element of a generic free module.

Mod()

EXAMPLES:

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

Return the square root of the sum of the squares of the entries of this vector.

EXAMPLES:

sage: v = vector([1..5]); abs(v)
sqrt(55)
sage: v = vector(RDF, [1..5]); abs(v)
7.4161984871
__copy__()

Make a copy of this vector.

EXAMPLES:

sage: v = vector([1..5]); v
(1, 2, 3, 4, 5)
sage: w = copy(v)
sage: v == w
True
sage: v is w
False
sage: v = vector([1..5], sparse=True); v
(1, 2, 3, 4, 5)
sage: copy(v)
(1, 2, 3, 4, 5)
__delitem__()
x.__delitem__(y) <==> del x[y]
__delslice__()

x.__delslice__(i, j) <==> del x[i:j]

Use of negative indices is not supported.

__eq__()
x.__eq__(y) <==> x==y
__ge__()
x.__ge__(y) <==> x>=y
__getitem__()
__getslice__()

x.__getslice__(i, j) <==> x[i:j]

Use of negative indices is not supported.

__gt__()
x.__gt__(y) <==> x>y
__hash__()
x.__hash__() <==> hash(x)
__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
__invert__()
__le__()
x.__le__(y) <==> x<=y
__len__()
x.__len__() <==> len(x)
__lt__()
x.__lt__(y) <==> x<y
__mod__()

EXAMPLES:

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
__ne__()
x.__ne__(y) <==> x!=y
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
__pos__()
__pow__()
x.__pow__(y[, z]) <==> pow(x, y[, z])
__rmod__()
x.__rmod__(y) <==> y%x
__rpow__()
y.__rpow__(x[, z]) <==> pow(x, y[, z])
__setitem__()
x.__setitem__(i, y) <==> x[i]=y
__setslice__()

x.__setslice__(i, j, y) <==> x[i:j]=y

Use of negative indices is not supported.

_derivative()

Differentiate with respect to var by differentiating each element with respect to var.

EXAMPLES:

sage: v = vector([1,x,x^2])
sage: v._derivative(x)
(0, 1, 2*x)
sage: type(v._derivative(x)) == type(v)
True
sage: v = vector([1,x,x^2], sparse=True)
sage: v._derivative(x)
(0, 1, 2*x)
sage: type(v._derivative(x)) == type(v)
True
_hash()
_latex_()

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). The vector is enclosed in parentheses by default, but the delimiters can be changed using the command latex.vector_delimiters(...).

EXAMPLES:

sage: v = vector(QQ, [1,2,3])
sage: latex(v)
\left(1,2,3

ight)

_magma_init_()

Convert self to Magma.

EXAMPLES:

sage: F = FreeModule(ZZ, 2, inner_product_matrix=matrix(ZZ, 2, 2, [1, 0, 0, -1]))
sage: v = F([1, 2])
sage: M = magma(v); M # optional - magma
(1 2)
sage: M.Type() # optional - magma
ModTupRngElt
sage: M.Parent() # optional - magma
Full RSpace of degree 2 over Integer Ring
Inner Product Matrix:
[ 1  0]
[ 0 -1]
sage: M.sage() # optional - magma
(1, 2)
sage: M.sage() == v # optional - magma
True
sage: M.sage().parent() is v.parent() # optional - magma
True
sage: v = vector(QQ, [1, 2, 5/6])
sage: M = magma(v); M # optional - magma
(  1   2 5/6)
sage: M.Type() # optional - magma
ModTupFldElt
sage: M.Parent() # optional - magma
Full Vector space of degree 3 over Rational Field
sage: M.sage() # optional - magma
(1, 2, 5/6)
sage: M.sage() == v # optional - magma
True
sage: M.sage().parent() is v.parent() # optional - magma
True
_maple_init_()

EXAMPLES:

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

Returns string representation of this vector as a Mathematica list.

EXAMPLES:

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}'
_matrix_()

Return self as a row matrix.

EXAMPLES:

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

String representation of a vector.

EXAMPLES:

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
(sqrt(2) + theta^3 + 1/2, 1/2)
sage: v._repr_()
'(sqrt(2) + theta^3 + 1/2, 1/2)'
_sage_input_()

Produce an expression which will reproduce this value when evaluated.

EXAMPLES:

sage: sage_input(vector(RR, [pi, e, 0.5]), verify=True)
# Verified
vector(RR, [3.1415926535897931, 2.7182818284590451, 0.5])
sage: sage_input(vector(GF(5), [1, 2, 3, 4, 5]), verify=True)
# Verified
vector(GF(5), [1, 2, 3, 4, 0])
sage: sage_input(vector([0, 0, 0, 1, 0, 0, 0], sparse=True), verify=True)
# Verified
vector(ZZ, {3:1, 6:0})
sage: sage_input(vector(ZZ, []), verify=True)
# Verified
vector(ZZ, [])
sage: sage_input(vector(RealField(27), [], sparse=True), verify=True)
# Verified
vector(RealField(27), {})
sage: from sage.misc.sage_input import SageInputBuilder
sage: vector(ZZ, [42, 389])._sage_input_(SageInputBuilder(), False)
{call: {atomic:vector}({atomic:ZZ}, {list: ({atomic:42}, {atomic:389})})}
sage: vector(RDF, {1:pi, 1000:e})._sage_input_(SageInputBuilder(), False)
{call: {atomic:vector}({atomic:RDF}, {dict: {{atomic:1}:{atomic:3.1415926535897931}, {atomic:1000}:{atomic:2.718281828459045...}}})}
_vector_()

Return self as a vector.

EXAMPLES:

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)

sage: vector(vector((1, 6.8)))
(1.00000000000000, 6.80000000000000)
sage: vector(vector(SR, (1, sqrt(2)) ) )
(1, sqrt(2))
additive_order()

Return the additive order of self.

EXAMPLES:

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

Apply the given map phi (an arbitrary Python function or callable object) to this free module element. If R is not given, automatically determine the base ring of the resulting element.

INPUT:
sparse – True or False will control whether the result
is sparse. By default, the result is sparse iff self is sparse.
  • phi - arbitrary Python function or callable object
  • R - (optional) ring

OUTPUT: a free module element over R

EXAMPLES:

sage: m = vector([1,x,sin(x+1)])
sage: m.apply_map(lambda x: x^2)
(1, x^2, sin(x + 1)^2)
sage: m.apply_map(sin)
(sin(1), sin(x), sin(sin(x + 1)))
sage: m = vector(ZZ, 9, range(9)) 
sage: k.<a> = GF(9) 
sage: m.apply_map(k)
(0, 1, 2, 0, 1, 2, 0, 1, 2)

In this example, we explicitly specify the codomain.

sage: s = GF(3)
sage: f = lambda x: s(x)
sage: n = m.apply_map(f, k); n
(0, 1, 2, 0, 1, 2, 0, 1, 2)
sage: n.parent()
Vector space of dimension 9 over Finite Field in a of size 3^2

If your map sends 0 to a non-zero value, then your resulting vector is not mathematically sparse:

sage: v = vector([0] * 6 + [1], sparse=True); v
(0, 0, 0, 0, 0, 0, 1)
sage: v2 = v.apply_map(lambda x: x+1); v2
(1, 1, 1, 1, 1, 1, 2)

but it’s still represented with a sparse data type:

sage: parent(v2)
Ambient sparse free module of rank 7 over the principal ideal domain Integer Ring

This data type is inefficient for dense vectors, so you may want to specify sparse=False:

sage: v2 = v.apply_map(lambda x: x+1, sparse=False); v2
(1, 1, 1, 1, 1, 1, 2)
sage: parent(v2)
Ambient free module of rank 7 over the principal ideal domain Integer Ring

Or if you have a map that will result in mostly zeroes, you may want to specify sparse=True:

sage: v = vector(srange(10))
sage: v2 = v.apply_map(lambda x: 0 if x else 1, sparse=True); v2
(1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
sage: parent(v2)
Ambient sparse free module of rank 10 over the principal ideal domain Integer Ring

TESTS:

sage: m = vector(SR,[])
sage: m.apply_map(lambda x: x*x) == m
True

Check that we don’t unnecessarily apply phi to 0 in the sparse case:

sage: m = vector(ZZ, range(1, 4), sparse=True)
sage: m.apply_map(lambda x: 1/x)
(1, 1/2, 1/3)

sage: parent(vector(RDF, (), sparse=True).apply_map(lambda x: x, sparse=True))
Sparse vector space of dimension 0 over Real Double Field
sage: parent(vector(RDF, (), sparse=True).apply_map(lambda x: x, sparse=False))
Vector space of dimension 0 over Real Double Field
sage: parent(vector(RDF, (), sparse=False).apply_map(lambda x: x, sparse=True))
Sparse vector space of dimension 0 over Real Double Field
sage: parent(vector(RDF, (), sparse=False).apply_map(lambda x: x, sparse=False))
Vector space of dimension 0 over Real Double Field
change_ring()

Change the base ring of this vector, by coercing each element of this vector into R.

EXAMPLES:

sage: v = vector(QQ['x,y'], [1..5]); v.change_ring(GF(3))
(1, 2, 0, 1, 2)
cross_product()

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.

EXAMPLES:

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
degree()
denominator()
dense_vector()
derivative()

Derivative with respect to variables supplied in args.

Multiple variables and iteration counts may be supplied; see documentation for the global derivative() function for more details.

EXAMPLES:

sage: v = vector([1,x,x^2])
sage: v.derivative(x)
(0, 1, 2*x)
sage: type(v.derivative(x)) == type(v)
True
sage: v = vector([1,x,x^2], sparse=True)
sage: v.derivative(x)
(0, 1, 2*x)
sage: type(v.derivative(x)) == type(v)
True
sage: v.derivative(x,x)
(0, 0, 2)
dict()
dot_product()

Return the dot product of self and right, which is the sum of the product of the corresponding entries.

INPUT:

  • right - vector of the same degree as self. It need not be in the same vector space as self, as long as the coefficients can be multiplied.

EXAMPLES:

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 (regardless of order), so we get 2 even if we do the dot product in the other order.

sage: v.dot_product(w)  
2
element()
get()
get is meant to be more efficient than getitem, because it does not do any error checking.
inner_product()

Returns the inner product of self and other, with respect to the inner product defined on the parent of self.

EXAMPLES:

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

Return True if this vector is immutable, i.e., the entries cannot be changed.

EXAMPLES:

sage: v = vector(QQ['x,y'], [1..5]); v.is_immutable()
False
sage: v.set_immutable()
sage: v.is_immutable()
True
is_mutable()

Return True if this vector is mutable, i.e., the entries can be changed.

EXAMPLES:

sage: v = vector(QQ['x,y'], [1..5]); v.is_mutable()
True
sage: v.set_immutable()
sage: v.is_mutable()
False
is_sparse()
is_vector()
iteritems()
lift()

EXAMPLES:

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
list()
list_from_positions()
nonzero_positions()
Return the sorted list of integers i such that self[i] != 0.
norm()

Return the p-norm of this vector, where p can be a real number \geq 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.

EXAMPLES:

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'); v=vector([a, b, c, d])
sage: v.norm(p)
(abs(a)^p + abs(b)^p + abs(c)^p + abs(d)^p)^(1/p)
normalize()

Return this vector divided through by the first nonzero entry of this vector.

EXAMPLES:

sage: v = vector(QQ,[0,4/3,5,1,2])
sage: v.normalize()
(0, 1, 15/4, 3/4, 3/2)
pairwise_product()

Return the pairwise product of self and right, which is a vector of the products of the corresponding entries.

INPUT:

  • right - vector of the same degree as self. It need not be in the same vector space as self, as long as the coefficients can be multiplied.

EXAMPLES:

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 (regardless 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]))) 
... 
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]))) 
... 
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]))) 
... 
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]))) 
... 
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'
sage: v = vector({1: 1, 3: 2})  # test sparse vectors
sage: w = vector({0: 6, 3: -4})
sage: v.pairwise_product(w)
(0, 0, 0, -8)
sage: w.pairwise_product(v) == v.pairwise_product(w)
True
plot()

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.

EXAMPLES:

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

INPUT:

  • xmin - (default: 0) start x position to start plotting
  • xmax - (default: 1) stop x position to stop plotting
  • eps - (default: determined by xmax) we view this vector as defining a function at the points xmin, xmin + eps, xmin + 2*eps, ...,
  • res - (default: all points) total number of points to include in the graph
  • connect - (default: True) if True draws a line; otherwise draw a list of points.

EXAMPLES:

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()
set is meant to be more efficient than setitem, because it does not do any error checking or coercion. Use with care.
set_immutable()

Make this vector immutable. This operation can’t be undone.

EXAMPLES:

sage: v = vector([1..5]); v
(1, 2, 3, 4, 5)
sage: v[1] = 10
sage: v.set_immutable()
sage: v[1] = 10
...
ValueError: vector is immutable; please change a copy instead (use copy())
sparse_vector()
support()
Return the integers i such that self[i] != 0. This is the same as the nonzero_positions function.
transpose()

EXAMPLES:

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]
class sage.modules.free_module_element.FreeModuleElement_generic_dense

A generic dense element of a free module.

__call__()

Calling a free module element returns the result of calling each component.

EXAMPLES:

sage: x, y = var('x,y')
sage: f = x^2 + y^2
sage: g = f.gradient()
sage: g
(2*x, 2*y)
sage: type(g)
<type 'sage.modules.free_module_element.FreeModuleElement_generic_dense'>
sage: g(y=2, x=3)
(6, 4)
sage: f(x,y) = x^2 + y^2
sage: g = f.gradient()
sage: g(3,2)
(6, 4)
sage: g(x=3, y=2)
(6, 4)
__copy__()
__delitem__()
x.__delitem__(y) <==> del x[y]
__delslice__()

x.__delslice__(i, j) <==> del x[i:j]

Use of negative indices is not supported.

__getitem__()

EXAMPLES:

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]
...
IndexError: index must be between -2 and 1
sage: v[-4]
...
IndexError: index must be between -2 and 1
__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
__reduce__()
__setitem__()
x.__setitem__(i, y) <==> x[i]=y
__setslice__()

x.__setslice__(i, j, y) <==> x[i:j]=y

Use of negative indices is not supported.

_add_()
Add left and right.
_dot_product_()

Return the dot product of left and right.

EXAMPLES:

sage: R.<x> = QQ[]
sage: v = vector([x,x^2,3*x]); w = vector([2*x,x,3+x])
sage: v*w
x^3 + 5*x^2 + 9*x
sage: (x*2*x) + (x^2*x) + (3*x*(3+x))
x^3 + 5*x^2 + 9*x
sage: w*v
x^3 + 5*x^2 + 9*x
_hash()
_lmul_()
_pairwise_product_()

EXAMPLES:

sage: R.<x> = QQ[]
sage: v = vector([x,x^2,3*x]); w = vector([2*x,x,3+x])
sage: v.pairwise_product(w)
(2*x^2, x^3, 3*x^2 + 9*x)
sage: w.pairwise_product(v)
(2*x^2, x^3, 3*x^2 + 9*x)
_rmul_()

EXAMPLES:

sage: V = ZZ['x']^5
sage: 5 * V.0
(5, 0, 0, 0, 0)
_sub_()

Subtract right from left.

EXAMPLES:

sage: V = QQ^5
sage: W = V.span([V.1, V.2])
sage: W.0 - V.0
(-1, 1, 0, 0, 0)
sage: V.0 - W.0
(1, -1, 0, 0, 0)
list()
n()

Returns a numerical approximation of self by calling the n() method on all of its entries.

EXAMPLES:

sage: v = vector(RealField(212), [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
class sage.modules.free_module_element.FreeModuleElement_generic_sparse

A generic sparse free module element is a dictionary with keys ints i and entries in the base ring.

EXAMPLES:

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)
sage: (b-a).dict()
{2: -1}
__copy__()
__delitem__()
x.__delitem__(y) <==> del x[y]
__getitem__()

EXAMPLES:

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]
...
IndexError: index must be between -2 and 1
sage: v[-3]
...
IndexError: index must be between -2 and 1
__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
__reduce__()
__setitem__()
x.__setitem__(i, y) <==> x[i]=y
_add_()
Add left and right.
_dot_product_()

Return the dot product of left and right.

EXAMPLES:

sage: v = vector([1,2,0], sparse=True); w = vector([0,5,-9], sparse=True)
sage: v * w
10
sage: w * v
10
_lmul_()
_pairwise_product_()
_rmul_()
_sub_()
denominator()
dict()
get()
Like __getitem__ but with no type or bounds checking. Returns 0 if access is out of bounds.
iteritems()
list()
n()

Returns a numerical approximation of self by calling the n() method on all of its entries.

EXAMPLES:

sage: v = vector(RealField(200), [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
nonzero_positions()

Returns the list of numbers i such that self[i] != 0.

EXAMPLES:

sage: v = vector({1: 1, 3: -2})
sage: w = vector({1: 4, 3: 2})
sage: v+w
(0, 5, 0, 0)
sage: (v+w).nonzero_positions()
[1]
set()
Like __setitem__ but with no type or bounds checking.
sage.modules.free_module_element._sparse_dot_product()
v and w are dictionaries with integer keys.
sage.modules.free_module_element.free_module_element()

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 or sparse=False as an option.

INPUT:

  • elts - entries of a vector (either a list or dict).
  • R - ring
  • sparse - optional

OUTPUT: An element of the free module over R of rank len(elts).

EXAMPLES:

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

Here we illustrate the creation of sparse vectors by using a dictionary:

sage: vector({1:1.1, 3:3.14})
(0.000000000000000, 1.10000000000000, 0.000000000000000, 3.14000000000000)

Any 1 dimensional numpy array of type float or complex may be passed to vector. The result will be a 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 column-wise slices of numpy matrices will raise 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)

If any of the arguments to vector have Python type int, long, real, or complex, they will first be coerced to the appropriate Sage objects. This fixes trac #3847:

sage: v = vector([int(0)]); v
(0)
sage: v[0].parent()
Integer Ring
sage: v = vector(range(10)); v
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
sage: v[3].parent()
Integer Ring
sage: v = vector([float(23.4), int(2), complex(2+7*I), long(1)]); v
(23.4, 2.0, 2.0 + 7.0*I, 1.0)
sage: v[1].parent()
Complex Double Field
sage.modules.free_module_element.is_FreeModuleElement()
sage.modules.free_module_element.make_FreeModuleElement_generic_dense()
sage.modules.free_module_element.make_FreeModuleElement_generic_dense_v1()
sage.modules.free_module_element.make_FreeModuleElement_generic_sparse()
sage.modules.free_module_element.make_FreeModuleElement_generic_sparse_v1()
sage.modules.free_module_element.prepare()
sage.modules.free_module_element.prepare_dict()

EXAMPLES:

sage: from sage.modules.free_module_element import prepare_dict
sage: prepare_dict({3:1 , 5:3}, QQ)
([0, 0, 0, 1, 0, 3], Rational Field)
sage: prepare_dict({},QQ)
([], Rational Field)
sage.modules.free_module_element.vector()

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 or sparse=False as an option.

INPUT:

  • elts - entries of a vector (either a list or dict).
  • R - ring
  • sparse - optional

OUTPUT: An element of the free module over R of rank len(elts).

EXAMPLES:

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

Here we illustrate the creation of sparse vectors by using a dictionary:

sage: vector({1:1.1, 3:3.14})
(0.000000000000000, 1.10000000000000, 0.000000000000000, 3.14000000000000)

Any 1 dimensional numpy array of type float or complex may be passed to vector. The result will be a 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 column-wise slices of numpy matrices will raise 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)

If any of the arguments to vector have Python type int, long, real, or complex, they will first be coerced to the appropriate Sage objects. This fixes trac #3847:

sage: v = vector([int(0)]); v
(0)
sage: v[0].parent()
Integer Ring
sage: v = vector(range(10)); v
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
sage: v[3].parent()
Integer Ring
sage: v = vector([float(23.4), int(2), complex(2+7*I), long(1)]); v
(23.4, 2.0, 2.0 + 7.0*I, 1.0)
sage: v[1].parent()
Complex Double Field

Previous topic

Free modules

Next topic

Pickling for the old CDF vector class.

This Page