From ee06dbcd9ee9a11d8956f3106988b76fd852576e Mon Sep 17 00:00:00 2001 From: Atsushi Sakai Date: Mon, 17 May 2021 07:39:25 +0900 Subject: [PATCH] RFC: Some degree trigonometric functions, `sind`, `cosd`, `tand`, `asind` , `acosd`, `asecd`, `acsd`, `acotd`, `atand` accept a square matrix. (#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 --- NEWS.md | 1 + base/special/trig.jl | 32 ++++++++++++++++++++------------ test/math.jl | 29 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/NEWS.md b/NEWS.md index ea05d66aac3a6e..93e3b6e5239927 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/base/special/trig.jl b/base/special/trig.jl index e7b9e2085bb981..363bec0e62c8cd 100644 --- a/base/special/trig.jl +++ b/base/special/trig.jl @@ -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 @@ -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)) diff --git a/test/math.jl b/test/math.jl index 074358da9c7915..120f1075e0818d 100644 --- a/test/math.jl +++ b/test/math.jl @@ -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