lib/nmatrix/shortcuts.rb in nmatrix-0.2.3 vs lib/nmatrix/shortcuts.rb in nmatrix-0.2.4
- old
+ new
@@ -492,15 +492,15 @@
raise(ArgumentError, "Only 2D matrices or 2D arrays allowed") unless b.shape.size == 2
raise(ArgumentError, "Only square-shaped blocks allowed") unless b.shape[0] == b.shape[1]
block_sizes << b.shape[0]
end
- block_diag_mat = NMatrix.zeros(block_sizes.sum, options)
+ block_diag_mat = NMatrix.zeros(block_sizes.inject(0,:+), options)
(0...params.length).each do |n|
# First determine the size and position of the n'th block in the block-diagonal matrix
block_size = block_sizes[n]
- block_pos = block_sizes[0...n].sum
+ block_pos = block_sizes[0...n].inject(0,:+)
# populate the n'th block in the block-diagonal matrix
(0...block_size).each do |i|
(0...block_size).each do |j|
block_diag_mat[block_pos+i,block_pos+j] = params[n][i,j]
end
@@ -534,10 +534,15 @@
# NMatrix.random([2, 2], :dtype => :byte, :scale => 255) # => [ [252, 108] [44, 12] ]
#
def random(shape, opts={})
scale = opts.delete(:scale) || 1.0
- rng = Random.new
+ if opts[:seed].nil?
+ rng = Random.new
+ else
+ rng = Random.new(opts[:seed])
+ end
+
random_values = []
# Construct the values of the final matrix based on the dimension.