module NMatrixExtras
Public Class Methods
# File lib/nmatrix_extras/nmatrix.rb, line 27 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
Successively yields submatrices at each coordinate along a specified dimension. Each submatrix will have the same number of dimensions as the matrix being iterated, but with the specified dimension's size equal to 1.
@param [Integer] dim the dimension being iterated over.
# File lib/nmatrix_extras/nmatrix.rb, line 37 def each_along_dim(dim=0) dims = shape shape.each_index { |i| dims[i] = 0...(shape[i]) unless i == dim } 0.upto(shape[dim]-1) do |i| dims[dim] = i yield self[*dims] end end
See Enumerable#map
# File lib/nmatrix_extras/nmatrix.rb, line 169 def map(&bl) cp = self.dup cp.map! &bl cp end
Maps in place. See map
# File lib/nmatrix_extras/nmatrix.rb, line 179 def map! self.each_stored_with_indices do |e, *i| self[*i] = (yield e) end self end
Calculates the maximum along the specified dimension.
@see reduce_along_dim
# File lib/nmatrix_extras/nmatrix.rb, line 128 def max(dim=0) reduce_along_dim(dim, -1.0*Float::MAX) do |max, sub_mat| max * (max >= sub_mat) + ((max)*0.0 + (max < sub_mat)) * sub_mat end end
Calculates the mean along the specified dimension.
@see reduce_along_dim
# File lib/nmatrix_extras/nmatrix.rb, line 95 def mean(dim=0) reduce_along_dim(dim, 0.0) do |mean, sub_mat| mean + sub_mat/shape[dim] end end
Calculates the minimum along the specified dimension.
@see reduce_along_dim
# File lib/nmatrix_extras/nmatrix.rb, line 117 def min(dim=0) reduce_along_dim(dim, Float::MAX) do |min, sub_mat| min * (min <= sub_mat) + ((min)*0.0 + (min > sub_mat)) * sub_mat end end
Reduces an NMatrix
using a supplied block over a specified dimension. The block should behave the same way as for Enumerable#reduce.
@param [Integer] dim the dimension being reduced @param [Numeric] initial the initial value for the reduction (i.e. the usual parameter to Enumerable#reduce). Supply nil or do not supply
this argument to have it follow the usual Enumerable#reduce behavior of using the first element as the initial value.
@return [NMatrix] an NMatrix
with the same number of dimensions as the input, but with the input dimension now having size 1.
Each element is the result of the reduction at that position along the specified dimension.
# File lib/nmatrix_extras/nmatrix.rb, line 55 def reduce_along_dim(dim=0, initial=nil, &bl) if dim > shape.size then raise ArgumentError, "Requested dimension does not exist. Requested: #{dim}, shape: #{shape}" end new_shape = shape new_shape[dim] = 1 first_as_acc = false if initial then acc = NMatrix.new(new_shape, initial) else each_along_dim(dim) do |sub_mat| acc = sub_mat break end first_as_acc = true end each_along_dim(dim) do |sub_mat| if first_as_acc then first_as_acc = false next end acc = bl.call(acc, sub_mat) end acc end
Calculates the sample standard deviation along the specified dimension.
@see reduce_along_dim
# File lib/nmatrix_extras/nmatrix.rb, line 152 def std(dim=0) variance(dim).map! { |e| Math.sqrt(e) } end
Calculates the sum along the specified dimension.
@see reduce_along_dim
# File lib/nmatrix_extras/nmatrix.rb, line 105 def sum(dim=0) reduce_along_dim(dim, 0.0) do |sum, sub_mat| sum + sub_mat end end
Converts an nmatrix with a single element (but any number of dimensions) to a float.
Raises an IndexError if the matrix does not have just a single element.
# File lib/nmatrix_extras/nmatrix.rb, line 161 def to_f raise IndexError, 'to_f only valid for matrices with a single element' unless shape.all? { |e| e == 1 } self[*Array.new(shape.size, 0)] end
Calculates the sample variance along the specified dimension.
@see reduce_along_dim
# File lib/nmatrix_extras/nmatrix.rb, line 140 def variance(dim=0) m = mean(dim) reduce_along_dim(dim, 0.0) do |var, sub_mat| var + (m - sub_mat)*(m - sub_mat)/(shape[dim]-1) end end