Class: Hornetseye::InternalComplex

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#imagObject

Returns the value of attribute imag.



43
44
45
# File 'lib/multiarray/complex.rb', line 43

def imag
  @imag
end

#realObject

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.

Parameters:

  • value (Object)

    The other Ruby object.

Returns:

  • (Boolean)

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

Parameters:

  • other (Object)

    Object to compare with.

Returns:

  • (Boolean)

    Returns boolean indicating whether objects are equal or not.



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

#absObject



88
89
90
# File 'lib/multiarray/complex.rb', line 88

def abs
  Math.hypot @real, @imag
end

#abs2Object



178
179
180
# File 'lib/multiarray/complex.rb', line 178

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

#argObject



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

#conjObject



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

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

#decomposeNode

Decompose complex number

This method decomposes the complex number into an array.

Returns:

  • (Node)

    Returns an array with the real and imaginary component as elements.



204
205
206
# File 'lib/multiarray/complex.rb', line 204

def decompose
  Hornetseye::Sequence[ @real, @imag ]
end

#inspectString

Return string with information about this object.

Returns:

  • (String)

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



52
53
54
# File 'lib/multiarray/complex.rb', line 52

def inspect
  "InternalComplex(#{@real.inspect},#{@imag.inspect})"
end

#nonzero?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/multiarray/complex.rb', line 174

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

#polarObject



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

Parameters:

  • value (Object)

    New value for this object.

Returns:



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

def store( value )
  @real, @imag = value.real, value.imag
end

#to_sString

Return string with information about this object.

Returns:

  • (String)

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



59
60
61
# File 'lib/multiarray/complex.rb', line 59

def to_s
  "InternalComplex(#{@real.to_s},#{@imag.to_s})"
end

#zero?Boolean

Returns:

  • (Boolean)


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

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