struct Complex

Overview

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

Defined in:

complex.cr

Constructors

Instance Method Summary

Constructor Detail

def self.additive_identity : selfSource

def self.new(real : Number, imag : Number = 0)Source

def self.new(c : Complex)Source

def self.zero : ComplexSource

Returns the number 0 in complex form.

Instance Method Detail

def *(other : Complex) : ComplexSource

Multiplies self by other.

def *(other : Number) : ComplexSource

Multiplies self by other.

def +(other : Complex) : ComplexSource

Adds the value of self to other.

def +(other : Number) : ComplexSource

Adds the value of self to other.

def + : ComplexSource

Returns self.

def -(other : Complex) : ComplexSource

Removes the value of other from self.

def -(other : Number) : ComplexSource

Removes the value of other from self.

def - : ComplexSource

Returns the opposite of self.

def /(other : Complex) : ComplexSource

Divides self by other.

def /(other : Number) : ComplexSource

Divides self by other.

def ==(other : Complex)Source

Determines whether self equals other or not.

def ==(other : Number)Source

Determines whether self equals other or not.

def ==(other)Source

Determines whether self equals other or not.

def abs : Float64Source

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

def abs2 : Float64Source

Returns the square of absolute value in a number form.

require "complex"

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

def cloneSource

def conj : ComplexSource

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

def hash(hasher)Source

def imag : Float64Source

Returns the imaginary part.

def inspect(io : IO) : NilSource

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

require "complex"

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

def inv : ComplexSource

Returns the inverse of self.

def multiplicative_identity : selfSource

def phase : Float64Source

Returns the phase of self.

def polar : Tuple(Float64, Float64)Source

Returns a Tuple with the #abs value and the #phase.

require "complex"

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

def real : Float64Source

Returns the real part.

def round(digits = 0) : ComplexSource

Rounds to the nearest digits.

def sign : ComplexSource

def to_cSource

Returns self.

def to_fSource

See #to_f64.

def to_f32 : Float32Source

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

def to_f64 : Float64Source

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

def to_iSource

See #to_i32.

def to_i16(*args, **options)Source

def to_i16(*args, **options, &)Source

def to_i32(*args, **options)Source

def to_i32(*args, **options, &)Source

def to_i64 : Int64Source

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

def to_i8(*args, **options)Source

def to_i8(*args, **options, &)Source

def to_s(io : IO) : NilSource

Writes this complex object to an io.

require "complex"

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

def to_u16(*args, **options)Source

def to_u16(*args, **options, &)Source

def to_u32(*args, **options)Source

def to_u32(*args, **options, &)Source

def to_u64 : UInt64Source

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

def to_u8(*args, **options)Source

def to_u8(*args, **options, &)Source

def zero? : BoolSource

© 2012–2021 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.2.1/Complex.html