0% found this document useful (0 votes)
2 views24 pages

LINEAR_PROJECT

This project report on Linear Algebra and Transforms explores key concepts such as matrix diagonalization, eigenvalues, eigenvectors, and the Gram-Schmidt process through both theoretical and computational approaches. It includes structured questions that require analytical solutions and C++ implementations to automate the processes discussed. The project aims to enhance understanding of linear algebra's applications in engineering and computer science while demonstrating the integration of programming with mathematical concepts.

Uploaded by

230638
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

LINEAR_PROJECT

This project report on Linear Algebra and Transforms explores key concepts such as matrix diagonalization, eigenvalues, eigenvectors, and the Gram-Schmidt process through both theoretical and computational approaches. It includes structured questions that require analytical solutions and C++ implementations to automate the processes discussed. The project aims to enhance understanding of linear algebra's applications in engineering and computer science while demonstrating the integration of programming with mathematical concepts.

Uploaded by

230638
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

A PROJECT REPORT OF

Course: LINEAR ALGEBRA AND TRANSFORM

SUBMITTED BY:

Muhammad Rehan (230638)

Muhammad Sherdil khan (230610)

in partial fulfillment for the award of the degree

of

Bachelor of Computer Engineering 2023-27

SUBMITTED TO:

Sir Rana Danish Aslam

Department of Electrical and Computer Engineering

AIR UNIVERSITY ISLAMABAD


Contents
Introduction ....................................................................... 3
Question#1 Solution ........................................................... 4
Question#2 Solution ........................................................... 7
Question#3 Solution ........................................................... 9
C++ Implementation ......................................................... 13
Question#4: ........................................................................ 13
Question#5: ........................................................................ 20
Conclusion ........................................................................ 24
Introduction
Linear Algebra is a core area of mathematics that plays a crucial role in numerous applications across
engineering, computer science, and data analysis. It provides the framework for understanding systems of
linear equations, vector spaces, transformations, and orthogonality. This project report, titled "Linear
Algebra and Transforms", presents a comprehensive exploration of several fundamental concepts through
both analytical problem solving and computational implementations.
The project consists of five structured questions, each designed to explore a specific topic within the
domain of linear algebra:

• Question 1 involves linear transformations and matrix diagonalization. The task requires
constructing the standard matrix A corresponding to a given linear operator T: R3→R3 followed by
testing whether A is diagonalizable. If it is, a matrix P is found such that P-1 AP is diagonal.

• Question 2 deepens the analysis of matrix diagonalization and matrix powers. You are given a
matrix A and a matrix P claimed to diagonalize it. The goal is to verify the diagonalization and
then compute a high power of the matrix, A20, using the property that powers of diagonal matrices
are easily calculated.

• Question 3 focuses on orthonormalization using the Gram-Schmidt process. Starting with a


set of linearly independent vectors in R3, the process is applied to construct an orthonormal basis,
a foundational tool in vector space geometry and inner product spaces.

• Question 4 shifts from theory to application by requiring students to write C++ or Python code
to perform matrix diagonalization. The program should take a matrix as input (such as the one
in Question 1) and compute its eigenvalues and eigenvectors to construct the diagonal and
transformation matrices.

• Question 5 continues the computational focus with a programmatic implementation of the Gram-
Schmidt orthonormalization algorithm. The goal is to automate the manual process used in
Question 3, reinforcing the link between algorithmic thinking and mathematical procedures.

This project provides a blend of theoretical understanding and practical computation, aiming to equip
students with both conceptual clarity and problem-solving skills. It demonstrates how abstract algebraic
operations like diagonalization and orthogonalization are not only mathematically elegant but also
computationally essential in fields such as computer graphics, data science, and machine learning.
Question#1 Solution
Question#2 Solution
Question#3 Solution
C++ Implementation
Question#4:
INPUT CODE
#include <iostream>

#include <vector>

#include <cmath>

using namespace std;

const double EPSILON = 1e-6;

// Function to print a matrix

