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.

NOTE To useComplex, you must explicitly import it withrequire "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

Defined in:

complex.cr

Constructors

Instance Method Summary

Instance methods inherited from struct Struct

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

Class methods inherited from struct Struct

pre_initialize(address : Pointer) : Nil pre_initialize

Instance methods inherited from struct Value

==(other : Log::Metadata::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?(collection : Object) : Bool
in?(*values : Object) : Bool
in?
, inspect(io : IO) : Nil
inspect : String
inspect
, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
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) : Nil
to_json : String
to_json
, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil
to_pretty_json
, to_s(io : IO) : Nil
to_s : String
to_s
, to_yaml(io : IO) : Nil
to_yaml : String
to_yaml
, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

Class methods inherited from class Object

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

Macros inherited from class Object

class_getter(*names, &block) class_getter, class_getter!(*names) class_getter!, class_getter?(*names, &block) class_getter?, class_property(*names, &block) class_property, class_property!(*names) class_property!, class_property?(*names, &block) class_property?, class_setter(*names) class_setter, def_clone def_clone, def_equals(*fields) def_equals, def_equals_and_hash(*fields) def_equals_and_hash, def_hash(*fields) def_hash, delegate(*methods, to object) delegate, forward_missing_to(delegate) forward_missing_to, getter(*names, &block) getter, getter!(*names) getter!, getter?(*names, &block) getter?, property(*names, &block) property, property!(*names) property!, property?(*names, &block) property?, setter(*names) setter

Constructor Detail

def self.additive_identity : self #

def self.multiplicative_identity : self #

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

def self.new(c : Complex) #

def self.zero : Complex #

Returns the number0 in complex form.


Instance Method Detail

def *(other : Complex) : Complex #

Multipliesself byother.


def *(other : Number) : Complex #

Multipliesself byother.


def +(other : Complex) : Complex #

Adds the value ofself toother.


def +(other : Number) : Complex #

Adds the value ofself toother.


def + : Complex #

Returnsself.


def -(other : Complex) : Complex #

Removes the value ofother fromself.


def -(other : Number) : Complex #

Removes the value ofother fromself.


def - : Complex #

Returns the opposite ofself.


def /(other : Complex) : Complex #

Dividesself byother.


def /(other : Number) : Complex #

Dividesself byother.


def ==(other : Complex) #

Determines whetherself equalsother or not.


def ==(other : Number) #

Determines whetherself equalsother or not.


def ==(other) #

Determines whetherself equalsother or not.


def abs : Float64 #

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

Returns the square of absolute value in a number form.

require "complex"

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

def clone #

def conj : Complex #

Returns the conjugate ofself.

require "complex"

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

def hash(hasher) #

def imag : Float64 #

Returns the imaginary part.


def inspect(io : IO) : Nil #

Writes this complex object to anio, surrounded by parentheses.

require "complex"

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

def inv : Complex #

Returns the inverse ofself.


def phase : Float64 #

Returns the phase ofself.


def polar : Tuple(Float64, Float64) #

Returns aTuple 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) : Complex #

Rounds to the nearestdigits.


def sign : Complex #

Returns the complex sign ofself.

Ifself is non-zero, the returned value has the same phase asself and absolute value1.0. Ifself is zero, returnsself.

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

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)

def to_c #

Returnsself.


def to_f #

See#to_f64.


def to_f32(*args, **options) #

def to_f32(*args, **options, &) #

def to_f64 : Float64 #

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


def to_i #

See#to_i32.


def to_i128(*args, **options) #

def to_i128(*args, **options, &) #

def to_i16(*args, **options) #

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

def to_i32(*args, **options) #

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

def to_i64(*args, **options) #

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

def to_i8(*args, **options) #

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

def to_s(io : IO) : Nil #

Writes this complex object to anio.

require "complex"

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

def to_u128(*args, **options) #

def to_u128(*args, **options, &) #

def to_u16(*args, **options) #

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

def to_u32(*args, **options) #

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

def to_u64(*args, **options) #

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

def to_u8(*args, **options) #

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

def zero? : Bool #

Returnstrue 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