Skip to content

Commit

Permalink
Add rad2deg!, deg2rad!, and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PerezHz committed Dec 5, 2017
1 parent f3165b4 commit 5f53f3b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/dictmutfunct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ const _dict_unary_ops = Dict(
:one => [:one!, (:_res, :_arg1, :_k), :(_res = one(_arg1))],
:abs => [:abs!, (:_res, :_arg1, :_k), :(_res = abs(_arg1))],
:abs2 => [:abs2!, (:_res, :_arg1, :_k), :(_res = abs2(_arg1))],
:deg2rad => [:deg2rad!, (:_res, :_arg1, :_k), :(_res = deg2rad(_arg1))],
:rad2deg => [:rad2deg!, (:_res, :_arg1, :_k), :(_res = rad2deg(_arg1))],
#
:sin => [:sincos!, (:_res, :_aux, :_arg1, :_k), :(_res = sin(_arg1)),
:(_aux = cos(_arg1))],
Expand Down
20 changes: 20 additions & 0 deletions src/other_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,23 @@ for T in (:Taylor1, :TaylorN)
@eval rad2deg(z::$T{T}) where {T<:AbstractFloat} = z * (180 / convert(T, pi))
@eval rad2deg(z::$T{T}) where {T<:Real} = z * (180 / convert(float(T), pi))
end

# Internal mutating deg2rad!, rad2deg! functions
for T in (:Taylor1, :TaylorN)
@eval @inline function deg2rad!(v::$T{T}, a::$T{T}, k::Int) where {T<:AbstractFloat}
@inbounds v[k] = a[k] * (convert(T, pi) / 180)
return nothing
end
@eval @inline function deg2rad!(v::$T{S}, a::$T{T}, k::Int) where {S<:AbstractFloat, T<:Real}
@inbounds v[k] = a[k] * (convert(float(T), pi) / 180)
return nothing
end
@eval @inline function rad2deg!(v::$T{T}, a::$T{T}, k::Int) where {T<:AbstractFloat}
@inbounds v[k] = a[k] * (180 / convert(T, pi))
return nothing
end
@eval @inline function rad2deg!(v::$T{S}, a::$T{T}, k::Int) where {S<:AbstractFloat, T<:Real}
@inbounds v[k] = a[k] * (180 / convert(float(T), pi))
return nothing
end
end
26 changes: 26 additions & 0 deletions test/manyvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,30 @@ end
@test Q() == evaluate.(Q)
@test Q() == evaluate(Q)
@test Q[1:3]() == evaluate(Q[1:3])

dx = set_variables("x", numvars=4, order=10)
for i in 1:4
@test deg2rad(180+dx[i]) == pi + deg2rad(1.0)dx[i]
@test rad2deg(pi+dx[i]) == 180.0+rad2deg(1.0)dx[i]
end
p = sin(exp(dx[1]*dx[2])+dx[3]*dx[2])/(1.0+dx[4]^2)
q = zero(p)
TaylorSeries.deg2rad!(q, p, 0)
@test q[0] == p[0]*(pi/180)
TaylorSeries.deg2rad!.(q, p, [1,3,5])
for i in [0,1,3,5]
@test q[i] == p[i]*(pi/180)
end
TaylorSeries.rad2deg!(q, p, 0)
@test q[0] == p[0]*(180/pi)
TaylorSeries.rad2deg!.(q, p, [1,3,5])
for i in [0,1,3,5]
@test q[i] == p[i]*(180/pi)
end
xT = 5+TaylorN(Int64, 1, order=10)
yT = TaylorN(2, order=10)
TaylorSeries.deg2rad!(yT, xT, 0)
@test yT[0] == xT[0]*(pi/180)
TaylorSeries.rad2deg!(yT, xT, 0)
@test yT[0] == xT[0]*(180/pi)
end
6 changes: 0 additions & 6 deletions test/mixtures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,4 @@ end
Qv = convert.(Taylor1{TaylorN{Float64}}, Pv)

@test jacobian(Pv) == jacobian(Qv)

dx = set_variables("x", numvars=4, order=10)
for i in 1:4
@test deg2rad(180+dx[i]) == pi + deg2rad(1.0)dx[i]
rad2deg(pi+dx[i]) == 180.0+rad2deg(1.0)dx[i]
end
end
2 changes: 1 addition & 1 deletion test/mutatingfuncts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end

# Dictionaries with calls
@test length(TaylorSeries._dict_binary_ops) == 5
@test length(TaylorSeries._dict_unary_ops) == 20
@test length(TaylorSeries._dict_unary_ops) == 22

@test all([haskey(TaylorSeries._dict_binary_ops, op)
for op in [:+, :-, :*, :/, :^]])
Expand Down
23 changes: 22 additions & 1 deletion test/onevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,28 @@ end
t = Taylor1(35)
@test Taylor1([180.0, rad2deg(1.0)], 35) == rad2deg(pi+t)
@test sin(pi/2+deg2rad(1.0)t) == sin(deg2rad(90+t))

a = Taylor1(rand(10))
b = Taylor1(rand(10))
c = deepcopy(a)
TaylorSeries.deg2rad!(b, a, 0)
@test a == c
@test a[0]*(pi/180) == b[0]
TaylorSeries.deg2rad!.(b, a, [0,1,2])
@test a == c
for i in 0:2
@test a[i]*(pi/180) == b[i]
end
a = Taylor1(rand(10))
b = Taylor1(rand(10))
c = deepcopy(a)
TaylorSeries.rad2deg!(b, a, 0)
@test a == c
@test a[0]*(180/pi) == b[0]
TaylorSeries.rad2deg!.(b, a, [0,1,2])
@test a == c
for i in 0:2
@test a[i]*(180/pi) == b[i]
end
end

@testset "Matrix multiplication for Taylor1" begin
Expand Down

0 comments on commit 5f53f3b

Please sign in to comment.