void printMatrix(const vector<vector<double>>& mat) {

for (const auto& row : mat) {


for (double val : row) {

cout << val << " ";

cout << endl; }}

// Function to multiply two matrices

vector<vector<double>> matrixMultiply(const vector<vector<double>>& A,

const vector<vector<double>>& B) {
int n = A.size();

vector<vector<double>> result(n, vector<double>(n, 0));

for (int i = 0; i < n; ++i)

for (int j = 0; j < n; ++j)

for (int k = 0; k < n; ++k)

result[i][j] += A[i][k] * B[k][j];

return result;
}
// Function to check if two matrices are equal (within epsilon)

bool matricesEqual(const vector<vector<double>>& A,

const vector<double>& eigenvalues) {


int n = A.size();

for (int i = 0; i < n; ++i)

for (int j = 0; j < n; ++j) {

if (i == j) {

if (fabs(A[i][j] - eigenvalues[i]) > EPSILON) return false; }

else {

if (fabs(A[i][j]) > EPSILON) return false;

}
}

return true;

// Function to find eigenvalues (simplified for 3x3 matrix)

vector<double> findEigenvalues(const vector<vector<double>>& A) {

double a = -(A[0][0] + A[1][1] + A[2][2]);


double b = A[0][0] * A[1][1] + A[0][0] * A[2][2] + A[1][1] * A[2][2]

- A[0][1] * A[1][0] - A[0][2] * A[2][0] - A[1][2] * A[2][1];

double c = -(A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1])

+ A[0][1] * (A[1][2] * A[2][0] - A[1][0] * A[2][2])

+ A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]));

