struct

Complex

Inherits Struct / Value / Object

A complex number is a number represented in the form a + bi. In this form, a and b are real numbers, and i is an imaginary number such as i² = -1. The a is the real part of the number, and the b is the imaginary part of the number.

NOTE: To use Complex, you must explicitly import it with require "complex"

require "complex"

Complex.new(1, 0)   # => 1.0 + 0.0.i
Complex.new(5, -12) # => 5.0 - 12.0.i

1.to_c # => 1.0 + 0.0.i
1.i    # => 0.0 + 1.0.i

Constructors

additive_identity
Source
multiplicative_identity
Source
new(real : Number, imag : Number = 0)
Source
new(c : Complex)
Source
zero

Returns the number 0 in complex form.

Source

Instance methods

*(other : Complex) : Complex

Multiplies self by other.

Source
*(other : Number) : Complex

Multiplies self by other.

Source
+(other : Complex) : Complex

Adds the value of self to other.

Source
+(other : Number) : Complex

Adds the value of self to other.

Source
+

Returns self.

Source
-(other : Complex) : Complex

Removes the value of other from self.

Source
-(other : Number) : Complex

Removes the value of other from self.

Source
-

Returns the opposite of self.

Source
/(other : Complex) : Complex

Divides self by other.

Source
/(other : Number) : Complex

Divides self by other.

Source
==(other : Complex)

Determines whether self equals other or not.

Source
==(other : Number)

Determines whether self equals other or not.

Source
==(other)

Determines whether self equals other or not.

Source
abs

Returns the absolute value of this complex number in a number form, using the Pythagorean theorem.

require "complex"

Complex.new(42, 2).abs  # => 42.04759208325728
Complex.new(-42, 2).abs # => 42.04759208325728
Source
abs2

Returns the square of absolute value in a number form.

require "complex"

Complex.new(42, 2).abs2 # => 1768
Source
clone
Source
conj

Returns the conjugate of self.

require "complex"

Complex.new(42, 2).conj  # => 42.0 - 2.0.i
Complex.new(42, -2).conj # => 42.0 + 2.0.i
Source
hash(hasher)

See Object#hash(hasher)

Source
imag

Returns the imaginary part.

Source
inspect(io : IO) : Nil

Writes this complex object to an io, surrounded by parentheses.

require "complex"

Complex.new(42, 2).inspect # => "(42.0 + 2.0i)"
Source
inv

Returns the inverse of self.

Source
phase

Returns the phase of self.

Source
polar

Returns a Tuple with the abs value and the phase.

require "complex"

Complex.new(42, 2).polar # => {42.047592083257278, 0.047583103276983396}
Source
real

Returns the real part.

Source
round(digits = 0) : Complex

Rounds to the nearest digits.

Source
sign

Returns the complex sign of self.

If self is non-zero, the returned value has the same phase as self and absolute value 1.0. If self is zero, returns self.

The returned value's real and imaginary components always have the same signs as the respective components of self.

require "complex"

Complex.new(7, -24).sign        # => (0.28 - 0.96.i)
Complex.new(1.0 / 0.0, 24).sign # => (1.0 + 0.0.i)
Complex.new(-0.0, +0.0).sign    # => (-0.0 + 0.0.i)
Source
to_c

Returns self.

Source
to_f

See #to_f64.

Source
to_f32(*args, **options)
Source
to_f32(*args, **options, &)
Source
to_f64

Returns the value as a Float64 if possible (the imaginary part should be exactly zero), raises otherwise.

Source
to_i

See #to_i32.

Source
to_i128(*args, **options)
Source
to_i128(*args, **options, &)
Source
to_i16(*args, **options)
Source
to_i16(*args, **options, &)
Source
to_i32(*args, **options)
Source
to_i32(*args, **options, &)
Source
to_i64(*args, **options)
Source
to_i64(*args, **options, &)
Source
to_i8(*args, **options)
Source
to_i8(*args, **options, &)
Source
to_s(io : IO) : Nil

Writes this complex object to an io.

require "complex"

Complex.new(42, 2).to_s # => "42.0 + 2.0i"
Source
to_u128(*args, **options)
Source
to_u128(*args, **options, &)
Source
to_u16(*args, **options)
Source
to_u16(*args, **options, &)
Source
to_u32(*args, **options)
Source
to_u32(*args, **options, &)
Source
to_u64(*args, **options)
Source
to_u64(*args, **options, &)
Source
to_u8(*args, **options)
Source
to_u8(*args, **options, &)
Source
zero?

Returns true if the complex number is zero. This means the real and imaginary are both zero.

require "complex"

Complex.new(0, 0).zero? # => true
Complex.new(1, 0).zero? # => false
Complex.new(0, 1).zero? # => false
Source