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);
249 Array<ElementType> data;
250 Array<size_t> dataAcceleration;
252 size_t rows, columns;
255 JUCE_LEAK_DETECTOR (
Matrix)
void setUnchecked(int indexToChange, ParameterType newValue)
Replaces an element with a new value without doing any bounds-checking.
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
void resize(int targetNumItems)
This will enlarge or shrink the array to the given number of elements, by adding or removing items fr...
General matrix and vectors class, meant for classic math manipulation such as additions,...
Matrix operator-(const Matrix &other) const
Addition of two matrices.
Matrix & hadarmard(const Matrix &other) noexcept
Does a hadarmard product with the receiver and other and stores the result in the receiver.
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) noexcept
Addition of two matrices.
size_t getNumRows() const noexcept
Returns the number of rows in the matrix.
Matrix(Matrix &&) noexcept=default
Moves a copy of another matrix.
static Matrix hadarmard(const Matrix &a, const Matrix &b)
Does a hadarmard product with a and b returns the result.
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Compare to matrices with a given tolerance.
ElementType * getRawDataPointer() noexcept
Returns a pointer to the raw data of the matrix object, ordered in row-major order (for modifying).
Matrix(const Matrix &)=default
Creates a copy of another matrix.
bool isOneColumnVector() const noexcept
Tells if the matrix is a one column vector.
bool isVector() const noexcept
Tells if the matrix is a vector.
bool isNullMatrix() const noexcept
Tells if the matrix is a null matrix.
Matrix & swapColumns(size_t columnOne, size_t columnTwo) noexcept
Swaps the contents of two columns in the matrix and returns a reference to itself.
Matrix(size_t numRows, size_t numColumns)
Creates a new matrix with a given number of rows and columns.
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.
Array< size_t > getSize() const noexcept
Returns an Array of 2 integers with the number of rows and columns in the matrix.
Matrix operator*(ElementType scalar) const
Scalar multiplication.
ElementType operator()(size_t row, size_t column) const noexcept
Returns the value of the matrix at a given row and column (for reading).
Matrix & operator-=(const Matrix &other) noexcept
Subtraction of two matrices.
Matrix operator+(const Matrix &other) const
Addition of two matrices.
static Matrix identity(size_t size)
Creates the identity matrix.
bool solve(Matrix &b) const noexcept
Solves a linear system of equations represented by this object and the argument b,...
static Matrix toeplitz(const Matrix &vector, size_t size)
Creates a Toeplitz Matrix from a vector with a given squared size.
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,...
Matrix & operator*=(ElementType scalar) noexcept
Scalar multiplication.
size_t getNumColumns() const noexcept
Returns the number of columns in the matrix.
bool isSquare() const noexcept
Tells if the matrix is a square matrix.
bool isOneRowVector() const noexcept
Tells if the matrix is a one row vector.
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).
String toString() const
Returns a String displaying in a convenient way the matrix contents.