Class: CooCoo::NMatrix::Vector

Inherits:
Math::AbstractVector show all
Defined in:
lib/coo-coo/math.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Math::AbstractVector

#collect_equal?, #collect_infinite?, #collect_nan?, #collect_not_equal?, #infinite?, #max, #min, #minmax, #minmax_normalize, #nan?, rand, #set2d!, #slice_2d, #zero

Constructor Details

#initialize(length, initial_value = 0.0, &block) ⇒ Vector

Returns a new instance of Vector.



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/coo-coo/math.rb', line 319

def initialize(length, initial_value = 0.0, &block)
  if length != nil
    if length <= 0
      raise ArgumentError.new("size must be larger than zero")
    end
    @elements = ::NMatrix.new([ 1, length ], initial_value)
    if block
      @elements.size.times do |i|
        @elements[i] = block.call(i)
      end
    end
  end
end

Class Method Details

.[](value, max_size = nil, default_value = 0.0) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/coo-coo/math.rb', line 333

def self.[](value, max_size = nil, default_value = 0.0)
  if value.kind_of?(::NMatrix)
    v = new(nil)
    v.instance_variable_set('@elements', value)
    v
  elsif value.respond_to?(:[])
    v = new(max_size || value.size, default_value) do |i|
      value[i] || default_value
    end
  else
    v = new(max_size || value.size, default_value) do |i|
      begin
        value.next || default_value
      rescue StopIteration
        default_value
      end
    end
  end
end

._load(args) ⇒ Object



381
382
383
384
# File 'lib/coo-coo/math.rb', line 381

def self._load(args)
  arr = args.unpack('E*')
  self[arr]
end

.ones(length) ⇒ Object



357
358
359
# File 'lib/coo-coo/math.rb', line 357

def self.ones(length)
  self[::NMatrix.ones([1, length])]
end

.zeros(length) ⇒ Object



353
354
355
# File 'lib/coo-coo/math.rb', line 353

def self.zeros(length)
  self[::NMatrix.zeros([1, length])]
end

Instance Method Details

#*(other) ⇒ Object



525
526
527
528
529
530
531
532
533
# File 'lib/coo-coo/math.rb', line 525

def *(other)
  if other.kind_of?(self.class)
    self.class[@elements * other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements * other]
  else
    self * self.class[other]
  end
end

#**(other) ⇒ Object



535
536
537
538
539
540
541
542
543
# File 'lib/coo-coo/math.rb', line 535

def **(other)
  if other.kind_of?(self.class)
    self.class[@elements ** other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements ** other]
  else
    self ** self.class[other]
  end
end

#+(other) ⇒ Object



493
494
495
496
497
498
499
500
501
# File 'lib/coo-coo/math.rb', line 493

def +(other)
  if other.kind_of?(self.class)
    self.class[@elements + other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements + other]
  else
    self + self.class[other]
  end
end

#-(other) ⇒ Object



507
508
509
510
511
512
513
514
515
# File 'lib/coo-coo/math.rb', line 507

def -(other)
  if other.kind_of?(self.class)
    self.class[@elements - other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements - other]
  else
    self - self.class[other]
  end
end

#-@Object



503
504
505
# File 'lib/coo-coo/math.rb', line 503

def -@
  self * -1.0
end

#/(other) ⇒ Object



545
546
547
548
549
550
551
552
553
# File 'lib/coo-coo/math.rb', line 545

def /(other)
  if other.kind_of?(self.class)
    self.class[@elements / other.elements]
  elsif other.kind_of?(Numeric)
    self.class[@elements / other]
  else
    self / self.class[other]
  end
end

#==(other) ⇒ Object



555
556
557
558
559
560
561
562
563
564
# File 'lib/coo-coo/math.rb', line 555

def ==(other)
  if other.kind_of?(self.class)
    size == other.size && @elements == other.elements
  elsif other != nil
    a, b = coerce(other)
    a == b
  else
    false
  end
end

#[](i, len = nil) ⇒ Object

Raises:

  • (RangeError)


386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/coo-coo/math.rb', line 386

