Module: NVector

Defined in:
lib/nmatrix/shortcuts.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.bindgen(n) ⇒ Object

call-seq:

bindgen(n) -> NVector

Returns a byte NVector. Equivalent to seq(n, :byte).

  • Arguments :

    • n -> Size of the sequence.

  • Returns :

    • NVector filled with n integers of dtype :byte.



625
626
627
# File 'lib/nmatrix/shortcuts.rb', line 625

def bindgen(n)
  NVector.seq(n, :byte)
end

.cindgen(n) ⇒ Object

call-seq:

cindgen(n) -> NVector

Returns a complex NVector. Equivalent to seq(n, :complex64).

  • Arguments :

    • n -> Size of the sequence.

  • Returns :

    • NVector filled with n integers of dtype :complex64.



640
641
642
# File 'lib/nmatrix/shortcuts.rb', line 640

def cindgen(n)
  NVector.seq(n, :complex64)
end

.findgen(n) ⇒ Object

call-seq:

findgen(n) -> NVector

Returns a float NVector. Equivalent to seq(n, :float32).

  • Arguments :

    • n -> Size of the sequence.

  • Returns :

    • NVector filled with n integers of dtype :float32.



610
611
612
# File 'lib/nmatrix/shortcuts.rb', line 610

def findgen(n)
  NVector.seq(n, :float32)
end

.indgen(n) ⇒ Object

call-seq:

indgen(n) -> NVector

Returns an integer NVector. Equivalent to seq(n, :int32).

  • Arguments :

    • n -> Size of the sequence.

  • Returns :

    • NVector filled with n integers of dtype :int32.



595
596
597
# File 'lib/nmatrix/shortcuts.rb', line 595

def indgen(n)
  NVector.seq(n, :int32)
end

.linspace(a, b, n = 100) ⇒ Object

call-seq:

linspace(a, b) -> NVector
linspace(a, b, n) -> NVector

Returns a NVector with n values of dtype :float64 equally spaced from a to b, inclusive.

See: www.mathworks.com/help/matlab/ref/linspace.html

  • Arguments :

    • a -> The first value in the sequence.

    • b -> The last value in the sequence.

    • n -> The number of elements. Default is 100.

  • Returns :

    • NVector with n :float64 values.

Example:

x = NVector.linspace(0, Math::PI, 1000)
x.pretty_print
  [0.0
  0.0031447373909807737
  0.006289474781961547
  ...
  3.135303178807831
  3.138447916198812
  3.141592653589793]
=> nil


673
674
675
676
677
678
679
680
681
682
683
# File 'lib/nmatrix/shortcuts.rb', line 673

def linspace(a, b, n = 100)
  # Formula: seq(n) * step + a

  # step = ((b - a) / (n - 1))
  step = (b - a) * (1.0 / (n - 1))

  # dtype = :float64 is used to prevent integer coercion.
  result = NVector.seq(n, :float64) * NMatrix.new([n,1], step, dtype: :float64)
  result += NMatrix.new([n,1], a, dtype: :float64)
  result
end

.logspace(a, b, n = 100) ⇒ Object

call-seq:

logspace(a, b) -> NVector
logspace(a, b, n) -> NVector

Returns a NVector with n values of dtype :float64 logarithmically spaced from 10^a to 10^b, inclusive.

See: www.mathworks.com/help/matlab/ref/logspace.html

  • Arguments :

    • a -> The first value in the sequence.

    • b -> The last value in the sequence.

    • n -> The number of elements. Default is 100.

  • Returns :

    • NVector with n :float64 values.

Example:

 x = NVector.logspace(0, Math::PI, 10)
 x.pretty_print
   [1.0
   2.2339109164570266
   4.990357982665873
   11.148015174505757
   24.903672795156997
   55.632586516975095
   124.27824233101062
   277.6265222213364
   620.1929186882427
   1385.4557313670107]
=> nil


717
718
719
720
721
722
723
# File 'lib/nmatrix/shortcuts.rb', line 717

def logspace(a, b, n = 100)
  # Formula: 10^a, 10^(a + step), ..., 10^b, where step = ((b-a) / (n-1)).

  result = NVector.linspace(a, b, n)
  result.each_stored_with_index { |element, i| result[i] = 10 ** element }
  result
end

.new(*args) ⇒ Object

call-seq:

