indexing description: "[ Simple implementation of complex numbers using double values. ]" date: "$Date$" revision: "$Revision$" class EM_COMPLEX inherit ANY redefine out end DOUBLE_MATH undefine out end create make, make_from_polar feature -- Creation make (a_real_part, an_imaginary_part: DOUBLE) is -- creates a complex number do real := a_real_part imaginary := an_imaginary_part ensure real_part_set: real = a_real_part imaginary_part_set: imaginary = an_imaginary_part end make_from_polar (a_radius, an_angle: DOUBLE) is -- creates a complex number from the given arguments do real := a_radius * cosine (an_angle) imaginary := a_radius * sine (an_angle) end feature -- Access real: DOUBLE -- real part of the complex number imaginary: DOUBLE -- imaginary part of the complex number feature -- Element change conjugate is -- conjugates current do imaginary := -imaginary ensure conugated: imaginary = - (old imaginary) end feature -- Calculations abs, length: DOUBLE is -- length of `Current'. do Result := sqrt (real*real + imaginary*imaginary) ensure result_calculated: Result >= sqrt (real*real + imaginary*imaginary) - epsilon and Result <= sqrt (real*real + imaginary*imaginary) + epsilon end arg: DOUBLE is -- returns the angle of the complex number in the complex plane (Argand diagram). local dir: EM_DIRECTION_2D do create dir.make_from_coefficients (real, imaginary) Result := dir.angle ensure result_in_range: Result >= 0 and Result < 2*pi end abs_squared, length_squared: DOUBLE is -- squared length of `Current'. do Result := (real*real + imaginary*imaginary) ensure result_calculated: Result >= (real*real + imaginary*imaginary) - epsilon and Result <= (real*real + imaginary*imaginary) + epsilon end conjugated: like Current is -- also see `conjugate' do Result := create {EM_COMPLEX}.make (real, -imaginary) ensure result_calculated: result.real = real and Result.imaginary = -imaginary end multiplicative_inverse: like Current is -- returns the multiplicative inverse of `current'. (1/Current) require length_not_zero: length_squared /= 0 local len: DOUBLE do len := length_squared Result := create {EM_COMPLEX}.make (real / len, -imaginary / len) end infix "+" (other: like Current): like Current is -- Sum with `other' (commutative). require other_not_void: other /= Void do create Result.make (real + other.real, imaginary + other.imaginary) end infix "-" (other: like Current): like Current is -- Result of subtracting `other' require other_not_void: other /= Void do create Result.make (real - other.real, imaginary - other.imaginary) end infix "*" (other: like current): like Current is -- multiplication with `other' require other_not_void: other /= Void do create Result.make (real*other.real - imaginary*other.imaginary, imaginary*other.real + real*other.imaginary) end times (other: DOUBLE): like Current is -- multiplication with `other' do create Result.make (real*other, imaginary*other) end infix "/" (other: like current): like Current is -- Scalar division by `a_divisor'. require other_not_void: other /= Void length_not_zero: other.length_squared /= 0 local len: DOUBLE do len := other.length_squared create Result.make ((real*other.real + imaginary*other.imaginary) / len, (imaginary*other.real - real*other.imaginary) / len) end divided_by (other: DOUBLE): like Current is -- division with `other' require other_not_zero: other /= 0 do create Result.make (real/other, imaginary/other) end prefix "+": like Current is -- Unary plus do create Result.make (real, imaginary) ensure result_is_same: Result.real = real and Result.imaginary = imaginary end prefix "-": like Current is -- Unary minus do create Result.make (-real, -imaginary) ensure result_is_negation: Result.real = -real and Result.imaginary = -imaginary end feature -- Output out: STRING is -- Textual representation do Result := "(" + real.out + " + " + imaginary.out + "i)" end to_vector_2d: EM_VECTOR_2D is -- returns a vector do Result := create {EM_VECTOR_2D}.make (real, imaginary) end feature {NONE} -- Implementation epsilon: DOUBLE is 0.000001 -- used to check post-conditions of double computations end