Class: Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/matrix_extension.rb

Instance Method Summary collapse

Instance Method Details

#covariance_matrixObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/matrix_extension.rb', line 3

def covariance_matrix
	dim = self.column_size
	buff = Array.new(dim){Array.new(dim,0)}
	0.upto(dim-1) do |i|
		i.upto(dim-1) do |j|
			if i==j
				buff[i][j] = self.column(i).to_a.variance
			else
				conv = self.column(i).to_a.covariance(self.column(j).to_a)
				buff[i][j] = conv
				buff[j][i] = conv
			end
		end
	end
	Matrix[*buff]
end

#inspectObject Also known as: to_s



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/matrix_extension.rb', line 44

def inspect
    buff = ""
    self.row_size.times do |i|
        if i==0
            buff += "Matrix["
        else
            buff += " "
        end

        buff += "[" + self.row(i).to_a.join(",\t") + "]"

        if i==self.row_size-1
            buff += "]"
        else
            buff += ",\n"
        end
    end
    return buff
end

#l1_normalizeObject



20
21
22
23
24
25
26
# File 'lib/matrix_extension.rb', line 20

def l1_normalize
    buff = Array.new
    self.row_size.times do |i|
        buff << self.row(i).to_a.l1_normalize
    end
    Matrix[*buff]
end

#l2_normalizeObject



28
29
30
31
32
33
34
# File 'lib/matrix_extension.rb', line 28

def l2_normalize
    buff = Array.new
    self.row_size.times do |i|
        buff << self.row(i).to_a.l2_normalize
    end
    Matrix[*buff]
end

#to_json(*param) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/matrix_extension.rb', line 36

def to_json(*param)
	buff = []
	self.row_size.times do |i|
           buff << self.row(i).to_a
	end
	return buff.to_json(param)
end