-
Notifications
You must be signed in to change notification settings - Fork 6
Matrix
A matrix must be initialised with an element type. This type has to conform to the FullMathValue protocol. This means it must support all basic functions like +, -, /, ** (power operator), Equatable, Comparable. See the Math page for more info on additional protocols.
#Init There are several ways to initialise a Matrix. The most common way is to initialise from an Array.
// 2x3 matrix of inferred type Int
let A = Matrix([[1, 2, 3], [4, 5, 6]])
// 2x3 matrix of type Double, both ways give the same result
let B = Matrix([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
-- or --
let B = Matrix<Double>([[1, 2, 3], [4, 5, 6]])Matrices can also be initialised by using an array literal, but the type of the variable must be known to be Matrix.
var A: Matrix<Int> = [[1, 2, 3], [4, 5, 6]]
// A is now known to be of type Matrix<Int>
A = [[7, 8], [9, 10]]You can construct an empty matrix of a certain type like this:
let A: Matrix<Double> = Matrix()
-- or --
let A = Matrix<Double>()###init(dimensions: Dimensions, generator: (Index) -> T) Initialises the matrix with the given dimensions using a generator function. The generator function accepts an Index and returns an appropriate value. The index's rows and columns start with 0. The type can be explicitly set or can be inferred from the generators return type. See the Dimensions page to learn more about how to initialise Dimensions.
// 2x3 matrix of inferred type Int, since the type of Index.row and Index.column are Int
let A = Matrix(dimensions: Dimensions(2, 3), generator: { $0.row * $0.column })
// 0 0 0
// 0 1 2