Created
December 2, 2015 15:17
-
-
Save darjanin/fda928b9dd3c282100fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'Matrix' | |
class B | |
attr_accessor :points | |
attr_reader :average | |
def initialize | |
@points = [] | |
@average = [0, 0, 0] | |
end | |
def add_point(coord) | |
@points << coord | |
end | |
def calculate_average | |
@points.each do |point| | |
@average[0] += point[0] | |
@average[1] += point[1] | |
@average[2] += point[2] | |
end | |
@average = @average.map { |item| item / @points.size } | |
end | |
def cov(i, j) | |
(@points.inject(0) do |acc, point| | |
acc += (point[i] - @average[i]) * (point[j] - @average[j]) | |
end / (@points.size - 1)).round(4) | |
end | |
end | |
b = B.new | |
b.add_point [50.0, 50.0, 50.0] | |
b.add_point [32.0, 34.0, 25.0] | |
b.add_point [25.0, 30.0, 30.0] | |
b.add_point [35.0, 30.0, 30.0] | |
b.calculate_average | |
puts "AVERAGE: #{b.average}" | |
k = [] | |
3.times do |i| | |
k << [] | |
3.times do |j| | |
k[i] << b.cov(i,j) | |
end | |
end | |
c = Matrix.rows(k) | |
p c | |
v, d, v_inv = c.eigensystem | |
p v | |
p d | |
p v_inv | |
puts '---' | |
puts '---' | |
p v_inv.row(0) | |
p c * v_inv.row(0) | |
p d.element(0,0) * v_inv.row(0) | |
if false | |
k.each { |row| p row } | |
print '{' | |
k.each do |row| | |
print '{' | |
out = [] | |
row.each do |item| | |
out << item | |
end | |
print out.join(',') | |
print '},' | |
end | |
print '}' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment