Skip to content

Commit

Permalink
Add documentation comments for dot method
Browse files Browse the repository at this point in the history
  • Loading branch information
wigging committed Jan 1, 2025
1 parent 74e1341 commit 60c2f5f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Sources/MatrixModule/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct Matrix<Scalar> {
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.
Expand Down
18 changes: 16 additions & 2 deletions Sources/VectorModule/Vector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float> = [1, 2, 3, 4, 5]
/// let b: Vector<Float> = [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.
Expand Down
44 changes: 22 additions & 22 deletions Tests/VectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float>([1, 2, 3, 4, 5])
Expand Down Expand Up @@ -108,17 +97,6 @@ struct VectorTests {
#expect(a * b == Vector([4, 10, 18, 28, 40]))
}

@Test func floatAlgebra() {
let a = Vector<Float>([1, 2, 3, 4, 5])
let b = Vector<Float>([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])
Expand Down Expand Up @@ -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<Float>([1, 2, 3, 4, 5])
let b = Vector<Float>([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])
Expand Down

0 comments on commit 60c2f5f

Please sign in to comment.