expanded class
	COMPLEX_NUMBER_IMPL

inherit
	COMPLEX_NUMBER
		rename
			a as real,
			b as imaginary
		redefine
			default_create,
			complex_anchor,
			real_anchor
		end

create
	default_create,
	make,
	make_from_real,
	make_from_native_integer,
	make_from_integer

convert
	make_from_real ({REAL_NUMBER}),
	make_from_native_integer ({INTEGER}),
	make_from_integer ({INTEGER_NUMBER})

feature {NONE} -- Initialization

	default_create
			-- Create the complex number 0 + 0i.
			--| Why not an empty body? Consider a local z: COMPLEX_NUMBER_IMPL; create z is issued implicitly every time the routine that declares z starts. That is true even when such a
			--| routine restarts due to a retry inside a rescue clause; if z is no more Zero at that point, a default_create empty body does not reverts z to Zero, so violating
			--| default_create post-condition.
		do
			create a
			create b
		ensure then
			real: a ≍ Zero
			imaginary: b ≍ Zero
		end

	make (a_real, a_imaginary: REAL_NUMBER)
			-- Create the complex number `a_real' + `a_imaginary'i.
		do
			a := converted_real (a_real)
			b := converted_real (a_imaginary)
		ensure
			real: a ≍ a_real
			imaginary: b ≍ a_imaginary
		end

	make_from_real (x: REAL_NUMBER)
			-- Create the complex number `x' + 0i.
		do
			make (x, Zero)
		ensure
			real: a ≍ x
			imaginary: b ≍ Zero
		end

	make_from_native_integer (v: INTEGER)
			-- Create a complex number with
			-- `real'.`value' = {REAL_NUMBER_IMPL}.normalized_value (`v'.`to_real') and `imaginary' = 0
		do
			make (create {REAL_NUMBER_IMPL}.make_from_native_integer (v), Zero)
		ensure
			real: a.value = {REAL_NUMBER_IMPL}.normalized_value (v)
			imaginary: b ≍ Zero
		end

	make_from_integer (i: INTEGER_NUMBER)
			-- Create a complex number with
			-- `real'.`value' = {REAL_NUMBER_IMPL}.normalized_value (`i'.`value'.`to_real') and `imaginary' = 0
		do
			make_from_native_integer (i.value)
		ensure
			real: a.value = {REAL_NUMBER_IMPL}.normalized_value (i.value)
			imaginary: b ≍ Zero
		end

feature -- Primitive

	a: like real_anchor
			-- Real part of current complex number

	real: like real_anchor
			-- <Precursor>
		do
			Result := a
		end

	b: like real_anchor
			-- Imaginary part of current complex number

	imaginary: like real_anchor
			-- <Precursor>
		do
			Result := b
		end

feature -- Access

	Zero: INTEGER_NUMBER_IMPL
			-- <Precursor>
		once
		ensure then
			class
		end

feature -- Operation

	conjugate: like complex_anchor
			-- <Precursor>
		do
			create Result.make (a, - b)
		end

	product alias "*" alias "×" (z: COMPLEX_NUMBER): like complex_anchor
			-- <Precursor>
		local
			x, y: like real_anchor
		do
			x := product_real (a.value, b.value, z.a.value, z.b.value)
			y := product_imaginary (a.value, b.value, z.a.value, z.b.value)
			create Result.make (x, y)
		end

feature -- Factory

	converted_real (x: REAL_NUMBER): like real_anchor
			-- `x' converted to a real number like `real_anchor'
		do
			if attached {like real_anchor} x as cx then
				Result := cx
			else
				Result := x
			end
		ensure
			class
			definition: Result ≍ x
		end

feature -- Anchor

	complex_anchor: COMPLEX_NUMBER_IMPL
			-- <Precursor>
		do
			Result := Current
		end

	real_anchor: REAL_NUMBER_IMPL
			-- <Precursor>
		do
			Result := a
		end

	integer_anchor: INTEGER_NUMBER_IMPL
			-- <Precursor>
		do
			Result := 0
		end

end
