Class: CArray
- Inherits:
-
Object
- Object
- CArray
- Defined in:
- lib/carray-gsl/core.rb,
lib/carray-gsl/core.rb
Instance Method Summary collapse
- #cholesky_decomp ⇒ Object
- #cholesky_solve(b) ⇒ Object
- #eigen_herm ⇒ Object
- #eigen_hermv ⇒ Object
- #eigen_nonsymm ⇒ Object
- #eigen_nonsymm_Z ⇒ Object
- #eigen_nonsymmv ⇒ Object
- #eigen_symm ⇒ Object
- #eigen_symmv ⇒ Object
- #fit_builtin(*args) ⇒ Object
- #fit_linear(x) ⇒ Object
- #fit_nonlinear(xx, sigma, procf, procdf, parms, errs) ⇒ Object
- #fit_wlinear(x, w) ⇒ Object
- #hh_solve(b) ⇒ Object
- #lu_decomp ⇒ Object
- #lu_det ⇒ Object
- #lu_invert ⇒ Object
- #lu_refine(b, x) ⇒ Object
- #lu_solve(b) ⇒ Object
- #qr_decomp ⇒ Object
- #qr_solve(b) ⇒ Object
- #qr_unpack ⇒ Object
- #stats_absdev ⇒ Object
- #stats_absdev_m(mean) ⇒ Object
- #stats_kurtosis ⇒ Object
- #stats_kurtosis_m_sd(mean, sd) ⇒ Object
- #stats_lag1_autocorrelation ⇒ Object
- #stats_lag1_autocorrelation_m(mean) ⇒ Object
- #stats_max ⇒ Object
- #stats_max_index ⇒ Object
- #stats_mean ⇒ Object
- #stats_median_from_sorted_data ⇒ Object
- #stats_min ⇒ Object
- #stats_min_index ⇒ Object
- #stats_minmax ⇒ Object
- #stats_minmax_index ⇒ Object
- #stats_quantile_from_sorted_data ⇒ Object
- #stats_sd ⇒ Object
- #stats_sd_m(mean) ⇒ Object
- #stats_sd_with_fixed_mean(mean) ⇒ Object
- #stats_skew ⇒ Object
- #stats_skew_m_sd(mean, sd) ⇒ Object
- #stats_tss ⇒ Object
- #stats_tss_m(mean) ⇒ Object
- #stats_variance ⇒ Object
- #stats_variance_m(mean) ⇒ Object
- #stats_variance_with_fixed_mean(mean) ⇒ Object
- #stats_wabsdev(w) ⇒ Object
- #stats_wabsdev_m(w, mean) ⇒ Object
- #stats_wkurtosis(w) ⇒ Object
- #stats_wkurtosis_m_sd(w, mean, sd) ⇒ Object
- #stats_wmean(w) ⇒ Object
- #stats_wsd(w) ⇒ Object
- #stats_wsd_m(w, mean) ⇒ Object
- #stats_wsd_with_fixed_mean(w, mean) ⇒ Object
- #stats_wskew(w) ⇒ Object
- #stats_wskew_m_sd(w, mean, sd) ⇒ Object
- #stats_wtss(w) ⇒ Object
- #stats_wtss_m(w, mean) ⇒ Object
- #stats_wvariance(w) ⇒ Object
- #stats_wvariance_m(w, mean) ⇒ Object
- #stats_wvariance_with_fixed_mean(w, mean) ⇒ Object
- #sv_decomp ⇒ Object
- #sv_solve(b) ⇒ Object
- #vt ⇒ Object
Instance Method Details
#cholesky_decomp ⇒ Object
181 182 183 184 185 186 187 188 |
# File 'lib/carray-gsl/core.rb', line 181 def cholesky_decomp attach { c = GSL::Linalg::Cholesky.decomp(self.gm) out = c.ca.m out["@cholesky"] = c return out } end |
#cholesky_solve(b) ⇒ Object
190 191 192 193 194 |
# File 'lib/carray-gsl/core.rb', line 190 def cholesky_solve (b) CArray.attach(self, b) { return GSL::Linalg::Cholesky.solve(self.gm, b.gv).ca.v } end |
#eigen_herm ⇒ Object
215 216 217 218 219 |
# File 'lib/carray-gsl/core.rb', line 215 def eigen_herm attach { return GSL::Eigen.herm(self.gm).ca.v } end |
#eigen_hermv ⇒ Object
221 222 223 224 225 226 |
# File 'lib/carray-gsl/core.rb', line 221 def eigen_hermv attach { e, v = *GSL::Eigen.hermv(self.gm) return e.ca.v, v.ca.m } end |
#eigen_nonsymm ⇒ Object
228 229 230 231 232 |
# File 'lib/carray-gsl/core.rb', line 228 def eigen_nonsymm attach { return GSL::Eigen.nonsymm(self.gm).ca.v } end |
#eigen_nonsymm_Z ⇒ Object
234 235 236 237 238 239 |
# File 'lib/carray-gsl/core.rb', line 234 def eigen_nonsymm_Z attach { e, z = *GSL::Eigen.nonsymm_Z(self.gm) return e.ca.v, z.ca.m } end |
#eigen_nonsymmv ⇒ Object
241 242 243 244 245 246 |
# File 'lib/carray-gsl/core.rb', line 241 def eigen_nonsymmv attach { e, v = *GSL::Eigen.nonsymmv(self.gm) return e.ca.v, v.ca.m } end |
#eigen_symm ⇒ Object
202 203 204 205 206 |
# File 'lib/carray-gsl/core.rb', line 202 def eigen_symm attach { return GSL::Eigen.symm(self.gm).ca.v } end |
#eigen_symmv ⇒ Object
208 209 210 211 212 213 |
# File 'lib/carray-gsl/core.rb', line 208 def eigen_symmv attach { e, v = *GSL::Eigen.symmv(self.gm) return e.ca.v, v.ca.m } end |
#fit_builtin(*args) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/carray-gsl/core.rb', line 305 def fit_builtin (*args) if args[1].is_a?(String) x, func, guess = *args CArray.attach(self, x) { if guess coef, err, chi2, dof = *GSL::MultiFit::FdfSolver.fit(x.gv, self.gv, func, guess) else coef, err, chi2, dof = *GSL::MultiFit::FdfSolver.fit(x.gv, self.gv, func) end return coef.to_a, err.to_a, chi2, dof } elsif args[2].is_a?(String) x, w, func, guess = *args CArray.attach(self, x, w) { if guess coef, err, chi2, dof = *GSL::MultiFit::FdfSolver.fit(x.gv, w.gv, self.gv, func, guess) else coef, err, chi2, dof = *GSL::MultiFit::FdfSolver.fit(x.gv, w.gv, self.gv, func) end return coef.to_a, err.to_a, chi2, dof } else raise ArgumentError, "invalid built-in function specifier" end end |
#fit_linear(x) ⇒ Object
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/carray-gsl/core.rb', line 248 def fit_linear (x) CArray.attach(self, x) { c0, c1, c00, c01, c11, chi2, status = *GSL::Fit.linear(x.gv, self.gv) dof = self.elements - 2 covar = CA_DOUBLE([[c00, c01],[c01, c11]]) err = Array.new(2){|i| Math::sqrt(chi2/dof*covar[i,i]) } return [c0, c1], err, chi2, dof, covar } end |
#fit_nonlinear(xx, sigma, procf, procdf, parms, errs) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/carray-gsl/core.rb', line 270 def fit_nonlinear (xx, sigma, procf, procdf, parms, errs) n = self.elements np = parms.size parms = GSL::Vector[*parms] gf = Proc.new { |a, x, y, sigma, f| f.ca[] = (procf.call(x, a.to_a) - y) / sigma } gdf = Proc.new { |a, x, y, sigma, jac| cjac = jac.ca df = procdf.call(x, a.to_a) a.size.times do |i| cjac[nil, i] = df[i] / sigma end } func = GSL::MultiFit::Function_fdf.alloc(gf, gdf, np) func.set_data(xx, self, sigma) lmsder = GSL::MultiFit::FdfSolver::LMSDER solver = GSL::MultiFit::FdfSolver.alloc(lmsder, n, np) solver.set(func, parms) iter = 0 begin iter += 1 status = solver.iterate status = solver.test_delta(*errs) end while status == GSL::CONTINUE and iter < 500 coef = solver.position.to_a chi2 = GSL::pow_2(solver.f.dnrm2) dof = n - np covar = solver.covar(0.0).ca err = Array.new(np){|i| Math::sqrt(chi2/dof*covar[i,i]) } return coef, err, chi2, dof, covar end |
#fit_wlinear(x, w) ⇒ Object
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/carray-gsl/core.rb', line 259 def fit_wlinear (x, w) CArray.attach(self, x, w) { c0, c1, c00, c01, c11, chi2, status = *GSL::Fit.wlinear(x.gv, w.gv, self.gv) dof = self.elements - 2 covar = CA_DOUBLE([[c00, c01],[c01, c11]]) err = Array.new(2){|i| Math::sqrt(chi2/dof*covar[i,i]) } return [c0, c1], err, chi2, dof, covar } end |
#hh_solve(b) ⇒ Object
196 197 198 199 200 |
# File 'lib/carray-gsl/core.rb', line 196 def hh_solve (b) CArray.attach(self, b) { return GSL::Linalg::HH.solve(self.gm, b.gv).ca.v } end |
#lu_decomp ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/carray-gsl/core.rb', line 96 def lu_decomp attach { lu, perm, sign = *GSL::Linalg::LU.decomp(self.gm) out = lu.ca.m out.attribute["@A"] = self.gm out.attribute["@LU"] = lu out.attribute["@perm"] = perm out.attribute["@sign"] = sign return out } end |
#lu_det ⇒ Object
137 138 139 140 141 |
# File 'lib/carray-gsl/core.rb', line 137 def lu_det attach { return GSL::Linalg::LU.det(self.gm) } end |
#lu_invert ⇒ Object
131 132 133 134 135 |
# File 'lib/carray-gsl/core.rb', line 131 def lu_invert attach { return GSL::Linalg::LU.invert(self.gm).ca.m } end |
#lu_refine(b, x) ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/carray-gsl/core.rb', line 122 def lu_refine (b, x) CArray.attach(self, b, x) { a = self["@A"] lu = self["@LU"] perm = self["@perm"] return GSL::Linalg::LU.refine(a, lu, perm, b.gv, x.gv).first.ca.v } end |
#lu_solve(b) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/carray-gsl/core.rb', line 108 def lu_solve (b) if self["@LU"] CArray.attach(self, b) { lu = self["@LU"] perm = self["@perm"] return GSL::Linalg::LU.solve(lu, perm, b.gv).ca.v } else CArray.attach(self, b) { return GSL::Linalg::LU.solve(self.gm, b.gv).ca.v } end end |
#qr_decomp ⇒ Object
143 144 145 146 147 148 149 150 151 |
# File 'lib/carray-gsl/core.rb', line 143 def qr_decomp attach { qr, tau = *GSL::Linalg::QR.decomp(self.gm) out = qr.ca.m out["@qr"] = qr out["@tau"] = tau return out } end |
#qr_solve(b) ⇒ Object
153 154 155 156 157 |
# File 'lib/carray-gsl/core.rb', line 153 def qr_solve (b) CArray.attach(self, b) { return GSL::Linalg::QR.solve(self.gm, b.gv).ca.v } end |
#qr_unpack ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/carray-gsl/core.rb', line 159 def qr_unpack attach { qr = self["@qr"] tau = self["@tau"] q, r = *GSL::Linalg::QR.unpack(qr, tau) return q.ca.m, r.ca.m } end |
#stats_absdev ⇒ Object
389 390 391 392 393 |
# File 'lib/carray-gsl/core.rb', line 389 def stats_absdev attach { return GSL::Stats.absdev(self.gv) } end |
#stats_absdev_m(mean) ⇒ Object
395 396 397 398 399 |
# File 'lib/carray-gsl/core.rb', line 395 def stats_absdev_m (mean) attach { return GSL::Stats.absdev_m(self.gv, mean) } end |
#stats_kurtosis ⇒ Object
413 414 415 416 417 |
# File 'lib/carray-gsl/core.rb', line 413 def stats_kurtosis attach { return GSL::Stats.kurtosis(self.gv) } end |
#stats_kurtosis_m_sd(mean, sd) ⇒ Object
419 420 421 422 423 |
# File 'lib/carray-gsl/core.rb', line 419 def stats_kurtosis_m_sd (mean, sd) attach { return GSL::Stats.kurtosis(self.gv, mean, sd) } end |
#stats_lag1_autocorrelation ⇒ Object
425 426 427 428 429 |
# File 'lib/carray-gsl/core.rb', line 425 def stats_lag1_autocorrelation attach { return GSL::Stats.lag1_autocorrelation(self.gv) } end |
#stats_lag1_autocorrelation_m(mean) ⇒ Object
431 432 433 434 435 |
# File 'lib/carray-gsl/core.rb', line 431 def stats_lag1_autocorrelation_m (mean) attach { return GSL::Stats.lag1_autocorrelation(self.gv, mean) } end |
#stats_max ⇒ Object
528 529 530 531 532 |
# File 'lib/carray-gsl/core.rb', line 528 def stats_max attach { return GSL::Stats.max(self.gv) } end |
#stats_max_index ⇒ Object
546 547 548 549 550 |
# File 'lib/carray-gsl/core.rb', line 546 def stats_max_index attach { return GSL::Stats.max_index(self.gv) } end |
#stats_mean ⇒ Object
335 336 337 338 339 |
# File 'lib/carray-gsl/core.rb', line 335 def stats_mean attach { return GSL::Stats.mean(self.gv) } end |
#stats_median_from_sorted_data ⇒ Object
564 565 566 567 568 |
# File 'lib/carray-gsl/core.rb', line 564 def stats_median_from_sorted_data attach { return GSL::Stats.median_from_sorted_data(self.gv) } end |
#stats_min ⇒ Object
534 535 536 537 538 |
# File 'lib/carray-gsl/core.rb', line 534 def stats_min attach { return GSL::Stats.min(self.gv) } end |
#stats_min_index ⇒ Object
552 553 554 555 556 |
# File 'lib/carray-gsl/core.rb', line 552 def stats_min_index attach { return GSL::Stats.min_index(self.gv) } end |
#stats_minmax ⇒ Object
540 541 542 543 544 |
# File 'lib/carray-gsl/core.rb', line 540 def stats_minmax attach { return GSL::Stats.minmax(self.gv) } end |
#stats_minmax_index ⇒ Object
558 559 560 561 562 |
# File 'lib/carray-gsl/core.rb', line 558 def stats_minmax_index attach { return GSL::Stats.minmax_index(self.gv) } end |
#stats_quantile_from_sorted_data ⇒ Object
570 571 572 573 574 |
# File 'lib/carray-gsl/core.rb', line 570 def stats_quantile_from_sorted_data attach { return GSL::Stats.quantile_from_sorted_data(self.gv) } end |
#stats_sd ⇒ Object
353 354 355 356 357 |
# File 'lib/carray-gsl/core.rb', line 353 def stats_sd attach { return GSL::Stats.sd(self.gv) } end |
#stats_sd_m(mean) ⇒ Object
359 360 361 362 363 |
# File 'lib/carray-gsl/core.rb', line 359 def stats_sd_m (mean) attach { return GSL::Stats.sd_m(self.gv, mean) } end |
#stats_sd_with_fixed_mean(mean) ⇒ Object
383 384 385 386 387 |
# File 'lib/carray-gsl/core.rb', line 383 def stats_sd_with_fixed_mean (mean) attach { return GSL::Stats.sd_with_fixed_mean(self.gv, mean) } end |
#stats_skew ⇒ Object
401 402 403 404 405 |
# File 'lib/carray-gsl/core.rb', line 401 def stats_skew attach { return GSL::Stats.skew(self.gv) } end |
#stats_skew_m_sd(mean, sd) ⇒ Object
407 408 409 410 411 |
# File 'lib/carray-gsl/core.rb', line 407 def stats_skew_m_sd (mean, sd) attach { return GSL::Stats.skew(self.gv, mean, sd) } end |
#stats_tss ⇒ Object
365 366 367 368 369 |
# File 'lib/carray-gsl/core.rb', line 365 def stats_tss attach { return GSL::Stats.tss(self.gv) } end |
#stats_tss_m(mean) ⇒ Object
371 372 373 374 375 |
# File 'lib/carray-gsl/core.rb', line 371 def stats_tss_m (mean) attach { return GSL::Stats.tss_m(self.gv, mean) } end |
#stats_variance ⇒ Object
341 342 343 344 345 |
# File 'lib/carray-gsl/core.rb', line 341 def stats_variance attach { return GSL::Stats.variance(self.gv) } end |
#stats_variance_m(mean) ⇒ Object
347 348 349 350 351 |
# File 'lib/carray-gsl/core.rb', line 347 def stats_variance_m (mean) attach { return GSL::Stats.variance_m(self.gv, mean) } end |
#stats_variance_with_fixed_mean(mean) ⇒ Object
377 378 379 380 381 |
# File 'lib/carray-gsl/core.rb', line 377 def stats_variance_with_fixed_mean (mean) attach { return GSL::Stats.variance_with_fixed_mean(self.gv, mean) } end |
#stats_wabsdev(w) ⇒ Object
492 493 494 495 496 |
# File 'lib/carray-gsl/core.rb', line 492 def stats_wabsdev (w) CArray.attach(self, w) { return GSL::Stats.wabsdev(self.gv, w.gv) } end |
#stats_wabsdev_m(w, mean) ⇒ Object
498 499 500 501 502 |
# File 'lib/carray-gsl/core.rb', line 498 def stats_wabsdev_m (w, mean) CArray.attach(self, w) { return GSL::Stats.wabsdev_m(self.gv, w.gv, mean) } end |
#stats_wkurtosis(w) ⇒ Object
516 517 518 519 520 |
# File 'lib/carray-gsl/core.rb', line 516 def stats_wkurtosis (w) CArray.attach(self, w) { return GSL::Stats.wkurtosis(self.gv, w.gv) } end |
#stats_wkurtosis_m_sd(w, mean, sd) ⇒ Object
522 523 524 525 526 |
# File 'lib/carray-gsl/core.rb', line 522 def stats_wkurtosis_m_sd (w, mean, sd) CArray.attach(self, w) { return GSL::Stats.wkurtosis(self.gv, w.gv, mean, sd) } end |
#stats_wmean(w) ⇒ Object
438 439 440 441 442 |
# File 'lib/carray-gsl/core.rb', line 438 def stats_wmean (w) CArray.attach(self, w) { return GSL::Stats.wmean(self.gv, w.gv) } end |
#stats_wsd(w) ⇒ Object
456 457 458 459 460 |
# File 'lib/carray-gsl/core.rb', line 456 def stats_wsd (w) CArray.attach(self, w) { return GSL::Stats.wsd(self.gv, w.gv) } end |
#stats_wsd_m(w, mean) ⇒ Object
462 463 464 465 466 |
# File 'lib/carray-gsl/core.rb', line 462 def stats_wsd_m (w, mean) CArray.attach(self, w) { return GSL::Stats.wsd_m(self.gv, w.gv, mean) } end |
#stats_wsd_with_fixed_mean(w, mean) ⇒ Object
486 487 488 489 490 |
# File 'lib/carray-gsl/core.rb', line 486 def stats_wsd_with_fixed_mean (w, mean) CArray.attach(self, w) { return GSL::Stats.wsd_with_fixed_mean(self.gv, w.gv, mean) } end |
#stats_wskew(w) ⇒ Object
504 505 506 507 508 |
# File 'lib/carray-gsl/core.rb', line 504 def stats_wskew (w) CArray.attach(self, w) { return GSL::Stats.wskew(self.gv, w.gv) } end |
#stats_wskew_m_sd(w, mean, sd) ⇒ Object
510 511 512 513 514 |
# File 'lib/carray-gsl/core.rb', line 510 def stats_wskew_m_sd (w, mean, sd) CArray.attach(self, w) { return GSL::Stats.wskew(self.gv, w.gv, mean, sd) } end |
#stats_wtss(w) ⇒ Object
468 469 470 471 472 |
# File 'lib/carray-gsl/core.rb', line 468 def stats_wtss (w) CArray.attach(self, w) { return GSL::Stats.wtss(self.gv, w.gv) } end |
#stats_wtss_m(w, mean) ⇒ Object
474 475 476 477 478 |
# File 'lib/carray-gsl/core.rb', line 474 def stats_wtss_m (w, mean) CArray.attach(self, w) { return GSL::Stats.wtss_m(self.gv, w.gv, mean) } end |
#stats_wvariance(w) ⇒ Object
444 445 446 447 448 |
# File 'lib/carray-gsl/core.rb', line 444 def stats_wvariance (w) CArray.attach(self, w) { return GSL::Stats.wvariance(self.gv, w.gv) } end |
#stats_wvariance_m(w, mean) ⇒ Object
450 451 452 453 454 |
# File 'lib/carray-gsl/core.rb', line 450 def stats_wvariance_m (w, mean) CArray.attach(self, w) { return GSL::Stats.wvariance_m(self.gv, w.gv, mean) } end |
#stats_wvariance_with_fixed_mean(w, mean) ⇒ Object
480 481 482 483 484 |
# File 'lib/carray-gsl/core.rb', line 480 def stats_wvariance_with_fixed_mean (w, mean) CArray.attach(self, w) { return GSL::Stats.wvariance_with_fixed_mean(self.gv, w.gv, mean) } end |
#sv_decomp ⇒ Object
168 169 170 171 172 173 |
# File 'lib/carray-gsl/core.rb', line 168 def sv_decomp attach { u, v, s = *GSL::Linalg::SV.decomp(self.gm) return u.ca.m, v.ca.m, s.ca.v } end |
#sv_solve(b) ⇒ Object
175 176 177 178 179 |
# File 'lib/carray-gsl/core.rb', line 175 def sv_solve (b) CArray.attach(self, b) { return GSL::Linalg::SV.solve(self.gm, b.gv).ca.v } end |
#vt ⇒ Object
763 764 765 |
# File 'lib/carray-gsl/core.rb', line 763 def vt return v.t end |