QPSolver Module
The QPSolver
submodule contains classes and functions related to defining the system network. It is a submodule of the Utils module.
QPSolver Components
Array.h
Functions
-
template<typename T>
inline Vector<T> operator+(const Vector<T> &rhs) Operator that returns the positive of the vector
rhs
.- Parameters:
rhs – The vector to add.
- Returns:
Vector<T>&
-
template<typename T>
inline Vector<T> operator+(const Vector<T> &lhs, const Vector<T> &rhs) Operator that adds each element in vector
rhs
to each element inrhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand side vector to add.
rhs – The right-hand sided vector to add.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator+(const Vector<T> &lhs, const T &a) Operator that adds a constant value
a
to each element inlhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand side vector to add.
a – The constant value to add.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator+(const T &a, const Vector<T> &rhs) Operator that adds a constant value
a
to each element inrhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to add.
rhs – The right-hand sided vector to add.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator-(const Vector<T> &rhs) Operator that returns the negative of the vector
rhs
.- Parameters:
rhs – The vector to turn negative.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator-(const Vector<T> &lhs, const Vector<T> &rhs) Operator that subtracts each element in vector
rhs
from each element inlhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand side vector to subtract.
rhs – The right-hand sided vector to subtract.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator-(const Vector<T> &lhs, const T &a) Operator that subtracts a constant value
a
from each element inlhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand side vector to subtract.
a – The constant value to subtract.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator-(const T &a, const Vector<T> &rhs) Operator that subtracts each element in
rhs
from a constant valuea
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to subtract.
rhs – The right-hand sided vector to subtract.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator*(const Vector<T> &lhs, const Vector<T> &rhs) Operator that multiplies each element in vector
lhs
with each element inrhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand side vector to multiply.
rhs – The right-hand sided vector to multiply.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator*(const Vector<T> &lhs, const T &a) Operator that multiplies a constant value
a
to each element inlhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand side vector to multiply.
a – The constant value to multiply.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator*(const T &a, const Vector<T> &rhs) Operator that multiplies a constant value
a
to each element inrhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to multiply.
rhs – The right-hand sided vector to multiply.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator/(const Vector<T> &lhs, const Vector<T> &rhs) Operator that divides each element in vector
lhs
with each element inrhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand side vector to divide.
rhs – The right-hand sided vector to divide.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator/(const Vector<T> &lhs, const T &a) Operator that divides a constant value
a
to each element inlhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand side vector to divide.
a – The constant value to divide.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator/(const T &a, const Vector<T> &rhs) Operator that divides each element in
rhs
from a constant valuea
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to divide.
rhs – The right-hand sided vector to divide.
- Returns:
Vector<T>
-
template<typename T>
inline Vector<T> operator^(const Vector<T> &lhs, const Vector<T> &rhs) Operator that raises each element in
lhs
to the power ofrhs
.- Parameters:
lhs – The base vector .
rhs – The vector to raise to the power of.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>&
-
template<typename T>
inline Vector<T> operator^(const Vector<T> &lhs, const T &a) Operator that raises each element in
lhs
to the power of the constant valuea
.- Parameters:
lhs – The base vector.
a – The constant value to raise to the power of.
- Returns:
Vector<T>&
-
template<typename T>
inline Vector<T> operator^(const T &a, const Vector<T> &rhs) Operator that raises a constant value
a
to each element inrhs
.- Parameters:
a – The base constant value.
rhs – The right-hand sided vector to raise to the power of.
- Returns:
Vector<T>&
-
template<typename T>
inline bool operator==(const Vector<T> &v, const Vector<T> &w) Equivalence operator that checks if all elements in two vectors are equal.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
v – A vector to compare.
w – A vector to compare.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
True is all values in
v
are equal to the corresponding values inw
. False otherwise.
-
template<typename T>
inline bool operator!=(const Vector<T> &v, const Vector<T> &w) A non-equivalence operator that checks if any element in two vectors is not equal.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
v – A vector to compare.
w – A vector to compare.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
True if any element in
v
is not equal to the corresponding element inw
. False otherwise.
-
template<typename T>
inline bool operator<(const Vector<T> &v, const Vector<T> &w) Operator that checks if all elements in the vector
v
are less than the corresponding elements in the vectorw
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
v – A vector to compare.
w – A vector to compare.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
True is all elements in
v
are less than the corresponding elements inw
. False otherwise.
-
template<typename T>
inline bool operator<=(const Vector<T> &v, const Vector<T> &w) Operator that checks if all elements in the vector
v
are less than or equal to the corresponding elements in the vectorw
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
v – A vector to compare.
w – A vector to compare.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
True if all elements in
v
are less than or equal to the corresponding elements inw
. False otherwise.
-
template<typename T>
inline bool operator>(const Vector<T> &v, const Vector<T> &w) Operator that checks if all elements in the vector
v
are greater than the corresponding elements in the vectorw
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
v – A vector to compare.
w – A vector to compare.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
True if all elements in
v
are greater than the corresponding elements inw
. False otherwise.
-
template<typename T>
inline bool operator>=(const Vector<T> &v, const Vector<T> &w) Operator that checks if all elements in the vector
v
are greater than or equal to the corresponding elements in the vectorw
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
v – A vector to compare.
w – A vector to compare.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
True if all elements in
v
are greater than or equal to the corresponding elements inw
. False otherwise.
-
template<typename T>
inline std::ostream &operator<<(std::ostream &os, const Vector<T> &v) Output stream operator that prints the vector to the output stream.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
os – The output stream.
v – The vector to print.
- Returns:
std::ostream&
-
template<typename T>
std::istream &operator>>(std::istream &is, Vector<T> &v) Input stream operator that reads the vector from the input stream.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
is – The input stream.
v – The vector to read.
- Returns:
std::istream&
-
template<typename T>
inline unsigned int partition(Vector<T> &v, unsigned int begin, unsigned int end) This function partitions a segment of a vector around a pivot for use in quicksort.
The partition operation rearranges elements in the vector such that all elements less than the pivot are on its left, and all greater elements are on its right.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors. The type of elements stored in the vector.
- Parameters:
v – The vector to be partitioned.
begin – The starting index of the segment to partition.
end – The ending index of the segment to partition.
- Returns:
unsigned int The index of the pivot after partitioning.
-
template<typename T>
inline void quicksort(Vector<T> &v, unsigned int begin, unsigned int end) This function performs a quicksort on a vector. Quicksort is performed using a recursive divide-and-conquer strategy.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
v – The vector to be sorted.
begin – The starting index of the segment to sort.
end – The ending index of the segment to sort.
-
template<typename T>
Matrix<T> operator+(const Matrix<T> &rhs) Converts the matrix to its positive equivalent.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The input matrix.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator+(const Matrix<T> &lhs, const Matrix<T> &rhs) Operator that adds two matrices of the same dimensions together.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand sided matrix to add.
rhs – The right-hand sided matrix to add.
- Throws:
std::logic_error – if the matrices are of different sizes.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator+(const Matrix<T> &lhs, const T &a) Operator that adds a constant value
a
to each element in the matrix.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand sided matrix to add.
a – The constant value to add.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator+(const T &a, const Matrix<T> &rhs) Operator that adds a constant value
a
to each element in the matrix.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to add.
rhs – The right-hand sided matrix to add.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator-(const Matrix<T> &rhs) Converts the matrix to its negative equivalent.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The matrix to convert.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator-(const Matrix<T> &lhs, const Matrix<T> &rhs) Operator that subtracts two matrices of the same dimensions.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand sided matrix to subtract.
rhs – The right-hand sided matrix to subtract.
- Throws:
std::logic_error – if the matrices are of different sizes.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator-(const Matrix<T> &lhs, const T &a) Operator that subtracts a constant value
a
from each element in the matrix.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand sided matrix to subtract.
a – The constant value to subtract.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator-(const T &a, const Matrix<T> &rhs) Operator that subtracts each element in the matrix from a constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to subtract from.
rhs – The right-hand sided matrix to subtract.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator*(const Matrix<T> &lhs, const Matrix<T> &rhs) Operator that multiplies each element of two matrices of the same dimensions together.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand sided matrix to multiply.
rhs – The right-hand sided matrix to multiply.
- Throws:
std::logic_error – if the matrices are of different sizes.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator*(const Matrix<T> &lhs, const T &a) Operator that multiplies each element of the matrix by a constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand sided matrix to multiply.
a – The constant value to multiply.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator*(const T &a, const Matrix<T> &rhs) Operator that multiplies each element of the matrix by a constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to multiply.
rhs – The right-hand sided matrix to multiply.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator/(const Matrix<T> &lhs, const Matrix<T> &rhs) Operator that divides each element of two matrices of the same dimensions.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand sided matrix to divide.
rhs – The right-hand sided matrix to divide.
- Throws:
std::logic_error – if the matrices are of different sizes.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator/(const Matrix<T> &lhs, const T &a) Operator that divides each element of the matrix by a constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand sided matrix to divide.
a – The constant value to divide.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator/(const T &a, const Matrix<T> &rhs) Operator that divides each element of the matrix by a constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to divide.
rhs – The right-hand sided matrix to divide.
- Returns:
Matrix<T>
-
template<typename T>
Matrix<T> operator^(const Matrix<T> &lhs, const T &a) Operator that raises each element of the matrix to the power of a constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
lhs – The left-hand sided matrix to raise to the power of.
a – The constant value to raise each element to the power of.
- Returns:
Matrix<T>
-
template<typename T>
inline bool operator==(const Matrix<T> &a, const Matrix<T> &b) Equality operator that compares two matrices for equality.
-
template<typename T>
inline bool operator!=(const Matrix<T> &a, const Matrix<T> &b) Inequality operator that compares two matrices for inequality.
-
template<typename T>
std::ostream &operator<<(std::ostream &os, const Matrix<T> &m) Output stream operator that writes the matrix to the output stream.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
os – The output stream to write to.
m – The matrix to write.
- Returns:
std::ostream& The output stream with the matrix written to it.
-
template<typename T>
std::istream &operator>>(std::istream &is, Matrix<T> &m) Input stream operator that reads the matrix from the input stream.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
is – The input stream to read from.
m – The matrix to read into.
- Returns:
std::istream& The input stream with the matrix read from it.
-
template<typename T>
Matrix<T> t(const Matrix<T> &a) Computes the transpose of the matrix.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The matrix to transpose.
- Returns:
Matrix<T> The transposed matrix.
-
template<typename T>
void cholesky_solve(const Matrix<T> &LL, Vector<T> &x, const Vector<T> &b) Solves a linear system using the Cholesky decomposition of a matrix.
This function solves the system ( LL^T x = b ), where ( L ) is a lower triangular matrix obtained via Cholesky decomposition.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
LL – The lower triangular matrix from the Cholesky decomposition.
x – The vector to store the solution of the linear system.
b – The vector representing the right-hand side of the equation.
-
template<typename T>
void forward_elimination(const Matrix<T> &L, Vector<T> &y, const Vector<T> b) Performs forward elimination to solve a lower triangular system ( Ly = b ).
This function solves the linear system where ( L ) is a lower triangular matrix.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
L – The square (lower triangular) matrix.
y – The vector to store the intermediate solution ( y ).
b – The vector representing the right-hand side of the equation.
- Throws:
std::logic_error – If the matrix ( L ) is not square or if the dimensions of ( b ) do not match the rows of ( L ).
-
template<typename T>
void backward_elimination(const Matrix<T> &U, Vector<T> &x, const Vector<T> &y) Performs backward elimination to solve an upper triangular system ( Ux = y ).
This function solves the linear system where ( U ) is an upper triangular matrix.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
U – The square (upper triangular) matrix.
x – The vector to store the solution ( x ).
y – The vector representing the right-hand side of the equation.
- Throws:
std::logic_error – If the matrix ( U ) is not square or if the dimensions of ( y ) do not match the rows of ( U ).
-
template<typename T>
Vector<T> cholesky_solve(const Matrix<T> &LL, const Vector<T> &b) Solves a linear system using the Cholesky decomposition of a matrix.
This function solves the system ( LL^T x = b ), where ( L ) is a lower triangular matrix obtained from Cholesky decomposition.
Steps:
Validate that ( LL ) is a square matrix and that the size of ( b ) matches the number of rows in ( LL ).
Solve ( L y = b ) using forward elimination to compute the intermediate vector ( y ).
Solve ( L^T x = y ) using backward elimination to compute the final solution ( x ).
Return the solution vector ( x ).
- Template Parameters:
T – The type of elements stored in the matrix and vectors.
- Parameters:
LL – The lower triangular matrix from the Cholesky decomposition.
b – The vector representing the right-hand side of the equation.
- Throws:
std::logic_error – If the matrix ( LL ) is not square or if the dimensions of ( b ) do not match the rows of ( LL ).
- Returns:
Vector<T> The solution vector ( x ).
-
template<typename T>
class Vector - #include <Array.h>
Public Functions
-
Vector()
Base constructor for a new Vector< T>:: Vector object with data type
T
with size 0 and no data.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
-
Vector(const unsigned int n)
Construct a new Vector< T>:: Vector object with size
n
and no data.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
n – The size of the vector.
-
Vector(const T &a, const unsigned int n)
Construct a new Vector< T>:: Vector object with size
n
and initialize all elements to the constant valuea
.This template takes a reference to the constant value
a
and initializes all elements to this value.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to initialize all elements to.
n – The size of the vector
-
Vector(const T *a, const unsigned int n)
Construct a new Vector< T>:: Vector object with size
n
and initialize all elements to the constant valuea
.This template takes a pointer to the constant value
a
. It first dereferences the pointer to get the value ofa
and then increments the pointer to get the next value.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The array to initialize the vector to.
n – The size of the vector.
-
Vector(const Vector &rhs)
Construct a new Vector< T>:: Vector object by copying the vector
rhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The vector to copy.
-
~Vector()
Destroy the Vector< T>:: Vector object.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
-
inline T &operator[](const unsigned int &i)
A subscripting operator to access the
i
-th element of the vector.- Parameters:
i – The index of the element to access.
- Returns:
T& A reference to the
i
-th element.
-
inline const T &operator[](const unsigned int &i) const
A subscripting operator to access the
i
-th element of the vector without changing the vector itself.- Parameters:
i – The index of the element to access.
- Returns:
T& A reference to the
i
-th element.
-
inline unsigned int size() const
Returns the size of the vector.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Returns:
unsigned int
-
inline void resize(const unsigned int n)
Resize the vector to size
n
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
n – The size to resize the vector to.
-
inline void resize(const T &a, const unsigned int n)
Resize the vector to size
n
and initialize all elements to the constant valuea
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to initialize all elements to.
n – The size to resize the vector to.
-
inline Vector<T> &operator=(const Vector<T> &rhs)
Assignment operator that assigns the current vector to the vector
rhs
.Postcondition: normal assignment via copying has been performed. If vector and rhs were different sizes, vector has been resized to match the size of
rhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The vector to assign to.
- Returns:
Vector<T>&
-
inline Vector<T> &operator=(const T &a)
Assignment operator that assigns the constant value
a
to every element of the vector.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to assign to every element.
- Returns:
Vector<T>&
-
inline Vector<T> &operator+=(const Vector<T> &rhs)
Operator that performs an in-place addition of each element in the vector
rhs
to each element in the current vector.- Parameters:
rhs – The vector to add.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>&
-
inline Vector<T> &operator-=(const Vector<T> &rhs)
Operator that performs an in-place subtraction of each element in vector
rhs
from each element in the current vector.- Parameters:
rhs – The vector to subtract.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>&
-
inline Vector<T> &operator*=(const Vector<T> &rhs)
Operator that multiplies each element in vector
rhs
with each element in the current vector.- Parameters:
rhs – The vector to multiply.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>&
-
inline Vector<T> &operator/=(const Vector<T> &rhs)
Operator that divides each element in vector
rhs
with each element in the current vector.- Parameters:
rhs – The vector to divide.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>&
-
inline Vector<T> &operator^=(const Vector<T> &rhs)
Operator that performs an in-place raising of each element in the vector to the power each element in
rhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The vector to raise to the power of.
- Throws:
std::logic_error – if the vectors are of different sizes.
- Returns:
Vector<T>&
-
inline Vector<T> &operator+=(const T &a)
Operator that performs an in-place addition of a constant value
a
to each element in the vector.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to add.
- Returns:
Vector<T>&
-
inline Vector<T> &operator-=(const T &a)
Operator that performs an in-place subtraction of a constant value
a
from each element in the vector.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to subtract.
- Returns:
Vector<T>&
-
inline Vector<T> &operator*=(const T &a)
Operator that multiplies each element in the vector with the constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to multiply.
- Returns:
Vector<T>&
-
inline Vector<T> &operator/=(const T &a)
Operator that divides each element in the vector with the constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to divide.
- Returns:
Vector<T>&
-
inline Vector<T> &operator^=(const T &a)
Operator that performs an in-place raising of each element in the vector to the power of the constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to raise to the power of.
- Returns:
Vector<T>&
-
Vector()
-
template<typename T>
class Matrix - #include <Array.h>
Public Functions
-
Matrix()
The default constructor for a new Matrix object. This constructor initializes the matrix to have 0 rows and 0 columns.
-
Matrix(const unsigned int n, const unsigned int m)
Construct a new Matrix< T>:: Matrix object with
n
rows andm
columns.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
n – The number of rows in the matrix.
m – The number of columns in the matrix.
-
Matrix(const T &a, const unsigned int n, const unsigned int m)
Construct a new Matrix< T>:: Matrix object with
n
rows andm
columns and initialize all elements to the constant valuea
. This constructor initializes all elements in the matrix to a reference to the constant valuea
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – A reference to the constant value to initialize all elements in the matrix to.
n – The number of rows in the matrix.
m – The number of columns in the matrix.
-
Matrix(MType t, const T &a, const T &o, const unsigned int n, const unsigned int m)
Constructs a diagonal matrix of specified dimensions and initializes its elements based on the matrix type.
This constructor supports creating a diagonal matrix with a constant value
a
on the diagonal ando
as the off-diagonal elements.- Template Parameters:
T – The type of the elements stored in the matrix and vectors. The type of elements stored in the matrix.
- Parameters:
t – The type of the matrix to create (e.g., DIAG for a diagonal matrix).
a – The value to initialize the diagonal elements (if applicable).
o – The value to initialize the off-diagonal elements (if applicable).
n – The number of rows in the matrix.
m – The number of columns in the matrix.
- Throws:
std::logic_error – If the specified matrix type is not supported.
-
Matrix(MType t, const Vector<T> &v, const T &o, const unsigned int n, const unsigned int m)
Constructs a diagonal matrix of specified dimensions and initializes its elements based on the matrix type.
This constructor supports creating a diagonal matrix with the input vector
a
’s elements on the diagonal and a pre-specified valueo
as the off-diagonal elements.- Template Parameters:
T – The type of the elements stored in the matrix and vectors. The type of elements stored in the matrix.
- Parameters:
t – The type of the matrix to create (e.g., DIAG for a diagonal matrix).
a – The vector used to initialize the diagonal elements (if applicable).
o – The value to initialize the off-diagonal elements (if applicable).
n – The number of rows in the matrix.
m – The number of columns in the matrix.
- Throws:
std::logic_error – If the specified matrix type is not supported.
-
Matrix(const T *a, const unsigned int n, const unsigned int m)
Construct a new Matrix< T>:: Matrix object with
n
rows andm
columns and initialize all elements to the constant valuea
. This constructor initializes all elements in the matrix to a pointer to the constant valuea
. It first dereferences the pointera
and then assigns the value to each element in the matrix.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – A reference to the constant value to initialize all elements in the matrix to.
n – The number of rows in the matrix.
m – The number of columns in the matrix.
-
Matrix(const Matrix<T> &rhs)
Constructs a new matrix as a deep copy of an existing matrix.
This copy constructor duplicates the dimensions and contents of the provided matrix into a new matrix instance.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors. The type of elements stored in the matrix.
- Parameters:
rhs – The matrix to copy.
-
~Matrix()
Destroys the matrix object and frees the memory allocated for the matrix data.
-
inline void resize(const unsigned int n, const unsigned int m)
Resizes the matrix to have
n
rows andm
columns.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
n – The number of rows of the resized matrix.
m – The number of columns of the resized matrix.
-
inline void resize(const T &a, const unsigned int n, const unsigned int m)
Resizes the matrix to have
n
rows andm
columns. Sets all elements to the constant valuea
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to set all elements in the matrix to.
n – The number of rows of the resized matrix.
m – The number of columns of the resized matrix.
-
inline Vector<T> extractRow(const unsigned int i) const
Extracts a specific row from the matrix as a vector.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
i – The row index to extract.
- Throws:
std::logic_error – if the row index is out of bounds.
- Returns:
Vector<T>
-
inline Vector<T> extractColumn(const unsigned int j) const
Extracts a specific column from the matrix as a vector.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
j – The column index to extract.
- Throws:
std::logic_error – if the column index is out of bounds.
- Returns:
Vector<T>
-
inline unsigned int nrows() const
-
inline unsigned int ncols() const
-
inline Matrix<T> &operator=(const Matrix<T> &rhs)
Assignment operator that copies the contents of the right-hand side matrix
rhs
into the current matrix. If matrix and rhs were different sizes, matrix has been resized to match the size ofrhs
.Postcondition: normal assignment via copying has been performed;
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The right-hand side matrix to copy.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator=(const T &a)
Assignment operator that assigns the constant value
a
to each element in the matrix.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to assign to each element in the matrix.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator+=(const Matrix<T> &rhs)
Operator that performs an in-place addition of two matrices with the same dimensions.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The right-hand sided matrix to add.
- Throws:
std::logic_error – if the matrices are of different sizes.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator-=(const Matrix<T> &rhs)
Operator that performs an in-place subtraction of two matrices with the same dimensions.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The right-hand sided matrix to subtract.
- Throws:
std::logic_error – if the matrices are of different sizes.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator*=(const Matrix<T> &rhs)
Operator that performs an in-place multiplication of each element of two matrices with the same dimensions.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The right-hand sided matrix to multiply.
- Throws:
std::logic_error – if the matrices are of different sizes.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator/=(const Matrix<T> &rhs)
Operator that performs an in-place division of each element of two matrices with the same dimensions.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The right-hand sided matrix to divide.
- Throws:
std::logic_error – if the matrices are of different sizes.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator^=(const Matrix<T> &rhs)
Operator that raises each element of the matrix to the power of each element in
rhs
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
rhs – The right-hand sided matrix to raise to the power of.
- Returns:
Matrix<T>
-
inline Matrix<T> &operator+=(const T &a)
Operator that performs an in-place addition of a constant value
a
to each element in the matrix.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to add.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator-=(const T &a)
Operator that performs an in-place subtraction of a constant value
a
from each element in the matrix.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to subtract.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator*=(const T &a)
Operator that performs an in-place multiplication of each element of the matrix by a constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to multiply.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator/=(const T &a)
Operator that performs an in-place division of each element of the matrix by a constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to divide.
- Returns:
Matrix<T>&
-
inline Matrix<T> &operator^=(const T &a)
Operator that performs an in-place operation to raise each element of the matrix to the power of a constant value
a
.- Template Parameters:
T – The type of the elements stored in the matrix and vectors.
- Parameters:
a – The constant value to raise each element to the power of.
- Returns:
Matrix<T>&
-
inline operator Vector<T>()
Converts a matrix to a vector if it has a single row or column.
This operator allows a matrix to be cast to a vector by extracting its single row or column.
- Template Parameters:
T – The type of the elements stored in the matrix and vectors. The type of elements stored in the matrix.
- Throws:
std::logic_error – If the matrix has more than one row and more than one column.
- Returns:
Vector<T> The extracted row or column as a vector.
-
Matrix()
QuadProg++.h
Functions
-
double solve_quadprog(Matrix<double> &G, Vector<double> &g0, const Matrix<double> &CE, const Vector<double> &ce0, const Matrix<double> &CI, const Vector<double> &ci0, Vector<double> &x)
Solves a quadratic programming problem using the Goldfarb-Idnani method. This function solves the quadratic programming problem:
Minimize: 0.5 * x^T * G * x + g0^T * x Subject to: CE^T * x + ce0 = 0 CI^T * x + ci0 >= 0
- Parameters:
G – The symmetric positive-definite matrix for the quadratic term.
g0 – The vector for the linear term in the objective function.
CE – The matrix for equality constraints.
ce0 – The vector for equality constraints.
CI – The matrix for inequality constraints.
ci0 – The vector for inequality constraints.
x – The vector to store the solution of the optimization problem.
- Throws:
- Returns:
The minimum value of the quadratic objective function.
-
void solve_quadprog_matlab_syntax(Matrix<double> &G, Vector<double> &g0, const Matrix<double> &CE, const Vector<double> &ce0, const Matrix<double> &CI, const Vector<double> &ci0, const Vector<double> &lb, const Vector<double> &ub, Vector<double> &x)
Solves a quadratic programming problem using MATLAB-style input format. This function provides a wrapper for
solve_quadprog
with MATLAB-style input, including lower and upper bounds on the solution.See also
- Parameters:
G – The symmetric positive-definite matrix for the quadratic term.
g0 – The vector for the linear term in the objective function.
CE – The matrix for equality constraints.
ce0 – The vector for equality constraints.
CI – The matrix for inequality constraints.
ci0 – The vector for inequality constraints.
lb – The vector for lower bounds.
ub – The vector for upper bounds.
x – The vector to store the solution of the optimization problem.
- Throws:
std::logic_error – If input dimensions are inconsistent.
-
void print_matrix(char *name, const Matrix<double> &A, int n = -1, int m = -1)
Prints the contents of a matrix to the console in a formatted style. This function formats and prints a matrix along with its name, for debugging or informational purposes.
- Parameters:
name – The name of the matrix to display as a label.
A – The matrix to be printed.
n – The number of rows to print. If -1, all rows are printed.
m – The number of columns to print. If -1, all columns are printed.
- Throws:
None –
-
void print_vector(char *name, const Vector<double> &v, int n = -1)
Prints the contents of a vector to the console in a formatted style. This function formats and prints a vector along with its name, for debugging or informational purposes.
- Parameters:
name – The name of the vector to display as a label.
v – The vector to be printed.
n – The number of elements to print. If -1, all elements are printed.