VariableSizeMatrix

public struct VariableSizeMatrix<MatrixType> : MatrixOperationsBase where MatrixType : Addable, MatrixType : Multiplicable, MatrixType : Numeric

A matrix whose dimensions are determined at runtime.

  • The type of the matrix’s elements.

    Declaration

    Swift

    public typealias ElementType = MatrixType
  • The dimensions of the matrix. The first component is the number of rows and the second component is the number of columns. Both components must be greater than 0.

    Declaration

    Swift

    public let dimensions: IntSize
  • The values of the matrix. The number of elements must equal the number of rows times the number of columns.

    Declaration

    Swift

    public private(set) var elements: [MatrixType]
  • Initializes the matrix with the given dimensions and all elements equal to MatrixType.zero.

    Declaration

    Swift

    public init(dimensions: IntSize)
  • Initializes the matrix with the given dimensions and elements. If elements does not contain enough elements, the remaining values are filled with MatrixType.zero. If elements contains too many elements, the extra elements are ignored.

    Declaration

    Swift

    public init(dimensions: IntSize, elements: [MatrixType])
  • Provides access to the underlying elements.

    Declaration

    Swift

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

    Parameters

    index

    The index of the element. Must be in range [0, self.numberOfElements).

    Return Value

    The value of the element at the given index.

  • Adds self and matrix component-wise.

    Throws

    MatrixError.incorrectDimensions if self.dimensions does not equal matrix.dimensions.

    Declaration

    Swift

    public func add<M>(by matrix: M) throws -> VariableSizeMatrix<MatrixType> where
        M : MatrixBase,
        ElementType == M.ElementType

    Parameters

    matrix

    The matrix to add to this matrix.

    Return Value

    The result of adding self and matrix.

  • Subtracts self and matrix component-wise.

    Throws

    MatrixError.incorrectDimensions if self.dimensions does not equal matrix.dimensions.

    Declaration

    Swift

    public func subtract<M>(by matrix: M) throws -> VariableSizeMatrix<MatrixType> where
        M: MatrixBase,
        ElementType == M.ElementType

    Parameters

    matrix

    The matrix to subtract from this matrix.

    Return Value

    The result of subracting matrix and self.

  • Performs matrix multiplication on self and matrix (not component-wise).

    Throws

    MatrixError.incorrectDimensions if self.dimensions.column does not equal matrix.dimensions.row.

    Declaration

    Swift

    public func multiply<M>(by matrix: M) throws -> VariableSizeMatrix<MatrixType> where
        M: MatrixBase,
        ElementType == M.ElementType

    Parameters

    matrix

    The matrix to multiply with this matrix.

    Return Value

    The result of multiplying self and matrix.

  • Returns the transpose of this matrix. Transposing a matrix turns rows to columns and vice versa.

    Declaration

    Swift

    public func transpose() -> VariableSizeMatrix<MatrixType>

    Return Value

    A matrix whose rows are the columns of self and vice versa.