Fixed Vector

This commit is contained in:
Chris 2019-07-09 19:45:06 -07:00
parent 9607b9b035
commit fd6da99f9d
No known key found for this signature in database
GPG Key ID: 37DAEF5F446370A4
2 changed files with 36 additions and 23 deletions

View File

@ -30,9 +30,9 @@ module Apatite::LinearAlgebra
# # => [ 25, 93,
# # -1, 66 ]
# ```
def self.rows(rows : Indexable(Array(T)), copy = true)
def self.rows(rows : Indexable(Indexable(T)), copy = true)
rows = rows.dup if copy
rows = rows.to_a
rows = rows.map(&.to_a).to_a
rows.map! do |row|
row = row.dup if row
row.to_a
@ -169,7 +169,7 @@ module Apatite::LinearAlgebra
# ```
def self.row_vector(row)
row = row.to_a
new([row])
Matrix.new([row])
end
# Creates a single-column matrix where the values of that column are as given
@ -183,7 +183,7 @@ module Apatite::LinearAlgebra
# ```
def self.column_vector(column)
column = column.to_a
new([column].transpose, 1)
Matrix.new([column].transpose, 1)
end
# Creates a empty matrix of `row_count` x `column_count`.
@ -1411,6 +1411,11 @@ module Apatite::LinearAlgebra
end
end
# Just in case
def to_matrix
self
end
#--
# PRINTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#++

View File

@ -40,14 +40,22 @@ module Apatite::LinearAlgebra
end
# Returns `true` if all of vectors are linearly independent.
# def Vector.independent?(*vs)
# vs.each do |v|
# raise TypeError.new("expected Vector, got #{v.class}") unless v.is_a?(Vector)
# raise ErrDimensionMismatch.new unless v.size == vs.first.size
# end
# return false if vs.size > vs.first.size
# Matrix[*vs].rank.eql?(vs.size)
# end
#
# ```
# 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 "expected Vector, but got #{v.class}" unless v.is_a?(Vector)
raise ErrDimensionMismatch.new unless v.size == vs.first.size
end
return false if vs.size > vs.first.size
Matrix.rows(vs.to_a).rank == vs.size
end
# Return a zero vector.
def self.zero(size)
@ -62,8 +70,8 @@ module Apatite::LinearAlgebra
when Number
els = @elements.map { |e| (e * x).as(T) }
self.class.elements(els, false)
# when Matrix
# Matrix.column_vector(self) * x
when Matrix
Matrix.column_vector(self) * x
when Vector
self.elements.zip(x.elements).map { |(x, y)| x * y }
else
@ -77,8 +85,8 @@ module Apatite::LinearAlgebra
when Number
els = @elements.map { |e| (e + x).as(T) }
self.class.elements(els, false)
# when Matrix
# Matrix.column_vector(self) + x
when Matrix
Matrix.column_vector(self) + x
when Vector
self.elements.zip(x.elements).map { |(x, y)| x + y }
else
@ -92,8 +100,8 @@ module Apatite::LinearAlgebra
when Number
els = @elements.map { |e| (e - x).as(T) }
self.class.elements(els, false)
# when Matrix
# Matrix.column_vector(self) - x
when Matrix
Matrix.column_vector(self) - x
when Vector
self.elements.zip(x.elements).map { |(x, y)| x - y }
else
@ -107,8 +115,8 @@ module Apatite::LinearAlgebra
when Number
els = @elements.map { |e| (e / x).as(T) }
self.class.elements(els, false)
# when Matrix
# Matrix.column_vector(self) / x
when Matrix
Matrix.column_vector(self) / x
when Vector
self.elements.zip(x.elements).map { |(x, y)| x / y }
else
@ -162,9 +170,9 @@ module Apatite::LinearAlgebra
end
# Creates a single-row matrix from this vector.
# def covector
# Matrix.row_vector(self)
# end
def covector
Matrix.row_vector(self)
end
# Returns the cross product of this vector with the others.
def cross_product(*vs)