GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
M_MATRIX(3) FreeBSD Library Functions Manual M_MATRIX(3)

M_MatrixAgar-Math matrix-related functions

#include <agar/core.h>
#include <agar/gui.h>
#include <agar/math/m.h>

The M_Vector(3) and M_Matrix interfaces implement linear algebra operations on (real or complex valued) n dimensional vectors, and m by n matrices. Optimized interfaces are provided for fixed-dimensional types (which have entries directly accessible as x, y, z and w). Arbitrary-dimensional types may or may not use fixed arrays in memory. For example, the "sparse" backend uses a sparse matrix representation, and the "db" backend stores vector entries in a database.

Backends can be selected at run-time, or Agar-Math can be compiled to provide inline expansions of all operations of a specific backend. Vector extensions (such as SSE and AltiVec) are used by default, if a runtime cpuinfo check determines that they are available (the build remains compatible with non-vector platforms, at the cost of extra function calls). For best performance, Agar should be compiled with "--with-sse=inline", or "--with-altivec=inline".

The following routines operate on dynamically-allocated m by n matrices:

fpu
Native scalar floating point methods.
sparse
Methods optimized for large, sparse matrices. Based on the excellent Sparse 1.4 package by Kenneth Kundert.

M_Matrix *
(Uint m, Uint n);


void
(M_Matrix *M);


int
(M_Matrix *M, Uint m, Uint n);


void
(M_Matrix *M);


void
(M_Matrix *M);


int
(M_Matrix *D, const M_Matrix *A);


M_Matrix *
(const M_Matrix *M);


M_Matrix *
(AG_DataSource *ds);


void
(AG_DataSource *ds, const M_Matrix *A);

The () function allocates a new m by n matrix. M_Free() releases all resources allocated for the specified matrix. M_Resize() resizes M to m by n. Existing entries are preserved, but new entries are left uninitialized. If insufficient memory is available, -1 is returned and an error message is set. On success, the function returns 0.

() initializes M to the identity matrix. M_SetZero() initializes M to all zeros.

() copies the contents of matrix A into D, which is assumed to have the same dimensions (otherwise, -1 is returned). M_Dup() returns a duplicate of M.

The () and M_WriteMatrix() functions are used to (de)serialize the contents of matrix A from/to the specified AG_DataSource(3).

M_Real
M_Get(M_Matrix *M, Uint i, Uint j);


void
(M_Matrix *M, Uint i, Uint j, M_Real val);


M_Real *
(M_Matrix *M, Uint i, Uint j);


void
(float *values, const M_Matrix *A);


void
(double *values, const M_Matrix *A);


void
(M_Matrix *A, const float *values);


void
(M_Matrix *A, const double *values);


void
(const M_Matrix *A);

The () and M_Set() routines respectively retrieve and set the element i, j.

() returns a pointer to the element i, j. As long as the entry exists, it is safe to read and write the element.

The () and M_ToDoubles() functions return a representation of matrix A as an array of float or double values in row-major order. The M_FromFloats() and M_FromDoubles() functions initialize matrix A from an array of float or double values in row-major order. In both cases, it is assumed that the arrays are of the correct size for the given matrix dimensions.

() dumps the individual matrix entries to the standard error output. It is only for debugging purposes. Agar GUI applications can use the provided M_Matview(3) widget to display matrix contents.

M_Matrix *
M_Transpose(M_Matrix *M);


M_Matrix *
(const M_Matrix *A, const M_Matrix *B);


int
(M_Matrix *A, const M_Matrix *B);


void
(M_Matrix *A, M_Real value);


M_Matrix *
(const M_Matrix *A, const M_Matrix *B);


M_Matrix *
(const M_Matrix *A, const M_Matrix *B);


int
(const M_Matrix *A, const M_Matrix *B, M_Matrix *AB);


M_Matrix *
(const M_Matrix *A, const M_Matrix *B);


int
(const M_Matrix *A, const M_Matrix *B, M_Matrix *AB);


void
(const M_Matrix *A, const M_Matrix *B, M_Real *diff);


int
(M_Real *trace, const M_Matrix *A);


void
(M_Matrix *A);


M_Matrix *
(const M_Matrix *A, M_Matrix *b);


int
(M_Matrix *A, M_Matrix *b);


int
(M_Matrix *A);


void
(M_Matrix *LU, M_Vector *b);


void
(M_Matrix *A);

The () function returns the transpose of M (i.e., all i, j elements are swapped against j, i elements).

() returns the sum of the matrices A and B. The M_Addv() variant returns the sum into an existing matrix, returning -1 if the dimensions are incorrect.

The () routine adds value to each diagonal entry i, i of matrix A.

() returns the direct sum of A and B.

() returns the product of matrices A and B. The M_Mulv() variant returns the product into an existing matrix, returning -1 if the dimensions are incorrect. M_EntMul() and M_EntMulv() perform entrywise multiplication as opposed to matrix multiplication.

The () function compares each entry of A and B, returning the largest difference into diff.

() returns the trace (the sum of elements on the diagonal) of a square matrix A into trace.

The () function returns 1 if A is a square (n-by-n) matrix.

The () function solves for x in Ax = b. The solver replaces the contents of A by its inverse, and returns the solution vector into b.

The () routine computes the LU factorization of a square matrix A. If successful, the original contents of A are destroyed and replaced by the LU factorization. On error, -1 is returned. Partial pivoting information is recorded in the M_Matrix structure for subsequent backsubstitution.

