Skip to content

Commit

Permalink
optimize Complex +/- Real
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Jun 13, 2013
1 parent 24dbefc commit d257be6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@ sign(z::Complex) = z/abs(z)
-(z::Complex, w::Complex) = complex(real(z) - real(w), imag(z) - imag(w))
*(z::Complex, w::Complex) = complex(real(z) * real(w) - imag(z) * imag(w),
real(z) * imag(w) + imag(z) * real(w))

# adding or multiplying real & complex is common
*(x::Real, z::Complex) = complex(x * real(z), x * imag(z))
*(z::Complex, x::Real) = complex(x * real(z), x * imag(z))
+(x::Real, z::Complex) = complex(x + real(z), imag(z))
+(z::Complex, x::Real) = complex(x + real(z), imag(z))
-(x::Real, z::Complex) = complex(x - real(z), -imag(z))
-(z::Complex, x::Real) = complex(real(z) - x, imag(z))

# multiplying by im is common
*(z::ImaginaryUnit, w::ImaginaryUnit) = complex(-imag(z), real(z))
Expand Down

0 comments on commit d257be6

Please sign in to comment.