Linear Algebra: Practice Programs in Python and C Languages: Linear Algebra: Vector Spaces: Computation of Span and Basis of Vectors by Using Python
Linear Algebra
PROGRAMMES
IN PYTHON AND C LANGUAGES
UNIT − 1:
VECTOR SPACES
COMPUTATION OF SPAN AND
BASIS OF VECTORS BY USING PYTHON
The
span of a set of vectors is the set of all possible linear combination of those
vectors. A basis for the span is a minimal set of linearly independent vectors
that still span the same space. In Python, the NumPy library can be used to
perform these operations, particularly by finding the row echelon form of a
matrix formed by the vectors.
Here's
how to compute the span and a basis for a set of vectors using Python:
Python
import numpy as np
def compute_basis_and_span (vectors):
" " "
computes a basis for the span of a set of vectors.
Args:
vectors (list of of
np.array): A list of 1D NumPy arrays representing the vector.
Returns:
tuple: A tuple containing:
- list of np.array: A
list of vectors forming a basis for the span.
- str: A description of the span.
" " "
if not vectors:
return [], "The span of an empty set of vectors is the zero
vector space.
# Stack the vectors as rows to form a matrix
matrix = np.array (vectors)
# Compute the reduced row echelon form (RREF)
#This process helps helps identify linearly independent vectors
#and implicity describes the span.
#Note: NumPy's linalg.matrix_rank can help determine the
dimension of the span
#but finding the basis vectors explicitly often involves RREF or
similar technical
For a direct basis, we can use QR decomposition or SVD, Debut
for simplicity
# and to illustrate the concept of linear independence, we will
focus on the rank.
#and that idea of selecting linearly independent vectors
#Find the rank of the matrix, which is the dimension of the span
rank = np.linalg.matrix_rank (matrix)
#To find a basis, we can select linearly independent columns
from the original
#corresponding to the pivot columns in the RREF.
#A simpler approach for finding a basis from a set of vectors is
to remove
#Linear dependent vectors until only a Linearly independent set
remains.
#This can be done by iteratively checking Linear independence.
basis_vectors = []
for vec in vectors:
#Try to add the vector to the basis and check if it's Linearly
independent
temp_basis_matrix = np.array (basis_vectors + [vec])
if np.linalg.matrix_rank (temp_basis_matrix) > len
(basis_vectors):
basis_vectors.append(vec)
#Describe the span based on the dimension
if rank == len (vectors [0]):
span_description = f" The span is R^(len (vectors [0])}
(the entire (len (vector))
elif rank == 0';
span_description = "The span is the zero vector space (origin)."
else:
span_description = f"The span is a {rank)−dimensional
subspace of R^(len (vector)
return basis_vectors, span_description}
#Example Usage:
vectors1 = [np.array([1, 0, 0]), np.array([0,1,0]), np.array([0,
0, 1])]
basisl, spanl = compute basis_and_span (vectors1)
print ("vectors 1:")
print ("Basis:", [v.tolist () for V in basis1])
print ("Span: span1)
vectors2 = [np.array([1, 2]), np.array([2, 4])]
basis2, span 2 = compute_basis_and_span (vectors2)
print ("\nvectors 2")
print ("Basis: ",[v.tolist() for V in basis2])
print ("Span:", span2)
vectors3 = [np.array([1, 0, 1]), np.array([0, 1, 1,]),
np.array([1, 1, 2])]
basis3, span3 = compute_basis_and_span (vectors3)
print ("\nvectors 3:")
Linear Algebra: Practice Programs in Python and C Languages : Tag: maths, mathematics : - Computation of Span and Basis of Vectors by Using Python (Linear Algebra: Vector Spaces)
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Transforms and its Applications
MA25C03 2nd Semester EEE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Physics (CE) II
PH25C02 2nd Semester Civil, Agri Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Physics (EE) II
PH25C04 2nd Semester EEE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Physics (ME) II
PH25C05 2nd Semester Mechanical Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Chemistry (CE) II
CY25C02 2nd Semester Civil Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Applied Chemistry (ME) II
CY25C03 2nd Semester Mechanical Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Civil and Mechanical Engineering
GE25C01 2nd Semester EEE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures and Algorithms
CS25C04 2nd Semester EEE Dept | 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Mechanics
ME25C02 2nd Semester Mech, Civil, Agri Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation