Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Some degree trigonometric functions, sind, cosd, tand, asind , acosd, asecd, acsd, acotd, atand accept a square matrix. #39758

Merged
merged 7 commits into from
May 16, 2021
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ViralBShah Thank you for your comment. The PR title and news.md was missing atand, but it supports a square matrix by this change.

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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ViralBShah atand is tested here.

@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