32 template <
typename ElementType>
35 Matrix result (size, size);
37 for (
size_t i = 0; i < size; ++i)
43 template <
typename ElementType>
47 jassert (size <= vector.rows);
49 Matrix result (size, size);
51 for (
size_t i = 0; i < size; ++i)
52 result (i, i) = vector (0, 0);
54 for (
size_t i = 1; i < size; ++i)
55 for (
size_t j = i; j < size; ++j)
56 result (j, j - i) = result (j - i, j) = vector (i, 0);
61 template <
typename ElementType>
65 jassert(vector.rows >= (2 * (size - 1) + 1));
67 Matrix result (size, size);
69 for (
size_t i = 0; i < size; ++i)
70 result (i, i) = vector ((2 * i) + offset, 0);
72 for (
size_t i = 1; i < size; ++i)
73 for (
size_t j = i; j < size; ++j)
74 result (j, j - i) = result (j - i, j) = vector (i + 2 * (j - i) + offset, 0);
80 template <
typename ElementType>
83 jassert (columnOne < columns && columnTwo < columns);
85 auto* p = data.getRawDataPointer();
87 for (
size_t i = 0; i < rows; ++i)
89 auto offset = dataAcceleration.getUnchecked (static_cast<int> (i));
90 std::swap (p[offset + columnOne], p[offset + columnTwo]);
96 template <
typename ElementType>
99 jassert (rowOne < rows && rowTwo < rows);
101 auto offset1 = rowOne * columns;
102 auto offset2 = rowTwo * columns;
104 auto* p = data.getRawDataPointer();
106 for (
size_t i = 0; i < columns; ++i)
107 std::swap (p[offset1 + i], p[offset2 + i]);
113 template <
typename ElementType>
116 auto n = getNumRows(), m = other.
getNumColumns(), p = getNumColumns();
121 size_t offsetMat = 0, offsetlhs = 0;
124 auto* a = getRawDataPointer();
127 for (
size_t i = 0; i < n; ++i)
129 size_t offsetrhs = 0;
131 for (
size_t k = 0; k < p; ++k)
133 auto ak = a[offsetlhs++];
135 for (
size_t j = 0; j < m; ++j)
136 dst[offsetMat + j] += ak * b[offsetrhs + j];
148 template <
typename ElementType>
151 if (a.rows != b.rows || a.columns != b.columns)
154 tolerance = std::abs (tolerance);
156 auto* bPtr = b.begin();
157 for (
auto aValue : a)
158 if (std::abs (aValue - *bPtr++) > tolerance)
165 template <
typename ElementType>
169 jassert (n == n && n == b.rows && b.isOneColumnVector());
171 auto* x = b.getRawDataPointer();
172 const auto& A = *
this;
178 auto denominator = A (0,0);
180 if (denominator == 0)
183 b (0, 0) /= denominator;
189 auto denominator = A (0, 0) * A (1, 1) - A (0, 1) * A (1, 0);
191 if (denominator == 0)
194 auto factor = (1 / denominator);
195 auto b0 = x[0], b1 = x[1];
197 x[0] = factor * (A (1, 1) * b0 - A (0, 1) * b1);
198 x[1] = factor * (A (0, 0) * b1 - A (1, 0) * b0);
204 auto denominator = A (0, 0) * (A (1, 1) * A (2, 2) - A (1, 2) * A (2, 1))
205 + A (0, 1) * (A (1, 2) * A (2, 0) - A (1, 0) * A (2, 2))
206 + A (0, 2) * (A (1, 0) * A (2, 1) - A (1, 1) * A (2, 0));
208 if (denominator == 0)
211 auto factor = 1 / denominator;
212 auto b0 = x[0], b1 = x[1], b2 = x[2];
214 x[0] = ( ( A (0, 1) * A (1, 2) - A (0, 2) * A (1, 1)) * b2
215 + (-A (0, 1) * A (2, 2) + A (0, 2) * A (2, 1)) * b1
216 + ( A (1, 1) * A (2, 2) - A (1, 2) * A (2, 1)) * b0) * factor;
218 x[1] = -( ( A (0, 0) * A (1, 2) - A (0, 2) * A (1, 0)) * b2
219 + (-A (0, 0) * A (2, 2) + A (0, 2) * A (2, 0)) * b1
220 + ( A (1, 0) * A (2, 2) - A (1, 2) * A (2, 0)) * b0) * factor;
222 x[2] = ( ( A (0, 0) * A (1, 1) - A (0, 1) * A (1, 0)) * b2
223 + (-A (0, 0) * A (2, 1) + A (0, 1) * A (2, 0)) * b1
224 + ( A (1, 0) * A (2, 1) - A (1, 1) * A (2, 0)) * b0) * factor;
233 for (
size_t j = 0; j < n; ++j)
238 while (i < n && M (i, j) == 0)
244 for (
size_t k = 0; k < n; ++k)
245 M (j, k) += M (i, k);
250 auto t = 1 / M (j, j);
252 for (
size_t k = 0; k < n; ++k)
257 for (
size_t k = j + 1; k < n; ++k)
261 for (
size_t l = 0; l < n; ++l)
262 M (k, l) += u * M (j, l);
268 for (
int k = static_cast<int> (n) - 2; k >= 0; --k)
269 for (
size_t i = static_cast<size_t> (k) + 1; i < n; ++i)
270 x[k] -= M (static_cast<size_t> (k), i) * x[i];
278 template <
typename ElementType>
284 auto* p = data.
begin();
286 for (
size_t i = 0; i < rows; ++i)
288 for (
size_t j = 0; j < columns; ++j)
291 sizeMax = jmax (sizeMax, entry.
length());
297 sizeMax = ((sizeMax + 1) / 4 + 1) * 4;
301 auto n =
static_cast<size_t> (entries.
size());
303 for (
size_t i = 0; i < n; ++i)
305 result << entries[(int) i].paddedRight (
' ', sizeMax);
307 if (i % columns == (columns - 1))
Matrix & swapRows(size_t rowOne, size_t rowTwo) noexcept
Swaps the contents of two rows in the matrix and returns a reference to itself.
String * begin() noexcept
Returns a pointer to the first String in the array.
Matrix operator*(ElementType scalar) const
Scalar multiplication.
size_t getNumColumns() const noexcept
Returns the number of columns in the matrix.
bool isOneColumnVector() const noexcept
Tells if the matrix is a one column vector.
static Matrix identity(size_t size)
Creates the identity 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.
A special array for holding a list of strings.
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.
size_t getNumRows() const noexcept
Returns the number of rows in the matrix.
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.
int size() const noexcept
Returns the number of strings in the array.
Writes data to an internal memory buffer, which grows as required.
int length() const noexcept
Returns the number of characters in the string.
static Matrix toeplitz(const Matrix &vector, size_t size)
Creates a Toeplitz Matrix from a vector with a given squared size.
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.
String toString() const
Attempts to detect the encoding of the data and convert it to a string.
void add(String stringToAdd)
Appends a string at the end of the array.