Linear Algebra helpers

Header file consisting a collection of basic linear algebra classes.

Author

Mukund Yedunuthala

class Vectors
#include <linalg.h>

A class representing a dynamic array of double values with vector operations.

Author

Mukund Yedunuthala

Public Functions

Vectors(unsigned int &s)

Constructs a Vectors object with the given size.

Parameters:

s – Reference to the size of the vector.

~Vectors()

Destructor to free allocated memory.

Vectors(const Vectors &other)

Copy constructor for deep copying another Vectors object.

Parameters:

other – The vector to be copied.

Vectors &operator=(const Vectors &other)

Copy assignment operator for deep copying another Vectors object.

Parameters:

other – The vector to be assigned.

Returns:

Reference to the assigned vector.

double getDataAtIndex(unsigned int i)

Retrieves the value at a specific index in the vector.

Parameters:

i – The index to access.

Returns:

The value at the specified index.

int getSize()

Gets the size of the vector.

Returns:

The size of the vector.

double *getData()

Retrieves the pointer to the data array.

Returns:

Pointer to the vector data.

void setData(double *&inputData)

Sets the vector data using an external array.

Parameters:

inputData – Reference to the pointer of the input data.

void setValueAtIndex(unsigned int i, double value)

Sets the value at a specific index.

Parameters:
  • i – The index where the value should be set.

  • value – The value to set.

void setToValue(double value)

Sets all elements of the vector to a given value.

Parameters:

value – The value to set all elements to.

double sum()

Computes the sum of all elements in the vector.

Returns:

The sum of vector elements.

double abssum()

Computes the absolute sum of all elements.

Returns:

The sum of absolute values of all elements.

double norm()

Computes the Euclidean norm (magnitude) of the vector.

Returns:

The norm of the vector.

double dot(Vectors &other)

Computes the dot product of this vector with another vector.

Parameters:

other – The other vector to compute the dot product with.

Returns:

The dot product result.

void print()

Prints the vector elements to the console.

Private Members

unsigned int size

The size of the vector.

double *data = nullptr

Pointer to the dynamically allocated array of vector elements.

class Matrix
#include <linalg.h>

A class representing a 2D matrix with basic operations.

Public Functions

Matrix(unsigned int &r, unsigned int &c)

Constructs a Matrix object with the given dimensions.

Parameters:
  • r – Reference to the number of rows.

  • c – Reference to the number of columns.

Matrix(const Matrix &other)

Copy constructor for deep copying another Matrix object.

Parameters:

other – The matrix to be copied.

Matrix &operator=(const Matrix &other)

Copy assignment operator for deep copying another Matrix object.

Parameters:

other – The matrix to be assigned.

Returns:

Reference to the assigned matrix.

~Matrix()

Destructor to free allocated memory.

double getValue(unsigned int i, unsigned int j)

Retrieves the value at a specific row and column.

Parameters:
  • i – The row index.

  • j – The column index.

Returns:

The value at the specified position.

int getSize()

Gets the total number of elements in the matrix.

Returns:

The total number of elements (rows * cols).

void setValue(unsigned int i, unsigned int j, double value)

Sets the value at a specific row and column.

Parameters:
  • i – The row index.

  • j – The column index.

  • value – The value to be set.

Vectors dot(Vectors &vec)

Computes the matrix-vector multiplication.

Parameters:

vec – The vector to multiply with the matrix.

Returns:

A new Vectors object containing the result of the multiplication.

void print()

Prints the matrix elements to the console.

Private Members

int rows
int cols

Number of rows and columns in the matrix.

double *data

Pointer to the dynamically allocated array of matrix elements.