From 20bd6423d9243130a42e574a365489158e33f371 Mon Sep 17 00:00:00 2001 From: Chris Watson Date: Mon, 17 Jun 2019 19:04:38 -0700 Subject: [PATCH] Added Matrix.random --- src/apatite.cr | 5 +++++ src/apatite/linear_algebra/matrix.cr | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/apatite.cr b/src/apatite.cr index b99fa7f..7e297f9 100644 --- a/src/apatite.cr +++ b/src/apatite.cr @@ -89,6 +89,11 @@ module Apatite Matrix.empty(row_count, column_count) 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`. def diagonal(values) Matrix.diagonal(values) diff --git a/src/apatite/linear_algebra/matrix.cr b/src/apatite/linear_algebra/matrix.cr index b439c58..96c67f1 100644 --- a/src/apatite/linear_algebra/matrix.cr +++ b/src/apatite/linear_algebra/matrix.cr @@ -108,6 +108,13 @@ module Apatite::LinearAlgebra scalar(n, 1) 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`. def self.row_vector(row) Matrix.new([row], 0)