The () routine solves a system of linear equations represented by a LU factorization LU (previously computed by M_FactorizeLU()) and a right-hand side b. The solution vector is returned into b.

The () routine attempts to remove zeros from the diagonal, by taking into account the structure of modified node admittance matrices (found in applications such as electronic simulators).

The following routines are optimized for 4x4 matrices, as frequently encountered in computer graphics. Entries are directly accessible as structure members. Available backends include:

fpu
Native scalar floating point methods.
sse
Accelerate operations using Streaming SIMD Extensions (SSE).

M_Matrix44
(void);


void
(M_Matrix44 *Z);


M_Matrix44
(void);


void
(M_Matrix44 *I);


void
(M_Matrix44 *Mdst, const M_Matrix44 *Msrc);

The () and M_MatZero44v() functions initializes the target matrix Z to the zero matrix.

() and M_MatIdentity44v() initializes the target matrix I to the identity matrix.

The () routine copies the contents of matrix Msrc into Mdst. The original contents of Mdst are overwritten.

The elements of M_Matrix44 are directly accessible via the m[4][4] member of the structure. Elements of the matrix are stored in row-major format. The structure is defined as:

#ifdef HAVE_SSE
typedef union m_matrix44 {
	struct { __m128 m1, m2, m3, m4; };
	float m[4][4];
} M_Matrix44;
#else
typedef struct m_matrix44 {
	M_Real m[4][4];
} M_Matrix44;
#endif

Notice that SIMD extensions force single-precision floats, regardless of the precision for which Agar-Math was built (if a 4x4 matrix of higher precision is required, the general M_Matrix type may be used).

The following functions convert between M_Matrix44 and numerical arrays:


void
(float *flts, const M_Matrix44 *A);


void
(double *dbls, const M_Matrix44 *A);


void
(M_Matrix44 *M, const float *flts);


void
(M_Matrix44 *M, const double *dbls);

() converts matrix A to a 4x4 array of floats flts. M_MatToDoubles44() converts matrix A to a 4x4 array of doubles dbls. M_MatFromFloats44() initializes matrix M from the contents of a 4x4 array of floats flts. M_MatFromDoubles44() initializes matrix M from the contents of a 4x4 array of doubles dbls.

M_Matrix44
M_MatTranspose44(M_Matrix44 A);


M_Matrix44
(const M_Matrix44 *A);


void
(M_Matrix44 *A);


M_Matrix44
(M_Matrix44 A);


int
(M_Matrix44 A, M_Matrix44 *Ainv);


M_Matrix44
(M_Matrix44 A, M_Matrix44 B);


void
(M_Matrix44 *A, const M_Matrix44 *B);


void
(M_Matrix44 *AB, const M_Matrix44 *A, const M_Matrix44 *B);


M_Vector4
(M_Matrix44 A, M_Vector4 x);


M_Vector4
(const M_Matrix44 *A, const M_Vector4 *x);


void
(M_Vector4 *x, const M_Matrix44 *A);


void
(M_Matrix44 *T, M_Real theta, M_Vector3 axis);


void
(M_Matrix44 *T, M_Vector3 center, M_Vector3 axis, M_Real theta);


void
(M_Matrix44 *T, M_Real pitch, M_Real roll, M_Real yaw);


void
(M_Matrix44 *T, M_Real theta);


void
(M_Matrix44 *T, M_Real theta);


void
(M_Matrix44 *T, M_Real theta);


void
(M_Matrix44 *T, M_Vector3 v);


void
(M_Matrix44 *T, M_Real x, M_Real y, M_Real z);


void
(M_Matrix44 *T, M_Real c);


void
(M_Matrix44 *T, M_Real c);


void
(M_Matrix44 *T, M_Real c);


void
(M_Matrix44 *T, M_Real x, M_Real y, M_Real z, M_Real w);


void
(M_Matrix44 *T, M_Real c);

The (), M_MatTranspose44p() and M_MatTranspose44v() function compute and return the transpose of matrix A (i.e., all elements i,j are swapped for elements j,i).

The function () computes the inverse of A using Cramer's rule and cofactors. If the matrix is not invertible, the return value is undefined.

The () function computes the inverse of A by systematic Gaussian elimination. If the matrix is not invertible (singular up to M_MACHEP precision), the function fails.

(), M_MatMult44v() and M_MatMult44pv() compute the product of matrices A and B.

The (), M_MatMultVector44p() and M_MatMultVector44v() functions perform matrix-vector multiplication Ax, and returns x.

() multiplies matrix T against a rotation matrix describing a rotation of theta radians about axis (relative to the origin). The M_MatOrbitAxis44() variant takes axis to be relative to the specified center point as opposed to the origin.

() multiplies T against a matrix describing a rotation about the origin in terms of Euler angles pitch, roll and yaw (given in radians).

(), M_MatRotate44J() and M_MatRotate44K() multiply T with a matrix describing a rotation of theta radians about the basis vector i, j or k, respectively.

() multiplies T against a matrix describing a translation by vector v. M_MatTranslate44(), M_MatTranslate44X(), M_MatTranslate44Y() and M_MatTranslate44Z() accept individual coordinate arguments.

() multiplies T against a matrix describing uniform/non-uniform scaling by [x,y,z,w]. M_MatUniScale44() performs uniform scaling by c.

AG_Intro(3), M_Complex(3), M_Quaternion(3), M_Real(3), M_Vector(3)

Sparse 1.4
 

The M_Matrix interface first appeared in Agar 1.3.3.

December 21, 2022 Agar 1.7

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.