Skip to content

Commit

Permalink
Add inverse trigonometric functions (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderTar authored Jan 9, 2023
1 parent c43a567 commit d8d6a11
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Sources/MatrixTrigonometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,32 @@ public func cos(_ A: Matrix) -> Matrix {
public func tan(_ A: Matrix) -> Matrix {
return matrixFunction(tan, A)
}

/// Return the arcsine of `A`, where return value is
/// in the range -pi/2 to pi/2.
///
/// - Parameters
/// - A: matrix
/// - Returns: arcsine of a matrix values
public func asin(_ A: Matrix) -> Matrix {
return matrixFunction(asin, A)
}

/// Return the arccosine of `A`, where return value is
/// in the range 0 to pi.
///
/// - Parameters
/// - A: matrix
/// - Returns: arccosine of a matrix values
public func acos(_ A: Matrix) -> Matrix {
return matrixFunction(acos, A)
}

/// Return the arctangent of `a`.
///
/// - Parameters
/// - A: matrix
/// - Returns: arctangent of a matrix values
public func atan(_ A: Matrix) -> Matrix {
return matrixFunction(atan, A)
}
29 changes: 29 additions & 0 deletions Sources/VectorTrigonometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,32 @@ public func cos(_ a: Vector) -> Vector {
public func tan(_ a: Vector) -> Vector {
return vectorFunction(vvtan, a)
}

/// Return the arcsine of `a`, where return value is
/// in the range -pi/2 to pi/2.
///
/// - Parameters
/// - a: vector
/// - Returns: arcsine of a vector values
public func asin(_ a: Vector) -> Vector {
return vectorFunction(vvasin, a)
}

/// Return the arccosine of `a`, where return value is
/// in the range 0 to pi.
///
/// - Parameters
/// - a: vector
/// - Returns: arccosine of a vector values
public func acos(_ a: Vector) -> Vector {
return vectorFunction(vvacos, a)
}

/// Return the arctangent of `a`.
///
/// - Parameters
/// - a: vector
/// - Returns: arctangent of a vector values
public func atan(_ a: Vector) -> Vector {
return vectorFunction(vvatan, a)
}
15 changes: 15 additions & 0 deletions Tests/MatrixTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,21 @@ class MatrixSpec: QuickSpec {
let res = Matrix([[tan(1.0), tan(2.0)], [tan(3.0), tan(4.0)]])
expect(tan(m1)) == res
}
it("asin") {
let m1 = Matrix([[0.5, -0.5], [1.0, -1.0]])
let res = Matrix([[asin(0.5), asin(-0.5)], [asin(1.0), asin(-1.0)]])
expect(asin(m1)) == res
}
it("acos") {
let m1 = Matrix([[0.5, -0.5], [1.0, -1.0]])
let res = Matrix([[acos(0.5), acos(-0.5)], [acos(1.0), acos(-1.0)]])
expect(acos(m1)) == res
}
it("atan") {
let m1 = Matrix([[1.0, 2.0], [3.0, 4.0]])
let res = Matrix([[atan(1.0), atan(2.0)], [atan(3.0), atan(4.0)]])
expect(atan(m1)) == res
}
}

describe("Matrix statistics tests") {
Expand Down
15 changes: 15 additions & 0 deletions Tests/VectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,21 @@ class VectorSpec: QuickSpec {
let res = [tan(1.0), tan(2.0), tan(3.0)]
expect(tan(vec)).to(beCloseTo(res))
}
it("asin") {
let vec = [1.0, 2.0, 3.0]
let res = [asin(1.0), asin(2.0), asin(3.0)]
expect(asin(vec)).to(beCloseTo(res))
}
it("acos") {
let vec = [1.0, 2.0, 3.0]
let res = [acos(1.0), acos(2.0), acos(3.0)]
expect(acos(vec)).to(beCloseTo(res))
}
it("atan") {
let vec = [1.0, 2.0, 3.0]
let res = [atan(1.0), atan(2.0), atan(3.0)]
expect(atan(vec)).to(beCloseTo(res))
}
}

describe("Vector statistics tests") {
Expand Down

0 comments on commit d8d6a11

Please sign in to comment.