def [](i, len = nil)
  i = size + i if i < 0
  raise RangeError.new if i >= size || i < 0

  if len
    len = (size - i) if (i + len) >= size
    raise ArgumentError.new("length must be > 0") if len <= 0
  end
  
  v = @elements[0, (i...(i + (len || 1))) ]
  
  if len
    self.class[v]
  else
    v[0]
  end
end

#[]=(i, l, v = nil) ⇒ Object

Raises:

  • (RangeError)


404
405
406
407
408
409
410
411
412
413
414
# File 'lib/coo-coo/math.rb', line 404

def []=(i, l, v = nil)
  i = size + i if i < 0
  raise RangeError.new if i >= size || i < 0

  if v
    @elements[i, l] = v
  else
    @elements[i] = l
  end
  # @elements[i] = v
end

#_dump(depth) ⇒ Object



377
378
379
# File 'lib/coo-coo/math.rb', line 377

def _dump(depth)
  @elements.to_a.pack('E*')
end

#append(other) ⇒ Object



427
428
429
430
431
432
433
# File 'lib/coo-coo/math.rb', line 427

def append(other)
  if other.kind_of?(self.class)
    self.class[@elements.concat(other.elements)]
  else
    append(self.class[other])
  end
end

#coerce(other) ⇒ Object



361
362
363
364
365
366
367
# File 'lib/coo-coo/math.rb', line 361

def coerce(other)
  if other.respond_to?(:each)
    return self.class[other], self
  else
    return self.class.new(self.size, other), self
 end
end

#dot(width, height, other, owidth, oheight) ⇒ Object



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/coo-coo/math.rb', line 475

def dot(width, height, other, owidth, oheight)
  owidth ||= width
  oheight ||= height
  
  if other.kind_of?(self.class)
    raise ArgumentError.new("invalid size") if other.size != owidth * oheight
    raise ArgumentError.new("invalid size") if size != width * height

    product = @elements.reshape([ height, width ]).
      dot(other.elements.reshape([ oheight, owidth ]))
    
    self.class[product.
               reshape([1, height * owidth ])]
  else
    self.dot(width, height, self.class[other], owidth, oheight)
  end
end

#each(&block) ⇒ Object



435
436
437
# File 'lib/coo-coo/math.rb', line 435

def each(&block)
  @elements.each(&block)
end

#each_slice(n, &block) ⇒ Object



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/coo-coo/math.rb', line 443

def each_slice(n, &block)
  if block
    last_slice = (size / n.to_f).ceil.to_i
    
    @elements.each_slice(n).with_index do |slice, i|
      if i == last_slice - 1
        slice = slice + Array.new(n - slice.size)
      end
      
      block.call(self.class[slice])
    end
  else
    to_enum(__method__, n)
  end
end

#each_with_index(&block) ⇒ Object



439
440
441
# File 'lib/coo-coo/math.rb', line 439

def each_with_index(&block)
  @elements.each_with_index(&block)
end

#lengthObject



521
522
523
# File 'lib/coo-coo/math.rb', line 521

def length
  @elements.shape[1]
end

#magnitudeObject



467
468
469
# File 'lib/coo-coo/math.rb', line 467

def magnitude
  magnitude_squared.sqrt
end

#magnitude_squaredObject



463
464
465
# File 'lib/coo-coo/math.rb', line 463

def magnitude_squared
  (self * self).sum
end

#normalizeObject



471
472
473
# File 'lib/coo-coo/math.rb', line 471

def normalize
  self / magnitude
end

#set(values) ⇒ Object



416
417
418
419
420
421
422
423
424
425
# File 'lib/coo-coo/math.rb', line 416

def set(values)
  values = [ values ].each.cycle(size) if values.kind_of?(Numeric)
  
  values.each_with_index do |v, i|
    break if i >= @elements.size
    @elements[i] = v
  end

  self
end

#sizeObject



517
518
519
# File 'lib/coo-coo/math.rb', line 517

def size
  length
end

#sumObject



459
460
461
# File 'lib/coo-coo/math.rb', line 459

def sum
  @elements.each.sum
end

#to_aObject



369
370
371
# File 'lib/coo-coo/math.rb', line 369

def to_a
  @elements.to_a
end

#to_sObject



373
374
375
# File 'lib/coo-coo/math.rb', line 373

def to_s
  "[" + to_a.join(", ") + "]"
end