Project

General

Profile

Feature #10072 ยป implement_Vector.basis.patch

gogotanaka (Kazuki Tanaka), 07/19/2014 07:15 AM

View differences:

NEWS
* Vector
* New methods:
* Vector.basis Creates a standard basis +n+-vector.
* Vector#@+ and Vector#@- .
* Method
lib/matrix.rb
# To create a Vector:
# * Vector.[](*array)
# * Vector.elements(array, copy = true)
# * Vector.basis(n, k)
#
# To access elements:
# * #[](i)
......
end
#
# Creates a standard basis +n+-vector.
#
def Vector.basis(n, k)
raise ArgumentError, "invalid n (#{n} for 1..)" if n < 1
raise ArgumentError, "invalid k (#{k} for 0..#{n - 1})" unless 0 <= k && k < n
array = Array.new(n) {|i| i == k ? 1 : 0 }
new convert_to_array(array, false)
end
#
# Vector.new is private; use Vector[] or Vector.elements to create.
#
def initialize(array)
test/matrix/test_vector.rb
@w1 = Vector[2,3,4]
end
def test_basis
assert_equal(Vector[1, 0, 0], Vector.basis(3, 0))
assert_raise(ArgumentError) { Vector.basis(-1, 2) }
assert_raise(ArgumentError) { Vector.basis(4, -1) }
assert_raise(ArgumentError) { Vector.basis(3, 3) }
end
def test_identity
assert_same @v1, @v1
assert_not_same @v1, @v2
    (1-1/1)