Method: Vector.basis

Defined in:
lib/matrix.rb

.basis(size:, index:) ⇒ Object

Returns a standard basis n-vector, where k is the index.

Vector.basis(size:, index:) # => Vector[0, 1, 0]

Raises:

  • (ArgumentError)


1969
1970
1971
1972
1973
1974
1975
# File 'lib/matrix.rb', line 1969

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