summaryrefslogtreecommitdiff
path: root/lib/matrix.rb
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-19 17:44:46 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-11-19 17:44:46 +0000
commit387e0dbe3fac787adc110a4e879d1026debc7f49 (patch)
treebad3a6ccfcc13bf78c06f1e034379b5e0155b2fb /lib/matrix.rb
parent4da89e192a757ee06e00bea15c6fe659d424ca32 (diff)
* lib/matrix.rb: Vector#independent? and associated class method
patch by gogo tanaka [#10451] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48506 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/matrix.rb')
-rw-r--r--lib/matrix.rb41
1 files changed, 40 insertions, 1 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb
index 4d211dd..569680f 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -1691,6 +1691,11 @@ end
# * #each2(v)
# * #collect2(v)
#
+# Properties of vectors:
+# * #angle_with(v)
+# * Vector.independent?(*vs)
+# * #independent?(*vs)
+#
# Vector arithmetic:
# * #*(x) "is matrix or number"
# * #+(v)
@@ -1699,7 +1704,6 @@ end
# * #-@
#
# Vector functions:
-# * #angle_with(v)
# * #inner_product(v), dot(v)
# * #cross_product(v), cross(v)
# * #collect
@@ -1833,6 +1837,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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#++