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.
Complex.new(1, 0) # => 1 + 0i
Complex.new(5, -12) # => 5 - 12i
Defined in:
complex.crClass Method Summary
- .new(real : Number, imag : Number)
-
.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 : Number)
Adds the value of self to other
-
#+(other : Complex)
Adds the value of self to other
-
#-(other : Number)
Removes the value from other to self
-
#-
Returns the opposite of self
-
#-(other : Complex)
Removes the value from other to self
-
#/(other : Complex)
Divides self by other
-
#/(other : Number)
ditto
-
#==(other : Number)
Determines whether self equals other or not
-
#==(other)
Determines whether self equals other or not
-
#==(other : Complex)
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
-
#imag : Float64
Returns the image part of self
-
#inspect(io : IO)
Write 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 of self
- #sign
-
#sqrt
Complex.sqrt was inspired by the following blog post of Pavel Panchekha on floating point precision: https://pavpanchekha.com/casio/index.html
-
#to_s(io : IO)
Write this complex object to an io
Instance methods inherited from struct Struct
==(other : self) : Bool
==,
hash : Int32
hash,
inspect(io : IO) : Nil
inspect,
to_s(io)
to_s
Instance methods inherited from struct Value
==(other)
==,
dup
dup
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, crystal_type_id crystal_type_id, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, tap(&block) tap, 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
to_yaml(io : IO) to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
clone
clone,
dup
dup,
from_json(string_or_io) : selffrom_json(string_or_io, root : String) : self from_json, from_yaml(string : String) : self from_yaml, hash hash, inspect(io) inspect, name : String name, nilable? nilable?, to_s(io) to_s, |(other : U.class) forall U |
Class Method Detail
Instance Method Detail
Returns the absolute value of this complex number in a number form, using the Pythagorean theorem.
Complex.new(42, 2).abs # => 42.0476
Complex.new(-42, 2).abs # => 42.0476
Returns the conjugate of self
Complex.new(42, 2).conj # => 42 - 2i
Complex.new(42, -2).conj # => 42 + 2i
Write this complex object to an io, surrounded by parentheses.
Complex.new(42, 2).inspect # => (42 + 2i)
Returns a tuple with the abs value and the phase.
Complex.new(42, 2).polar # => {2.54311, 0.665774}
Complex.sqrt was inspired by the following blog post of Pavel Panchekha on floating point precision: https://pavpanchekha.com/casio/index.html
Write this complex object to an io
Complex.new(42, 2).to_s # => 42 + 2i