Added Matrix.random

This commit is contained in:
Chris Watson 2019-06-17 19:04:38 -07:00
parent 8e8a72345c
commit 20bd6423d9
No known key found for this signature in database
GPG Key ID: 37DAEF5F446370A4
2 changed files with 12 additions and 0 deletions

View File

@ -89,6 +89,11 @@ module Apatite
Matrix.empty(row_count, column_count) Matrix.empty(row_count, column_count)
end end
# Creates a matrix of the given shape with random vectors.
def random_matrix(row_count, column_count, range = nil)
Matrix.random(row_count, column_count, range)
end
# Creates a matrix where the diagonal elements are composed of `values`. # Creates a matrix where the diagonal elements are composed of `values`.
def diagonal(values) def diagonal(values)
Matrix.diagonal(values) Matrix.diagonal(values)

View File

@ -108,6 +108,13 @@ module Apatite::LinearAlgebra
scalar(n, 1) scalar(n, 1)
end end
# Creates a matrix of the given shape with random vectors.
def self.random(num_rows, num_columns, range = nil)
Matrix.build(num_rows, num_columns) do |i, j|
rand(range || -1e+1..1e+1)
end
end
# Creates a single-row matrix where the values of that row are as given in `row`. # Creates a single-row matrix where the values of that row are as given in `row`.
def self.row_vector(row) def self.row_vector(row)
Matrix.new([row], 0) Matrix.new([row], 0)