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

View File

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