Matrix & Vectors

  • Simple Matrix type


    Not available on Linux

    Matrix uses the Accelerate.framework for most of its operations, so it should be pretty fast – but no doubt there’s lots of room for improvement. Since the Accelerate framework works a lot with Double, I had to find a compromise between performance and compatibility with other Euler object, such as BigNumber. I made some convenience initializer, but make sure your code converts these BigNumber to Double using BigNumber.asDouble

    For example, here’s how you can use Matrix as part of the k-nearest neighbors algorithm:

    // load your data set into matrix X, where each row represents one training
    // example, and each column a feature
    let X = Matrix(rows: 10000, columns: 200)
    // load your test example into the row vector x
    let x = Matrix(rows: 1, columns: 200)
    // Calculate the distance between the test example and every training example
    // and store this in a new column vector
    let distances = (x.tile(X.rows) - X).pow(2).sumRows().sqrt()
    See more



    public struct Matrix
    extension Matrix: ExpressibleByArrayLiteral
    extension Matrix: CustomStringConvertible
    extension Matrix: Sequence
  • Vector object

    In the Euclidean geometry, a vector is what is needed to “carry” the point A to the point B; it is a geometric object that has magnitude (or length) and direction. Vectors can be added to other vectors according to vector algebra. A Euclidean vector is frequently represented by a line segment with a definite direction, or graphically as an arrow, connecting an initial point A with a terminal point B, and denoted by \overrightarrow{AB}.

    See more



    public struct Vector : Equatable