Linear algebra

The algebra.h file provides a few functions for linear algebra operations. Thoese functions include calculating the norm of a vector, computing inner product of vectors, performing matrix vector operations, etc. We provide both naive implementation (under namespace MyAlgebra) and (Sparse) BLAS based implementation (under namespace BLASAlgebra) for these functions. You can specify the namespace through the algebra_namespace_switcher.h header file.

algebra.h

Vector functions

double norm(Vector &x, int type)

Return the norm of a vector of different type:

  • type = 0: \(\ell_0\) norm, the number of nonzeros;
  • type = 1: \(\ell_1\) norm. Sum of the absolute values;
  • type = 2: \(\ell_2\) norm. Euclidean norm;
  • type = 3: \(\ell_\infty\) norm. Maximum of the absolute value.
double norm(Vector &x)

Calculate the \(\ell_2\) norm of a vector.

void sub(Vector &a, Vector &b)

Subtract b from a, i.e., a = a - b.

void add(Vector &a, Vector &b)

Add dense vector b to vector a, i.e., a = a + b.

void add(Vector &a, SpVec &b)

Add sparse vector b to vector a, i.e., a = a + b.

double dot(Vector &a, Vector &b)

Calculate the inner product of vector a and vector b.

void copy(Vector &x, Vector &y, int start, int end)

Copy part of a vector to another vector, i.e., y = x(start:end)

Matrix functions

void calculate_column_norm(Matrix &A, Vector &nrm)

Calculate the \(\ell_2\) norm for each column of dense matrix A.

void calculate_column_norm(SpMat &A, Vector &nrm)

Calculate the \(\ell_2\) norm for each column of sparse matrix A.

void multiply(SpMat &A, SpMat &AAt)

Multiply A with A’, i.e., AAt = A * A’

void multiply(Matrix &A, Matrix &AAt)

Multiply A with A’, i.e., AAt = A * A’

void copy(Matrix &A, Matrix &B, int start, int end)

Copy part of a dense matrix to another dense matrix, i.e., B = A(start:end, :)

void copy(SpMat &A, SpMat &B, int start, int end)

Copy part of a sparse matrix to another sparse matrix, i.e., B = A(start:end, :)

Matrix vector functions

void trans_multiply(Matrix &A, Vector &x, Vector &Atx)

Calculate \(Atx = A^T \, x\).

void sub(Vector &a, Matrix &A, int row, double scalar)

Subtract a scalar times a row of matrix from a given vector, i.e., a = a - scalar * A(row, :)

void sub(Vector &a, SpMat &A, int row, double scalar)

Subtract a scalar times a row of matrix from a given vector, i.e., a = a - scalar * A(row, :)

void sub(SpVec &a, SpMat &A, int row, double scalar)

Subtract a scalar times a row of matrix from a given vector, i.e., a = a - scalar * A(row, :)

void sub(SpVec &a, Matrix &A, int row, double scalar)

Subtract a scalar times a row of matrix from a given vector, i.e., a = a - scalar * A(row, :)

double dot(SpMat &A, Vector &x, int row)
double dot(Matrix &A, Vector &x, int row)

Calcuate inner product of A(row, :) * x.

void multiply(SpMat &A, Vector &x, Vector &Ax)

multiply sparse matrix A with x, i.e., \(Ax = A \cdot x\)

void multiply(Matrix &A, Vector &x, Vector &Ax)

multiply dense matrix A with x, i.e., \(Ax = A \cdot x\)

Print functions

void print(Vector &x)

Print the vector.

void print(Matrix &x)

Print the dense matrix.

void print(SpMat &x)

Print the sparse matrix.