# Matrix

``public struct Matrix<T>``

A Matrix is a fixed size generic 2D collection. You can set and get elements using subscript notation. Example: `matrix[row, column] = value`

This collection also provides linear algebra functions and operators such as `inverse()`, `+` and `*` using Apple’s Accelerate framework where vailable. Please note that these operations are designed to work exclusively with `Double` matrices. Check the `Functions` section for more information.

Conforms to `MutableCollection`, `ExpressibleByArrayLiteral` and `CustomStringConvertible`.

• ``` init(rows:columns:repeating:) ```

Constructs a new matrix with all positions set to the specified value.

#### Declaration

Swift

``public init(rows: Int, columns: Int, repeating repeatedValue: T)``
• ``` init(rows:columns:grid:) ```

Constructs a new matrix using a 1D array in row-major order.

`Matrix[i,j] == grid[i*columns + j]`

#### Declaration

Swift

``public init(rows: Int, columns: Int, grid: [T])``
• ``` init(_:) ```

Constructs a new matrix using a 2D array. All columns must be the same size, otherwise an error is triggered.

#### Declaration

Swift

``public init(_ rowsArray: [[T]])``
• ``` rows ```

The number of rows in the matrix.

#### Declaration

Swift

``public let rows: Int``
• ``` columns ```

The number of columns in the matrix.

#### Declaration

Swift

``public let columns: Int``
• ``` grid ```

The one-dimensional array backing the matrix in row-major order.

`Matrix[i,j] == grid[i*columns + j]`

#### Declaration

Swift

``public internal(set) var grid: [T]``
• ``` transpose ```

Returns the transpose of the matrix.

#### Declaration

Swift

``public var transpose: Matrix<T>``
• ``` subscript(_:) ```

Provides random access to elements using the matrix back-end array coordinate in row-major order. Matrix[row, column] is preferred.

#### Declaration

Swift

``public subscript(position: MatrixIndex) -> T``
• ``` startIndex ```

Always zero, which is the index of the first element when non-empty.

#### Declaration

Swift

``public var startIndex : MatrixIndex``
• ``` endIndex ```

Always `rows*columns`, which is the successor of the last valid subscript argument.

#### Declaration

Swift

``public var endIndex : MatrixIndex``
• ``` index(after:) ```

Returns the position immediately after the given index.

Parameter

Parameter i: A valid index of the collection. `i` must be less than `endIndex`.

Returns

The index value immediately after `i`.

#### Declaration

Swift

``public func index(after i: Int) -> Int``

#### Parameters

 ``` i ``` A valid index of the collection. `i` must be less than `endIndex`.

#### Return Value

The index value immediately after `i`.

• ``` init(arrayLiteral:) ```

Constructs a matrix using an array literal.

#### Declaration

Swift

``public init(arrayLiteral elements: Array<T>...)``
• ``` description ```

A string containing a suitable textual representation of the matrix.

#### Declaration

Swift

``public var description: String``