SymPy Demo 2: General Vector Spaces#

Demo by Jakob Lemvig, Christian Mikkelstrup, Hans Henrik Hermansen, Karl Johan Måstrup Kristiansen, and Magnus Troen. Revised 24-10-24 by shsp.

from sympy import *
init_printing()

Some Vector Operations#

Consider the following vectors:

xs = symbols('x:7')
v1 = Matrix(xs[:3])
v2 = Matrix(xs[4:])
v1,v2
([x0x1x2], [x4x5x6])

The common dot product and cross product are defined in SymPy as .dot() and .cross().

v1.dot(v2), v1.cross(v2)
(x0x4+x1x5+x2x6, [x1x6x2x5x0x6+x2x4x0x5x1x4])

Norms for vectors are defined by .norm(ord = None). By default SymPy uses the Euclidean 2-norm. For other types of norms and how to use them, please refer to the SymPy Matrix documentation.

v1.norm()
../_images/2b17cbd9549b55da826932a00905bf9ee8f711023f4eb7042ffa809c924e16ac.png

Be aware that it makes a big difference for the norm whether the symbols are defined as real or complex.

Thinning out a Span#

Consider the following vectors in C4:

v1=[1+i307i], v2=[24i2i8i], v3=[3+i7i2i8+6i], v4=[31i7i0].
v1 = Matrix([1+I, 3, 0 , 7*I])
v2 = Matrix([2, 4-I, 2*I, 8-I])
v3 = Matrix([3+I, 7-I, 2*I, 8+6*I])
v4 = Matrix([3,-1-I, 7*I, 0])

These vectors span a vector space, span(v1,v2,v3,v4). But can this span be expressed using fewer vectors? What we are asking for is the largest possible linearly independent subset of them. If the four vectors are merged as columns in a matrix:

V = Matrix.hstack(v1,v2,v3,v4)
V
[1+i23+i334i7i1i02i2i7i7i8i8+6i0]

then it is the column space of this matrix that we are asking for. SymPy can find that for us with the command .columnspace():

V.columnspace()
[[1+i307i], [24i2i8i], [31i7i0]]

From the above we see that

span(v1,v2,v3,v4)={c1v1+c2v2+c4v4|c1,c2,c3C}=span(v1,v2,v4).

Basis#

Let the following matrices A, B, C, and D span a subspace in R2×2:

A=[1620],B=[1220]C=[2210],D=[3410].
A = Matrix([[1,-6],[2,0]])
B = Matrix([[1,2],[-2,0]])
C = Matrix([[-2,2],[1,0]])
D = Matrix([[3,-4],[-1,0]])
A, B, C, D
([1620], [1220], [2210], [3410])

We want to find the dimension of and a basis for this subspace span(A,B,C,D). The standard basis for R2×2 is

ER2×2=([1000], [0010], [0100], [0001]),

and SymPy can create the vector representations of each matrix in this basis using the command .vec():

A.vec(),B.vec(),C.vec(),D.vec()
([1260], [1220], [2120], [3140])

So, the above output shows the coordinate vectors of the matrices with respect to the standard basis. These are merged as column into a coordinate matrix:

V = Matrix.hstack(A.vec(),B.vec(),C.vec(),D.vec())
V
[1123221162240000]

We now have a couple of ways for finding the dimension of and a basis for the subspace.

Method 1 - Investigate the Column Space of V#

Use the .columnspace() command:

V.columnspace()
[[1260], [1220]]

The output contains two vectors, which we recognize as the coordinate vectors for the matrices A and B. Hence, the subspace span(A,B,C,D) has a dimension of 2 and is spanned by A and B, meaning these two matrices constitute a basis for the subspace.

Method 2 - Investigate the Reduced Row-Echelon Form#

Use the .rref() command:

V.rref()
([10345401547400000000], (0, 1))

