Class: Hornetseye::InternalComplex

Inherits:
Object
  • Object
show all
Defined in:
lib/multiarray/complex.rb

Overview

When compiling operations this class replaces Complex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(real, imag) ⇒ InternalComplex

Constructor for complex number

Parameters:



84
85
86
# File 'lib/multiarray/complex.rb', line 84

def initialize( real, imag )
  @real, @imag = real, imag
end

Instance Attribute Details

#imagGCCValue, Numeric

Get imaginary component of complex number

Returns:



76
77
78
# File 'lib/multiarray/complex.rb', line 76

def imag
  @imag
end

#realGCCValue, Numeric

Get real component of complex number

Returns:



69
70
71
# File 'lib/multiarray/complex.rb', line 69

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.

Parameters:

  • value (Object)

    The other Ruby object.

Returns:

  • (Boolean)

    Returns false if Ruby object requires 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

Parameters:

Returns:



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

Parameters:

  • Second (Object)

    operand for binary operation.

Returns:



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

Parameters:

  • Second (Object)

    operand for binary operation.

Returns:



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

Parameters:

  • Second (Object)

    operand for binary operation.

Returns:



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

Returns:



171
172
173
# File 'lib/multiarray/complex.rb', line 171

def +@
  self
end

#-(other) ⇒ InternalComplex

Subtract complex values

Parameters:

  • Second (Object)

    operand for binary operation.

Returns:



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

Returns:



180
181
182
# File 'lib/multiarray/complex.rb', line 180

def -@
  InternalComplex.new -@real, -@imag
end

#/(other) ⇒ InternalComplex

Divide complex values

Parameters:

  • Second (Object)

    operand for binary operation.

Returns:



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

Parameters:

  • other (Object)

    Object to compare with.

Returns:

  • (Boolean)

    Returns boolean indicating whether objects are equal or not.



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

#absInternalComplex

Compute complex modulus

Returns:



144
145
146
# File 'lib/multiarray/complex.rb', line 144

def abs
  Math.hypot @real, @imag
end

#abs2Numeric, GCCValue

Compute square of complex modulus

Returns:



305
306
307
# File 'lib/multiarray/complex.rb', line 305

def abs2
  @real * @real + @imag * @imag
end

#argInternalComplex

Compute complex argument

Returns:



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

Parameters:

  • value (Object)

    New value for this object.

Returns:



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

Parameters:

Returns:



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

#conjInternalComplex

Compute complex conjugate

Returns:



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.

Returns:



334
335
336
# File 'lib/multiarray/complex.rb', line 334

def decompose(i)
  [@real, @imag][i]
end

#inspectString

Return string with information about this object

Returns:

  • (String)

    Returns a string (e.g. “InternalComplex(1,2)”).



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

Returns:



296
297
298
# File 'lib/multiarray/complex.rb', line 296

def nonzero?
  @real.nonzero?.or @imag.nonzero?
end

#polarArray<Object>

Compute polar coordinates

Returns:

  • (Array<Object>)

    Returns complex modulus and argument.



162
163
164
# File 'lib/multiarray/complex.rb', line 162

def polar
  return abs, arg
end

#to_sString

Return string with information about this object

Returns:

  • (String)

    Returns a string (e.g. “InternalComplex(1,2)”).



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

Returns:



287
288
289
# File 'lib/multiarray/complex.rb', line 287

def zero?
  @real.zero?.and @imag.zero?
end