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
Returns the number 0
in complex form.
Multiplies self
by other.
Multiplies self
by other.
Adds the value of self
to other.
Adds the value of self
to other.
Returns absolute value of self
.
Removes the value of other from self
.
Returns the opposite of self
.
Removes the value of other from self
.
Divides self
by other.
Divides self
by other.
Determines whether self
equals other or not.
Determines whether self
equals other or not.
Determines whether self
equals other or not.
Returns the absolute value of this complex number in a number form, using the Pythagorean theorem.
Returns the square of absolute value in a number form.
Returns the conjugate of self
.
Calculates the exp of self
.
Returns the imaginary part.
Writes this complex object to an io, surrounded by parentheses.
Returns the inverse of self
.
Calculates the log of self
.
Calculates the log10 of self
.
Calculates the log2 of self
.
Returns the phase of self
.
Returns the real part.
Rounds to the nearest digits.
Complex#sqrt
was inspired by the following blog post of Pavel Panchekha on floating point precision.
Returns self
.
See #to_f64
.
Returns the value as a Float32
if possible (the imaginary part should be exactly zero), raises otherwise.
Returns the value as a Float64
if possible (the imaginary part should be exactly zero), raises otherwise.
See #to_i32
.
Returns the value as an Int64
if possible (the imaginary part should be exactly zero), raises otherwise.
Writes this complex object to an io.
Returns the value as an UInt64
if possible (the imaginary part should be exactly zero), raises otherwise.
Struct
Value
Object
Object
Returns absolute value of self
.
Returns the opposite of self
.
Determines whether self
equals other or not.
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
Returns the square of absolute value in a number form.
require "complex" Complex.new(42, 2).abs2 # => 1768
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
Calculates the exp of self
.
require "complex" Complex.new(4, 2).exp # => -22.720847417619233 + 49.645957334580565i
Writes this complex object to an io, surrounded by parentheses.
require "complex" Complex.new(42, 2).inspect # => "(42.0 + 2.0i)"
Returns the inverse of self
.
Calculates the log of self
.
Calculates the log10 of self
.
Calculates the log2 of self
.
Returns the phase of self
.
Rounds to the nearest digits.
Complex#sqrt
was inspired by the following blog post of Pavel Panchekha on floating point precision.
Returns self
.
Returns the value as a Float32
if possible (the imaginary part should be exactly zero), raises otherwise.
Returns the value as a Float64
if possible (the imaginary part should be exactly zero), raises otherwise.
Returns the value as an Int64
if possible (the imaginary part should be exactly zero), raises otherwise.
Writes this complex object to an io.
require "complex" Complex.new(42, 2).to_s # => "42.0 + 2.0i"
Returns the value as an UInt64
if possible (the imaginary part should be exactly zero), raises otherwise.
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/Complex.html