Class: NVector

Inherits:
NArray show all
Defined in:
lib/nmatrix.rb

Overview

—— NVector ——

Constant Summary collapse

CLASS_DIMENSION =
1

Instance Method Summary collapse

Methods inherited from NArray

#==, #all?, #any?, #complex?, #integer?, #mean, #median, #none?, #randomn, #rank_total, #rms, #rmsdev, #stddev

Instance Method Details

#*(other) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/nmatrix.rb', line 184

def *(other)
  case other
  when NMatrix
    NVector.mul_add( NArray.refer(self).newdim!(0), other, 1 )
  when NVector
    NArray.mul_add( NArray.refer(self), other, 0 ) # inner product
  when NArray
    if other.instance_of?(NArray)
	NVector.mul( NArray.refer(self), other.newdim(0) )
    else
	other.coerce_rev( self, :* )
    end
  when Numeric
    NVector.mul( NArray.refer(self), other )
  else
    raise TypeError,"Illegal operation: NVector * %s" % other.class
  end
end

#**(n) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/nmatrix.rb', line 222

def **(n)
  if n==2
    self*self
  else
    raise ArgumentError,"Only v**2 is implemented"
  end
end

#+(other) ⇒ Object

Raises:

  • (TypeError)


160
161
162
163
164
165
166
167
168
169
170
# File 'lib/nmatrix.rb', line 160

def +(other)
  case other
  when NVector
    return super(NArray.refer(other))
  when NArray
    unless other.instance_of?(NArray)
      return other.coerce_rev( self, :+ )
    end
  end
  raise TypeError,"Illegal operation: NVector + %s" % other.class
end

#-(other) ⇒ Object

Raises:

  • (TypeError)


172
173
174
175
176
177
178
179
180
181
182
# File 'lib/nmatrix.rb', line 172

def -(other)
  case other
  when NVector
    return super(NArray.refer(other))
  when NArray
    unless other.instance_of?(NArray)
      return other.coerce_rev( self, :- )
    end
  end
  raise TypeError,"Illegal operation: NVector - %s" % other.class
end

#/(other) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/nmatrix.rb', line 203

def /(other)
  case other
  when NMatrix
    other.lu.solve(self)
  when NVector
    raise TypeError,"Illegal operation: NVector / %s" % other.class
  when NArray
    if other.instance_of?(NArray)
	NVector.div( NArray.refer(self), other.newdim(0) )
    else
	other.coerce_rev( self, :/ )
    end
  when Numeric
    NVector.div( NArray.refer(self), other )
  else
    raise TypeError,"Illegal operation: NVector / %s" % other.class
  end
end

#coerce_rev(other, id) ⇒ Object

Raises:

  • (TypeError)


230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/nmatrix.rb', line 230

def coerce_rev(other,id)
  case id
  when :*
	if other.instance_of?(NArray)
 return NVector.mul( other.newdim(0), self )
	end
	if other.instance_of?(NArrayScalar)
 return NVector.mul( other, self )
	end
  end
  raise TypeError,"Illegal operation: %s %s NVector" %
    [other.class, id.id2name]
end