Method: NVector.logspace

Defined in:
lib/nmatrix/shortcuts.rb

.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