Matrix & Vectors
-
Simple Matrix type
Warning
Not available on LinuxMatrix 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 otherEulerobject, such asBigNumber. I made some convenience initializer, but make sure your code converts theseBigNumbertoDoubleusingBigNumber.asDoubleFor example, here’s how you can use Matrix as part of the k-nearest neighbors algorithm:
See more// 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()Declaration
Swift
public struct Matrixextension Matrix: ExpressibleByArrayLiteralextension Matrix: CustomStringConvertibleextension 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 moreDeclaration
Swift
public struct Vector : Equatable
View on GitHub
Matrix & Vectors Reference