Complex - Crystal 0.35.1

# struct Complex

- Complex
- Struct
- Value
- Object

##
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:

##
Constructors

- .new(real : Number, imag : Number = 0)
- .new(c : Complex)
- .zero : Complex

  Returns the number `0` in complex form.

##
Instance Method Summary

- #*(other : Number)

  Multiplies `self` by other.

- #*(other : Complex)

  Multiplies `self` by other.

- #+(other : Complex)

  Adds the value of `self` to other.

- #+(other : Number)

  Adds the value of `self` to other.

- #+

  Returns absolute value of `self`.

- #-(other : Complex)

  Removes the value of other from `self`.

- #-

  Returns the opposite of `self`.

- #-(other : Number)

  Removes the value of other from `self`.

- #/(other : Number)

  Divides `self` by other.

- #/(other : Complex)

  Divides `self` by other.

- #==(other : Complex)

  Determines whether `self` equals other or not.

- #==(other : Number)

  Determines whether `self` equals other or not.

- #==(other)

  Determines whether `self` equals other or not.

- #abs

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

- #abs2

  Returns the square of absolute value in a number form.

- #clone
- #conj

  Returns the conjugate of `self`.

- #exp

  Calculates the exp of `self`.

- #hash(hasher)

  See `Object#hash(hasher)`

- #imag : Float64

  Returns the imaginary part.

- #inspect(io : IO) : Nil

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

- #inv

  Returns the inverse of `self`.

- #log

  Calculates the log of `self`.

- #log10

  Calculates the log10 of `self`.

- #log2

  Calculates the log2 of `self`.

- #phase

  Returns the phase of `self`.

- #polar

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

- #real : Float64

  Returns the real part.

- #round(digits = 0)

  Rounds to the nearest digits.

- #sign
- #sqrt

  `Complex#sqrt` was inspired by the following blog post of Pavel Panchekha on floating point precision.

- #to_c

  Returns `self`.

- #to_f

  See `#to_f64`.

- #to_f32

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

- #to_f64

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

- #to_i

  See `#to_i32`.

- #to_i16(*args, **options)
- #to_i16(*args, **options, &)
- #to_i32(*args, **options)
- #to_i32(*args, **options, &)
- #to_i64

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

- #to_i8(*args, **options)
- #to_i8(*args, **options, &)
- #to_s(io : IO) : Nil

  Writes this complex object to an io.

- #to_u16(*args, **options)
- #to_u16(*args, **options, &)
- #to_u32(*args, **options)
- #to_u32(*args, **options, &)
- #to_u64

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

- #to_u8(*args, **options)
- #to_u8(*args, **options, &)
- #zero? : Bool

### Instance methods inherited from struct `Struct`

==(other) : Bool ==, hash(hasher) hash, inspect(io : IO) : Nil inspect, pretty_print(pp) : Nil pretty_print, to_s(io : IO) : Nil to_s

### Instance methods inherited from struct `Value`

==(other : JSON::Any)
==(other : YAML::Any)
==(other) ==, dup dup

### Instance methods inherited from class `Object`

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other) ===, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash hash, in?(*values : Object) : Bool
in?(collection) : Bool in?, inspect : String
inspect(io : IO) : Nil inspect, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO)
to_json to_json, to_pretty_json(io : IO, indent : String = " ")
to_pretty_json(indent : String = " ") to_pretty_json, to_s : String
to_s(io : IO) : Nil to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

### Class methods inherited from class `Object`

from_json(string_or_io, root : String)
from_json(string_or_io) from_json, from_yaml(string_or_io : String | IO) from_yaml

##
Constructor Detail

def self.new(real : Number, imag : Number = 0) #
def self.new(c : Complex) #
def self.zero : Complex #

Returns the number `0` in complex form.

##
Instance Method Detail

def *(other : Number) #

Multiplies `self` by other.

def *(other : Complex) #

Multiplies `self` by other.

def +(other : Complex) #

Adds the value of `self` to other.

def +(other : Number) #

Adds the value of `self` to other.

def + #

Returns absolute value of `self`.

def -(other : Complex) #

Removes the value of other from `self`.

def - #

Returns the opposite of `self`.

def -(other : Number) #

Removes the value of other from `self`.

def /(other : Number) #

Divides `self` by other.

def /(other : Complex) #

Divides `self` by other.

def ==(other : Complex) #

Determines whether `self` equals other or not.

def ==(other : Number) #

Determines whether `self` equals other or not.

def ==(other) #

Determines whether `self` equals other or not.

def 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

def abs2 #

Returns the square of absolute value in a number form.

    require "complex"

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

def clone #
def 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

def exp #

Calculates the exp of `self`.

    require "complex"

    Complex.new(4, 2).exp # => -22.720847417619233 + 49.645957334580565i

def hash(hasher) #

See `Object#hash(hasher)`

def imag : Float64 #

Returns the imaginary part.

def 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)"

def inv #

Returns the inverse of `self`.

def log #

Calculates the log of `self`.

def log10 #

Calculates the log10 of `self`.

def log2 #

Calculates the log2 of `self`.

def phase #

Returns the phase of `self`.

def polar #

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

    require "complex"

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

def real : Float64 #

Returns the real part.

def round(digits = 0) #

Rounds to the nearest digits.

def sign #
def sqrt #

`Complex#sqrt` was inspired by the following blog post of Pavel Panchekha on floating point precision.

def to_c #

Returns `self`.

def to_f #

See `#to_f64`.

def to_f32 #

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

def to_f64 #

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

def to_i #

See `#to_i32`.

def to_i16(*args, **options) #
def to_i16(*args, **options, &) #
def to_i32(*args, **options) #
def to_i32(*args, **options, &) #
def to_i64 #

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

def to_i8(*args, **options) #
def to_i8(*args, **options, &) #
def to_s(io : IO) : Nil #

Writes this complex object to an io.

    require "complex"

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

def to_u16(*args, **options) #
def to_u16(*args, **options, &) #
def to_u32(*args, **options) #
def to_u32(*args, **options, &) #
def to_u64 #

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

def to_u8(*args, **options) #
def to_u8(*args, **options, &) #
def zero? : Bool #