Class: GSL::Matrix

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

Instance Method Summary collapse

Instance Method Details

#column_sizeObject


107
108
109
# File 'lib/statsample/matrix.rb', line 107

def column_size
  size2
end

#determinantObject


111
112
113
# File 'lib/statsample/matrix.rb', line 111

def determinant
  det
end

#eigenpairsObject


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

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


119
120
121
# File 'lib/statsample/matrix.rb', line 119

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

#eigenvectorsObject


123
124
125
# File 'lib/statsample/matrix.rb', line 123

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

#eigenvectors_matrixObject


134
135
136
137
138
# File 'lib/statsample/matrix.rb', line 134

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

#inverseObject


115
116
117
# File 'lib/statsample/matrix.rb', line 115

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

#mssqObject

Matrix sum of squares


128
129
130
131
132
# File 'lib/statsample/matrix.rb', line 128

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

#row_sizeObject


103
104
105
# File 'lib/statsample/matrix.rb', line 103

def row_size
  size1
end

#square?Boolean

def eigenpairs_ruby

self.to_matrix.eigenpairs_ruby

end

Returns:

  • (Boolean)

151
152
153
# File 'lib/statsample/matrix.rb', line 151

def square?
  size1==size2
end

#to_dataframeObject Also known as: to_dataset


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

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.rename(self.name) if self.respond_to? :name
  ds
end

#to_gslObject


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

def to_gsl
  self
end

#to_matrixObject


155
156
157
158
159
160
# File 'lib/statsample/matrix.rb', line 155

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


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

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