Solve simultaneous equations and transform points in space.
Framework
 Accelerate
Overview
A matrix is a 2D array of values arranged in rows and columns. The simd library provides support for matrices of up to four rows and four columns, containing 16 elements. It uses a column major naming convention; for example, a simd
is a matrix containing four columns and two rows.
The simd library provides initializers that include options for creating matrices from either rows or columns from the appropriately sized vectors. For example, the following code uses two vectors of four elements to create a 2 x 4 matrix and a 4 x 2 matrix:
The following examples show a few common uses of matrices.
Solve Simultaneous Equations
You can use matrices to solve simultaneous equations of the form AX = B; for example, to find x and y in the following equations:
You first create a 2 x 2 matrix containing the leftside coefficients:
Then create a vector containing the rightside values:
To find the values of x and y, multiply the inverse of the matrix a
with the vector b
:
The result, x
, is a twoelement vector containing (x = 2
.
Transform Vectors with Matrix Multiplication
Matrices provide a convenient way to transform (translate, rotate, and scale) points in 2D and 3D space.
The following image shows point A translated to B, rotated to C, and scaled to D:
By representing 2D coordinates as a threeelement vector, you can transform points using matrix multiplication. Typically, the third component of the vector, z
, is set to 1, which indicates that the vector represents a position in space.
For example, the vector shown as A in the preceding illustration is defined as a simd
with the following code:
Transform matrices for 2D coordinates are represented by 3 x 3 matrices.
Translate
A translate matrix takes the following form:









The simd library provides constants for identity matrices (matrices with ones along the diagonal, and zeros elsewhere). The 3 x 3 Float
identity matrix is matrix
.
The following function returns a simd
matrix using the specified tx
and ty
translate values by setting the elements in an identity matrix:
To apply a translate to the position vector, you multiply the pair together:
The resulting translated
has the values (x: 4
, shown as B in the illustration above.
Rotate
A rotation matrix around the zaxis (that is, on the xy plane) takes the following form:









The following function returns a simd
matrix using the specified rotation angle in radians:
To apply a rotation to the previously translated vector, you multiply the pair together:
The resulting rotated
has the values (x: 0
, shown as C in the illustration above.
Scale
A scale matrix takes the following form:









The following function returns a simd
matrix using the specified x and y scale values:
To apply a scale to the previously rotated vector, you multiply the pair together:
The resulting scaled
has the values (x: 7
, shown as D in the illustration above.
The three transform matrices can be multiplied together and the product multiplied with the position vector to get the same result: