summaryrefslogtreecommitdiff
path: root/lib/matrix.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/matrix.rb')
-rw-r--r--lib/matrix.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb
index 81834ab898..054c197b52 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -1624,6 +1624,7 @@ end
# To create a Vector:
# * Vector.[](*array)
# * Vector.elements(array, copy = true)
+# * Vector.basis(size: n, index: k)
#
# To access elements:
# * #[](i)
@@ -1686,6 +1687,19 @@ class Vector
end
#
+ # Returns a standard basis +n+-vector, where k is the index.
+ #
+ # Vector.basis(size:, index:) # => Vector[0, 1, 0]
+ #
+ def Vector.basis(size:, index:)
+ raise ArgumentError, "invalid size (#{size} for 1..)" if size < 1
+ raise ArgumentError, "invalid index (#{index} for 0...#{size})" unless 0 <= index && index < size
+ array = Array.new(size, 0)
+ array[index] = 1
+ new convert_to_array(array, false)
+ end
+
+ #
# Vector.new is private; use Vector[] or Vector.elements to create.
#
def initialize(array)