Skip to content

struct Complex
inherits Struct

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.

require "complex"

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

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

Class methods

.additive_identity : self

View source

.new(real : Number, imag : Number = 0)

View source

.new(c : Complex)

View source

.zero : Complex

Returns the number 0 in complex form.

View source

Methods

#*(other : Complex)

Multiplies self by other.

View source

#*(other : Number)

Multiplies self by other.

View source

#+(other : Complex)

Adds the value of self to other.

View source

#+(other : Number)

Adds the value of self to other.

View source

#+

Returns self.

View source

#-

Returns the opposite of self.

View source

#-(other : Complex)

Removes the value of other from self.

View source

#-(other : Number)

Removes the value of other from self.

View source

#/(other : Complex)

Divides self by other.

View source

#/(other : Number)

Divides self by other.

View source

#==(other)

Determines whether self equals other or not.

View source

#==(other : Number)

Determines whether self equals other or not.

View source

#==(other : Complex)

Determines whether self equals other or not.

View 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
View source

#abs2

Returns the square of absolute value in a number form.

require "complex"

Complex.new(42, 2).abs2 # => 1768
View source

#clone

View source

#conj

Returns the conjugate of self.

require "complex"

Complex.new(42, 2).conj  # => 42.0 - 2.0i
Complex.new(42, -2).conj # => 42.0 + 2.0i
View source

#hash(hasher)

View source

#imag : Float64

Returns the imaginary part.

View 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)"
View source

#inv

Returns the inverse of self.

View source

#multiplicative_identity : self

View source

#phase

Returns the phase of self.

View source

#polar

Returns a Tuple with the abs value and the phase.

require "complex"

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

#real : Float64

Returns the real part.

View source

#round(digits = 0)

Rounds to the nearest digits.

View source

#sign

View source

#to_c

Returns self.

View source

#to_f

See #to_f64.

View source

#to_f32

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

View source

#to_f64

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

View source

#to_i

See #to_i32.

View source

#to_i16(*args, **options)

View source

#to_i16

View source

#to_i32(*args, **options)

View source

#to_i32

View source

#to_i64

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

View source

#to_i8(*args, **options)

View source

#to_i8

View 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"
View source

#to_u16(*args, **options)

View source

#to_u16

View source

#to_u32(*args, **options)

View source

#to_u32

View source

#to_u64

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

View source

#to_u8(*args, **options)

View source

#to_u8

View source

#zero? : Bool

View source