lib/nmatrix/shortcuts.rb in nmatrix-0.0.6 vs lib/nmatrix/shortcuts.rb in nmatrix-0.0.7

- old
+ new

@@ -32,13 +32,82 @@ # There are some questions to be answered before this can be done, tho. #++ class NMatrix + + + # + # call-seq: + # dense? -> true or false + # list? -> true or false + # yale? -> true or false + # + # Shortcut functions for quickly determining a matrix's stype. + # + def dense?; return stype == :dense; end + def yale?; return stype == :yale; end + def list?; return stype == :list; end + + class << self # # call-seq: + # NMatrix[array-of-arrays, dtype = nil] + # + # You can use the old +N+ constant in this way: + # N = NMatrix + # N[1, 2, 3] + # NMatrix needs to have a succinct way to create a matrix by specifying the + # components directly. This is very useful for using it as an advanced + # calculator, it is useful for learning how to use, for testing language + # features and for developing algorithms. + # + # The NMatrix::[] method provides a way to create a matrix in a way that is compact and + # natural. The components are specified using Ruby array syntax. Optionally, + # one can specify a dtype as the last parameter (default is :float64). + # + # Examples: + # + # a = NMatrix[ 1,2,3,4 ] => 1.0 2.0 3.0 4.0 + # + # a = NMatrix[ 1,2,3,4, :int32 ] => 1 2 3 4 + # + # a = NMatrix[ [1,2,3], [3,4,5] ] => 1.0 2.0 3.0 + # 3.0 4.0 5.0 + # + # SYNTAX COMPARISON: + # + # MATLAB: a = [ [1 2 3] ; [4 5 6] ] or [ 1 2 3 ; 4 5 6 ] + # IDL: a = [ [1,2,3] , [4,5,6] ] + # NumPy: a = array( [1,2,3], [4,5,6] ) + # + # SciRuby: a = NMatrix[ [1,2,3], [4,5,6] ] + # Ruby array: a = [ [1,2,3], [4,5,6] ] + # + def [](*params) + dtype = params.last.is_a?(Symbol) ? params.pop : nil + + # First find the dimensions of the array. + i = 0 + shape = [] + row = params + while row.is_a?(Array) + shape[i] = row.length + row = row[0] + i += 1 + end + + # A row vector should be stored as 1xN, not N + #shape.unshift(1) if shape.size == 1 + + # Then flatten the array. + NMatrix.new(shape, params.flatten, dtype) + end + + # + # call-seq: # zeros(size) -> NMatrix # zeros(size, dtype) -> NMatrix # zeros(stype, size, dtype) -> NMatrix # # Creates a new matrix of zeros with the dimensions supplied as @@ -95,12 +164,40 @@ dim = params.first NMatrix.new(dim, 1, dtype) end + ## + # call-seq: + # ones_like(nm) -> NMatrix # + # Creates a new matrix of ones with the same dtype and shape as the + # provided matrix. + # + # @param [NMatrix] nm the nmatrix whose dtype and shape will be used + # @return [NMatrix] a new nmatrix filled with ones. + # + def ones_like(nm) + NMatrix.ones(nm.shape, nm.dtype) + end + + ## # call-seq: + # zeros_like(nm) -> NMatrix + # + # Creates a new matrix of zeros with the same stype, dtype, and shape + # as the provided matrix. + # + # @param [NMatrix] nm the nmatrix whose stype, dtype, and shape will be used + # @return [NMatrix] a new nmatrix filled with zeros. + # + def zeros_like(nm) + NMatrix.zeros(nm.stype, nm.shape, nm.dtype) + end + + # + # call-seq: # eye(size) -> NMatrix # eye(size, dtype) -> NMatrix # eye(stype, size, dtype) -> NMatrix # # Creates an identity matrix (square matrix rank 2). @@ -138,11 +235,10 @@ end m end alias :identity :eye - # # call-seq: # diagonals(array) -> NMatrix # diagonals(stype, array, dtype) -> NMatrix # diagonals(array, dtype) -> NMatrix @@ -322,79 +418,54 @@ def cindgen(size) NMatrix.seq(size, :complex64) end end +end - # - # call-seq: - # column(column_number) -> NMatrix - # column(column_number, get_by) -> NMatrix - # - # Returns the column specified. Uses slicing by copy as default. - # - # * *Arguments* : - # - +column_number+ -> Integer. - # - +get_by+ -> Type of slicing to use, +:copy+ or +:reference+. - # * *Returns* : - # - A NMatrix representing the requested column as a column vector. - # - # Examples: - # - # m = NMatrix.new(2, [1, 4, 9, 14], :int32) # => 1 4 - # 9 14 - # - # m.column(1) # => 4 - # 14 - # - def column(column_number, get_by = :copy) - unless [:copy, :reference].include?(get_by) - raise ArgumentError, "column() 2nd parameter must be :copy or :reference" - end +module NVector - if get_by == :copy - self.slice(0 ... self.shape[0], column_number) - else # by reference - self[0 ... self.shape[0], column_number] - end - end + class << self + # + # 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* : + # - + # + def new(*args) + stype = args[0].is_a?(Symbol) ? args.shift : :dense + shape = args[0].is_a?(Array) ? args.shift : [1,args.shift] - alias :col :column + 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 - # - # call-seq: - # row(row_number) -> NMatrix - # row(row_number, get_by) -> NMatrix - # - # * *Arguments* : - # - +row_number+ -> Integer. - # - +get_by+ -> Type of slicing to use, +:copy+ or +:reference+. - # * *Returns* : - # - A NMatrix representing the requested row . - # - def row(row_number, get_by = :copy) - unless [:copy, :reference].include?(get_by) - raise ArgumentError, "row() 2nd parameter must be :copy or :reference" + NMatrix.new(stype, shape, *args) end - if get_by == :copy - self.slice(row_number, 0 ... self.shape[1]) - else # by reference - self[row_number, 0 ... self.shape[1]] - end - end -end - -class NVector < NMatrix - - class << self # # call-seq: # zeros(size) -> NMatrix # zeros(size, dtype) -> NMatrix # - # Creates a new matrix of zeros with the dimensions supplied as + # 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+. @@ -490,18 +561,14 @@ # # NVector.seq(3, :float32) # => 0.0 # 1.0 # 2.0 # - def seq(n, dtype = :int64) - unless n.is_a?(Integer) - raise ArgumentError, "NVector::seq() only accepts integers as size." - end + def seq(size, dtype = :int64) + values = (0 ... size).to_a - values = (0 ... n).to_a - - NVector.new(n, values, dtype) + NVector.new(size, values, dtype) end # # call-seq: # indgen(n) -> NVector @@ -643,56 +710,17 @@ result end end end -# NMatrix needs to have a succinct way to create a matrix by specifying the -# components directly. This is very useful for using it as an advanced -# calculator, it is useful for learning how to use, for testing language -# features and for developing algorithms. -# -# The N class provides a way to create a matrix in a way that is compact and -# natural. The components are specified using Ruby array syntax. Optionally, -# one can specify a dtype as the last parameter (default is :float64). -# + +# Use this constant as you would use NMatrix[]. # Examples: # # a = N[ 1,2,3,4 ] => 1.0 2.0 3.0 4.0 # # a = N[ 1,2,3,4, :int32 ] => 1 2 3 4 # # a = N[ [1,2,3], [3,4,5] ] => 1.0 2.0 3.0 # 3.0 4.0 5.0 # -# SYNTAX COMPARISON: -# -# MATLAB: a = [ [1 2 3] ; [4 5 6] ] or [ 1 2 3 ; 4 5 6 ] -# IDL: a = [ [1,2,3] , [4,5,6] ] -# NumPy: a = array( [1,2,3], [4,5,6] ) -# -# SciRuby: a = N[ [1,2,3], [4,5,6] ] -# Ruby array: a = [ [1,2,3], [4,5,6] ] -# -class N - class << self - # - # call-seq: - # N[array-of-arrays, dtype = nil] - # - def [](*params) - dtype = params.last.is_a?(Symbol) ? params.pop : nil - - # First find the dimensions of the array. - i = 0 - dim = [] - foo = params - while foo.is_a?(Array) - dim[i] = foo.length - foo = foo[0] - i += 1 - end - - # Then flatten the array. - NMatrix.new(dim, params.flatten, dtype) - end - end -end +N = NMatrix