Loading...
Searching...
No Matches
juce_Matrix.h
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
Agreement and JUCE Privacy Policy.
End User License Agreement: www.juce.com/juce-7-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce::dsp
{
template <typename ElementType>
class Matrix
{
public:
//==============================================================================
Matrix (size_t numRows, size_t numColumns)
: rows (numRows), columns (numColumns)
{
resize();
clear();
}
Matrix (size_t numRows, size_t numColumns, const ElementType* dataPointer)
: rows (numRows), columns (numColumns)
{
resize();
memcpy (data.getRawDataPointer(), dataPointer, rows * columns * sizeof (ElementType));
}
Matrix (const Matrix&) = default;
Matrix (Matrix&&) noexcept = default;
Matrix& operator= (const Matrix&) = default;
Matrix& operator= (Matrix&&) noexcept = default;
//==============================================================================
static Matrix identity (size_t size);
static Matrix toeplitz (const Matrix& vector, size_t size);
static Matrix hankel (const Matrix& vector, size_t size, size_t offset = 0);
//==============================================================================
size_t getNumRows() const noexcept { return rows; }
size_t getNumColumns() const noexcept { return columns; }
Array<size_t> getSize() const noexcept { return { rows, columns }; }
void clear() noexcept { zeromem (data.begin(), (size_t) data.size() * sizeof (ElementType)); }
//==============================================================================
Matrix& swapRows (size_t rowOne, size_t rowTwo) noexcept;
Matrix& swapColumns (size_t columnOne, size_t columnTwo) noexcept;
//==============================================================================
inline ElementType operator() (size_t row, size_t column) const noexcept
{
jassert (row < rows && column < columns);
return data.getReference (static_cast<int> (dataAcceleration.getReference (static_cast<int> (row))) + static_cast<int> (column));
}
inline ElementType& operator() (size_t row, size_t column) noexcept
{
jassert (row < rows && column < columns);
return data.getReference (static_cast<int> (dataAcceleration.getReference (static_cast<int> (row))) + static_cast<int> (column));
}
inline ElementType* getRawDataPointer() noexcept { return data.getRawDataPointer(); }
inline const ElementType* getRawDataPointer() const noexcept { return data.begin(); }
//==============================================================================
inline Matrix& operator+= (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a + b; } ); }
inline Matrix& operator-= (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a - b; } ); }
inline Matrix& operator*= (ElementType scalar) noexcept
{
std::for_each (begin(), end(), [scalar] (ElementType& x) { x *= scalar; });
return *this;
}
inline Matrix operator+ (const Matrix& other) const { Matrix result (*this); result += other; return result; }
inline Matrix operator- (const Matrix& other) const { Matrix result (*this); result -= other; return result; }
inline Matrix operator* (ElementType scalar) const { Matrix result (*this); result *= scalar; return result; }
Matrix operator* (const Matrix& other) const;
inline Matrix& hadarmard (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a * b; } ); }
static Matrix hadarmard (const Matrix& a, const Matrix& b) { Matrix result (a); result.hadarmard (b); return result; }
//==============================================================================
static bool compare (const Matrix& a, const Matrix& b, ElementType tolerance = 0) noexcept;
/* Comparison operator */
inline bool operator== (const Matrix& other) const noexcept { return compare (*this, other); }
//==============================================================================
bool isSquare() const noexcept { return rows == columns; }
bool isVector() const noexcept { return isOneColumnVector() || isOneRowVector(); }
bool isOneColumnVector() const noexcept { return columns == 1; }
bool isOneRowVector() const noexcept { return rows == 1; }
bool isNullMatrix() const noexcept { return rows == 0 || columns == 0; }
//==============================================================================
bool solve (Matrix& b) const noexcept;
//==============================================================================
String toString() const;
//==============================================================================
ElementType* begin() noexcept { return data.begin(); }
ElementType* end() noexcept { return data.end(); }
const ElementType* begin() const noexcept { return &data.getReference (0); }
const ElementType* end() const noexcept { return begin() + data.size(); }
private:
//==============================================================================
void resize()
{
data.resize (static_cast<int> (columns * rows));
dataAcceleration.resize (static_cast<int> (rows));
for (size_t i = 0; i < rows; ++i)
dataAcceleration.setUnchecked (static_cast<int> (i), i * columns);
}
template <typename BinaryOperation>
Matrix& apply (const Matrix& other, BinaryOperation binaryOp)
{
jassert (rows == other.rows && columns == other.columns);
auto* dst = getRawDataPointer();
for (auto src : other)
{
*dst = binaryOp (*dst, src);
++dst;
}
return *this;
}
//==============================================================================
Array<ElementType> data;
Array<size_t> dataAcceleration;
size_t rows, columns;
//==============================================================================
JUCE_LEAK_DETECTOR (Matrix)
};
} // namespace juce::dsp
juce::Array
Definition juce_Array.h:56
juce::String
Definition juce_String.h:53
juce::dsp::Matrix
Definition juce_Matrix.h:39
juce::dsp::Matrix::operator-
Matrix operator-(const Matrix &other) const
Definition juce_Matrix.h:153
juce::dsp::Matrix::swapRows
Matrix & swapRows(size_t rowOne, size_t rowTwo) noexcept
Definition juce_Matrix.cpp:94
juce::dsp::Matrix::hadarmard
Matrix & hadarmard(const Matrix &other) noexcept
Definition juce_Matrix.h:162
juce::dsp::Matrix::getNumRows
size_t getNumRows() const noexcept
Definition juce_Matrix.h:90
juce::dsp::Matrix::Matrix
Matrix(Matrix &&) noexcept=default
juce::dsp::Matrix::hadarmard
static Matrix hadarmard(const Matrix &a, const Matrix &b)
Definition juce_Matrix.h:165
juce::dsp::Matrix::compare
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Definition juce_Matrix.cpp:146
juce::dsp::Matrix::Matrix
Matrix(const Matrix &)=default
juce::dsp::Matrix::isOneColumnVector
bool isOneColumnVector() const noexcept
Definition juce_Matrix.h:182
juce::dsp::Matrix::isVector
bool isVector() const noexcept
Definition juce_Matrix.h:179
juce::dsp::Matrix::isNullMatrix
bool isNullMatrix() const noexcept
Definition juce_Matrix.h:188
juce::dsp::Matrix::getSize
Array< size_t > getSize() const noexcept
Definition juce_Matrix.h:98
juce::dsp::Matrix::swapColumns
Matrix & swapColumns(size_t columnOne, size_t columnTwo) noexcept
Definition juce_Matrix.cpp:78
juce::dsp::Matrix::Matrix
Matrix(size_t numRows, size_t numColumns)
Definition juce_Matrix.h:43
juce::dsp::Matrix::hankel
static Matrix hankel(const Matrix &vector, size_t size, size_t offset=0)
Definition juce_Matrix.cpp:59
juce::dsp::Matrix::operator*
Matrix operator*(ElementType scalar) const
Definition juce_Matrix.h:156
juce::dsp::Matrix::operator()
ElementType operator()(size_t row, size_t column) const noexcept
Definition juce_Matrix.h:112
juce::dsp::Matrix::getRawDataPointer
const ElementType * getRawDataPointer() const noexcept
Definition juce_Matrix.h:133
juce::dsp::Matrix::operator-=
Matrix & operator-=(const Matrix &other) noexcept
Definition juce_Matrix.h:140
juce::dsp::Matrix::operator+
Matrix operator+(const Matrix &other) const
Definition juce_Matrix.h:150
juce::dsp::Matrix::identity
static Matrix identity(size_t size)
Definition juce_Matrix.cpp:30
juce::dsp::Matrix::solve
bool solve(Matrix &b) const noexcept
Definition juce_Matrix.cpp:163
juce::dsp::Matrix::toeplitz
static Matrix toeplitz(const Matrix &vector, size_t size)
Definition juce_Matrix.cpp:41
juce::dsp::Matrix::Matrix
Matrix(size_t numRows, size_t numColumns, const ElementType *dataPointer)
Definition juce_Matrix.h:53
juce::dsp::Matrix::getRawDataPointer
ElementType * getRawDataPointer() noexcept
Definition juce_Matrix.h:128
juce::dsp::Matrix::getNumColumns
size_t getNumColumns() const noexcept
Definition juce_Matrix.h:93
juce::dsp::Matrix::isSquare
bool isSquare() const noexcept
Definition juce_Matrix.h:176
juce::dsp::Matrix::operator*=
Matrix & operator*=(ElementType scalar) noexcept
Definition juce_Matrix.h:143
juce::dsp::Matrix::isOneRowVector
bool isOneRowVector() const noexcept
Definition juce_Matrix.h:185
juce::dsp::Matrix::clear
void clear() noexcept
Definition juce_Matrix.h:101
juce::dsp::Matrix::toString
String toString() const
Definition juce_Matrix.cpp:276
juce::dsp::Matrix::operator+=
Matrix & operator+=(const Matrix &other) noexcept
Definition juce_Matrix.h:137