Skip to content

Commit

Permalink
RFC: Some degree trigonometric functions, sind, cosd, tand, `as…
Browse files Browse the repository at this point in the history
…ind` , `acosd`, `asecd`, `acsd`, `acotd`, `atand` accept a square matrix. (JuliaLang#39758)

* Some degree trigonometric functions, `sind`, `cosd`, `tand`, `asind`, `acosd`, `asecd`, `acsd`, `acotd` accept an square matrix.

* add PR number and change argument name to be the same as in the document.

* Update NEWS.md

* add `atand` for News.md.

Co-authored-by: Viral B. Shah <[email protected]>
  • Loading branch information
2 people authored and johanmon committed Jul 5, 2021
1 parent dbdf40b commit d7a2108
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Standard library changes
([#39322])
* `@lock` is now exported from Base ([#39588]).
* The experimental function `Base.catch_stack()` has been renamed to `current_exceptions()`, exported from Base and given a more specific return type ([#29901])
* Some degree trigonometric functions, `sind`, `cosd`, `tand`, `asind`, `acosd`, `asecd`, `acscd`, `acotd`, `atand` now accept an square matrix ([#39758]).

#### Package Manager

Expand Down
32 changes: 20 additions & 12 deletions base/special/trig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1269,22 +1269,30 @@ end
sincosd(::Missing) = (missing, missing)

for (fd, f, fn) in ((:sind, :sin, "sine"), (:cosd, :cos, "cosine"), (:tand, :tan, "tangent"))
name = string(fd)
@eval begin
@doc """
$($name)(x)
Compute $($fn) of `x`, where `x` is in degrees. """ ($fd)(z) = ($f)(deg2rad(z))
for (fu, un) in ((:deg2rad, "degrees"),)
name = string(fd)
@eval begin
@doc """
$($name)(x)
Compute $($fn) of `x`, where `x` is in $($un).
If `x` is a matrix, `x` needs to be a square matrix. """ ($fd)(x) = ($f)(($fu).(x))
end
end
end

for (fd, f, fn) in ((:asind, :asin, "sine"), (:acosd, :acos, "cosine"),
(:asecd, :asec, "secant"), (:acscd, :acsc, "cosecant"), (:acotd, :acot, "cotangent"))
name = string(fd)
@eval begin
@doc """
$($name)(x)

Compute the inverse $($fn) of `x`, where the output is in degrees. """ ($fd)(y) = rad2deg(($f)(y))
for (fu, un) in ((:rad2deg, "degrees"),)
name = string(fd)
@eval begin
@doc """
$($name)(x)
Compute the inverse $($fn) of `x`, where the output is in $($un).
If `x` is a matrix, `x` needs to be a square matrix. """ ($fd)(x) = ($fu).(($f)(x))
end
end
end

Expand All @@ -1294,5 +1302,5 @@ end
Compute the inverse tangent of `y` or `y/x`, respectively, where the output is in degrees.
"""
atand(y) = rad2deg(atan(y))
atand(y, x) = rad2deg(atan(y,x))
atand(y) = rad2deg.(atan(y))
atand(y, x) = rad2deg.(atan(y,x))
29 changes: 29 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,35 @@ end
@test Array(acosh.(STAA)) == acosh.(TAA)
@test Array(acsch.(STAA)) == acsch.(TAA)
@test Array(acoth.(STAA)) == acoth.(TAA)
@test sind(TAA) == sin(deg2rad.(TAA))
@test cosd(TAA) == cos(deg2rad.(TAA))
@test tand(TAA) == tan(deg2rad.(TAA))
@test asind(TAA) == rad2deg.(asin(TAA))
@test acosd(TAA) == rad2deg.(acos(TAA))
@test atand(TAA) == rad2deg.(atan(TAA))
@test asecd(TAA) == rad2deg.(asec(TAA))
@test acscd(TAA) == rad2deg.(acsc(TAA))
@test acotd(TAA) == rad2deg.(acot(TAA))

m = rand(3,2) # not square matrix
ex = @test_throws DimensionMismatch sind(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch cosd(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch tand(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch asind(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch acosd(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch atand(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch asecd(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch acscd(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch acotd(m)
@test startswith(ex.value.msg, "matrix is not square")
end

@testset "check exp2(::Integer) matches exp2(::Float)" begin
Expand Down

0 comments on commit d7a2108

Please sign in to comment.