# Matrix

class Matrix

A Matrix is represented by rows and columns. The first index is the row, and the second is the column.

Subclassed by feasst::RotationMatrix

Public Functions

void set_size(const int num_rows, const int num_columns)

Set the size by number of rows and columns.

Matrix(std::vector<std::vector<double>> matrix)

Alternatively, construct with 2d vector data.

int num_rows() const

Return the number of rows.

int num_columns() const

Return the number of columns.

void set_value(const int row, const int column, const double value)

Set the value of an element given by the row and column index.

double value(const int row, const int column) const

Return the value of the element.

const std::vector<std::vector<double>> &matrix() const

Return the entire matrix.

virtual void check() const

Check that each row and column are the same size.

void transpose()

Switch the rows and columns.

void transpose(Matrix *tmp)

Optimized version of above with pre-sized temporary.

Add all elements of Matrix to self.

void multiply(const double constant)

Multiply all elements by a constant.

void zero()

Set all elements to zero.

void identity(const int size)

Set to identity matrix.

Position multiply(const Position &vec) const

Return the vector which is a product of the multiplication of a matrix with the given vector.

void multiply(const Position &vec, Position *result) const

Same as above, but optimized to avoid construction of Position.

Matrix multiply(const Matrix &matrix) const

Multiply self with another matrix, m2 (e.g., self*m2).

void multiply(const Matrix &matrix, Matrix *result, Position *tmp1, Position *tmp2) const

Multiply self with another matrix, m2 (e.g., self*m2). This is the optimized verison.

bool is_equal(const Matrix &matrix) const

Return true if all elements are equal to the given matrix.

std::string str() const

Return the matrix as a human readable string.

double determinant() const

Return the determinant (only implemented for 2x2 and 3x3 matrices).

void invert()

Invert the matrix (only implemented for 2x2 and 3x3 matrices).

bool is_identity(const double tolerance = NEAR_ZERO) const

Return true if identity matrix.

void skew_symmetric_cross_product(const Position &position)

For 3D, return the 3x3 matrix to write cross products as a x b = [a]_x b. See https://en.wikipedia.org/wiki/Skew-symmetric_matrix

class RotationMatrix : public feasst::Matrix

Rotation matrices represent a rotation of a rigid body. They must be square with a unit determinant.

Public Functions

RotationMatrix &axis_angle(const Position &axis, const double degree_angle)

Compute the rotation matrix given an angle of rotation about a given axis. In 2D, the axis is simply used to specify the number of dimensions.

param degree_angle:

The angle is in units of degrees.

void axis_angle_opt(const Position &unit_axis, const double degree_angle)

Same as above, but optimized (1) axis is assumed to be unit length and (2) rotation matrix is the correct size and (3) determinant is not checked.

param degree_angle:

The angle is in units of degrees.

void rotate(const Position &pivot, Position *rotated) const

Rotate a position given a pivot point.

void rotate(const Position &pivot, Position *rotated, Position *temp) const

Same as above, but optimized with temporary storage.

void quaternion(const Position &quaternion)

Compute rotation matrix based on quaternions (3D only).

void vector_onto_vector(const Position &vec1, const Position &vec2)

Compute the rotation matrix that rotates vector a onto vector b. See https://math.stackexchange.com/a/476311

void vector_onto_vector(const Position &vec1, const Position &vec2, Position *tmp_vec, Matrix *tmp_mat)

Same as above, but optimized to avoid creating temporary objects.

void check() const

Check square matrix, unit derminant, in addition to Matrix::check.