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.



1045
1046
1047
# File 'lib/nmatrix/shortcuts.rb', line 1045

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.



1060
1061
1062
# File 'lib/nmatrix/shortcuts.rb', line 1060

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.



1030
1031
1032
# File 'lib/nmatrix/shortcuts.rb', line 1030

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.



1015
1016
1017
# File 'lib/nmatrix/shortcuts.rb', line 1015

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


1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'lib/nmatrix/shortcuts.rb', line 1093

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


1137
1138
1139
1140
1141
1142
1143
# File 'lib/nmatrix/shortcuts.rb', line 1137

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 : -



880
881
882
883
884
885
886
887
888
889
890
891
# File 'lib/nmatrix/shortcuts.rb', line 880

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


944
945
946
# File 'lib/nmatrix/shortcuts.rb', line 944

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


966
967
968
969
970
971
972
973
# File 'lib/nmatrix/shortcuts.rb', line 966

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


998
999
1000
1001
1002
# File 'lib/nmatrix/shortcuts.rb', line 998

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


916
917
918
# File 'lib/nmatrix/shortcuts.rb', line 916

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