Skip to content

Commit

Permalink
Added Matrix Sub functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rmzr7 committed Feb 22, 2017
1 parent 3d1eb77 commit c4524ce
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Sources/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ public func add(_ x: Matrix<Double>, y: Matrix<Double>) -> Matrix<Double> {
return results
}

public func sub(_ x: Matrix<Float>, y: Matrix<Float>) -> Matrix<Float> {
precondition(x.rows == y.rows && x.columns == y.columns, "Matrix dimensions not compatible with addition")

var results = negate(y)
cblas_saxpy(Int32(x.grid.count), 1.0, x.grid, 1, &(results.grid), 1)

return results
}

public func sub(_ x: Matrix<Double>, y: Matrix<Double>) -> Matrix<Double> {
precondition(x.rows == y.rows && x.columns == y.columns, "Matrix dimensions not compatible with addition")

var results = negate(y)
cblas_daxpy(Int32(x.grid.count), 1.0, x.grid, 1, &(results.grid), 1)

return results
}
public func mul(_ alpha: Float, x: Matrix<Float>) -> Matrix<Float> {
var results = x
cblas_sscal(Int32(x.grid.count), alpha, &(results.grid), 1)
Expand Down Expand Up @@ -365,6 +382,15 @@ public func + (lhs: Matrix<Double>, rhs: Matrix<Double>) -> Matrix<Double> {
return add(lhs, y: rhs)
}

public func - (lhs: Matrix<Float>, rhs: Matrix<Float>) -> Matrix<Float> {
return sub(lhs, y: rhs)
}

public func - (lhs: Matrix<Double>, rhs: Matrix<Double>) -> Matrix<Double> {
return sub(lhs, y: rhs)
}


public func * (lhs: Float, rhs: Matrix<Float>) -> Matrix<Float> {
return mul(lhs, x: rhs)
}
Expand Down

0 comments on commit c4524ce

Please sign in to comment.