new(shape) -> NVector
new(stype, shape) -> NVector
new(shape, init) -> NVector
new(:dense, shape, init) -> NVector
new(:list, shape, init) -> NVector
new(shape, init, dtype) -> NVector
new(stype, shape, init, dtype) -> NVector
new(stype, shape, dtype) -> NVector

Creates a new NVector. See also NMatrix#initialize for a more detailed explanation of the arguments.

  • Arguments :

    • stype -> (optional) Storage type of the vector (:list, :dense, :yale). Defaults to :dense.

    • shape -> Shape of the vector. Accepts [n,1], [1,n], or n, where n is a Fixnum.

    • init -> (optional) Yale: capacity; List: default value (0); Dense: initial value or values (uninitialized by default).

    • dtype -> (optional if init provided) Data type stored in the vector. For :dense and :list, can be inferred from init.

  • Returns : -



460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/nmatrix/shortcuts.rb', line 460

def new(*args)
  stype = args[0].is_a?(Symbol) ? args.shift : :dense
  shape = args[0].is_a?(Array) ? args.shift  : [1,args.shift]

  if shape.size != 2 || !shape.include?(1) || shape == [1,1]
    raise(ArgumentError, "shape must be a Fixnum or an Array of positive Fixnums where exactly one value is 1")
  end

  warn "NVector is deprecated and not guaranteed to work any longer"

  NMatrix.new(stype, shape, *args)
end

.ones(size, dtype = :float64) ⇒ Object

call-seq:

ones(size) -> NVector
ones(size, dtype) -> NVector

Creates a vector of ones with the dimensions supplied as parameters.

  • Arguments :

    • size -> Array (or integer for square matrix) specifying the dimensions.

    • dtype -> (optional) Default is :float64.

  • Returns :

    • NVector filled with ones.

Examples:

NVector.ones(2) # =>  1.0
                      1.0

NVector.ones(3, :int32) # =>  1
                              1
                              1


524
525
526
# File 'lib/nmatrix/shortcuts.rb', line 524

def ones(size, dtype = :float64)
  NMatrix.new([size,1], 1, dtype: dtype)
end

.random(size, opts = {}) ⇒ Object

call-seq:

random(size) -> NVector

Creates a vector with random numbers between 0 and 1 generated by Random::rand with the dimensions supplied as parameters.

  • Arguments :

    • size -> Array (or integer for square matrix) specifying the dimensions.

    • opts -> (optional) NMatrix#initialize options

  • Returns :

    • NVector filled with random numbers generated by the Random class.

Examples:

NVector.rand(2) # =>  0.4859439730644226
                      0.1783195585012436


546
547
548
549
550
551
552
553
# File 'lib/nmatrix/shortcuts.rb', line 546

def random(size, opts = {})
  rng = Random.new

  random_values = []
  size.times { |i| random_values << rng.rand }

  NMatrix.new([size,1], random_values, opts)
end

.seq(size, dtype = :int64) ⇒ Object

call-seq:

seq(n) -> NVector
seq(n, dtype) -> NVector

Creates a vector with a sequence of n integers starting at zero. You can choose other types based on the dtype parameter.

  • Arguments :

    • n -> Number of integers in the sequence.

    • dtype -> (optional) Default is :int64.

  • Returns :

    • NVector filled with n integers.

Examples:

NVector.seq(2) # =>  0
                     1

NVector.seq(3, :float32) # =>  0.0
                               1.0
                               2.0


578
579
580
581
582
# File 'lib/nmatrix/shortcuts.rb', line 578

def seq(size, dtype = :int64)
  values = (0 ... size).to_a

  NMatrix.new([size,1], values, dtype: dtype)
end

.zeros(size, dtype = :float64) ⇒ Object Also known as: zeroes

call-seq:

zeros(size) -> NMatrix
zeros(size, dtype) -> NMatrix

Creates a new vector of zeros with the dimensions supplied as parameters.

  • Arguments :

    • size -> Array (or integer for square matrix) specifying the dimensions.

    • dtype -> (optional) Default is :float64.

  • Returns :

    • NVector filled with zeros.

Examples:

NVector.zeros(2) # =>  0.0
                       0.0

NVector.zeros(3, :int32) # =>  0
                               0
                               0


496
497
498
# File 'lib/nmatrix/shortcuts.rb', line 496

def zeros(size, dtype = :float64)
  NMatrix.new([size,1], 0, dtype: dtype)
end