Class: Hornetseye::InternalComplex
Instance Attribute Summary collapse
-
#imag ⇒ Object
Returns the value of attribute imag.
-
#real ⇒ Object
Returns the value of attribute real.
Class Method Summary collapse
-
.generic?(value) ⇒ Boolean
Check compatibility of other type.
- .polar(r, theta) ⇒ Object
Instance Method Summary collapse
- #*(other) ⇒ Object
- #**(other) ⇒ Object
- #+(other) ⇒ Object
- #+@ ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
-
#==(other) ⇒ Boolean
Test on equality.
- #abs ⇒ Object
- #abs2 ⇒ Object
- #arg ⇒ Object
- #coerce(other) ⇒ Object
- #conj ⇒ Object
-
#decompose ⇒ Node
Decompose complex number.
-
#initialize(real, imag) ⇒ InternalComplex
constructor
A new instance of InternalComplex.
-
#inspect ⇒ String
Return string with information about this object.
- #nonzero? ⇒ Boolean
- #polar ⇒ Object
-
#store(value) ⇒ Object
Store other value in this object.
-
#to_s ⇒ String
Return string with information about this object.
- #zero? ⇒ Boolean
Constructor Details
#initialize(real, imag) ⇒ InternalComplex
Returns a new instance of InternalComplex.
45 46 47 |
# File 'lib/multiarray/complex.rb', line 45 def initialize( real, imag ) @real, @imag = real, imag end |
Instance Attribute Details
#imag ⇒ Object
Returns the value of attribute imag.
43 44 45 |
# File 'lib/multiarray/complex.rb', line 43 def imag @imag end |
#real ⇒ Object
Returns the value of attribute real.
43 44 45 |
# File 'lib/multiarray/complex.rb', line 43 def real @real end |
Class Method Details
.generic?(value) ⇒ Boolean
Check compatibility of other type.
This method checks whether binary operations with the other Ruby object can be performed without requiring coercion.
33 34 35 |
# File 'lib/multiarray/complex.rb', line 33 def generic?( value ) value.is_a?( Numeric ) or value.is_a?( GCCValue ) end |
.polar(r, theta) ⇒ Object
37 38 39 |
# File 'lib/multiarray/complex.rb', line 37 def polar( r, theta ) new r * Math.cos( theta ), r * Math.sin( theta ) end |
Instance Method Details
#*(other) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/multiarray/complex.rb', line 130 def *( other ) if other.is_a?( InternalComplex ) or other.is_a?( Complex ) InternalComplex.new @real * other.real - @imag * other.imag, @real * other.imag + @imag * other.real elsif InternalComplex.generic? other InternalComplex.new @real * other, @imag * other else x, y = other.coerce self x * y end end |
#**(other) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/multiarray/complex.rb', line 153 def **( other ) if other.is_a?( InternalComplex ) or other.is_a?( Complex ) r, theta = polar ore = other.real oim = other.imag nr = Math.exp ore * Math.log( r ) - oim * theta ntheta = theta * ore + oim * Math.log( r ) InternalComplex.polar nr, ntheta elsif InternalComplex.generic? other r, theta = polar InternalComplex.polar r ** other, theta * other else x, y = other.coerce self x ** y end end |
#+(other) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/multiarray/complex.rb', line 108 def +( other ) if other.is_a?( InternalComplex ) or other.is_a?( Complex ) InternalComplex.new @real + other.real, @imag + other.imag elsif InternalComplex.generic? other InternalComplex.new @real + other, @imag else x, y = other.coerce self x + y end end |
#+@ ⇒ Object
100 101 102 |
# File 'lib/multiarray/complex.rb', line 100 def +@ self end |
#-(other) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/multiarray/complex.rb', line 119 def -( other ) if other.is_a?( InternalComplex ) or other.is_a?( Complex ) InternalComplex.new @real - other.real, @imag - other.imag elsif InternalComplex.generic? other InternalComplex.new @real - other, @imag else x, y = other.coerce self x - y end end |
#-@ ⇒ Object
104 105 106 |
# File 'lib/multiarray/complex.rb', line 104 def -@ InternalComplex.new -@real, -@imag end |
#/(other) ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/multiarray/complex.rb', line 142 def /( other ) if other.is_a?( InternalComplex ) or other.is_a?( Complex ) self * other.conj / other.abs2 elsif InternalComplex.generic? other InternalComplex.new @real / other, @imag / other else x, y = other.coerce self x / y end end |
#==(other) ⇒ Boolean
Test on equality
188 189 190 191 192 193 194 195 196 |
# File 'lib/multiarray/complex.rb', line 188 def ==( other ) if other.is_a?( InternalComplex ) or other.is_a?( Complex ) @real.eq( other.real ).and( @imag.eq( other.imag ) ) elsif InternalComplex.generic? other @real.eq( other ).and( @imag.eq( 0 ) ) else false end end |
#abs ⇒ Object
88 89 90 |
# File 'lib/multiarray/complex.rb', line 88 def abs Math.hypot @real, @imag end |
#abs2 ⇒ Object
178 179 180 |
# File 'lib/multiarray/complex.rb', line 178 def abs2 @real * @real + @imag * @imag end |
#arg ⇒ Object
92 93 94 |
# File 'lib/multiarray/complex.rb', line 92 def arg Math.atan2 @imag, @real end |
#coerce(other) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/multiarray/complex.rb', line 74 def coerce( other ) if other.is_a? InternalComplex return other, self elsif other.is_a? Complex return InternalComplex.new( other.real, other.imag ), self else return InternalComplex.new( other, 0 ), self end end |
#conj ⇒ Object
84 85 86 |
# File 'lib/multiarray/complex.rb', line 84 def conj InternalComplex.new @real, -@imag end |
#decompose ⇒ Node
Decompose complex number
This method decomposes the complex number into an array.
204 205 206 |
# File 'lib/multiarray/complex.rb', line 204 def decompose Hornetseye::Sequence[ @real, @imag ] end |
#inspect ⇒ String
Return string with information about this object.
52 53 54 |
# File 'lib/multiarray/complex.rb', line 52 def inspect "InternalComplex(#{@real.inspect},#{@imag.inspect})" end |
#nonzero? ⇒ Boolean
174 175 176 |
# File 'lib/multiarray/complex.rb', line 174 def nonzero? @real.nonzero?.or @imag.nonzero? end |
#polar ⇒ Object
96 97 98 |
# File 'lib/multiarray/complex.rb', line 96 def polar return abs, arg end |
#store(value) ⇒ Object
Store other value in this object
70 71 72 |
# File 'lib/multiarray/complex.rb', line 70 def store( value ) @real, @imag = value.real, value.imag end |
#to_s ⇒ String
Return string with information about this object.
59 60 61 |
# File 'lib/multiarray/complex.rb', line 59 def to_s "InternalComplex(#{@real.to_s},#{@imag.to_s})" end |
#zero? ⇒ Boolean
170 171 172 |
# File 'lib/multiarray/complex.rb', line 170 def zero? @real.zero?.and @imag.zero? end |