Class: GSL::Matrix

Inherits:
Object show all
Defined in:
lib/statsample/matrix.rb

Instance Method Summary collapse

Instance Method Details

#column_sizeObject



109
110
111
# File 'lib/statsample/matrix.rb', line 109

def column_size
  size2
end

#determinantObject



113
114
115
# File 'lib/statsample/matrix.rb', line 113

def determinant
  det
end

#eigenpairsObject



142
143
144
145
146
147
148
# File 'lib/statsample/matrix.rb', line 142

def eigenpairs
  eigval, eigvec= GSL::Eigen.symmv(self)
  GSL::Eigen::symmv_sort(eigval, eigvec, GSL::Eigen::SORT_VAL_DESC)
  @eigenpairs=eigval.size.times.map {|i|
    [eigval[i],eigvec.get_col(i)]
  }
end

#eigenvaluesObject



121
122
123
# File 'lib/statsample/matrix.rb', line 121

def eigenvalues
  eigenpairs.collect {|v| v[0]}
end

#eigenvectorsObject



125
126
127
# File 'lib/statsample/matrix.rb', line 125

def eigenvectors
  eigenpairs.collect {|v| v[1]}
end

#eigenvectors_matrixObject



136
137
138
139
140
# File 'lib/statsample/matrix.rb', line 136

def eigenvectors_matrix
  eigval, eigvec= GSL::Eigen.symmv(self)
  GSL::Eigen::symmv_sort(eigval, eigvec, GSL::Eigen::SORT_VAL_DESC)
  eigvec
end

#inverseObject



117
118
119
# File 'lib/statsample/matrix.rb', line 117

def inverse
  GSL::Linalg::LU.invert(self)
end

#mssqObject

Matrix sum of squares



130
131
132
133
134
# File 'lib/statsample/matrix.rb', line 130

def mssq
  sum=0
  to_v.each {|i| sum+=i**2}
  sum
end

#row_sizeObject



105
106
107
# File 'lib/statsample/matrix.rb', line 105

def row_size
  size1
end

#square?Boolean

def eigenpairs_ruby

self.to_matrix.eigenpairs_ruby

end

Returns:

  • (Boolean)


153
154
155
# File 'lib/statsample/matrix.rb', line 153

def square?
  size1==size2
end

#to_dataframeObject Also known as: to_dataset



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/statsample/matrix.rb', line 88

def to_dataframe
  f = (self.respond_to? :fields_y) ? fields_y : column_size.times.map { |i| "VAR_#{i+1}".to_sym }
  ds=Daru::DataFrame.new({}, order: f)
  f.each do |ff|
    ds[ff].rename ff
  end

  row_size.times {|i|
    ds.add_row(self.row(i).to_a)
  }
  ds.update
  ds.rename(self.name) if self.respond_to? :name
  ds
end

#to_gslObject



84
85
86
# File 'lib/statsample/matrix.rb', line 84

def to_gsl
  self
end

#to_matrixObject



157
158
159
160
161
162
# File 'lib/statsample/matrix.rb', line 157

def to_matrix
  rows=self.size1
  cols=self.size2
  out=(0...rows).collect{|i| (0...cols).collect {|j| self[i,j]} }
  ::Matrix.rows(out)
end

#total_sumObject



164
165
166
167
168
169
170
171
172
# File 'lib/statsample/matrix.rb', line 164

def total_sum
  sum=0
  size1.times {|i|
    size2.times {|j|
      sum+=self[i,j]
    }
  }
  sum
end