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

Defined in:

complex.cr

Constructors

Instance Method Summary

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) to_s

Instance methods inherited from struct Value

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

Instance methods inherited from class Object

!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, class class, dup dup, hash(hasher)
hash
hash
, inspect(io : IO)
inspect
inspect
, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json
to_json
, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ")
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as

Constructor methods inherited from class Object

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

Constructor Detail

def self.new(real : Number, imag : Number) #

[View source]
def self.zero : Complex #

Returns the number 0 in complex form.


[View source]

Instance Method Detail

def *(other : Number) #

Multiplies self by other.


[View source]
def *(other : Complex) #

Multiplies self by other.


[View source]
def +(other : Number) #

Adds the value of self to other.


[View source]
def +(other : Complex) #

Adds the value of self to other.


[View source]
def - #

Returns the opposite of self.


[View source]
def -(other : Number) #

Removes the value from other to self.


[View source]
def -(other : Complex) #

Removes the value from other to self.


[View source]
def /(other : Number) #

Divides self by other.


[View source]
def /(other : Complex) #

Divides self by other.


[View source]
def ==(other : Complex) #

Determines whether self equals other or not.


[View source]
def ==(other : Number) #

Determines whether self equals other or not.


[View source]
def ==(other) #

Determines whether self equals other or not.


[View source]
def abs #

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

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

[View source]
def abs2 #

Returns the square of absolute value in a number form.

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

[View source]
def clone #

[View source]
def conj #

Returns the conjugate of self.

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

[View source]
def exp #

Calculates the exp of self.

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

[View source]
def hash(hasher) #

[View source]
def imag : Float64 #

Returns the image part of self.


[View source]
def inspect(io : IO) #

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

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

[View source]
def inv #

Returns the inverse of self.


[View source]
def log #

Calculates the log of self.


[View source]
def log10 #

Calculates the log10 of self.


[View source]
def log2 #

Calculates the log2 of self.


[View source]
def phase #

Returns the phase of self.


[View source]
def polar #

Returns a tuple with the abs value and the phase.

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

[View source]
def real : Float64 #

Returns the real part of self.


[View source]
def sign #

[View source]
def sqrt #

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


[View source]
def to_s(io : IO) #

Write this complex object to an io.

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

[View source]
def zero? : Bool #

[View source]