Class: Hornetseye::InternalComplex
Overview
When compiling operations this class replaces Complex
Instance Attribute Summary collapse
-
#imag ⇒ GCCValue, Numeric
Get imaginary component of complex number.
-
#real ⇒ GCCValue, Numeric
Get real component of complex number.
Class Method Summary collapse
-
.generic?(value) ⇒ Boolean
Check compatibility of other type.
-
.polar(r, theta) ⇒ InternalComplex
Construct complex number using polar coordinates.
Instance Method Summary collapse
-
#*(other) ⇒ InternalComplex
Multiply complex values.
-
#**(other) ⇒ InternalComplex
Complex exponentiation.
-
#+(other) ⇒ InternalComplex
Add complex values.
-
#+@ ⇒ InternalComplex
This operation has no effect.
-
#-(other) ⇒ InternalComplex
Subtract complex values.
-
#-@ ⇒ InternalComplex
Negate complex value.
-
#/(other) ⇒ InternalComplex
Divide complex values.
-
#==(other) ⇒ Boolean
Test on equality.
-
#abs ⇒ InternalComplex
Compute complex modulus.
-
#abs2 ⇒ Numeric, GCCValue
Compute square of complex modulus.
-
#arg ⇒ InternalComplex
Compute complex argument.
-
#assign(value) ⇒ Object
Store other value in this object.
-
#coerce(other) ⇒ Array<InternalComplex>
Coerce with other object.
-
#conj ⇒ InternalComplex
Compute complex conjugate.
-
#decompose(i) ⇒ Numeric, GCCValue
Decompose complex number.
-
#initialize(real, imag) ⇒ InternalComplex
constructor
Constructor for complex number.
-
#inspect ⇒ String
Return string with information about this object.
-
#nonzero? ⇒ Boolean, GCCValue
Check whether value is not equal to zero.
-
#polar ⇒ Array<Object>
Compute polar coordinates.
-
#to_s ⇒ String
Return string with information about this object.
-
#zero? ⇒ Boolean, GCCValue
Check whether value is equal to zero.
Constructor Details
#initialize(real, imag) ⇒ InternalComplex
Constructor for complex number
84 85 86 |
# File 'lib/multiarray/complex.rb', line 84 def initialize( real, imag ) @real, @imag = real, imag end |
Instance Attribute Details
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.
46 47 48 |
# File 'lib/multiarray/complex.rb', line 46 def generic?(value) value.is_a?(Numeric) or value.is_a?(GCCValue) end |
.polar(r, theta) ⇒ InternalComplex
Construct complex number using polar coordinates
58 59 60 |
# File 'lib/multiarray/complex.rb', line 58 def polar( r, theta ) new r * Math.cos(theta), r * Math.sin(theta) end |
Instance Method Details
#*(other) ⇒ InternalComplex
Multiply complex values
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/multiarray/complex.rb', line 227 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) ⇒ InternalComplex
Complex exponentiation
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/multiarray/complex.rb', line 265 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) ⇒ InternalComplex
Add complex values
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/multiarray/complex.rb', line 191 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 |
#+@ ⇒ InternalComplex
This operation has no effect
171 172 173 |
# File 'lib/multiarray/complex.rb', line 171 def +@ self end |
#-(other) ⇒ InternalComplex
Subtract complex values
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/multiarray/complex.rb', line 209 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 |
#-@ ⇒ InternalComplex
Negate complex value
180 181 182 |
# File 'lib/multiarray/complex.rb', line 180 def -@ InternalComplex.new -@real, -@imag end |
#/(other) ⇒ InternalComplex
Divide complex values
246 247 248 249 250 251 252 253 254 255 |
# File 'lib/multiarray/complex.rb', line 246 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
317 318 319 320 321 322 323 324 325 |
# File 'lib/multiarray/complex.rb', line 317 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 ⇒ InternalComplex
Compute complex modulus
144 145 146 |
# File 'lib/multiarray/complex.rb', line 144 def abs Math.hypot @real, @imag end |
#abs2 ⇒ Numeric, GCCValue
Compute square of complex modulus
305 306 307 |
# File 'lib/multiarray/complex.rb', line 305 def abs2 @real * @real + @imag * @imag end |
#arg ⇒ InternalComplex
Compute complex argument
153 154 155 |
# File 'lib/multiarray/complex.rb', line 153 def arg Math.atan2 @imag, @real end |
#assign(value) ⇒ Object
Store other value in this object
109 110 111 |
# File 'lib/multiarray/complex.rb', line 109 def assign(value) @real, @imag = value.real, value.imag end |
#coerce(other) ⇒ Array<InternalComplex>
Coerce with other object
120 121 122 123 124 125 126 127 128 |
# File 'lib/multiarray/complex.rb', line 120 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 ⇒ InternalComplex
Compute complex conjugate
135 136 137 |
# File 'lib/multiarray/complex.rb', line 135 def conj InternalComplex.new @real, -@imag end |
#decompose(i) ⇒ Numeric, GCCValue
Decompose complex number
This method decomposes the complex number.
334 335 336 |
# File 'lib/multiarray/complex.rb', line 334 def decompose(i) [@real, @imag][i] end |
#inspect ⇒ String
Return string with information about this object
91 92 93 |
# File 'lib/multiarray/complex.rb', line 91 def inspect "InternalComplex(#{@real.inspect},#{@imag.inspect})" end |
#nonzero? ⇒ Boolean, GCCValue
Check whether value is not equal to zero
296 297 298 |
# File 'lib/multiarray/complex.rb', line 296 def nonzero? @real.nonzero?.or @imag.nonzero? end |
#polar ⇒ Array<Object>
Compute polar coordinates
162 163 164 |
# File 'lib/multiarray/complex.rb', line 162 def polar return abs, arg end |
#to_s ⇒ String
Return string with information about this object
98 99 100 |
# File 'lib/multiarray/complex.rb', line 98 def to_s "InternalComplex(#{@real.to_s},#{@imag.to_s})" end |
#zero? ⇒ Boolean, GCCValue
Check whether value is equal to zero
287 288 289 |
# File 'lib/multiarray/complex.rb', line 287 def zero? @real.zero?.and @imag.zero? end |