Skip to content

Commit 1c03fc9

Browse files
authored
Merge pull request #59 from SebastianM-C/hypot
Use hypot for the radial coordinate
2 parents 00f11e2 + 86a0389 commit 1c03fc9

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/coordinatesystems.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ Base.show(io::IO, trans::CartesianFromPolar) = print(io, "CartesianFromPolar()")
2424
function (::PolarFromCartesian)(x::AbstractVector)
2525
length(x) == 2 || error("Polar transform takes a 2D coordinate")
2626

27-
Polar(sqrt(x[1]*x[1] + x[2]*x[2]), atan(x[2], x[1]))
27+
Polar(hypot(x[1], x[2]), atan(x[2], x[1]))
2828
end
2929

3030
function transform_deriv(::PolarFromCartesian, x::AbstractVector)
3131
length(x) == 2 || error("Polar transform takes a 2D coordinate")
3232

33-
r = sqrt(x[1]*x[1] + x[2]*x[2])
33+
r = hypot(x[1], x[2])
3434
f = x[2] / x[1]
3535
c = one(eltype(x))/(x[1]*(one(eltype(x)) + f*f))
3636
@SMatrix [ x[1]/r x[2]/r ;
@@ -114,14 +114,14 @@ Base.show(io::IO, trans::SphericalFromCylindrical) = print(io, "SphericalFromCyl
114114
function (::SphericalFromCartesian)(x::AbstractVector)
115115
length(x) == 3 || error("Spherical transform takes a 3D coordinate")
116116

117-
Spherical(sqrt(x[1]*x[1] + x[2]*x[2] + x[3]*x[3]), atan(x[2],x[1]), atan(x[3]/sqrt(x[1]*x[1] + x[2]*x[2])))
117+
Spherical(hypot(x[1], x[2], x[3]), atan(x[2], x[1]), atan(x[3], hypot(x[1], x[2])))
118118
end
119119
function transform_deriv(::SphericalFromCartesian, x::AbstractVector)
120120
length(x) == 3 || error("Spherical transform takes a 3D coordinate")
121121
T = eltype(x)
122122

123-
r = sqrt(x[1]*x[1] + x[2]*x[2] + x[3]*x[3])
124-
rxy = sqrt(x[1]*x[1] + x[2]*x[2])
123+
r = hypot(x[1], x[2], x[3])
124+
rxy = hypot(x[1], x[2])
125125
fxy = x[2] / x[1]
126126
cxy = one(T)/(x[1]*(one(T) + fxy*fxy))
127127
f = -x[3]/(rxy*r*r)
@@ -150,14 +150,14 @@ transform_deriv_params(::CartesianFromSpherical, x::Spherical) = error("Cartesia
150150
function (::CylindricalFromCartesian)(x::AbstractVector)
151151
length(x) == 3 || error("Cylindrical transform takes a 3D coordinate")
152152

153-
Cylindrical(sqrt(x[1]*x[1] + x[2]*x[2]), atan(x[2],x[1]), x[3])
153+
Cylindrical(hypot(x[1], x[2]), atan(x[2], x[1]), x[3])
154154
end
155155

156156
function transform_deriv(::CylindricalFromCartesian, x::AbstractVector)
157157
length(x) == 3 || error("Cylindrical transform takes a 3D coordinate")
158158
T = eltype(x)
159159

160-
r = sqrt(x[1]*x[1] + x[2]*x[2])
160+
r = hypot(x[1], x[2])
161161
f = x[2] / x[1]
162162
c = one(T)/(x[1]*(one(T) + f*f))
163163
@SMatrix [ x[1]/r x[2]/r zero(T) ;

0 commit comments

Comments
 (0)