carray-gsl 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +10 -0
- data/carray-gsl.gemspec +23 -0
- data/carray_gsl.c +501 -0
- data/carray_mathfunc_gsl.c +1620 -0
- data/extconf.rb +34 -0
- data/lib/autoload/autoload_math_gsl.rb +5 -0
- data/lib/carray-gsl.rb +2 -0
- data/lib/math/gsl.rb +769 -0
- data/lib/math/interp/adapter_gsl_spline.rb +44 -0
- metadata +54 -0
data/extconf.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require "mkmf"
|
2
|
+
require "carray/mkmf"
|
3
|
+
|
4
|
+
begin
|
5
|
+
have_gsl = true
|
6
|
+
$CPPFLAGS << " " << `gsl-config --cflags`.chomp
|
7
|
+
$LOCAL_LIBS << " " << `gsl-config --libs`.chomp
|
8
|
+
`gsl-config --libs`.split(/\s+/).each do |opt|
|
9
|
+
case opt
|
10
|
+
when /\-l(.*)/
|
11
|
+
unless have_library($1)
|
12
|
+
have_gsl = false
|
13
|
+
break
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
rescue
|
18
|
+
end
|
19
|
+
|
20
|
+
$CFLAGS << " -Wall "
|
21
|
+
|
22
|
+
if have_carray()
|
23
|
+
if have_header("gsl/gsl_matrix.h") and have_gsl
|
24
|
+
create_makefile("carray/carray_gsl")
|
25
|
+
else
|
26
|
+
open("Makefile", "w") { |io|
|
27
|
+
io << "all:" << "\n"
|
28
|
+
io << "install:" << "\n"
|
29
|
+
io << "clean:" << "\n"
|
30
|
+
io << "distclean:" << "\n"
|
31
|
+
io << "\trm -rf mkmf.log Makefile" << "\n"
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
data/lib/carray-gsl.rb
ADDED
data/lib/math/gsl.rb
ADDED
@@ -0,0 +1,769 @@
|
|
1
|
+
# ----------------------------------------------------------------------------
|
2
|
+
#
|
3
|
+
# carray/math/gsl.rb
|
4
|
+
#
|
5
|
+
# This file is part of Ruby/CArray extension library.
|
6
|
+
# You can redistribute it and/or modify it under the terms of
|
7
|
+
# the GNU General Public License (GPL).
|
8
|
+
#
|
9
|
+
# Copyright (C) 2005-2008 Hiroki Motoyoshi
|
10
|
+
#
|
11
|
+
# ----------------------------------------------------------------------------
|
12
|
+
|
13
|
+
require 'gsl'
|
14
|
+
require 'carray'
|
15
|
+
require 'carray/carray_gsl'
|
16
|
+
|
17
|
+
module CA::GSL
|
18
|
+
end
|
19
|
+
|
20
|
+
[
|
21
|
+
GSL::Vector, GSL::Vector::Int, GSL::Vector::Complex,
|
22
|
+
GSL::Vector::Col, GSL::Vector::Int::Col, GSL::Vector::Complex::Col,
|
23
|
+
GSL::Matrix, GSL::Matrix::Int, GSL::Matrix::Complex,
|
24
|
+
].each do |klass|
|
25
|
+
klass.class_eval {
|
26
|
+
def to_ca
|
27
|
+
return self.ca.to_ca
|
28
|
+
end
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
class CArray
|
33
|
+
|
34
|
+
[
|
35
|
+
["airy_Ai", []],
|
36
|
+
["airy_Bi", []],
|
37
|
+
["airy_Ai_scaled", []],
|
38
|
+
["airy_Bi_scaled", []],
|
39
|
+
["airy_deriv_Ai", []],
|
40
|
+
["airy_deriv_Bi", []],
|
41
|
+
["airy_deriv_Ai_scaled", []],
|
42
|
+
["airy_deriv_Bi_scaled", []],
|
43
|
+
].each do |name,parms|
|
44
|
+
parms1 = parms + ["mode=GSL::PREC_DOUBLE"]
|
45
|
+
parms2 = parms + ["mode"]
|
46
|
+
eval %{
|
47
|
+
def #{name} (#{parms1.join(",")})
|
48
|
+
return self.#{name}_(#{parms2.join(",")})
|
49
|
+
end
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
[
|
54
|
+
["random_gaussian", ["sigma"]],
|
55
|
+
["random_ugaussian", []],
|
56
|
+
["random_gaussian_tail", ["a", "sigma"]],
|
57
|
+
["random_ugaussian_tail", ["a"]],
|
58
|
+
["random_exponential", ["mu"]],
|
59
|
+
["random_laplace", ["a"]],
|
60
|
+
["random_exppaw", ["a", "b"]],
|
61
|
+
["random_cauchy", ["a"]],
|
62
|
+
["random_rayleigh", ["sigma"]],
|
63
|
+
["random_rayleigh_tail", ["a", "sigma"]],
|
64
|
+
["random_landau", []],
|
65
|
+
["random_levy", ["c", "alpha"]],
|
66
|
+
["random_levy_skew", ["c", "alpha", "beta"]],
|
67
|
+
["random_gamma", ["a", "b"]],
|
68
|
+
["random_erlang", ["a", "n"]],
|
69
|
+
["random_flat", ["a", "b"]],
|
70
|
+
["random_lognormal", ["zeta", "sigma"]],
|
71
|
+
["random_chisq", ["nu"]],
|
72
|
+
["random_fdist", ["nu1", "nu2"]],
|
73
|
+
["random_tdist", ["nu"]],
|
74
|
+
["random_beta", ["a", "b"]],
|
75
|
+
["random_logistic", ["a"]],
|
76
|
+
["random_pareto", ["a", "b"]],
|
77
|
+
["random_weibull", ["a", "b"]],
|
78
|
+
["random_gumbel1", ["a", "b"]],
|
79
|
+
["random_gumbel2", ["a", "b"]],
|
80
|
+
["random_poisson", ["mu"]],
|
81
|
+
["random_bernoulli", ["p"]],
|
82
|
+
["random_binomial", ["p", "n"]],
|
83
|
+
["random_negative_binomial", ["p", "n"]],
|
84
|
+
["random_pascal", ["p", "n"]],
|
85
|
+
["random_geometric", ["p"]],
|
86
|
+
["random_hypergeometric", ["n1", "n2", "t"]],
|
87
|
+
["random_logarithmic", ["p"]],
|
88
|
+
].each do |name, parms|
|
89
|
+
eval %{
|
90
|
+
def #{name} (#{parms.join(",")})
|
91
|
+
return CAMath.#{name}(#{(["self"]+parms).join(",")}).to_type(data_type)
|
92
|
+
end
|
93
|
+
def #{name}! (#{parms.join(",")})
|
94
|
+
return self.asign{ CAMath.#{name}(#{(["self"]+parms).join(",")}) }
|
95
|
+
end
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
def lu_decomp
|
100
|
+
attach {
|
101
|
+
lu, perm, sign = *GSL::Linalg::LU.decomp(self.gm)
|
102
|
+
out = lu.ca.m
|
103
|
+
out.attribute[:A] = self.gm
|
104
|
+
out.attribute[:LU] = lu
|
105
|
+
out.attribute[:perm] = perm
|
106
|
+
out.attribute[:sign] = sign
|
107
|
+
return out
|
108
|
+
}
|
109
|
+
end
|
110
|
+
|
111
|
+
def lu_solve (b)
|
112
|
+
if self.attribute[:LU]
|
113
|
+
CArray.attach(self, b) {
|
114
|
+
lu = self.attribute[:LU]
|
115
|
+
perm = self.attribute[:perm]
|
116
|
+
return GSL::Linalg::LU.solve(lu, perm, b.gv).ca.v
|
117
|
+
}
|
118
|
+
else
|
119
|
+
CArray.attach(self, b) {
|
120
|
+
return GSL::Linalg::LU.solve(self.gm, b.gv).ca.v
|
121
|
+
}
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def lu_refine (b, x)
|
126
|
+
CArray.attach(self, b, x) {
|
127
|
+
a = self.attribute[:A]
|
128
|
+
lu = self.attribute[:LU]
|
129
|
+
perm = self.attribute[:perm]
|
130
|
+
return GSL::Linalg::LU.refine(a, lu, perm, b.gv, x.gv).first.ca.v
|
131
|
+
}
|
132
|
+
end
|
133
|
+
|
134
|
+
def lu_invert
|
135
|
+
attach {
|
136
|
+
return GSL::Linalg::LU.invert(self.gm).ca.m
|
137
|
+
}
|
138
|
+
end
|
139
|
+
|
140
|
+
def lu_det
|
141
|
+
attach {
|
142
|
+
return GSL::Linalg::LU.det(self.gm)
|
143
|
+
}
|
144
|
+
end
|
145
|
+
|
146
|
+
def qr_decomp
|
147
|
+
attach {
|
148
|
+
qr, tau = *GSL::Linalg::QR.decomp(self.gm)
|
149
|
+
out = qr.ca.m
|
150
|
+
out.attribute[:qr] = qr
|
151
|
+
out.attribute[:tau] = tau
|
152
|
+
return out
|
153
|
+
}
|
154
|
+
end
|
155
|
+
|
156
|
+
def qr_solve (b)
|
157
|
+
CArray.attach(self, b) {
|
158
|
+
return GSL::Linalg::QR.solve(self.gm, b.gv).ca.v
|
159
|
+
}
|
160
|
+
end
|
161
|
+
|
162
|
+
def qr_unpack
|
163
|
+
attach {
|
164
|
+
qr = self.attribute[:qr]
|
165
|
+
tau = self.attribute[:tau]
|
166
|
+
q, r = *GSL::Linalg::QR.unpack(qr, tau)
|
167
|
+
return q.ca.m, r.ca.m
|
168
|
+
}
|
169
|
+
end
|
170
|
+
|
171
|
+
def sv_decomp
|
172
|
+
attach {
|
173
|
+
u, v, s = *GSL::Linalg::SV.decomp(self.gm)
|
174
|
+
return u.ca.m, v.ca.m, s.ca.v
|
175
|
+
}
|
176
|
+
end
|
177
|
+
|
178
|
+
def sv_solve (b)
|
179
|
+
CArray.attach(self, b) {
|
180
|
+
return GSL::Linalg::SV.solve(self.gm, b.gv).ca.v
|
181
|
+
}
|
182
|
+
end
|
183
|
+
|
184
|
+
def cholesky_decomp
|
185
|
+
attach {
|
186
|
+
c = GSL::Linalg::Cholesky.decomp(self.gm)
|
187
|
+
out = c.ca.m
|
188
|
+
out.attribute[:cholesky] = c
|
189
|
+
return out
|
190
|
+
}
|
191
|
+
end
|
192
|
+
|
193
|
+
def cholesky_solve (b)
|
194
|
+
CArray.attach(self, b) {
|
195
|
+
return GSL::Linalg::Cholesky.solve(self.gm, b.gv).ca.v
|
196
|
+
}
|
197
|
+
end
|
198
|
+
|
199
|
+
def hh_solve (b)
|
200
|
+
CArray.attach(self, b) {
|
201
|
+
return GSL::Linalg::HH.solve(self.gm, b.gv).ca.v
|
202
|
+
}
|
203
|
+
end
|
204
|
+
|
205
|
+
def eigen_symm
|
206
|
+
attach {
|
207
|
+
return GSL::Eigen.symm(self.gm).ca.v
|
208
|
+
}
|
209
|
+
end
|
210
|
+
|
211
|
+
def eigen_symmv
|
212
|
+
attach {
|
213
|
+
e, v = *GSL::Eigen.symmv(self.gm)
|
214
|
+
return e.ca.v, v.ca.m
|
215
|
+
}
|
216
|
+
end
|
217
|
+
|
218
|
+
def eigen_herm
|
219
|
+
attach {
|
220
|
+
return GSL::Eigen.herm(self.gm).ca.v
|
221
|
+
}
|
222
|
+
end
|
223
|
+
|
224
|
+
def eigen_hermv
|
225
|
+
attach {
|
226
|
+
e, v = *GSL::Eigen.hermv(self.gm)
|
227
|
+
return e.ca.v, v.ca.m
|
228
|
+
}
|
229
|
+
end
|
230
|
+
|
231
|
+
def eigen_nonsymm
|
232
|
+
attach {
|
233
|
+
return GSL::Eigen.nonsymm(self.gm).ca.v
|
234
|
+
}
|
235
|
+
end
|
236
|
+
|
237
|
+
def eigen_nonsymm_Z
|
238
|
+
attach {
|
239
|
+
e, z = *GSL::Eigen.nonsymm_Z(self.gm)
|
240
|
+
return e.ca.v, z.ca.m
|
241
|
+
}
|
242
|
+
end
|
243
|
+
|
244
|
+
def eigen_nonsymmv
|
245
|
+
attach {
|
246
|
+
e, v = *GSL::Eigen.nonsymmv(self.gm)
|
247
|
+
return e.ca.v, v.ca.m
|
248
|
+
}
|
249
|
+
end
|
250
|
+
|
251
|
+
def fit_linear (x)
|
252
|
+
CArray.attach(self, x) {
|
253
|
+
c0, c1, c00, c01, c11, chi2, status =
|
254
|
+
*GSL::Fit.linear(x.gv, self.gv)
|
255
|
+
dof = self.elements - 2
|
256
|
+
covar = CA_DOUBLE([[c00, c01],[c01, c11]])
|
257
|
+
err = Array.new(2){|i| Math::sqrt(chi2/dof*covar[i,i]) }
|
258
|
+
return [c0, c1], err, chi2, dof, covar
|
259
|
+
}
|
260
|
+
end
|
261
|
+
|
262
|
+
def fit_wlinear (x, w)
|
263
|
+
CArray.attach(self, x, w) {
|
264
|
+
c0, c1, c00, c01, c11, chi2, status =
|
265
|
+
*GSL::Fit.wlinear(x.gv, w.gv, self.gv)
|
266
|
+
dof = self.elements - 2
|
267
|
+
covar = CA_DOUBLE([[c00, c01],[c01, c11]])
|
268
|
+
err = Array.new(2){|i| Math::sqrt(chi2/dof*covar[i,i]) }
|
269
|
+
return [c0, c1], err, chi2, dof, covar
|
270
|
+
}
|
271
|
+
end
|
272
|
+
|
273
|
+
def fit_nonlinear (xx, sigma, procf, procdf, parms, errs)
|
274
|
+
n = self.elements
|
275
|
+
np = parms.size
|
276
|
+
parms = GSL::Vector[*parms]
|
277
|
+
gf = Proc.new { |a, x, y, sigma, f|
|
278
|
+
f.ca[] = (procf.call(x, a.to_a) - y) / sigma
|
279
|
+
}
|
280
|
+
gdf = Proc.new { |a, x, y, sigma, jac|
|
281
|
+
cjac = jac.ca
|
282
|
+
df = procdf.call(x, a.to_a)
|
283
|
+
a.size.times do |i|
|
284
|
+
cjac[nil, i] = df[i] / sigma
|
285
|
+
end
|
286
|
+
}
|
287
|
+
func = GSL::MultiFit::Function_fdf.alloc(gf, gdf, np)
|
288
|
+
func.set_data(xx, self, sigma)
|
289
|
+
lmsder = GSL::MultiFit::FdfSolver::LMSDER
|
290
|
+
solver = GSL::MultiFit::FdfSolver.alloc(lmsder, n, np)
|
291
|
+
solver.set(func, parms)
|
292
|
+
|
293
|
+
iter = 0
|
294
|
+
begin
|
295
|
+
iter += 1
|
296
|
+
status = solver.iterate
|
297
|
+
status = solver.test_delta(*errs)
|
298
|
+
end while status == GSL::CONTINUE and iter < 500
|
299
|
+
|
300
|
+
coef = solver.position.to_a
|
301
|
+
chi2 = GSL::pow_2(solver.f.dnrm2)
|
302
|
+
dof = n - np
|
303
|
+
covar = solver.covar(0.0).ca
|
304
|
+
err = Array.new(np){|i| Math::sqrt(chi2/dof*covar[i,i]) }
|
305
|
+
return coef, err, chi2, dof, covar
|
306
|
+
end
|
307
|
+
|
308
|
+
def fit_builtin (*args)
|
309
|
+
if args[1].is_a?(String)
|
310
|
+
x, func, guess = *args
|
311
|
+
CArray.attach(self, x) {
|
312
|
+
if guess
|
313
|
+
coef, err, chi2, dof =
|
314
|
+
*GSL::MultiFit::FdfSolver.fit(x.gv, self.gv, func, guess)
|
315
|
+
else
|
316
|
+
coef, err, chi2, dof =
|
317
|
+
*GSL::MultiFit::FdfSolver.fit(x.gv, self.gv, func)
|
318
|
+
end
|
319
|
+
return coef.to_a, err.to_a, chi2, dof
|
320
|
+
}
|
321
|
+
elsif args[2].is_a?(String)
|
322
|
+
x, w, func, guess = *args
|
323
|
+
CArray.attach(self, x, w) {
|
324
|
+
if guess
|
325
|
+
coef, err, chi2, dof =
|
326
|
+
*GSL::MultiFit::FdfSolver.fit(x.gv, w.gv, self.gv, func, guess)
|
327
|
+
else
|
328
|
+
coef, err, chi2, dof =
|
329
|
+
*GSL::MultiFit::FdfSolver.fit(x.gv, w.gv, self.gv, func)
|
330
|
+
end
|
331
|
+
return coef.to_a, err.to_a, chi2, dof
|
332
|
+
}
|
333
|
+
else
|
334
|
+
raise ArgumentError, "invalid built-in function specifier"
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
def stats_mean
|
339
|
+
attach {
|
340
|
+
return GSL::Stats.mean(self.gv)
|
341
|
+
}
|
342
|
+
end
|
343
|
+
|
344
|
+
def stats_variance
|
345
|
+
attach {
|
346
|
+
return GSL::Stats.variance(self.gv)
|
347
|
+
}
|
348
|
+
end
|
349
|
+
|
350
|
+
def stats_variance_m (mean)
|
351
|
+
attach {
|
352
|
+
return GSL::Stats.variance_m(self.gv, mean)
|
353
|
+
}
|
354
|
+
end
|
355
|
+
|
356
|
+
def stats_sd
|
357
|
+
attach {
|
358
|
+
return GSL::Stats.sd(self.gv)
|
359
|
+
}
|
360
|
+
end
|
361
|
+
|
362
|
+
def stats_sd_m (mean)
|
363
|
+
attach {
|
364
|
+
return GSL::Stats.sd_m(self.gv, mean)
|
365
|
+
}
|
366
|
+
end
|
367
|
+
|
368
|
+
def stats_tss
|
369
|
+
attach {
|
370
|
+
return GSL::Stats.tss(self.gv)
|
371
|
+
}
|
372
|
+
end
|
373
|
+
|
374
|
+
def stats_tss_m (mean)
|
375
|
+
attach {
|
376
|
+
return GSL::Stats.tss_m(self.gv, mean)
|
377
|
+
}
|
378
|
+
end
|
379
|
+
|
380
|
+
def stats_variance_with_fixed_mean (mean)
|
381
|
+
attach {
|
382
|
+
return GSL::Stats.variance_with_fixed_mean(self.gv, mean)
|
383
|
+
}
|
384
|
+
end
|
385
|
+
|
386
|
+
def stats_sd_with_fixed_mean (mean)
|
387
|
+
attach {
|
388
|
+
return GSL::Stats.sd_with_fixed_mean(self.gv, mean)
|
389
|
+
}
|
390
|
+
end
|
391
|
+
|
392
|
+
def stats_absdev
|
393
|
+
attach {
|
394
|
+
return GSL::Stats.absdev(self.gv)
|
395
|
+
}
|
396
|
+
end
|
397
|
+
|
398
|
+
def stats_absdev_m (mean)
|
399
|
+
attach {
|
400
|
+
return GSL::Stats.absdev_m(self.gv, mean)
|
401
|
+
}
|
402
|
+
end
|
403
|
+
|
404
|
+
def stats_skew
|
405
|
+
attach {
|
406
|
+
return GSL::Stats.skew(self.gv)
|
407
|
+
}
|
408
|
+
end
|
409
|
+
|
410
|
+
def stats_skew_m_sd (mean, sd)
|
411
|
+
attach {
|
412
|
+
return GSL::Stats.skew(self.gv, mean, sd)
|
413
|
+
}
|
414
|
+
end
|
415
|
+
|
416
|
+
def stats_kurtosis
|
417
|
+
attach {
|
418
|
+
return GSL::Stats.kurtosis(self.gv)
|
419
|
+
}
|
420
|
+
end
|
421
|
+
|
422
|
+
def stats_kurtosis_m_sd (mean, sd)
|
423
|
+
attach {
|
424
|
+
return GSL::Stats.kurtosis(self.gv, mean, sd)
|
425
|
+
}
|
426
|
+
end
|
427
|
+
|
428
|
+
def stats_lag1_autocorrelation
|
429
|
+
attach {
|
430
|
+
return GSL::Stats.lag1_autocorrelation(self.gv)
|
431
|
+
}
|
432
|
+
end
|
433
|
+
|
434
|
+
def stats_lag1_autocorrelation_m (mean)
|
435
|
+
attach {
|
436
|
+
return GSL::Stats.lag1_autocorrelation(self.gv, mean)
|
437
|
+
}
|
438
|
+
end
|
439
|
+
|
440
|
+
|
441
|
+
def stats_wmean (w)
|
442
|
+
CArray.attach(self, w) {
|
443
|
+
return GSL::Stats.wmean(self.gv, w.gv)
|
444
|
+
}
|
445
|
+
end
|
446
|
+
|
447
|
+
def stats_wvariance (w)
|
448
|
+
CArray.attach(self, w) {
|
449
|
+
return GSL::Stats.wvariance(self.gv, w.gv)
|
450
|
+
}
|
451
|
+
end
|
452
|
+
|
453
|
+
def stats_wvariance_m (w, mean)
|
454
|
+
CArray.attach(self, w) {
|
455
|
+
return GSL::Stats.wvariance_m(self.gv, w.gv, mean)
|
456
|
+
}
|
457
|
+
end
|
458
|
+
|
459
|
+
def stats_wsd (w)
|
460
|
+
CArray.attach(self, w) {
|
461
|
+
return GSL::Stats.wsd(self.gv, w.gv)
|
462
|
+
}
|
463
|
+
end
|
464
|
+
|
465
|
+
def stats_wsd_m (w, mean)
|
466
|
+
CArray.attach(self, w) {
|
467
|
+
return GSL::Stats.wsd_m(self.gv, w.gv, mean)
|
468
|
+
}
|
469
|
+
end
|
470
|
+
|
471
|
+
def stats_wtss (w)
|
472
|
+
CArray.attach(self, w) {
|
473
|
+
return GSL::Stats.wtss(self.gv, w.gv)
|
474
|
+
}
|
475
|
+
end
|
476
|
+
|
477
|
+
def stats_wtss_m (w, mean)
|
478
|
+
CArray.attach(self, w) {
|
479
|
+
return GSL::Stats.wtss_m(self.gv, w.gv, mean)
|
480
|
+
}
|
481
|
+
end
|
482
|
+
|
483
|
+
def stats_wvariance_with_fixed_mean (w, mean)
|
484
|
+
CArray.attach(self, w) {
|
485
|
+
return GSL::Stats.wvariance_with_fixed_mean(self.gv, w.gv, mean)
|
486
|
+
}
|
487
|
+
end
|
488
|
+
|
489
|
+
def stats_wsd_with_fixed_mean (w, mean)
|
490
|
+
CArray.attach(self, w) {
|
491
|
+
return GSL::Stats.wsd_with_fixed_mean(self.gv, w.gv, mean)
|
492
|
+
}
|
493
|
+
end
|
494
|
+
|
495
|
+
def stats_wabsdev (w)
|
496
|
+
CArray.attach(self, w) {
|
497
|
+
return GSL::Stats.wabsdev(self.gv, w.gv)
|
498
|
+
}
|
499
|
+
end
|
500
|
+
|
501
|
+
def stats_wabsdev_m (w, mean)
|
502
|
+
CArray.attach(self, w) {
|
503
|
+
return GSL::Stats.wabsdev_m(self.gv, w.gv, mean)
|
504
|
+
}
|
505
|
+
end
|
506
|
+
|
507
|
+
def stats_wskew (w)
|
508
|
+
CArray.attach(self, w) {
|
509
|
+
return GSL::Stats.wskew(self.gv, w.gv)
|
510
|
+
}
|
511
|
+
end
|
512
|
+
|
513
|
+
def stats_wskew_m_sd (w, mean, sd)
|
514
|
+
CArray.attach(self, w) {
|
515
|
+
return GSL::Stats.wskew(self.gv, w.gv, mean, sd)
|
516
|
+
}
|
517
|
+
end
|
518
|
+
|
519
|
+
def stats_wkurtosis (w)
|
520
|
+
CArray.attach(self, w) {
|
521
|
+
return GSL::Stats.wkurtosis(self.gv, w.gv)
|
522
|
+
}
|
523
|
+
end
|
524
|
+
|
525
|
+
def stats_wkurtosis_m_sd (w, mean, sd)
|
526
|
+
CArray.attach(self, w) {
|
527
|
+
return GSL::Stats.wkurtosis(self.gv, w.gv, mean, sd)
|
528
|
+
}
|
529
|
+
end
|
530
|
+
|
531
|
+
def stats_max
|
532
|
+
attach {
|
533
|
+
return GSL::Stats.max(self.gv)
|
534
|
+
}
|
535
|
+
end
|
536
|
+
|
537
|
+
def stats_min
|
538
|
+
attach {
|
539
|
+
return GSL::Stats.min(self.gv)
|
540
|
+
}
|
541
|
+
end
|
542
|
+
|
543
|
+
def stats_minmax
|
544
|
+
attach {
|
545
|
+
return GSL::Stats.minmax(self.gv)
|
546
|
+
}
|
547
|
+
end
|
548
|
+
|
549
|
+
def stats_max_index
|
550
|
+
attach {
|
551
|
+
return GSL::Stats.max_index(self.gv)
|
552
|
+
}
|
553
|
+
end
|
554
|
+
|
555
|
+
def stats_min_index
|
556
|
+
attach {
|
557
|
+
return GSL::Stats.min_index(self.gv)
|
558
|
+
}
|
559
|
+
end
|
560
|
+
|
561
|
+
def stats_minmax_index
|
562
|
+
attach {
|
563
|
+
return GSL::Stats.minmax_index(self.gv)
|
564
|
+
}
|
565
|
+
end
|
566
|
+
|
567
|
+
def stats_median_from_sorted_data
|
568
|
+
attach {
|
569
|
+
return GSL::Stats.median_from_sorted_data(self.gv)
|
570
|
+
}
|
571
|
+
end
|
572
|
+
|
573
|
+
def stats_quantile_from_sorted_data
|
574
|
+
attach {
|
575
|
+
return GSL::Stats.quantile_from_sorted_data(self.gv)
|
576
|
+
}
|
577
|
+
end
|
578
|
+
|
579
|
+
end
|
580
|
+
|
581
|
+
module CAMath
|
582
|
+
|
583
|
+
include GSL::CONST::MKSA
|
584
|
+
include GSL::CONST::NUM
|
585
|
+
|
586
|
+
module_function
|
587
|
+
|
588
|
+
def coupling_9j (*argv)
|
589
|
+
return GSL.Sf.coupling_9j(*argv)
|
590
|
+
end
|
591
|
+
|
592
|
+
def lu_solve (lu, b)
|
593
|
+
CArray.attach(lu, b) {
|
594
|
+
perm = lu.attribute[:perm]
|
595
|
+
return GSL::Linalg::LU.solve(lu.gm, perm, b.gv).ca.v
|
596
|
+
}
|
597
|
+
end
|
598
|
+
|
599
|
+
def qr_solve (qr, b)
|
600
|
+
CArray.attach(qr, b) {
|
601
|
+
qrm = qr.attribute[:qr]
|
602
|
+
tau = qr.attribute[:tau]
|
603
|
+
return GSL::Linalg::QR.solve(qrm, tau, b.gv).ca.v
|
604
|
+
}
|
605
|
+
end
|
606
|
+
|
607
|
+
def sv_solve (u, v, s, b)
|
608
|
+
CArray.attach(u, v, s, b) {
|
609
|
+
return GSL::Linalg::SV.solve(u.gm, v.gm, s.gv, b.gv).ca.v
|
610
|
+
}
|
611
|
+
end
|
612
|
+
|
613
|
+
def cholesky_solve (c, b)
|
614
|
+
CArray.attach(c, b) {
|
615
|
+
cholesky = c.attribute[:cholesky]
|
616
|
+
return GSL::Linalg::Cholesky.solve(cholesky, b.gv).ca.v
|
617
|
+
}
|
618
|
+
end
|
619
|
+
|
620
|
+
def stats_covariance (v1, v2)
|
621
|
+
CArray.attach(v1, v2) {
|
622
|
+
return GSL::Stats.covariance(v1.gv, v2.gv)
|
623
|
+
}
|
624
|
+
end
|
625
|
+
|
626
|
+
def stats_covariance_m (v1, v2, mean1, mean2)
|
627
|
+
CArray.attach(v1, v2) {
|
628
|
+
return GSL::Stats.covariance_m(v1.gv, v2.gv, mean1, mean2)
|
629
|
+
}
|
630
|
+
end
|
631
|
+
|
632
|
+
def stats_correlation (v1, v2)
|
633
|
+
CArray.attach(v1, v2) {
|
634
|
+
return GSL::Stats.correlation(v1.gv, v2.gv)
|
635
|
+
}
|
636
|
+
end
|
637
|
+
|
638
|
+
def graph (*args)
|
639
|
+
if args.last.is_a?(String)
|
640
|
+
options = args.pop
|
641
|
+
else
|
642
|
+
options = nil
|
643
|
+
end
|
644
|
+
args.map! do |arg|
|
645
|
+
case arg
|
646
|
+
when CArray
|
647
|
+
arg.to_gv
|
648
|
+
when Array
|
649
|
+
arg.map{|a| a.is_a?(CArray) ? a.to_gv : a }
|
650
|
+
else
|
651
|
+
arg
|
652
|
+
end
|
653
|
+
end
|
654
|
+
return GSL.graph(*args)
|
655
|
+
end
|
656
|
+
|
657
|
+
end
|
658
|
+
|
659
|
+
class CAMatrix
|
660
|
+
|
661
|
+
def * (other)
|
662
|
+
case other
|
663
|
+
when CAVector
|
664
|
+
CArray.attach(self, other) {
|
665
|
+
return (self.gm * other.gv).ca.v
|
666
|
+
}
|
667
|
+
when CAMatrix
|
668
|
+
CArray.attach(self, other) {
|
669
|
+
return (self.gm * other.gm).ca.m
|
670
|
+
}
|
671
|
+
when CArray
|
672
|
+
CArray.attach(self, other) {
|
673
|
+
return (self[] * other).m
|
674
|
+
}
|
675
|
+
else
|
676
|
+
attach {
|
677
|
+
return (self.gm * other).ca.m
|
678
|
+
}
|
679
|
+
end
|
680
|
+
end
|
681
|
+
|
682
|
+
def lower
|
683
|
+
attach {
|
684
|
+
return self.gm.lower.ca.m
|
685
|
+
}
|
686
|
+
end
|
687
|
+
|
688
|
+
def upper
|
689
|
+
attach {
|
690
|
+
return self.gm.lower.ca.m
|
691
|
+
}
|
692
|
+
end
|
693
|
+
|
694
|
+
def diagonal
|
695
|
+
attach {
|
696
|
+
return self.gm.diagonal.ca.v
|
697
|
+
}
|
698
|
+
end
|
699
|
+
|
700
|
+
end
|
701
|
+
|
702
|
+
class CAVector
|
703
|
+
|
704
|
+
def * (other)
|
705
|
+
case other
|
706
|
+
when CAVector
|
707
|
+
CArray.attach(self, other) {
|
708
|
+
out = self.gv * other.gv
|
709
|
+
case out
|
710
|
+
when GSL::Matrix
|
711
|
+
return out.ca.m
|
712
|
+
when GSL::Vector
|
713
|
+
return out.ca.v
|
714
|
+
else
|
715
|
+
return out
|
716
|
+
end
|
717
|
+
}
|
718
|
+
when CAMatrix
|
719
|
+
CArray.attach(self, other) {
|
720
|
+
return (self.gv * other.gm).ca.v
|
721
|
+
}
|
722
|
+
when CArray
|
723
|
+
CArray.attach(self, other) {
|
724
|
+
return (self[] * other).v
|
725
|
+
}
|
726
|
+
else
|
727
|
+
attach {
|
728
|
+
return (self.gv * other).ca.v
|
729
|
+
}
|
730
|
+
end
|
731
|
+
end
|
732
|
+
|
733
|
+
alias t_orig t
|
734
|
+
|
735
|
+
def t
|
736
|
+
case rank
|
737
|
+
when 1
|
738
|
+
return self.reshape(elements,1).v
|
739
|
+
else
|
740
|
+
return t_orig.v
|
741
|
+
end
|
742
|
+
end
|
743
|
+
|
744
|
+
def norm
|
745
|
+
attach {
|
746
|
+
return self.gv.nrm2
|
747
|
+
}
|
748
|
+
end
|
749
|
+
|
750
|
+
def normalize (nrm=1.0)
|
751
|
+
attach {
|
752
|
+
return self.gv.normalize(nrm).ca.v
|
753
|
+
}
|
754
|
+
end
|
755
|
+
|
756
|
+
def normalize!
|
757
|
+
attach {
|
758
|
+
self.gv.normalize!(nrm)
|
759
|
+
}
|
760
|
+
return self
|
761
|
+
end
|
762
|
+
|
763
|
+
end
|
764
|
+
|
765
|
+
class CArray
|
766
|
+
def vt
|
767
|
+
return v.t
|
768
|
+
end
|
769
|
+
end
|