diff --git a/Sources/MatrixModule/Matrix.swift b/Sources/MatrixModule/Matrix.swift index d925fbf..264b789 100644 --- a/Sources/MatrixModule/Matrix.swift +++ b/Sources/MatrixModule/Matrix.swift @@ -24,7 +24,7 @@ public struct Matrix { self.columns = content[0].count self.data = DataBuffer(array: content.flatMap { $0 }) } - + /// Create an empty matrix with size `m×n` where `m` is number of rows and `n` is number of columns. /// - Parameters: /// - rows: Number of rows in the matrix. diff --git a/Sources/VectorModule/Vector.swift b/Sources/VectorModule/Vector.swift index 32f8854..14f74ad 100644 --- a/Sources/VectorModule/Vector.swift +++ b/Sources/VectorModule/Vector.swift @@ -154,8 +154,22 @@ extension Vector where Scalar: VectorArithmetic { extension Vector where Scalar: VectorAlgebra { - public func dot(_ vector: Vector) -> Scalar { - Scalar.dot(self, vector) + /// Calculate the dot product of two vectors. + /// + /// This calculates the dot product as `c = aᵀb` where `a` and `b` are vectors + /// that must be the same length. + /// ```swift + /// let a: Vector = [1, 2, 3, 4, 5] + /// let b: Vector = [9, 2, 3, 4, 5] + /// let c = a.dot(b) + /// // c is 63.0 + /// ``` + /// + /// - Parameter b: The second vector. + /// - Returns: The dot product of two vectors + public func dot(_ b: Vector) -> Scalar { + precondition(self.size == b.size, "Vectors must be same size") + return Scalar.dot(self, b) } /// The Euclidean norm of the vector. Also known as the L² norm, 2-norm, vector magnitude, or Euclidean length. diff --git a/Tests/VectorTests.swift b/Tests/VectorTests.swift index 24bc281..909d9c7 100644 --- a/Tests/VectorTests.swift +++ b/Tests/VectorTests.swift @@ -68,17 +68,6 @@ struct VectorTests { #expect(a * b == Vector([4, 10, 18, 28, 40])) } - @Test func integerAlgebra() { - let a = Vector([1, 2, 3, 4, 5]) - let b = Vector([4, 5, 6, 7, 8]) - - #expect(a.dot(b) == 100) - #expect(a.norm() == 7) - #expect(a.sum() == 15) - #expect(a.absoluteSum() == 15) - #expect(a.cumulativeSum() == [1, 3, 6, 10, 15]) - } - @Test func floatArithmetic() { let k: Float = 5 let a = Vector([1, 2, 3, 4, 5]) @@ -108,17 +97,6 @@ struct VectorTests { #expect(a * b == Vector([4, 10, 18, 28, 40])) } - @Test func floatAlgebra() { - let a = Vector([1, 2, 3, 4, 5]) - let b = Vector([4, 5, 6, 7, 8]) - - #expect(a.dot(b) == 100) - #expect(a.norm() == 7.4161984871) - #expect(a.sum() == 15) - #expect(a.absoluteSum() == 15) - #expect(a.cumulativeSum() == [1, 3, 6, 10, 15]) - } - @Test func doubleArithmetic() { let k = 5.0 let a = Vector([1, 2, 3, 4, 5.0]) @@ -148,6 +126,28 @@ struct VectorTests { #expect(a * b == Vector([4, 10, 18, 28, 40])) } + @Test func integerAlgebra() { + let a = Vector([1, 2, 3, 4, 5]) + let b = Vector([4, 5, 6, 7, 8]) + + #expect(a.dot(b) == 100) + #expect(a.norm() == 7) + #expect(a.sum() == 15) + #expect(a.absoluteSum() == 15) + #expect(a.cumulativeSum() == [1, 3, 6, 10, 15]) + } + + @Test func floatAlgebra() { + let a = Vector([1, 2, 3, 4, 5]) + let b = Vector([4, 5, 6, 7, 8]) + + #expect(a.dot(b) == 100) + #expect(a.norm() == 7.4161984871) + #expect(a.sum() == 15) + #expect(a.absoluteSum() == 15) + #expect(a.cumulativeSum() == [1, 3, 6, 10, 15]) + } + @Test func doubleAlgebra() { let a = Vector([1, 2, 3, 4, 5.0]) let b = Vector([4, 5, 6, 7, 8.0])