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.

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

    Declaration

    Swift

    public init(rows: Int, columns: Int, repeating repeatedValue: T)
  • 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])
  • 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]])
  • The number of rows in the matrix.

    Declaration

    Swift

    public let rows: Int
  • The number of columns in the matrix.

    Declaration

    Swift

    public let columns: Int
  • 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]
  • Returns the transpose of the matrix.

    Declaration

    Swift

    public var transpose: Matrix<T>
  • 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
  • Always zero, which is the index of the first element when non-empty.

    Declaration

    Swift

    public var startIndex : MatrixIndex
  • Always rows*columns, which is the successor of the last valid subscript argument.

    Declaration

    Swift

    public var endIndex : MatrixIndex
  • 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.

  • Constructs a matrix using an array literal.

    Declaration

    Swift

    public init(arrayLiteral elements: Array<T>...)
  • A string containing a suitable textual representation of the matrix.

    Declaration

    Swift

    public var description: String