44 template<
typename ElementType>
50 Matrix (
size_t numRows,
size_t numColumns)
51 : rows (numRows), columns (numColumns)
60 Matrix (
size_t numRows,
size_t numColumns,
const ElementType* dataPointer)
61 : rows (numRows), columns (numColumns)
64 memcpy (data.
getRawDataPointer(), dataPointer, rows * columns *
sizeof (ElementType));
108 void clear() noexcept { zeromem (data.
begin(), (size_t) data.
size() *
sizeof (ElementType)); }
119 inline ElementType
operator() (
size_t row,
size_t column)
const noexcept
121 jassert (row < rows && column < columns);
122 return data.
getReference (static_cast<int> (dataAcceleration.
getReference (static_cast<int> (row))) + static_cast<int> (column));
126 inline ElementType&
operator() (
size_t row,
size_t column) noexcept
128 jassert (row < rows && column < columns);
129 return data.
getReference (static_cast<int> (dataAcceleration.
getReference (static_cast<int> (row))) + static_cast<int> (column));
144 inline Matrix&
operator+= (
const Matrix& other) noexcept {
return apply (other, [] (ElementType a, ElementType b) {
return a + b; } ); }
147 inline Matrix&
operator-= (
const Matrix& other) noexcept {
return apply (other, [] (ElementType a, ElementType b) {
return a - b; } ); }
152 std::for_each (begin(), end(), [scalar] (ElementType& x) { x *= scalar; });
169 inline Matrix&
hadarmard (
const Matrix& other) noexcept {
return apply (other, [] (ElementType a, ElementType b) {
return a * b; } ); }
176 static bool compare (
const Matrix& a,
const Matrix& b, ElementType tolerance = 0) noexcept;
179 inline bool operator== (
const Matrix& other)
const noexcept {
return compare (*
this, other); }
183 bool isSquare() const noexcept {
return rows == columns; }
195 bool isNullMatrix() const noexcept {
return rows == 0 || columns == 0; }
214 ElementType* begin() noexcept {
return data.
begin(); }
215 ElementType* end() noexcept {
return data.
end(); }
217 const ElementType* begin()
const noexcept {
return &data.
getReference (0); }
218 const ElementType* end()
const noexcept {
return begin() + data.
size(); }
225 data.
resize (static_cast<int> (columns * rows));
226 dataAcceleration.
resize (static_cast<int> (rows));
228 for (
size_t i = 0; i < rows; ++i)
229 dataAcceleration.
setUnchecked (static_cast<int> (i), i * columns);
232 template <
typename BinaryOperation>
233 Matrix& apply (
const Matrix& other, BinaryOperation binaryOp)
235 jassert (rows == other.rows && columns == other.columns);
239 for (
auto src : other)
241 *dst = binaryOp (*dst, src);
252 size_t rows, columns;
255 JUCE_LEAK_DETECTOR (
Matrix)
Matrix & swapRows(size_t rowOne, size_t rowTwo) noexcept
Swaps the contents of two rows in the matrix and returns a reference to itself.
Matrix operator+(const Matrix &other) const
Addition of two matrices.
Matrix operator*(ElementType scalar) const
Scalar multiplication.
size_t getNumColumns() const noexcept
Returns the number of columns in the matrix.
ElementType * end() noexcept
Returns a pointer to the element which follows the last element in the array.
bool isOneColumnVector() const noexcept
Tells if the matrix is a one column vector.
static Matrix identity(size_t size)
Creates the identity matrix.
Matrix & hadarmard(const Matrix &other) noexcept
Does a hadarmard product with the receiver and other and stores the result in the receiver...
Matrix & swapColumns(size_t columnOne, size_t columnTwo) noexcept
Swaps the contents of two columns in the matrix and returns a reference to itself.
bool isOneRowVector() const noexcept
Tells if the matrix is a one row vector.
static Matrix hadarmard(const Matrix &a, const Matrix &b)
Does a hadarmard product with a and b returns the result.
void resize(int targetNumItems)
This will enlarge or shrink the array to the given number of elements, by adding or removing items fr...
static Matrix hankel(const Matrix &vector, size_t size, size_t offset=0)
Creates a squared size x size Hankel Matrix from a vector with an optional offset.
String toString() const
Returns a String displaying in a convenient way the matrix contents.
Matrix(size_t numRows, size_t numColumns)
Creates a new matrix with a given number of rows and columns.
ElementType * begin() noexcept
Returns a pointer to the first element in the array.
size_t getNumRows() const noexcept
Returns the number of rows in the matrix.
Matrix operator-(const Matrix &other) const
Addition of two matrices.
void setUnchecked(int indexToChange, ParameterType newValue)
Replaces an element with a new value without doing any bounds-checking.
ElementType * getRawDataPointer() noexcept
Returns a pointer to the raw data of the matrix object, ordered in row-major order (for modifying)...
bool solve(Matrix &b) const noexcept
Solves a linear system of equations represented by this object and the argument b, using various algorithms depending on the size of the arguments.
bool isSquare() const noexcept
Tells if the matrix is a square matrix.
Matrix & operator=(const Matrix &)=default
Creates a copy of another matrix.
Array< size_t > getSize() const noexcept
Returns an Array of 2 integers with the number of rows and columns in the matrix. ...
int size() const noexcept
Returns the current number of elements in the array.
Matrix & operator+=(const Matrix &other) noexcept
Addition of two matrices.
ElementType * getRawDataPointer() noexcept
Returns a pointer to the actual array data.
Matrix & operator*=(ElementType scalar) noexcept
Scalar multiplication.
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in...
Matrix(size_t numRows, size_t numColumns, const ElementType *dataPointer)
Creates a new matrix with a given number of rows and columns, with initial data coming from an array...
static Matrix toeplitz(const Matrix &vector, size_t size)
Creates a Toeplitz Matrix from a vector with a given squared size.
void clear() noexcept
Fills the contents of the matrix with zeroes.
const ElementType * getRawDataPointer() const noexcept
Returns a pointer to the raw data of the matrix object, ordered in row-major order (for reading)...
General matrix and vectors class, meant for classic math manipulation such as additions, multiplications, and linear systems of equations solving.
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Compare to matrices with a given tolerance.
ElementType operator()(size_t row, size_t column) const noexcept
Returns the value of the matrix at a given row and column (for reading).
bool isNullMatrix() const noexcept
Tells if the matrix is a null matrix.
bool isVector() const noexcept
Tells if the matrix is a vector.
Matrix & operator-=(const Matrix &other) noexcept
Subtraction of two matrices.