vector<double> roots;
for (double x = -10; x <= 10; x += 0.1) {
double val = x * x * x + a * x * x + b * x + c;

if (fabs(val) < EPSILON) {

bool found = false;

for (double r : roots)


if (fabs(r - x) < EPSILON)

found = true;

if (!found)

roots.push_back(x);

return roots;

// Function to find eigenvectors (simplified)

vector<vector<double>> findEigenvectors(const vector<vector<double>>& A,

const vector<double>& eigenvalues) {

vector<vector<double>> eigenvectors;

int n = A.size();

for (double lambda : eigenvalues) {

vector<vector<double>> B = A;

for (int i = 0; i < n; ++i)

B[i][i] -= lambda;

for (int col = 0; col < n - 1; ++col) {

int pivot = col;


while (pivot < n && fabs(B[pivot][col]) < EPSILON) pivot++;
if (pivot >= n) continue;

if (pivot != col) swap(B[col], B[pivot]);

for (int row = col + 1; row < n; ++row) {

double factor = B[row][col] / B[col][col];

for (int c = col; c < n; ++c)

B[row][c] -= factor * B[col][c];

vector<double> eigenvector(n, 1.0); // Initial guess


for (int row = n - 1; row >= 0; --row) {

double sum = 0;

int pivot_col = -1;

for (int col = 0; col < n; ++col) {

if (fabs(B[row][col]) > EPSILON) {

if (pivot_col == -1)

pivot_col = col;
else

sum += B[row][col] * eigenvector[col];

if (pivot_col != -1)

eigenvector[pivot_col] = -sum / B[row][pivot_col];

}
double norm = 0;

for (double val : eigenvector) norm += val * val;

norm = sqrt(norm);

for (double& val : eigenvector) val /= norm;

eigenvectors.push_back(eigenvector);

return eigenvectors;

int main() {
vector<vector<double>> A = {

{8, 3, -4},

{-3, 1, 3},

{4, 3, 0}

};

cout << "Standard matrix A:" << endl;


printMatrix(A);

cout << endl;

vector<double> eigenvalues = findEigenvalues(A);

cout << "Eigenvalues: ";

for (double val : eigenvalues) cout << val << " ";
cout << endl << endl;
vector<vector<double>> eigenvectors = findEigenvectors(A, eigenvalues);

if (eigenvectors.size() < A.size()) {


cout << "Matrix A is not diagonalizable (not enough eigenvectors)." << endl;

return 0;

vector<vector<double>> P(A.size(), vector<double>(A.size()));

for (int i = 0; i < A.size(); ++i)

for (int j = 0; j < A.size(); ++j)

P[j][i] = eigenvectors[i][j]; // Each eigenvector as column


cout << "Matrix P that diagonalizes A:" << endl;

printMatrix(P);

cout << endl;

cout << "Diagonal matrix (should have eigenvalues on diagonal):" << endl;

for (int i = 0; i < A.size(); ++i) {

for (int j = 0; j < A.size(); ++j) {

if (i == j) cout << eigenvalues[i] << " ";


else cout << "0 ";

cout << endl;

return 0;

}
CODE OUTPUT
Question#5:
INPUT CODE
#include <iostream>

#include <vector>

#include <cmath>

using namespace std;


const double EPSILON = 1e-6;

// Function to compute the dot product of two vectors

double dotProduct(const vector<double>& v1, const vector<double>& v2) {


double result = 0.0;

for (size_t i = 0; i < v1.size(); ++i) {

result += v1[i] * v2[i];

}
return result;

// Function to compute the norm (length) of a vector

double vectorNorm(const vector<double>& v) {

return sqrt(dotProduct(v, v));

// Function to normalize a vector (make it unit length)

vector<double> normalize(const vector<double>& v) {


double norm = vectorNorm(v);

vector<double> result(v.size());

for (size_t i = 0; i < v.size(); ++i) {

result[i] = v[i] / norm;

}
return result;}
// Function to subtract the projection of v onto u from v

vector<double> subtractProjection(const vector<double>& v, const vector<double>& u) {

double projectionScale = dotProduct(v, u) / dotProduct(u, u);

vector<double> result(v.size());
for (size_t i = 0; i < v.size(); ++i) {

result[i] = v[i] - projectionScale * u[i];

return result;

// Function to print a vector

void printVector(const vector<double>& v) {

cout << "(";


for (size_t i = 0; i < v.size(); ++i) {

cout << v[i];

if (i != v.size() - 1) cout << ", ";

cout << ")" << endl;

int main() {
// Original basis vectors

vector<vector<double>> basis = {

{1, 1, 1}, // u1

{-1, 1, 0}, // u2

{1, 2, 1} // u3

};

cout << "Original basis vectors:" << endl;


for (const auto& vec : basis) {
printVector(vec); }

cout << endl;

// Apply Gram-Schmidt process

vector<vector<double>> orthonormalBasis;
// First vector: just normalize u1

orthonormalBasis.push_back(normalize(basis[0]));

// Second vector: subtract projection of u2 onto e1, then normalize

vector<double> v2 = subtractProjection(basis[1], orthonormalBasis[0]);

orthonormalBasis.push_back(normalize(v2));

// Third vector: subtract projections of u3 onto e1 and e2, then normalize

vector<double> v3 = subtractProjection(basis[2], orthonormalBasis[0]);

v3 = subtractProjection(v3, orthonormalBasis[1]);
orthonormalBasis.push_back(normalize(v3));

// Verify orthonormality

cout << "Orthonormal basis vectors:" << endl;

for (const auto& vec : orthonormalBasis) {

printVector(vec);

cout << endl;


// Verify they are orthonormal (dot product should be 0 for different vectors, 1 for same)

cout << "Verification of orthonormality:" << endl;

for (size_t i = 0; i < orthonormalBasis.size(); ++i) {

for (size_t j = 0; j < orthonormalBasis.size(); ++j) {

double dp = dotProduct(orthonormalBasis[i], orthonormalBasis[j]);

cout << "e" << i + 1 << "·e" << j + 1 << " = " << dp;

if (i == j && fabs(dp - 1.0) < EPSILON) {


cout << " (unit length)" << endl; }
else if (i != j && fabs(dp) < EPSILON) {

cout << " (orthogonal)" << endl;

else {
cout << " (not orthonormal)" << endl;

return 0;

}
CODE OUTPUT
Conclusion
In this project, we learned how to work with important topics in linear algebra like matrix diagonalization,
eigenvalues, eigenvectors, and the Gram-Schmidt process. We found the standard matrix for a given
transformation, checked if it was diagonalizable, and created a matrix that diagonalizes it.

We also used the Gram-Schmidt method to turn a set of vectors into an orthonormal basis. Along with
solving the questions by hand, we wrote simple C++ programs to do the same tasks using code.

This project helped us understand both the theory and the practical use of linear algebra. It showed how
we can use programming to solve mathematical problems in a clear and simple way.

<<THE END>>

You might also like