URL: https://graphicscomputing.fr/cgp/reference/matN/
Description: Operations shared by all matrices (mat2, mat3, mat4), written once for any size.

# matN

C++ type: `matrix_stack<T,N1,N2>`.
Shared by: mat2, mat3, mat4.
Operations shared by all matrices (mat2, mat3, mat4), written once for any size.

Fields:

- `numarray_stack<numarray_stack<T,N2>,N1> data` - Coefficients stored row by row as N1 rows of N2 values (a numarray_stack of numarray_stack), contiguous in memory.

Functions taking it:

- `std::string type_str(matrix_stack<T,N1,N2> const&)` - Type name as a string, e.g. "matrix_stack<float,3,4>".
- `std::string str(matrix_stack<T,N1,N2> const& v, std::string const& separator = " ", std::string const& begin = "", std::string const& end = "", std::string const& begin_line = "", std::string const& end_line = " ")` - Text representation of the matrix row by row, each row wrapped by begin_line and end_line with its values joined by separator, the whole surrounded by begin and end.
- `std::string str_pretty(matrix_stack<T,N1,N2> const& M, std::string const& separator = " ", std::string const& begin = "", std::string const& end = "", std::string const& begin_line = "(", std::string const& end_line = ")\n")` - Multi-line text of the matrix, one parenthesized row per line, values in fixed notation with 2 decimals and width 7.
- `T const* ptr(matrix_stack<T,N1,N2> const& M)` - Pointer to the first coefficient; coefficients are stored contiguously in row-major order.
- `int size_in_memory(matrix_stack<T,N1,N2> const& M)` - Size in bytes of the N1 x N2 coefficients.
- `T const& get(matrix_stack<T,N1,N2> const& data)` - Coefficient (idx1,idx2) with indices checked at compile time (static_assert).
- `numarray_stack<T,N2> const& get(matrix_stack<T,N1,N2> const& data)` - Row idx1 as a vector, with the index checked at compile time (static_assert).
- `T const& get_offset(matrix_stack<T,N1,N2> const& data)` - Coefficient at a row-major offset, with the offset checked at compile time (static_assert).
- `bool is_equal(matrix_stack<Ta,Na1,Na2> const& a, matrix_stack<Tb,Nb1,Nb2> const& b)` - Equality of two matrices: same dimensions and coefficient-wise equal values (approximate for floating-point).
- `matrix_stack<T,N1,N2> multiply_componentwise(matrix_stack<T,N1,N2> const& a, matrix_stack<T,N1,N2> const& b)` - Coefficient-wise (Hadamard) product of two matrices of the same size.
- `matrix_stack<T,N2,N1> transpose(matrix_stack<T,N1,N2> const& m)` - Transposed matrix.
- `T trace(matrix_stack<T,N,N> const& m)` - Trace (sum of the diagonal entries).
- `T norm(matrix_stack<T,N1,N2> const& m)` - Frobenius norm of the matrix.
- `std::istream& read_from_stream(std::istream& stream, matrix_stack<T,N1,N2>& data)` - Reads the N1 x N2 coefficients from the stream in row-major order, stopping early if the stream fails.
- `void check_index_bounds(int index1, int index2, matrix_stack<T,N1,N2> const& data)` - Raises an error if (row,column) is outside [0,N1-1]x[0,N2-1]; no-op when CGP_NO_DEBUG is defined.
- `void check_index_bounds(int index1, matrix_stack<T,N1,N2> const& data)` - Raises an error if the row index is outside [0,N1-1]; no-op when CGP_NO_DEBUG is defined.
- `void check_offset_bounds(int offset, matrix_stack<T,N1,N2> const& data)` - Raises an error if the offset is outside [0,N1*N2-1]; no-op when CGP_NO_DEBUG is defined.
- `std::string str_2D(matrix_stack<T,N1,N2> const& v, std::string const& separator, std::string const& begin, std::string const& end, std::string const& begin_line, std::string const& end_line)` - Same as str() for a matrix: text of the coefficients row by row with configurable separators.

Operators:

- `std::ostream& operator<<(std::ostream& s, matrix_stack<T,N1,N2> const& v)` - Writes all coefficients of the matrix to the stream, row by row separated by spaces.
- `matrix_stack<T,N1,N2>& operator+=(matrix_stack<T,N1,N2>& a, matrix_stack<T,N1,N2> const& b)` - Adds a matrix coefficient-wise in place.
- `matrix_stack<T,N1,N2> operator+(matrix_stack<T,N1,N2> const& a, matrix_stack<T,N1,N2> const& b)` - Sum of two matrices.
- `matrix_stack<T,N1,N2>& operator-=(matrix_stack<T,N1,N2>& a, matrix_stack<T,N1,N2> const& b)` - Subtracts a matrix coefficient-wise in place.
- `matrix_stack<T,N1,N2> operator-(matrix_stack<T,N1,N2> const& a, matrix_stack<T,N1,N2> const& b)` - Difference of two matrices.
- `matrix_stack<T,N,N>& operator*=(matrix_stack<T,N,N>& a, matrix_stack<T,N,N> const& b)` - Replaces a square matrix a by the matrix product a*b.
- `matrix_stack<T,N1,N2>& operator*=(matrix_stack<T,N1,N2>& a, S b)` - Multiplies each coefficient by a scalar in place.
- `matrix_stack<T,N1,N3> operator*(matrix_stack<T,N1,N2> const& a, matrix_stack<T,N2,N3> const& b)` - Matrix product of an N1xN2 and an N2xN3 matrix.
- `matrix_stack<T,N1,N2> operator*(matrix_stack<T,N1,N2> const& a, S b)` - Matrix scaled by a scalar.
- `matrix_stack<T,N1,N2> operator*(S a, matrix_stack<T,N1,N2> const& b)` - Matrix scaled by a scalar.
- `matrix_stack<T,N1,N2>& operator/=(matrix_stack<T,N1,N2>& a, S b)` - Divides each coefficient by a scalar in place.
- `matrix_stack<T,N1,N2> operator/(matrix_stack<T,N1,N2> const& a, S b)` - Matrix with each coefficient divided by a scalar.
- `matrix_stack<T,N1,N2> operator-(matrix_stack<T,N1,N2> const& m)` - Negated matrix (each coefficient multiplied by -1).
- `numarray_stack<T,N1> operator*(matrix_stack<T,N1,N2> const& a, numarray_stack<T,N2> const& b)` - Matrix-vector product, giving a vector of size N1.
