Class: CArray

Inherits:
Object
  • Object
show all
Defined in:
lib/carray-gsl/core.rb,
lib/carray-gsl/core.rb

Instance Method Summary collapse

Instance Method Details

#cholesky_decompObject



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_hermObject



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_hermvObject



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_nonsymmObject



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_ZObject



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_nonsymmvObject



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_symmObject



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_symmvObject



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_decompObject



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_detObject



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_invertObject



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_decompObject



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_unpackObject



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_absdevObject



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_kurtosisObject



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_autocorrelationObject



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_maxObject



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_indexObject



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_meanObject



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_dataObject



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_minObject



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_indexObject



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_minmaxObject



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_indexObject



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_dataObject



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_sdObject



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_skewObject



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_tssObject



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_varianceObject



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_decompObject



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

#vtObject



763
764
765
# File 'lib/carray-gsl/core.rb', line 763

def vt
  return v.t
end