From d16bf4c89536519dfbae7f83435e2d96f44f8261 Mon Sep 17 00:00:00 2001 From: gogotanaka Date: Wed, 29 Oct 2014 11:39:03 -0700 Subject: [PATCH 1/3] Implement Vector.independent? --- lib/matrix.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/matrix.rb b/lib/matrix.rb index e1a0604..18b9a2f 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1724,6 +1724,10 @@ end # * #each2(v) # * #collect2(v) # +# Properties of vectors: +# * Vector.independent?(*vs) +# * #independent?(*vs) +# # Vector arithmetic: # * #*(x) "is matrix or number" # * #+(v) @@ -1879,6 +1883,41 @@ class Vector end #-- + # PROPERTIES -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + #++ + + # + # Returns +true+ iff all of vectors are linearly independent. + # + # Vector.independent?(Vector[1,0], Vector[0,1]) + # => true + # + # Vector.independent?(Vector[1,2], Vector[2,4]) + # => false + # + def Vector.independent?(*vs) + vs.each do |v| + raise TypeError, "expected Vector, got #{v.class}" unless v.is_a?(Vector) + Vector.Raise ErrDimensionMismatch unless v.size == vs.first.size + end + return false if vs.count > vs.first.size + Matrix[*vs].rank.eql?(vs.count) + end + + # + # Returns +true+ iff all of vectors are linearly independent. + # + # Vector[1,0].independent?(Vector[0,1]) + # => true + # + # Vector[1,2].independent?(Vector[2,4]) + # => false + # + def independent?(*vs) + self.class.independent?(self, *vs) + end + + #-- # COMPARING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ -- 1.8.5.2 (Apple Git-48)