Since the reduced row-echelon form reveals a rank of 2, the dimension of span(A,B,C,D) is also 2. From the rref we can read that A and B are linearly independent, since they are the only ones associated with pivots in the rref, and therefore they constitute a basis for span(A,B,C,D) - see the course textbook for the relevant definition. Let us denote the basis that consists of the basis vectors A and B by β. New coordinates for C and D with respect to basis β are found by solving the following systems of equations:

x1A+x2B=Cx3A+x4B=D.

These are solved by setting up augmented matrices whose coefficient matrix must consist of the coordinate vectors for A and B as columns, and whose right-hand sides must be the coordinate vector for C, respectively D. Note that the coordinate vector for C is the third column in V, respectively the fourth column for D.

# Coefficient matrix as left-hand side
V_12 = V[:,[0,1]] # This extracts the coordinate vectors for A and B

# Solving for C
linsolve((V_12, V.col(2)))
../_images/afd112c15d90d2057d6a76440f0d9f838b3d88566f8e878d8bfe2e3d7f3c67db.png
# Solving for D
linsolve((V_12, V.col(3)))
../_images/7ccb8edf7005f64bc13e7d31065aad80fe1f0802fc5b34a491ca6eb133e8fa63.png

The coefficients are now known, and we have the linear combinations:

C=34A54BandD=54A+74B,

and the coordinate vectors for matrices C and D in basis β are thus:

[C]β=[3454]and[D]β=[5474].

A check to be certain:

Eq(C,-A * Rational(3/4) - Rational(5/4) * B), \
Eq(D, Rational(5/4) * A + Rational(7/4) * B)
../_images/939ce10d50b68c18e918b789f060b4425716a2c09369048b4446540d2d64c2a3.png

Difference between Real and Complex Vector Spaces#

Consider these three vectors in C3:

v1=[1+i307i], v2=[24i2i8i], v3=[2+2i7+2i2i1+6i].
v1 = Matrix([1+I, 3, 0])
v2 = Matrix([2, 4-I, 2*I])
v3 = Matrix([2 + 2*I, 7 + 2*I, 2*I])
A = Matrix.hstack(v1,v2,v3)
A
[1+i22+2i34i7+2i02i2i]

We want to determine whether the vectors v1,v2,v3 constitute a basis for, respectively, the real vector space C3 (so, over R) and the complex vector space C3 (so, over C). C3 is 3-dimensional, and the three vectors constitute a basis for a vector space of dimension 3 precisely if they are linearly independent (see the relevant theorem in the course textbook).

As A=[v1,v2,v3]C3×3 is a square matrix, we could use the determinant to investigate linear independence of v1,v2,v3 (see the relevant theorem in the course textbook). This is enough when investigating whether the vectors are linearly independent in - thus investigating whether they constitute a basis for - the vector space C3 over C. However, this will not determine linear independence over R. For that we instead consider their linear combination (see the relevant definition in the course textbook).

Let c1,c2,c3F. The vectors are linearly independent if the equation

c1v1+c2v2+c3v3=0

only holds true for c1=c2=c3=0. Written as a matrix-vector product,

[1+i22+2i34i7+2i02i2i][c1c2c3]=[000],

we solve as follows:

cs = symbols('c1:4', real=True)
sol = linsolve((A, zeros(3,1)))
Eq(Matrix(cs), Matrix(list(sol)[0]), evaulate=False)
[c1c2c3]=[τ0(1i)τ0τ0]

We see that there are other solutions than the zero-solution, so at first look we might think that this means linear dependence - but we should not be too quick!

  • Indeed, v1,v2,v3 are linearly dependent in the complex vector space C3 (so, over C) since there exist non-zero c1,c2,c3C that fulfill the linear combination.

  • But another look at the solution tells that it contains imaginary, non-real values. In the vector space C3 over R we must have c1,c2,c3R. This is only fulfilled when τ0=0, meaning that c1=c2=c3=0 is the only solution in this case. Hence v1,v2,v3 are linearly independent in the vector space C3 over R, and they constitute a basis for C3 over R.