Complex

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

Class Method Summary

Instance Method Summary

Class Method Detail

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

def self.zero : ComplexSource

Returns the number 0 in complex form.

Instance Method Detail

def *(other : Complex)Source

Multiplies self by other.

def *(other : Number)Source

Multiplies self by other.

def +(other : Number)Source

Adds the value of self to other.

def +(other : Complex)Source

Adds the value of self to other.

def -Source

Returns the opposite of self.

def -(other : Number)Source

Removes the value from other to self.

def -(other : Complex)Source

Removes the value from other to self.

def /(other : Number)Source

Divides self by other.

def /(other : Complex)Source

Divides self by other.

def ==(other : Complex)Source

Determines whether self equals other or not.

def ==(other : Number)Source

Determines whether self equals other or not.

def ==(other)Source

Determines whether self equals other or not.

def absSource

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

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

def abs2Source

Returns the square of absolute value in a number form.

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

def cloneSource

def conjSource

Returns the conjugate of self.

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

def expSource

Calculates the exp of self.

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

def imag : Float64Source

Returns the image part of self.

def inspect(io : IO)Source

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

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

def invSource

Returns the inverse of self.

def logSource

Calculates the log of self.

def log10Source

Calculates the log10 of self.

def log2Source

Calculates the log2 of self.

def phaseSource

Returns the phase of self.

def polarSource

Returns a tuple with the abs value and the phase.

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

def real : Float64Source

Returns the real part of self.

def signSource

def sqrtSource

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

def to_s(io : IO)Source

Write this complex object to an io.

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

© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/Complex.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部