Matrix

public class Matrix
extension Matrix: Sequence
extension Matrix: CustomStringConvertible
extension Matrix: Equatable
extension Matrix: Comparable

Matrix of Double values

  • Number of rows in Matrix.

    Declaration

    Swift

    public var rows: Int { get }
  • Number of columns in Matrix.

    Declaration

    Swift

    public var cols: Int { get }
  • T

    Transpose

    Declaration

    Swift

    public var T: Matrix { get }
  • Create new Matrix by copying existing.

    Declaration

    Swift

    public init(_ M: Matrix)
  • Create 1-column Matrix (transposed Vector)

    Declaration

    Swift

    public init(_ v: Vector)
  • Create Matrix from array of Vectors (two-dimensional array)

    Declaration

    Swift

    public init(_ data: [Vector])

Gathering

  • Get M(row, column) element of Matrix.

    Declaration

    Swift

    public subscript(row: Int, col: Int) -> Double { get set }

    Parameters

    row

    row position of element (0-based)

    col

    col position of element (0-based)

  • Get M(index) element of row-major represented Matrix.

    Declaration

    Swift

    public subscript(index: Int) -> Double { get set }

    Parameters

    index

    index of element (0-based, 0 <= index < M.rows * M.cols)

  • Get M(row) row of Matrix.

    Declaration

    Swift

    public subscript(row row: Int) -> Vector { get set }

    Parameters

    row

    row index (0-based)

  • Get M(col) column of Matrix.

    Declaration

    Swift

    public subscript(col col: Int) -> Vector { get set }

    Parameters

    col

    column index (0-based)

  • Get and set M(row, col) submatrix of Matrix.

    The range-based subscript methods for getting and setting submatricies.

    var M = Matrix([[1,  2,  3,  4],
                    [5,  6,  7,  8],
                    [9, 10, 11, 12])
    
    var K = Matrix([[1, 0],
                    [0, 1])
    
    • Using bounded ranges, including partial (e.g. ..<3):

      M[1..<3, 0..1] = K
      // M is now:
      // [[1,  2,  3,  4],
      //  [1,  0,  7,  8],
      //  [0,  1, 11, 12]]
      
      K = M[0...1, 2...]
      // K is now:
      // [[3, 4]
      //  [7, 8]]
      
    • Using unbounded ranges:

      K = M[..., 1..2]
      // K is now:
      // [[ 2,  3],
      //  [ 6,  7],
      //  [10, 11]]
      

    Declaration

    Swift

    public subscript<A, B>(row: A, col: B) -> Matrix where A : RangeExpression, B : RangeExpression, A.Bound == Int, B.Bound == Int { get set }

    Parameters

    row

    Range for rows (0-based)

    col

    Range for cols (0-based)

    Return Value

    submatrix of size row.count by col.count

  • Get and set submatrix with a top-left corner at (row, col).

    The method allows to get and set a submatrix using just the coordinates of its top-left corner.

    var M = Matrix([[1,  2,  3,  4],
                    [5,  6,  7,  8],
                    [9, 10, 11, 12])
    
    var K = Matrix([[1, 0],
                    [0, 1])
    
    M[1, 2] = K
    // M is now:
    // [[1,  2,  3,  4],
    //  [5,  6,  1,  0],
    //  [9, 10,  0,  1]]
    

    Warning

    Getter and setter are using submatrices of different dimensions.

    Declaration

    Swift

    public subscript(row: Int, col: Int) -> Matrix { get set }

    Parameters

    row

    row index (0-based)

    col

    column index (0-based)

    Return Value

    Submatrix equivalent to M[row..., col...]

  • Construct new matrix from source using specified extractor.

    Alternatively, m[e] can be executed with m ?? e or slice(m, e)

    Declaration

    Swift

    public subscript(e: (er: Extractor, ec: Extractor)) -> Matrix { get }

    Return Value

    extracted matrix

Sequence

  • Iterate through matrix by rows

    Declaration

    Swift

    public func makeIterator() -> MatrixIterator

Printable

  • Declaration

    Swift

    public var description: String { get }