rb-gsl 1.15.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (660) hide show
  1. checksums.yaml +7 -0
  2. data/AUTHORS +6 -0
  3. data/COPYING +339 -0
  4. data/ChangeLog +567 -0
  5. data/README +32 -0
  6. data/Rakefile +99 -0
  7. data/THANKS +17 -0
  8. data/VERSION +1 -0
  9. data/examples/alf/alf.gp +15 -0
  10. data/examples/alf/alf.rb +32 -0
  11. data/examples/blas/blas.rb +13 -0
  12. data/examples/blas/dnrm2.rb +16 -0
  13. data/examples/blas/level1.rb +81 -0
  14. data/examples/blas/level2.rb +11 -0
  15. data/examples/blas/level3.rb +12 -0
  16. data/examples/bspline.rb +57 -0
  17. data/examples/cdf.rb +16 -0
  18. data/examples/cheb.rb +21 -0
  19. data/examples/combination.rb +23 -0
  20. data/examples/complex/RC-lpf.rb +47 -0
  21. data/examples/complex/add.rb +36 -0
  22. data/examples/complex/coerce.rb +14 -0
  23. data/examples/complex/complex.rb +25 -0
  24. data/examples/complex/fpmi.rb +70 -0
  25. data/examples/complex/functions.rb +77 -0
  26. data/examples/complex/michelson.rb +36 -0
  27. data/examples/complex/mul.rb +28 -0
  28. data/examples/complex/oscillator.rb +17 -0
  29. data/examples/complex/set.rb +37 -0
  30. data/examples/const/physconst.rb +151 -0
  31. data/examples/const/travel.rb +45 -0
  32. data/examples/deriv/demo.rb +13 -0
  33. data/examples/deriv/deriv.rb +36 -0
  34. data/examples/deriv/diff.rb +35 -0
  35. data/examples/dht.rb +42 -0
  36. data/examples/dirac.rb +56 -0
  37. data/examples/eigen/eigen.rb +34 -0
  38. data/examples/eigen/herm.rb +22 -0
  39. data/examples/eigen/narray.rb +9 -0
  40. data/examples/eigen/nonsymm.rb +37 -0
  41. data/examples/eigen/nonsymmv.rb +43 -0
  42. data/examples/eigen/qhoscillator.gp +35 -0
  43. data/examples/eigen/qhoscillator.rb +90 -0
  44. data/examples/eigen/vander.rb +41 -0
  45. data/examples/fft/fft.rb +17 -0
  46. data/examples/fft/fft2.rb +17 -0
  47. data/examples/fft/forward.rb +25 -0
  48. data/examples/fft/forward2.rb +26 -0
  49. data/examples/fft/radix2.rb +18 -0
  50. data/examples/fft/real-halfcomplex.rb +33 -0
  51. data/examples/fft/real-halfcomplex2.rb +30 -0
  52. data/examples/fft/realradix2.rb +19 -0
  53. data/examples/fft/sunspot.dat +256 -0
  54. data/examples/fft/sunspot.rb +16 -0
  55. data/examples/fit/expdata.dat +20 -0
  56. data/examples/fit/expfit.rb +31 -0
  57. data/examples/fit/gaussfit.rb +29 -0
  58. data/examples/fit/gaussian_2peaks.rb +34 -0
  59. data/examples/fit/hillfit.rb +40 -0
  60. data/examples/fit/lognormal.rb +26 -0
  61. data/examples/fit/lorentzfit.rb +22 -0
  62. data/examples/fit/multifit.rb +72 -0
  63. data/examples/fit/ndlinear.rb +133 -0
  64. data/examples/fit/nonlinearfit.rb +89 -0
  65. data/examples/fit/plot.gp +36 -0
  66. data/examples/fit/polyfit.rb +9 -0
  67. data/examples/fit/powerfit.rb +21 -0
  68. data/examples/fit/sigmoidfit.rb +40 -0
  69. data/examples/fit/sinfit.rb +22 -0
  70. data/examples/fit/wlinear.rb +46 -0
  71. data/examples/fresnel.rb +11 -0
  72. data/examples/function/function.rb +36 -0
  73. data/examples/function/log.rb +7 -0
  74. data/examples/function/min.rb +33 -0
  75. data/examples/function/sin.rb +10 -0
  76. data/examples/function/synchrotron.rb +18 -0
  77. data/examples/gallery/butterfly.rb +7 -0
  78. data/examples/gallery/cayley.rb +12 -0
  79. data/examples/gallery/cornu.rb +23 -0
  80. data/examples/gallery/eight.rb +11 -0
  81. data/examples/gallery/koch.rb +40 -0
  82. data/examples/gallery/lemniscate.rb +11 -0
  83. data/examples/gallery/polar.rb +11 -0
  84. data/examples/gallery/rgplot/cossin.rb +35 -0
  85. data/examples/gallery/rgplot/rgplot.replaced +0 -0
  86. data/examples/gallery/rgplot/roesller.rb +55 -0
  87. data/examples/gallery/roesller.rb +39 -0
  88. data/examples/gallery/scarabaeus.rb +14 -0
  89. data/examples/histogram/cauchy.rb +27 -0
  90. data/examples/histogram/cauchy.sh +2 -0
  91. data/examples/histogram/exponential.rb +19 -0
  92. data/examples/histogram/gauss.rb +16 -0
  93. data/examples/histogram/gsl-histogram.rb +40 -0
  94. data/examples/histogram/histo2d.rb +31 -0
  95. data/examples/histogram/histo3d.rb +34 -0
  96. data/examples/histogram/histogram-pdf.rb +27 -0
  97. data/examples/histogram/histogram.rb +26 -0
  98. data/examples/histogram/integral.rb +28 -0
  99. data/examples/histogram/poisson.rb +27 -0
  100. data/examples/histogram/power.rb +25 -0
  101. data/examples/histogram/rebin.rb +17 -0
  102. data/examples/histogram/smp.dat +5 -0
  103. data/examples/histogram/xexp.rb +21 -0
  104. data/examples/integration/ahmed.rb +21 -0
  105. data/examples/integration/cosmology.rb +75 -0
  106. data/examples/integration/friedmann.gp +16 -0
  107. data/examples/integration/friedmann.rb +35 -0
  108. data/examples/integration/gamma-zeta.rb +35 -0
  109. data/examples/integration/integration.rb +22 -0
  110. data/examples/integration/qag.rb +8 -0
  111. data/examples/integration/qag2.rb +14 -0
  112. data/examples/integration/qag3.rb +8 -0
  113. data/examples/integration/qagi.rb +28 -0
  114. data/examples/integration/qagi2.rb +49 -0
  115. data/examples/integration/qagiu.rb +29 -0
  116. data/examples/integration/qagp.rb +20 -0
  117. data/examples/integration/qags.rb +14 -0
  118. data/examples/integration/qawc.rb +18 -0
  119. data/examples/integration/qawf.rb +41 -0
  120. data/examples/integration/qawo.rb +29 -0
  121. data/examples/integration/qaws.rb +30 -0
  122. data/examples/integration/qng.rb +17 -0
  123. data/examples/interp/demo.gp +20 -0
  124. data/examples/interp/demo.rb +45 -0
  125. data/examples/interp/interp.rb +37 -0
  126. data/examples/interp/points +10 -0
  127. data/examples/interp/spline.rb +20 -0
  128. data/examples/jacobi/deriv.rb +40 -0
  129. data/examples/jacobi/integrate.rb +34 -0
  130. data/examples/jacobi/interp.rb +43 -0
  131. data/examples/jacobi/jacobi.rb +11 -0
  132. data/examples/linalg/HH.rb +15 -0
  133. data/examples/linalg/HH_narray.rb +13 -0
  134. data/examples/linalg/LQ_solve.rb +73 -0
  135. data/examples/linalg/LU.rb +84 -0
  136. data/examples/linalg/LU2.rb +31 -0
  137. data/examples/linalg/LU_narray.rb +24 -0
  138. data/examples/linalg/PTLQ.rb +47 -0
  139. data/examples/linalg/QR.rb +18 -0
  140. data/examples/linalg/QRPT.rb +47 -0
  141. data/examples/linalg/QR_solve.rb +78 -0
  142. data/examples/linalg/QR_solve_narray.rb +13 -0
  143. data/examples/linalg/SV.rb +16 -0
  144. data/examples/linalg/SV_narray.rb +12 -0
  145. data/examples/linalg/SV_solve.rb +49 -0
  146. data/examples/linalg/chol.rb +29 -0
  147. data/examples/linalg/chol_narray.rb +15 -0
  148. data/examples/linalg/complex.rb +57 -0
  149. data/examples/linalg/invert_narray.rb +10 -0
  150. data/examples/math/const.rb +67 -0
  151. data/examples/math/elementary.rb +35 -0
  152. data/examples/math/functions.rb +41 -0
  153. data/examples/math/inf_nan.rb +34 -0
  154. data/examples/math/minmax.rb +22 -0
  155. data/examples/math/power.rb +18 -0
  156. data/examples/math/test.rb +31 -0
  157. data/examples/matrix/a.dat +0 -0
  158. data/examples/matrix/add.rb +45 -0
  159. data/examples/matrix/b.dat +4 -0
  160. data/examples/matrix/cat.rb +31 -0
  161. data/examples/matrix/colvectors.rb +24 -0
  162. data/examples/matrix/complex.rb +41 -0
  163. data/examples/matrix/det.rb +29 -0
  164. data/examples/matrix/diagonal.rb +23 -0
  165. data/examples/matrix/get_all.rb +159 -0
  166. data/examples/matrix/hilbert.rb +31 -0
  167. data/examples/matrix/iterator.rb +19 -0
  168. data/examples/matrix/matrix.rb +57 -0
  169. data/examples/matrix/minmax.rb +53 -0
  170. data/examples/matrix/mul.rb +39 -0
  171. data/examples/matrix/rand.rb +20 -0
  172. data/examples/matrix/read.rb +29 -0
  173. data/examples/matrix/rowcol.rb +47 -0
  174. data/examples/matrix/set.rb +41 -0
  175. data/examples/matrix/set_all.rb +100 -0
  176. data/examples/matrix/view.rb +32 -0
  177. data/examples/matrix/view_all.rb +148 -0
  178. data/examples/matrix/write.rb +23 -0
  179. data/examples/min.rb +29 -0
  180. data/examples/monte/miser.rb +47 -0
  181. data/examples/monte/monte.rb +47 -0
  182. data/examples/monte/plain.rb +47 -0
  183. data/examples/monte/vegas.rb +46 -0
  184. data/examples/multimin/bundle.rb +66 -0
  185. data/examples/multimin/cqp.rb +109 -0
  186. data/examples/multimin/fdfminimizer.rb +40 -0
  187. data/examples/multimin/fminimizer.rb +41 -0
  188. data/examples/multiroot/demo.rb +36 -0
  189. data/examples/multiroot/fdfsolver.rb +50 -0
  190. data/examples/multiroot/fsolver.rb +33 -0
  191. data/examples/multiroot/fsolver2.rb +32 -0
  192. data/examples/multiroot/fsolver3.rb +26 -0
  193. data/examples/narray/histogram.rb +14 -0
  194. data/examples/narray/mandel.rb +27 -0
  195. data/examples/narray/narray.rb +28 -0
  196. data/examples/narray/narray2.rb +44 -0
  197. data/examples/narray/sf.rb +26 -0
  198. data/examples/ntuple/create.rb +17 -0
  199. data/examples/ntuple/project.rb +31 -0
  200. data/examples/odeiv/binarysystem.gp +23 -0
  201. data/examples/odeiv/binarysystem.rb +104 -0
  202. data/examples/odeiv/demo.gp +24 -0
  203. data/examples/odeiv/demo.rb +69 -0
  204. data/examples/odeiv/demo2.gp +26 -0
  205. data/examples/odeiv/duffing.rb +45 -0
  206. data/examples/odeiv/frei1.rb +109 -0
  207. data/examples/odeiv/frei2.rb +76 -0
  208. data/examples/odeiv/legendre.rb +52 -0
  209. data/examples/odeiv/odeiv.rb +32 -0
  210. data/examples/odeiv/odeiv2.rb +45 -0
  211. data/examples/odeiv/oscillator.rb +42 -0
  212. data/examples/odeiv/sedov.rb +97 -0
  213. data/examples/odeiv/whitedwarf.gp +40 -0
  214. data/examples/odeiv/whitedwarf.rb +158 -0
  215. data/examples/ool/conmin.rb +100 -0
  216. data/examples/ool/gencan.rb +99 -0
  217. data/examples/ool/pgrad.rb +100 -0
  218. data/examples/ool/spg.rb +100 -0
  219. data/examples/pdf/bernoulli.rb +5 -0
  220. data/examples/pdf/beta.rb +7 -0
  221. data/examples/pdf/binomiral.rb +10 -0
  222. data/examples/pdf/cauchy.rb +6 -0
  223. data/examples/pdf/chisq.rb +8 -0
  224. data/examples/pdf/exponential.rb +7 -0
  225. data/examples/pdf/exppow.rb +6 -0
  226. data/examples/pdf/fdist.rb +7 -0
  227. data/examples/pdf/flat.rb +7 -0
  228. data/examples/pdf/gamma.rb +8 -0
  229. data/examples/pdf/gauss-tail.rb +5 -0
  230. data/examples/pdf/gauss.rb +6 -0
  231. data/examples/pdf/geometric.rb +5 -0
  232. data/examples/pdf/gumbel.rb +6 -0
  233. data/examples/pdf/hypergeometric.rb +11 -0
  234. data/examples/pdf/landau.rb +5 -0
  235. data/examples/pdf/laplace.rb +7 -0
  236. data/examples/pdf/logarithmic.rb +5 -0
  237. data/examples/pdf/logistic.rb +6 -0
  238. data/examples/pdf/lognormal.rb +6 -0
  239. data/examples/pdf/neg-binomiral.rb +10 -0
  240. data/examples/pdf/pareto.rb +7 -0
  241. data/examples/pdf/pascal.rb +10 -0
  242. data/examples/pdf/poisson.rb +5 -0
  243. data/examples/pdf/rayleigh-tail.rb +6 -0
  244. data/examples/pdf/rayleigh.rb +6 -0
  245. data/examples/pdf/tdist.rb +6 -0
  246. data/examples/pdf/weibull.rb +8 -0
  247. data/examples/permutation/ex1.rb +22 -0
  248. data/examples/permutation/permutation.rb +16 -0
  249. data/examples/poly/bell.rb +6 -0
  250. data/examples/poly/bessel.rb +6 -0
  251. data/examples/poly/cheb.rb +6 -0
  252. data/examples/poly/cheb_II.rb +6 -0
  253. data/examples/poly/cubic.rb +9 -0
  254. data/examples/poly/demo.rb +20 -0
  255. data/examples/poly/eval.rb +28 -0
  256. data/examples/poly/eval_derivs.rb +14 -0
  257. data/examples/poly/fit.rb +21 -0
  258. data/examples/poly/hermite.rb +6 -0
  259. data/examples/poly/poly.rb +13 -0
  260. data/examples/poly/quadratic.rb +25 -0
  261. data/examples/random/diffusion.rb +34 -0
  262. data/examples/random/gaussian.rb +9 -0
  263. data/examples/random/generator.rb +27 -0
  264. data/examples/random/hdsobol.rb +21 -0
  265. data/examples/random/poisson.rb +9 -0
  266. data/examples/random/qrng.rb +19 -0
  267. data/examples/random/randomwalk.rb +37 -0
  268. data/examples/random/randomwalk2d.rb +19 -0
  269. data/examples/random/rayleigh.rb +36 -0
  270. data/examples/random/rng.rb +33 -0
  271. data/examples/random/rngextra.rb +14 -0
  272. data/examples/roots/bisection.rb +25 -0
  273. data/examples/roots/brent.rb +43 -0
  274. data/examples/roots/demo.rb +30 -0
  275. data/examples/roots/newton.rb +46 -0
  276. data/examples/roots/recombination.gp +12 -0
  277. data/examples/roots/recombination.rb +61 -0
  278. data/examples/roots/steffenson.rb +48 -0
  279. data/examples/sf/ShiChi.rb +6 -0
  280. data/examples/sf/SiCi.rb +6 -0
  281. data/examples/sf/airy_Ai.rb +8 -0
  282. data/examples/sf/airy_Bi.rb +8 -0
  283. data/examples/sf/bessel_IK.rb +12 -0
  284. data/examples/sf/bessel_JY.rb +13 -0
  285. data/examples/sf/beta_inc.rb +9 -0
  286. data/examples/sf/clausen.rb +6 -0
  287. data/examples/sf/dawson.rb +5 -0
  288. data/examples/sf/debye.rb +9 -0
  289. data/examples/sf/dilog.rb +6 -0
  290. data/examples/sf/ellint.rb +6 -0
  291. data/examples/sf/expint.rb +8 -0
  292. data/examples/sf/fermi.rb +10 -0
  293. data/examples/sf/gamma_inc_P.rb +9 -0
  294. data/examples/sf/gegenbauer.rb +8 -0
  295. data/examples/sf/hyperg.rb +7 -0
  296. data/examples/sf/laguerre.rb +19 -0
  297. data/examples/sf/lambertW.rb +5 -0
  298. data/examples/sf/legendre_P.rb +10 -0
  299. data/examples/sf/lngamma.rb +5 -0
  300. data/examples/sf/psi.rb +54 -0
  301. data/examples/sf/sphbessel.gp +27 -0
  302. data/examples/sf/sphbessel.rb +30 -0
  303. data/examples/sf/synchrotron.rb +5 -0
  304. data/examples/sf/transport.rb +10 -0
  305. data/examples/sf/zetam1.rb +5 -0
  306. data/examples/siman.rb +44 -0
  307. data/examples/sort/heapsort.rb +23 -0
  308. data/examples/sort/heapsort_vector_complex.rb +21 -0
  309. data/examples/sort/sort.rb +23 -0
  310. data/examples/sort/sort2.rb +16 -0
  311. data/examples/stats/mean.rb +17 -0
  312. data/examples/stats/statistics.rb +18 -0
  313. data/examples/stats/test.rb +9 -0
  314. data/examples/sum.rb +34 -0
  315. data/examples/tamu_anova.rb +18 -0
  316. data/examples/vector/a.dat +0 -0
  317. data/examples/vector/add.rb +56 -0
  318. data/examples/vector/b.dat +4 -0
  319. data/examples/vector/c.dat +3 -0
  320. data/examples/vector/collect.rb +26 -0
  321. data/examples/vector/compare.rb +28 -0
  322. data/examples/vector/complex.rb +51 -0
  323. data/examples/vector/complex_get_all.rb +85 -0
  324. data/examples/vector/complex_set_all.rb +131 -0
  325. data/examples/vector/complex_view_all.rb +77 -0
  326. data/examples/vector/connect.rb +22 -0
  327. data/examples/vector/decimate.rb +38 -0
  328. data/examples/vector/diff.rb +31 -0
  329. data/examples/vector/filescan.rb +17 -0
  330. data/examples/vector/floor.rb +23 -0
  331. data/examples/vector/get_all.rb +82 -0
  332. data/examples/vector/gnuplot.rb +38 -0
  333. data/examples/vector/graph.rb +28 -0
  334. data/examples/vector/histogram.rb +22 -0
  335. data/examples/vector/linspace.rb +24 -0
  336. data/examples/vector/log.rb +17 -0
  337. data/examples/vector/logic.rb +33 -0
  338. data/examples/vector/logspace.rb +25 -0
  339. data/examples/vector/minmax.rb +47 -0
  340. data/examples/vector/mul.rb +49 -0
  341. data/examples/vector/narray.rb +46 -0
  342. data/examples/vector/read.rb +29 -0
  343. data/examples/vector/set.rb +35 -0
  344. data/examples/vector/set_all.rb +121 -0
  345. data/examples/vector/smpv.dat +15 -0
  346. data/examples/vector/test.rb +43 -0
  347. data/examples/vector/test_gslblock.rb +58 -0
  348. data/examples/vector/vector.rb +110 -0
  349. data/examples/vector/view.rb +35 -0
  350. data/examples/vector/view_all.rb +73 -0
  351. data/examples/vector/where.rb +29 -0
  352. data/examples/vector/write.rb +24 -0
  353. data/examples/vector/zip.rb +34 -0
  354. data/examples/wavelet/ecg.dat +256 -0
  355. data/examples/wavelet/wavelet1.rb +50 -0
  356. data/ext/alf.c +206 -0
  357. data/ext/array.c +642 -0
  358. data/ext/array_complex.c +248 -0
  359. data/ext/blas.c +29 -0
  360. data/ext/blas1.c +734 -0
  361. data/ext/blas2.c +1093 -0
  362. data/ext/blas3.c +881 -0
  363. data/ext/block.c +44 -0
  364. data/ext/block_source.c +887 -0
  365. data/ext/bspline.c +130 -0
  366. data/ext/bundle.c +3 -0
  367. data/ext/cdf.c +754 -0
  368. data/ext/cheb.c +542 -0
  369. data/ext/combination.c +283 -0
  370. data/ext/common.c +325 -0
  371. data/ext/complex.c +1004 -0
  372. data/ext/const.c +673 -0
  373. data/ext/const_additional.c +120 -0
  374. data/ext/cqp.c +283 -0
  375. data/ext/deriv.c +195 -0
  376. data/ext/dht.c +361 -0
  377. data/ext/diff.c +166 -0
  378. data/ext/dirac.c +389 -0
  379. data/ext/eigen.c +2373 -0
  380. data/ext/error.c +193 -0
  381. data/ext/extconf.rb +303 -0
  382. data/ext/fcmp.c +66 -0
  383. data/ext/fft.c +1102 -0
  384. data/ext/fit.c +205 -0
  385. data/ext/fresnel.c +312 -0
  386. data/ext/function.c +524 -0
  387. data/ext/geometry.c +139 -0
  388. data/ext/graph.c +1640 -0
  389. data/ext/gsl.c +280 -0
  390. data/ext/gsl_narray.c +804 -0
  391. data/ext/histogram.c +1991 -0
  392. data/ext/histogram2d.c +1068 -0
  393. data/ext/histogram3d.c +884 -0
  394. data/ext/histogram3d_source.c +750 -0
  395. data/ext/histogram_find.c +101 -0
  396. data/ext/histogram_oper.c +159 -0
  397. data/ext/ieee.c +100 -0
  398. data/ext/integration.c +1179 -0
  399. data/ext/interp.c +512 -0
  400. data/ext/jacobi.c +739 -0
  401. data/ext/linalg.c +4042 -0
  402. data/ext/linalg_complex.c +739 -0
  403. data/ext/math.c +725 -0
  404. data/ext/matrix.c +39 -0
  405. data/ext/matrix_complex.c +1737 -0
  406. data/ext/matrix_double.c +560 -0
  407. data/ext/matrix_int.c +256 -0
  408. data/ext/matrix_source.c +2734 -0
  409. data/ext/min.c +250 -0
  410. data/ext/monte.c +992 -0
  411. data/ext/multifit.c +1871 -0
  412. data/ext/multimin.c +806 -0
  413. data/ext/multimin_fsdf.c +156 -0
  414. data/ext/multiroots.c +955 -0
  415. data/ext/multiset.c +214 -0
  416. data/ext/ndlinear.c +321 -0
  417. data/ext/nmf.c +171 -0
  418. data/ext/nmf_wrap.c +75 -0
  419. data/ext/ntuple.c +469 -0
  420. data/ext/odeiv.c +962 -0
  421. data/ext/ool.c +879 -0
  422. data/ext/oper_complex_source.c +253 -0
  423. data/ext/permutation.c +596 -0
  424. data/ext/poly.c +42 -0
  425. data/ext/poly2.c +265 -0
  426. data/ext/poly_source.c +1915 -0
  427. data/ext/qrng.c +171 -0
  428. data/ext/randist.c +1873 -0
  429. data/ext/rational.c +480 -0
  430. data/ext/rng.c +612 -0
  431. data/ext/root.c +408 -0
  432. data/ext/sf.c +1494 -0
  433. data/ext/sf_airy.c +200 -0
  434. data/ext/sf_bessel.c +871 -0
  435. data/ext/sf_clausen.c +28 -0
  436. data/ext/sf_coulomb.c +206 -0
  437. data/ext/sf_coupling.c +121 -0
  438. data/ext/sf_dawson.c +29 -0
  439. data/ext/sf_debye.c +157 -0
  440. data/ext/sf_dilog.c +43 -0
  441. data/ext/sf_elementary.c +46 -0
  442. data/ext/sf_ellint.c +206 -0
  443. data/ext/sf_elljac.c +30 -0
  444. data/ext/sf_erfc.c +93 -0
  445. data/ext/sf_exp.c +169 -0
  446. data/ext/sf_expint.c +211 -0
  447. data/ext/sf_fermi_dirac.c +148 -0
  448. data/ext/sf_gamma.c +347 -0
  449. data/ext/sf_gegenbauer.c +97 -0
  450. data/ext/sf_hyperg.c +203 -0
  451. data/ext/sf_laguerre.c +113 -0
  452. data/ext/sf_lambert.c +47 -0
  453. data/ext/sf_legendre.c +368 -0
  454. data/ext/sf_log.c +105 -0
  455. data/ext/sf_mathieu.c +238 -0
  456. data/ext/sf_power.c +47 -0
  457. data/ext/sf_psi.c +98 -0
  458. data/ext/sf_synchrotron.c +48 -0
  459. data/ext/sf_transport.c +76 -0
  460. data/ext/sf_trigonometric.c +210 -0
  461. data/ext/sf_zeta.c +119 -0
  462. data/ext/signal.c +308 -0
  463. data/ext/siman.c +717 -0
  464. data/ext/sort.c +208 -0
  465. data/ext/spline.c +395 -0
  466. data/ext/stats.c +800 -0
  467. data/ext/sum.c +168 -0
  468. data/ext/tamu_anova.c +56 -0
  469. data/ext/tensor.c +38 -0
  470. data/ext/tensor_source.c +1123 -0
  471. data/ext/vector.c +38 -0
  472. data/ext/vector_complex.c +2246 -0
  473. data/ext/vector_double.c +1438 -0
  474. data/ext/vector_int.c +204 -0
  475. data/ext/vector_source.c +3336 -0
  476. data/ext/wavelet.c +945 -0
  477. data/include/rb_gsl.h +158 -0
  478. data/include/rb_gsl_array.h +238 -0
  479. data/include/rb_gsl_cheb.h +21 -0
  480. data/include/rb_gsl_common.h +348 -0
  481. data/include/rb_gsl_complex.h +25 -0
  482. data/include/rb_gsl_config.h +62 -0
  483. data/include/rb_gsl_const.h +29 -0
  484. data/include/rb_gsl_dirac.h +13 -0
  485. data/include/rb_gsl_eigen.h +17 -0
  486. data/include/rb_gsl_fft.h +62 -0
  487. data/include/rb_gsl_fit.h +25 -0
  488. data/include/rb_gsl_function.h +27 -0
  489. data/include/rb_gsl_graph.h +70 -0
  490. data/include/rb_gsl_histogram.h +63 -0
  491. data/include/rb_gsl_histogram3d.h +97 -0
  492. data/include/rb_gsl_integration.h +17 -0
  493. data/include/rb_gsl_interp.h +46 -0
  494. data/include/rb_gsl_linalg.h +25 -0
  495. data/include/rb_gsl_math.h +26 -0
  496. data/include/rb_gsl_odeiv.h +21 -0
  497. data/include/rb_gsl_poly.h +71 -0
  498. data/include/rb_gsl_rational.h +37 -0
  499. data/include/rb_gsl_rng.h +21 -0
  500. data/include/rb_gsl_root.h +22 -0
  501. data/include/rb_gsl_sf.h +119 -0
  502. data/include/rb_gsl_statistics.h +17 -0
  503. data/include/rb_gsl_tensor.h +45 -0
  504. data/include/rb_gsl_with_narray.h +29 -0
  505. data/include/templates_off.h +87 -0
  506. data/include/templates_on.h +241 -0
  507. data/lib/gsl.rb +3 -0
  508. data/lib/gsl/gnuplot.rb +41 -0
  509. data/lib/gsl/oper.rb +68 -0
  510. data/lib/ool.rb +22 -0
  511. data/lib/ool/conmin.rb +30 -0
  512. data/lib/rbgsl.rb +3 -0
  513. data/rdoc/alf.rdoc +77 -0
  514. data/rdoc/blas.rdoc +269 -0
  515. data/rdoc/bspline.rdoc +42 -0
  516. data/rdoc/changes.rdoc +164 -0
  517. data/rdoc/cheb.rdoc +99 -0
  518. data/rdoc/cholesky_complex.rdoc +46 -0
  519. data/rdoc/combi.rdoc +125 -0
  520. data/rdoc/complex.rdoc +210 -0
  521. data/rdoc/const.rdoc +546 -0
  522. data/rdoc/dht.rdoc +122 -0
  523. data/rdoc/diff.rdoc +133 -0
  524. data/rdoc/ehandling.rdoc +50 -0
  525. data/rdoc/eigen.rdoc +401 -0
  526. data/rdoc/fft.rdoc +535 -0
  527. data/rdoc/fit.rdoc +284 -0
  528. data/rdoc/function.rdoc +94 -0
  529. data/rdoc/graph.rdoc +137 -0
  530. data/rdoc/hist.rdoc +409 -0
  531. data/rdoc/hist2d.rdoc +279 -0
  532. data/rdoc/hist3d.rdoc +112 -0
  533. data/rdoc/index.rdoc +62 -0
  534. data/rdoc/integration.rdoc +398 -0
  535. data/rdoc/interp.rdoc +231 -0
  536. data/rdoc/intro.rdoc +27 -0
  537. data/rdoc/linalg.rdoc +681 -0
  538. data/rdoc/linalg_complex.rdoc +88 -0
  539. data/rdoc/math.rdoc +276 -0
  540. data/rdoc/matrix.rdoc +1093 -0
  541. data/rdoc/min.rdoc +189 -0
  542. data/rdoc/monte.rdoc +234 -0
  543. data/rdoc/multimin.rdoc +312 -0
  544. data/rdoc/multiroot.rdoc +293 -0
  545. data/rdoc/narray.rdoc +177 -0
  546. data/rdoc/ndlinear.rdoc +250 -0
  547. data/rdoc/nonlinearfit.rdoc +348 -0
  548. data/rdoc/ntuple.rdoc +88 -0
  549. data/rdoc/odeiv.rdoc +378 -0
  550. data/rdoc/perm.rdoc +221 -0
  551. data/rdoc/poly.rdoc +335 -0
  552. data/rdoc/qrng.rdoc +90 -0
  553. data/rdoc/randist.rdoc +233 -0
  554. data/rdoc/ref.rdoc +93 -0
  555. data/rdoc/rng.rdoc +203 -0
  556. data/rdoc/rngextra.rdoc +11 -0
  557. data/rdoc/roots.rdoc +305 -0
  558. data/rdoc/screenshot.rdoc +40 -0
  559. data/rdoc/sf.rdoc +1622 -0
  560. data/rdoc/siman.rdoc +89 -0
  561. data/rdoc/sort.rdoc +94 -0
  562. data/rdoc/start.rdoc +16 -0
  563. data/rdoc/stats.rdoc +219 -0
  564. data/rdoc/sum.rdoc +65 -0
  565. data/rdoc/tensor.rdoc +251 -0
  566. data/rdoc/tut.rdoc +5 -0
  567. data/rdoc/use.rdoc +177 -0
  568. data/rdoc/vector.rdoc +1243 -0
  569. data/rdoc/vector_complex.rdoc +347 -0
  570. data/rdoc/wavelet.rdoc +218 -0
  571. data/setup.rb +1585 -0
  572. data/tests/blas/amax.rb +14 -0
  573. data/tests/blas/asum.rb +16 -0
  574. data/tests/blas/axpy.rb +25 -0
  575. data/tests/blas/copy.rb +23 -0
  576. data/tests/blas/dot.rb +23 -0
  577. data/tests/bspline.rb +53 -0
  578. data/tests/cdf.rb +1388 -0
  579. data/tests/cheb.rb +112 -0
  580. data/tests/combination.rb +123 -0
  581. data/tests/complex.rb +17 -0
  582. data/tests/const.rb +24 -0
  583. data/tests/deriv.rb +85 -0
  584. data/tests/dht/dht1.rb +17 -0
  585. data/tests/dht/dht2.rb +23 -0
  586. data/tests/dht/dht3.rb +23 -0
  587. data/tests/dht/dht4.rb +23 -0
  588. data/tests/diff.rb +78 -0
  589. data/tests/eigen/eigen.rb +220 -0
  590. data/tests/eigen/gen.rb +105 -0
  591. data/tests/eigen/genherm.rb +66 -0
  592. data/tests/eigen/gensymm.rb +68 -0
  593. data/tests/eigen/nonsymm.rb +53 -0
  594. data/tests/eigen/nonsymmv.rb +53 -0
  595. data/tests/eigen/symm-herm.rb +74 -0
  596. data/tests/err.rb +58 -0
  597. data/tests/fit.rb +124 -0
  598. data/tests/gsl_test.rb +118 -0
  599. data/tests/gsl_test2.rb +110 -0
  600. data/tests/histo.rb +12 -0
  601. data/tests/integration/integration1.rb +72 -0
  602. data/tests/integration/integration2.rb +71 -0
  603. data/tests/integration/integration3.rb +71 -0
  604. data/tests/integration/integration4.rb +71 -0
  605. data/tests/interp.rb +45 -0
  606. data/tests/linalg/HH.rb +64 -0
  607. data/tests/linalg/LU.rb +47 -0
  608. data/tests/linalg/QR.rb +77 -0
  609. data/tests/linalg/SV.rb +24 -0
  610. data/tests/linalg/TDN.rb +116 -0
  611. data/tests/linalg/TDS.rb +122 -0
  612. data/tests/linalg/bidiag.rb +73 -0
  613. data/tests/linalg/cholesky.rb +20 -0
  614. data/tests/linalg/linalg.rb +158 -0
  615. data/tests/matrix/matrix_complex_test.rb +36 -0
  616. data/tests/matrix/matrix_nmf_test.rb +39 -0
  617. data/tests/matrix/matrix_test.rb +48 -0
  618. data/tests/min.rb +99 -0
  619. data/tests/monte/miser.rb +31 -0
  620. data/tests/monte/vegas.rb +45 -0
  621. data/tests/multifit/test_2dgauss.rb +112 -0
  622. data/tests/multifit/test_brown.rb +90 -0
  623. data/tests/multifit/test_enso.rb +246 -0
  624. data/tests/multifit/test_filip.rb +155 -0
  625. data/tests/multifit/test_gauss.rb +97 -0
  626. data/tests/multifit/test_longley.rb +110 -0
  627. data/tests/multifit/test_multifit.rb +52 -0
  628. data/tests/multimin.rb +139 -0
  629. data/tests/multiroot.rb +131 -0
  630. data/tests/multiset.rb +52 -0
  631. data/tests/narray/blas_dnrm2.rb +20 -0
  632. data/tests/odeiv.rb +353 -0
  633. data/tests/poly/poly.rb +290 -0
  634. data/tests/poly/special.rb +65 -0
  635. data/tests/qrng.rb +131 -0
  636. data/tests/quartic.rb +29 -0
  637. data/tests/randist.rb +134 -0
  638. data/tests/rng.rb +305 -0
  639. data/tests/roots.rb +76 -0
  640. data/tests/run-test.sh +17 -0
  641. data/tests/sf/gsl_test_sf.rb +249 -0
  642. data/tests/sf/test_airy.rb +83 -0
  643. data/tests/sf/test_bessel.rb +306 -0
  644. data/tests/sf/test_coulomb.rb +17 -0
  645. data/tests/sf/test_dilog.rb +25 -0
  646. data/tests/sf/test_gamma.rb +209 -0
  647. data/tests/sf/test_hyperg.rb +356 -0
  648. data/tests/sf/test_legendre.rb +227 -0
  649. data/tests/sf/test_mathieu.rb +59 -0
  650. data/tests/sf/test_mode.rb +19 -0
  651. data/tests/sf/test_sf.rb +839 -0
  652. data/tests/stats.rb +174 -0
  653. data/tests/stats_mt.rb +16 -0
  654. data/tests/sum.rb +98 -0
  655. data/tests/sys.rb +323 -0
  656. data/tests/tensor.rb +419 -0
  657. data/tests/vector/vector_complex_test.rb +101 -0
  658. data/tests/vector/vector_test.rb +141 -0
  659. data/tests/wavelet.rb +142 -0
  660. metadata +789 -0
@@ -0,0 +1,256 @@
1
+ /*
2
+ matrix_int.c
3
+ Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
4
+ (C) Copyright 2001-2006 by Yoshiki Tsunesada
5
+
6
+ Ruby/GSL is free software: you can redistribute it and/or modify it
7
+ under the terms of the GNU General Public License.
8
+ This library is distributed in the hope that it will be useful, but
9
+ WITHOUT ANY WARRANTY.
10
+ */
11
+ #include "rb_gsl_config.h"
12
+ #include "rb_gsl_array.h"
13
+ #include "rb_gsl_complex.h"
14
+ #ifdef HAVE_NARRAY_H
15
+ #include "rb_gsl_with_narray.h"
16
+ #endif
17
+
18
+ int gsl_linalg_matmult_int(const gsl_matrix_int *A,
19
+ const gsl_matrix_int *B, gsl_matrix_int *C);
20
+
21
+
22
+ VALUE rb_gsl_matrix_to_i(VALUE obj);
23
+
24
+ static VALUE rb_gsl_matrix_int_to_i(VALUE obj)
25
+ {
26
+ return obj;
27
+ }
28
+
29
+ VALUE rb_gsl_matrix_int_to_f(VALUE obj)
30
+ {
31
+ gsl_matrix_int *m;
32
+ gsl_matrix *mnew;
33
+ size_t i, j;
34
+ Data_Get_Struct(obj, gsl_matrix_int, m);
35
+ mnew = gsl_matrix_alloc(m->size1, m->size2);
36
+ for (i = 0; i < m->size1; i++) {
37
+ for (j = 0; j < m->size2; j++) {
38
+ gsl_matrix_set(mnew, i, j, (double) gsl_matrix_int_get(m, i, j));
39
+ }
40
+ }
41
+ return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
42
+ }
43
+
44
+ static VALUE rb_gsl_matrix_int_to_complex(VALUE obj)
45
+ {
46
+ gsl_matrix_int *m;
47
+ gsl_matrix_complex *mnew;
48
+ gsl_complex z;
49
+ size_t i, j;
50
+ Data_Get_Struct(obj, gsl_matrix_int, m);
51
+ mnew = gsl_matrix_complex_alloc(m->size1, m->size2);
52
+ for (i = 0; i < m->size1; i++) {
53
+ for (j = 0; j < m->size2; j++) {
54
+ GSL_SET_REAL(&z, (double) gsl_matrix_int_get(m, i, j));
55
+ GSL_SET_IMAG(&z, 0.0);
56
+ gsl_matrix_complex_set(mnew, i, j, z);
57
+ }
58
+ }
59
+ return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, mnew);
60
+ }
61
+
62
+ static VALUE rb_gsl_matrix_int_coerce(VALUE obj, VALUE other)
63
+ {
64
+ return rb_ary_new3(2, other, rb_gsl_matrix_int_to_f(obj));
65
+ }
66
+
67
+ enum {
68
+ GSL_MATRIX_INT_ADD,
69
+ GSL_MATRIX_INT_SUB,
70
+ GSL_MATRIX_INT_MUL,
71
+ GSL_MATRIX_INT_DIV,
72
+ };
73
+
74
+ static VALUE rb_gsl_matrix_int_operation1(VALUE obj, VALUE other, int flag)
75
+ {
76
+ gsl_matrix_int *a, *anew, *b;
77
+ gsl_vector_int *vi, *vinew;
78
+ double bval;
79
+ // local variable "result" declared and set, but never used
80
+ //int result;
81
+ Data_Get_Struct(obj, gsl_matrix_int, a);
82
+ switch (TYPE(other)) {
83
+ case T_FIXNUM:
84
+ case T_FLOAT:
85
+ bval = NUM2INT(other);
86
+ anew = make_matrix_int_clone(a);
87
+ switch (flag) {
88
+ case GSL_MATRIX_INT_ADD:
89
+ /*result =*/ gsl_matrix_int_add_constant(anew, bval);
90
+ break;
91
+ case GSL_MATRIX_INT_SUB:
92
+ /*result =*/ gsl_matrix_int_add_constant(anew, -bval);
93
+ break;
94
+ case GSL_MATRIX_INT_MUL:
95
+ /*result =*/ gsl_matrix_int_scale(anew, bval);
96
+ break;
97
+ case GSL_MATRIX_INT_DIV:
98
+ /*result =*/ gsl_matrix_int_scale(anew, 1.0/bval);
99
+ break;
100
+ default:
101
+ break;
102
+ }
103
+ break;
104
+ default:
105
+ if (MATRIX_P(other)) other = rb_gsl_matrix_to_i(other);
106
+ if (VECTOR_P(other)) other = rb_gsl_vector_to_i(other);
107
+ if (MATRIX_INT_P(other)) {
108
+ anew = make_matrix_int_clone(a);
109
+ Data_Get_Struct(other, gsl_matrix_int, b);
110
+ switch (flag) {
111
+ case GSL_MATRIX_INT_ADD:
112
+ /*result =*/ gsl_matrix_int_add(anew, b);
113
+ break;
114
+ case GSL_MATRIX_INT_SUB:
115
+ /*result =*/ gsl_matrix_int_sub(anew, b);
116
+ break;
117
+ case GSL_MATRIX_INT_MUL:
118
+ /*result =*/ gsl_matrix_int_mul_elements(anew, b);
119
+ break;
120
+ case GSL_MATRIX_INT_DIV:
121
+ /*result =*/ gsl_matrix_int_div_elements(anew, b);
122
+ break;
123
+ default:
124
+ break;
125
+ }
126
+ } else if (VECTOR_INT_COL_P(other)) {
127
+ switch (flag) {
128
+ case GSL_MATRIX_INT_MUL:
129
+ Data_Get_Struct(other, gsl_vector_int, vi);
130
+ vinew = gsl_vector_int_alloc(vi->size);
131
+ gsl_matrix_int_mul_vector(vinew, a, vi);
132
+ return Data_Wrap_Struct(cgsl_vector_int_col, 0, gsl_vector_int_free, vinew);
133
+ break;
134
+ default:
135
+ rb_raise(rb_eRuntimeError, "Operation not defined");
136
+ }
137
+ } else {
138
+ rb_raise(rb_eTypeError, "Operation not defined with %s",
139
+ rb_class2name(CLASS_OF(other)));
140
+ }
141
+ break;
142
+ }
143
+ return Data_Wrap_Struct(cgsl_matrix_int, 0, gsl_matrix_int_free, anew);
144
+ }
145
+
146
+ static VALUE rb_gsl_matrix_int_add(VALUE obj, VALUE other)
147
+ {
148
+ return rb_gsl_matrix_int_operation1(obj, other, GSL_MATRIX_INT_ADD);
149
+ }
150
+
151
+ static VALUE rb_gsl_matrix_int_sub(VALUE obj, VALUE other)
152
+ {
153
+ return rb_gsl_matrix_int_operation1(obj, other, GSL_MATRIX_INT_SUB);
154
+ }
155
+
156
+ static VALUE rb_gsl_matrix_int_mul(VALUE obj, VALUE other)
157
+ {
158
+ return rb_gsl_matrix_int_operation1(obj, other, GSL_MATRIX_INT_MUL);
159
+ }
160
+
161
+ static VALUE rb_gsl_matrix_int_div(VALUE obj, VALUE other)
162
+ {
163
+ return rb_gsl_matrix_int_operation1(obj, other, GSL_MATRIX_INT_DIV);
164
+ }
165
+
166
+ static VALUE rb_gsl_matrix_int_matrix_mul(VALUE obj, VALUE bb)
167
+ {
168
+ gsl_matrix_int *m = NULL, *b = NULL, *mnew = NULL;
169
+ gsl_vector_int *vi, *vinew;
170
+ Data_Get_Struct(obj, gsl_matrix_int, m);
171
+ if (MATRIX_INT_P(bb)) {
172
+ Data_Get_Struct(bb, gsl_matrix_int, b);
173
+ mnew = gsl_matrix_int_alloc(m->size1, b->size2);
174
+ gsl_linalg_matmult_int(m, b, mnew);
175
+ return Data_Wrap_Struct(cgsl_matrix_int, 0, gsl_matrix_int_free, mnew);
176
+ } else {
177
+ if (VECTOR_INT_COL_P(bb)) {
178
+ Data_Get_Struct(bb, gsl_vector_int, vi);
179
+ vinew = gsl_vector_int_alloc(vi->size);
180
+ gsl_matrix_int_mul_vector(vinew, m, vi);
181
+ return Data_Wrap_Struct(cgsl_vector_int_col, 0, gsl_vector_int_free, vinew);
182
+ }
183
+ switch (TYPE(bb)) {
184
+ case T_FIXNUM:
185
+ return rb_gsl_matrix_int_mul(obj, bb);
186
+ /* return rb_gsl_matrix_int_power(obj, bb);*/
187
+ break;
188
+ default:
189
+ rb_raise(rb_eTypeError, "wrong argument type %s (Matrix::Int, Vector::Int::Col or Fixnum expected)",
190
+ rb_class2name(CLASS_OF(bb)));
191
+ break;
192
+ }
193
+ }
194
+ }
195
+
196
+ int gsl_linalg_matmult_int(const gsl_matrix_int *A,
197
+ const gsl_matrix_int *B, gsl_matrix_int *C)
198
+ {
199
+ if (A->size2 != B->size1 || A->size1 != C->size1 || B->size2 != C->size2)
200
+ {
201
+ GSL_ERROR ("matrix sizes are not conformant", GSL_EBADLEN);
202
+ }
203
+ else
204
+ {
205
+ int a, b;
206
+ int temp;
207
+ size_t i, j, k;
208
+
209
+ for (i = 0; i < C->size1; i++)
210
+ {
211
+ for (j = 0; j < C->size2; j++)
212
+ {
213
+ a = gsl_matrix_int_get(A, i, 0);
214
+ b = gsl_matrix_int_get(B, 0, j);
215
+ temp = a * b;
216
+ for (k = 1; k < A->size2; k++)
217
+ {
218
+ a = gsl_matrix_int_get(A, i, k);
219
+ b = gsl_matrix_int_get(B, k, j);
220
+ temp += a * b;
221
+ }
222
+ gsl_matrix_int_set(C, i, j, temp);
223
+ }
224
+ }
225
+
226
+ return GSL_SUCCESS;
227
+ }
228
+ }
229
+
230
+ void Init_gsl_matrix_int_init(VALUE module);
231
+ void Init_gsl_matrix_int(VALUE module)
232
+ {
233
+ Init_gsl_matrix_int_init(module);
234
+ /*****/
235
+
236
+ rb_define_method(cgsl_matrix_int, "to_f", rb_gsl_matrix_int_to_f, 0);
237
+ rb_define_method(cgsl_matrix_int, "to_i", rb_gsl_matrix_int_to_i, 0);
238
+ rb_define_method(cgsl_matrix_int, "to_complex", rb_gsl_matrix_int_to_complex, 0);
239
+
240
+ /*****/
241
+ rb_define_method(cgsl_matrix_int, "coerce", rb_gsl_matrix_int_coerce, 1);
242
+ /*****/
243
+ rb_define_method(cgsl_matrix_int, "add", rb_gsl_matrix_int_add, 1);
244
+ rb_define_alias(cgsl_matrix_int, "+", "add");
245
+ rb_define_method(cgsl_matrix_int, "sub", rb_gsl_matrix_int_sub, 1);
246
+ rb_define_alias(cgsl_matrix_int, "-", "sub");
247
+ rb_define_method(cgsl_matrix_int, "mul", rb_gsl_matrix_int_mul, 1);
248
+ /* rb_define_alias(cgsl_matrix_int, "*", "mul");*/
249
+ rb_define_method(cgsl_matrix_int, "div", rb_gsl_matrix_int_div, 1);
250
+ rb_define_alias(cgsl_matrix_int, "/", "div");
251
+
252
+ rb_define_method(cgsl_matrix_int, "matrix_mul", rb_gsl_matrix_int_matrix_mul, 1);
253
+ rb_define_alias(cgsl_matrix_int, "*", "matrix_mul");
254
+ /*****/
255
+
256
+ }
@@ -0,0 +1,2734 @@
1
+ /*
2
+ matrix_source.c
3
+ Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
4
+ (C) Copyright 2001-2006 by Yoshiki Tsunesada
5
+
6
+ Ruby/GSL is free software: you can redistribute it and/or modify it
7
+ under the terms of the GNU General Public License.
8
+ This library is distributed in the hope that it will be useful, but
9
+ WITHOUT ANY WARRANTY.
10
+ */
11
+
12
+ #ifdef BASE_DOUBLE
13
+ #define NUMCONV(x) NUM2DBL(x)
14
+ #define NUMCONV2(x) NUM2DBL(x)
15
+ #define PRINTF_FORMAT "%4.3e "
16
+ #define MAT_ROW_COL MATRIX_ROW_COL
17
+ #define MAT_P MATRIX_P
18
+ #define MAT_ROW_P MATRIX_ROW_P
19
+ #define MAT_COL_P MATRIX_COL_P
20
+ #define C_TO_VALUE rb_float_new
21
+ #define C_TO_VALUE2 rb_float_new
22
+ #define CHECK_MAT CHECK_MATRIX
23
+ #define MAT_VIEW_P MATRIX_VIEW_P
24
+ #define VEC_ROW_COL VECTOR_ROW_COL
25
+ #define VEC_P VECTOR_P
26
+ #define VEC_ROW_P VECTOR_ROW_P
27
+ #define VEC_COL_P VECTOR_COL_P
28
+ #define CHECK_VEC CHECK_VECTOR
29
+ #define VEC_VIEW_P VECTOR_VIEW_P
30
+ #elif defined(BASE_INT)
31
+ #define NUMCONV(x) FIX2INT(x)
32
+ #define NUMCONV2(x) NUM2INT(x)
33
+ #define PRINTF_FORMAT "%d "
34
+ #define MAT_ROW_COL MATRIX_INT_ROW_COL
35
+ #define MAT_P MATRIX_INT_P
36
+ #define C_TO_VALUE INT2FIX
37
+ #define C_TO_VALUE2 INT2NUM
38
+ #define MAT_ROW_P MATRIX_INT_ROW_P
39
+ #define MAT_COL_P MATRIX_INT_COL_P
40
+ #define CHECK_MAT CHECK_MATRIX_INT
41
+ #define MAT_VIEW_P MATRIX_INT_VIEW_P
42
+ #define VEC_ROW_COL VECTOR_INT_ROW_COL
43
+ #define VEC_P VECTOR_INT_P
44
+ #define VEC_ROW_P VECTOR_INT_ROW_P
45
+ #define VEC_COL_P VECTOR_INT_COL_P
46
+ #define CHECK_VEC CHECK_VECTOR_INT
47
+ #define VEC_VIEW_P VECTOR_INT_VIEW_P
48
+ #endif
49
+
50
+ // From ext/vector_source.c
51
+ void FUNCTION(get_range,beg_en_n)(VALUE range, BASE *beg, BASE *en, size_t *n, int *step);
52
+
53
+ // From ext/vector_source.c
54
+ void get_range_beg_en_n_for_size(VALUE range,
55
+ int *beg, int *en, size_t *n, int *step, size_t size);
56
+
57
+ void parse_submatrix_args(int argc, VALUE *argv, size_t size1, size_t size2,
58
+ size_t *i, size_t *j, size_t *n1, size_t *n2);
59
+
60
+ #ifdef BASE_DOUBLE
61
+ void parse_submatrix_args(int argc, VALUE *argv, size_t size1, size_t size2,
62
+ size_t *i, size_t *j, size_t *n1, size_t *n2)
63
+ {
64
+ int ii, ij, in1, in2, end, step;
65
+
66
+ switch (argc) {
67
+ // no args -> same as submatrix(0, 0, size1, size2)
68
+ case 0:
69
+ *i = 0; *j = 0;
70
+ *n1 = size1; *n2 = size2;
71
+ break;
72
+
73
+ // Fixnum -> Same as submatrix(i/size2, i%size2, 1, 1)
74
+ case 1:
75
+ CHECK_FIXNUM(argv[0]);
76
+ ii = FIX2INT(argv[0]);
77
+ *n1 = size1 * size2;
78
+ if(ii < 0) ii += *n1;
79
+ // TODO Bounds check?
80
+ *i = ii / size2; *j = ii % size2;
81
+ *n1 = 1; *n2 = 1;
82
+ break;
83
+
84
+
85
+ // nil, nil -> all rows, all cols (Matrix::View)
86
+ // nil, Range -> all rows, Range cols (Matrix::View)
87
+ // nil, Fixnum -> all rows, single col (Vector::Col::View)
88
+ // Range, nil -> Range rows, all cols (Matrix::View)
89
+ // Range, Range -> Range rows, Range cols (Matrix::View)
90
+ // Range, Fixnum -> Range rows, single col (Vector::Col::View)
91
+ // Fixnum, nil -> single row, all cols (Vector::View)
92
+ // Fixnum, Range -> single row, Range cols (Vector::View)
93
+ // Fixnum, Fixnum -> single row, single col (Matrix::View)
94
+ case 2:
95
+ // nil, ...
96
+ if(NIL_P(argv[0])) {
97
+ // Parse second arg
98
+ if(NIL_P(argv[1])) {
99
+ // nil, nil -> all rows, all cols (Matrix::View)
100
+ *i = 0; *j = 0;
101
+ *n1 = size1; *n2 = size2;
102
+ } else if(rb_obj_is_kind_of(argv[1], rb_cRange)) {
103
+ // nil, Range -> all rows, Range cols (Matrix::View)
104
+ *i = 0; *n1 = size1;
105
+ get_range_beg_en_n_for_size(argv[1], &ij, &end, n2, &step, size2);
106
+ if(step < 0 || *n2 <=0) {
107
+ rb_raise(rb_eRangeError, "begin > end");
108
+ }
109
+ *j = (size_t)ij;
110
+ } else {
111
+ // nil, Fixnum -> all rows, single col (Vector::Col::View)
112
+ ij = NUM2INT(argv[1]);
113
+ if(ij < 0) ij += size2;
114
+ *i = 0; *j = (size_t)ij;
115
+ *n1 = size1; *n2 = 0; // *n2 == 0 tells #submatrix to return Vector::Col::View
116
+ }
117
+ // Range, ...
118
+ } else if(rb_obj_is_kind_of(argv[0], rb_cRange)) {
119
+ get_range_beg_en_n_for_size(argv[0], &ii, &end, n1, &step, size1);
120
+ if(step < 0 || *n1 <= 0) {
121
+ rb_raise(rb_eRangeError, "arg0: begin > end");
122
+ }
123
+ *i = (size_t)ii;
124
+ // Parse second arg
125
+ if(NIL_P(argv[1])) {
126
+ // Range, nil -> Range rows, all cols (Matrix::View)
127
+ *j = 0; *n2 = size2;
128
+ } else if(rb_obj_is_kind_of(argv[1], rb_cRange)) {
129
+ // Range, Range -> Range rows, Range cols (Matrix::View)
130
+ get_range_beg_en_n_for_size(argv[1], &ij, &end, n2, &step, size2);
131
+ if(step < 0 || *n2 <= 0) {
132
+ rb_raise(rb_eRangeError, "arg1: begin > end");
133
+ }
134
+ *j = (size_t)ij;
135
+ } else {
136
+ // Range, Fixnum -> Range rows, single col (Vector::Col::View)
137
+ ij = NUM2INT(argv[1]);
138
+ if(ij < 0) ij += size2;
139
+ *j = (size_t) ij;
140
+ *n2 = 0; // *n2 == 0 tells #submatrix to return Vector::Col::View
141
+ }
142
+ // Fixnum, ...
143
+ } else {
144
+ ii = NUM2INT(argv[0]);
145
+ if(ii < 0) ii += size1;
146
+ if(NIL_P(argv[1])) {
147
+ // Fixnum, nil -> single row, all cols (Vector::View)
148
+ *i = (size_t)ii; *j = 0;
149
+ *n1 = 0; *n2 = size2; // *n1 == 0 tells #submatrix to return Vector::View
150
+ } else if(rb_obj_is_kind_of(argv[1], rb_cRange)) {
151
+ // Fixnum, Range -> single row, Range cols (Vector::View)
152
+ get_range_beg_en_n_for_size(argv[1], &ij, &end, n2, &step, size2);
153
+ if(step < 0 || *n2 <= 0) {
154
+ rb_raise(rb_eRangeError, "arg1: begin > end");
155
+ }
156
+ *i = (size_t)ii;
157
+ *j = (size_t)ij;
158
+ *n1 = 0; // *n1 == 0 tells #submatrix to return Vector::View
159
+ } else {
160
+ // Fixnum, Fixnum -> single row, single col (Matrix::View)
161
+ ij = NUM2INT(argv[1]);
162
+ if(ij < 0) ij += size2;
163
+ *i = (size_t)ii; *j = (size_t)ij;
164
+ *n1 = 1; *n2 = 1;
165
+ }
166
+ }
167
+ break;
168
+
169
+ // nil, Fixnum, Fixnum -> All rows, some cols
170
+ // Range, Fixnum, Fixnum -> Range rows, some cols
171
+ // Fixnum, Fixnum, nil -> Some rows, all cols
172
+ // Fixnum, Fixnum, Range -> Some rows, Range cols
173
+ case 3:
174
+ // nil, Fixnum, Fixnum
175
+ if(NIL_P(argv[0])) {
176
+ // nil, Fixnum, Fixnum -> All rows, some cols
177
+ CHECK_FIXNUM(argv[1]); CHECK_FIXNUM(argv[2]);
178
+ *i = 0;
179
+ ij = FIX2INT(argv[1]);
180
+ *n1 = size1;
181
+ in2 = FIX2INT(argv[2]);
182
+ if(ij < 0) ij += size2;
183
+ *j = (size_t) ij;
184
+ *n2 = (size_t) in2;
185
+ // Range, Fixnum, Fixnum
186
+ } else if(rb_obj_is_kind_of(argv[0], rb_cRange)) {
187
+ // Range, Fixnum, Fixnum -> Range rows, some cols
188
+ CHECK_FIXNUM(argv[1]); CHECK_FIXNUM(argv[2]);
189
+ get_range_beg_en_n_for_size(argv[0], &ii, &end, n1, &step, size1);
190
+ if(step < 0 || *n1 <= 0) {
191
+ rb_raise(rb_eRangeError, "arg0: begin > end");
192
+ }
193
+ ij = FIX2INT(argv[1]);
194
+ in2 = FIX2INT(argv[2]);
195
+ if(ij < 0) ij += size2;
196
+ *i = (size_t)ii;
197
+ *j = (size_t) ij;
198
+ *n2 = (size_t) in2;
199
+ // Fixnum, Fixnum, ...
200
+ } else {
201
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
202
+ ii = FIX2INT(argv[0]);
203
+ if(ii < 0) ii += size1;
204
+ in1 = FIX2INT(argv[1]);
205
+ *i = (size_t)ii;
206
+ *n1 = (size_t)in1;
207
+ // Parse arg2
208
+ if(NIL_P(argv[2])) {
209
+ // Fixnum, Fixnum, nil -> Some rows, all cols
210
+ *j = 0;
211
+ *n2 = size2;
212
+ } else if(rb_obj_is_kind_of(argv[2], rb_cRange)) {
213
+ // Fixnum, Fixnum, Range -> Some rows, Range cols
214
+ get_range_beg_en_n_for_size(argv[2], &ij, &end, n2, &step, size2);
215
+ if(step < 0 || *n2 <= 0) {
216
+ rb_raise(rb_eRangeError, "arg2: begin > end");
217
+ }
218
+ *j = (size_t)ij;
219
+ } else {
220
+ rb_raise(rb_eArgError, "expected third argument to be nil or Range, not %s",
221
+ rb_class2name(CLASS_OF(argv[2])));
222
+ }
223
+ }
224
+ break;
225
+
226
+ case 4:
227
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
228
+ CHECK_FIXNUM(argv[2]); CHECK_FIXNUM(argv[3]);
229
+ ii = FIX2INT(argv[0]); ij = FIX2INT(argv[1]);
230
+ in1 = FIX2INT(argv[2]); in2 = FIX2INT(argv[3]);
231
+ if(ii < 0) ii += size1;
232
+ if(ij < 0) ij += size2;
233
+ // TODO Bounds check?
234
+ *i = (size_t)ii; *j = (size_t)ij;
235
+ *n1 = (size_t)in1; *n2 = (size_t)in2;
236
+ break;
237
+ default:
238
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 to 4)", argc);
239
+ break;
240
+ }
241
+ }
242
+ #endif
243
+
244
+ VALUE FUNCTION(rb_gsl_matrix,do_something)(VALUE obj, void (*f)(GSL_TYPE(gsl_matrix) *))
245
+ {
246
+ GSL_TYPE(gsl_matrix) *m = NULL;
247
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
248
+ (*f)(m);
249
+ return obj;
250
+ }
251
+
252
+ static VALUE FUNCTION(create_matrix,from_range_shape)(VALUE range, VALUE nn1, VALUE nn2);
253
+ static VALUE FUNCTION(create_matrix,from_ranges)(int argc, VALUE *argv);
254
+ GSL_TYPE(gsl_matrix)* FUNCTION(gsl_matrix,alloc_from_colvectors)(int argc, VALUE *argv);
255
+
256
+ static VALUE FUNCTION(rb_gsl_matrix,alloc)(int argc, VALUE *argv, VALUE klass)
257
+ {
258
+ GSL_TYPE(gsl_matrix) *m = NULL;
259
+ size_t n1, n2;
260
+ #ifdef HAVE_NARRAY_H
261
+ size_t n;
262
+ VALUE ary;
263
+ struct NARRAY *na;
264
+ #endif
265
+ if (argc < 1) rb_raise(rb_eArgError,
266
+ "wrong number of arguments (%d for >= 1)", argc);
267
+
268
+ #ifdef HAVE_NARRAY_H
269
+ if (NA_IsNArray(argv[0])) {
270
+ GetNArray(argv[0], na);
271
+ n = na->shape[0]*na->shape[1];
272
+ m = FUNCTION(gsl_matrix,alloc)(na->shape[1], na->shape[0]);
273
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
274
+ #ifdef BASE_DOUBLE
275
+ ary = na_change_type(argv[0], NA_DFLOAT);
276
+ #else
277
+ ary = na_change_type(argv[0], NA_LINT);
278
+ #endif
279
+ memcpy(m->data, NA_PTR_TYPE(ary,BASE*), n*sizeof(BASE));
280
+ return Data_Wrap_Struct(klass, 0, FUNCTION(gsl_matrix,free), m);
281
+ }
282
+ #endif
283
+ switch (TYPE(argv[0])) {
284
+ case T_FIXNUM:
285
+ if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)",
286
+ argc);
287
+ CHECK_FIXNUM(argv[1]);
288
+ n1 = FIX2INT(argv[0]); n2 = FIX2INT(argv[1]);
289
+ m = FUNCTION(gsl_matrix,calloc)(n1, n2);
290
+ break;
291
+ case T_ARRAY:
292
+ if (argc == 1) {
293
+ m = FUNCTION(gsl_matrix,alloc_from_arrays)(argc, argv);
294
+ break;
295
+ }
296
+ if (CLASS_OF(argv[1]) == rb_cRange) argv[1] = rb_gsl_range2ary(argv[1]);
297
+ switch (TYPE(argv[1])) {
298
+ case T_ARRAY:
299
+ m = FUNCTION(gsl_matrix,alloc_from_arrays)(argc, argv);
300
+ break;
301
+ case T_FIXNUM:
302
+ if (argc != 3) rb_raise(rb_eArgError, "wrong number of arguments (%d for 3)",
303
+ argc);
304
+ CHECK_FIXNUM(argv[2]);
305
+ m = FUNCTION(gsl_matrix,alloc_from_array_sizes)(argv[0], argv[1], argv[2]);
306
+ break;
307
+ default:
308
+ rb_raise(rb_eTypeError,
309
+ "wrong argument type %s\nUsage: new(n1, n2), "
310
+ "new([], [], [], ...), new([], n1, n2)",
311
+ rb_class2name(CLASS_OF(argv[1])));
312
+ break;
313
+ }
314
+ break;
315
+ default:
316
+ if (CLASS_OF(argv[0]) == rb_cRange) {
317
+ if (argc==3 && TYPE(argv[1]) == T_FIXNUM && TYPE(argv[2]) == T_FIXNUM)
318
+ return FUNCTION(create_matrix,from_range_shape)(argv[0], argv[1], argv[2]);
319
+ else
320
+ return FUNCTION(create_matrix,from_ranges)(argc, argv);
321
+ } else if (VEC_P(argv[0])) {
322
+ if (argc == 3 && FIXNUM_P(argv[1]) && FIXNUM_P(argv[2])) {
323
+ m = FUNCTION(gsl_matrix,alloc_from_vector_sizes)(argv[0], argv[1], argv[2]);
324
+ } else {
325
+ if (VEC_COL_P(argv[0])) {
326
+ m = FUNCTION(gsl_matrix,alloc_from_colvectors)(argc, argv);
327
+ } else {
328
+ m = FUNCTION(gsl_matrix,alloc_from_vectors)(argc, argv);
329
+ }
330
+ }
331
+ } else {
332
+ rb_raise(rb_eTypeError, "wrong argument type %s\n"
333
+ "Usage: new(n1, n2), new([], [], [], ...), new([], n1, n2)",
334
+ rb_class2name(CLASS_OF(argv[0])));
335
+ }
336
+ break;
337
+ }
338
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
339
+ }
340
+
341
+ void FUNCTION(set_ptr_data,by_range)(BASE *ptr, size_t n, VALUE range);
342
+
343
+ static GSL_TYPE(gsl_matrix)* FUNCTION(cr_matrix,from_ranges)(int argc, VALUE *argv)
344
+ {
345
+ GSL_TYPE(gsl_matrix) *m;
346
+ BASE beg, en;
347
+ size_t i, n;
348
+ int step;
349
+ FUNCTION(get_range,beg_en_n)(argv[0], &beg, &en, &n, &step);
350
+ m = FUNCTION(gsl_matrix,calloc)(argc, n);
351
+ FUNCTION(set_ptr_data,by_range)(m->data, n, argv[0]);
352
+ for (i = 1; i < argc; i++) {
353
+ if (CLASS_OF(argv[i]) != rb_cRange)
354
+ rb_raise(rb_eTypeError, "wrong argument type %s (Range expected)",
355
+ rb_class2name(CLASS_OF(argv[i])));
356
+ FUNCTION(set_ptr_data,by_range)(m->data+i*n, n, argv[i]);
357
+ }
358
+ return m;
359
+ }
360
+
361
+ static VALUE FUNCTION(create_matrix,from_ranges)(int argc, VALUE *argv)
362
+ {
363
+ GSL_TYPE(gsl_matrix) *m;
364
+ m = FUNCTION(cr_matrix,from_ranges)(argc, argv);
365
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
366
+ }
367
+
368
+ static GSL_TYPE(gsl_matrix)* FUNCTION(cr_matrix,from_range_shape)(VALUE range, VALUE nn1, VALUE nn2)
369
+ {
370
+ size_t n1, n2;
371
+ GSL_TYPE(gsl_matrix) *m;
372
+ n1 = FIX2INT(nn1); n2 = FIX2INT(nn2);
373
+ m = FUNCTION(gsl_matrix,alloc)(n1, n2);
374
+ FUNCTION(set_ptr_data,by_range)(m->data, n1*n2, range);
375
+ return m;
376
+ }
377
+
378
+ static VALUE FUNCTION(create_matrix,from_range_shape)(VALUE range, VALUE nn1, VALUE nn2)
379
+ {
380
+ GSL_TYPE(gsl_matrix) *m;
381
+ m = FUNCTION(cr_matrix,from_range_shape)(range, nn1, nn2);
382
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
383
+ }
384
+
385
+ /* create a matrix from arrays */
386
+ GSL_TYPE(gsl_matrix)* FUNCTION(gsl_matrix,alloc_from_arrays)(int argc, VALUE *argv)
387
+ {
388
+ size_t n, i, j;
389
+ GSL_TYPE(gsl_matrix) *m = NULL;
390
+ if (CLASS_OF(argv[0]) == rb_cRange) argv[0] = rb_gsl_range2ary(argv[0]);
391
+ else Check_Type(argv[0], T_ARRAY);
392
+ n = RARRAY_LEN(argv[0]);
393
+ m = FUNCTION(gsl_matrix,alloc)(argc, n);
394
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
395
+ for (i = 0; i < argc; i++) {
396
+ if (CLASS_OF(argv[i]) == rb_cRange) argv[i] = rb_gsl_range2ary(argv[i]);
397
+ else Check_Type(argv[i], T_ARRAY);
398
+ for (j = 0; j < n; j++) {
399
+ if (j >= RARRAY_LEN(argv[i])) FUNCTION(gsl_matrix,set)(m, i, j, 0);
400
+ else FUNCTION(gsl_matrix,set)(m, i, j, NUMCONV2(rb_ary_entry(argv[i], j)));
401
+ }
402
+ }
403
+ return m;
404
+ }
405
+
406
+ GSL_TYPE(gsl_matrix)* FUNCTION(gsl_matrix,alloc_from_vectors)(int argc, VALUE *argv)
407
+ {
408
+ size_t i;
409
+ GSL_TYPE(gsl_matrix) *m = NULL;
410
+ GSL_TYPE(gsl_vector) *v = NULL;
411
+ if (argc < 1) rb_raise(rb_eArgError, "too few arguments");
412
+ CHECK_VEC(argv[0]);
413
+ Data_Get_Struct(argv[0], GSL_TYPE(gsl_vector), v);
414
+ m = FUNCTION(gsl_matrix,alloc)(argc, v->size);
415
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
416
+ for (i = 0; i < argc; i++) {
417
+ CHECK_VEC(argv[i]);
418
+ Data_Get_Struct(argv[i], GSL_TYPE(gsl_vector), v);
419
+ FUNCTION(gsl_matrix,set_row)(m, i, v);
420
+ }
421
+ return m;
422
+ }
423
+
424
+ GSL_TYPE(gsl_matrix)* FUNCTION(gsl_matrix,alloc_from_colvectors)(int argc, VALUE *argv)
425
+ {
426
+ size_t i;
427
+ GSL_TYPE(gsl_matrix) *m = NULL;
428
+ GSL_TYPE(gsl_vector) *v = NULL;
429
+ if (argc < 1) rb_raise(rb_eArgError, "too few arguments");
430
+ CHECK_VEC(argv[0]);
431
+ Data_Get_Struct(argv[0], GSL_TYPE(gsl_vector), v);
432
+ // m = FUNCTION(gsl_matrix,alloc)(argc, v->size);
433
+ m = FUNCTION(gsl_matrix,alloc)(v->size, argc);
434
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
435
+ for (i = 0; i < argc; i++) {
436
+ CHECK_VEC(argv[i]);
437
+ Data_Get_Struct(argv[i], GSL_TYPE(gsl_vector), v);
438
+ FUNCTION(gsl_matrix,set_col)(m, i, v);
439
+ }
440
+ return m;
441
+ }
442
+
443
+ /* create a matrix from two sizes and an array */
444
+ GSL_TYPE(gsl_matrix)* FUNCTION(gsl_matrix,alloc_from_array_sizes)(VALUE ary,
445
+ VALUE nn1, VALUE nn2)
446
+ {
447
+ size_t n1, n2, len;
448
+ GSL_TYPE(gsl_matrix) *m = NULL;
449
+ size_t i, j, k;
450
+ CHECK_FIXNUM(nn1); CHECK_FIXNUM(nn2);
451
+ Check_Type(ary, T_ARRAY);
452
+ n1 = FIX2INT(nn1);
453
+ n2 = FIX2INT(nn2);
454
+ m = FUNCTION(gsl_matrix,alloc)(n1, n2);
455
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
456
+ k = 0;
457
+ len = RARRAY_LEN(ary);
458
+ for (i = 0; i < n1; i++) {
459
+ for (j = 0; j < n2; j++, k++) {
460
+ if (k >= len)
461
+ FUNCTION(gsl_matrix,set)(m, i, j, (BASE) 0);
462
+ else
463
+ FUNCTION(gsl_matrix,set)(m, i, j, (BASE) NUMCONV2(rb_ary_entry(ary, k)));
464
+
465
+ }
466
+ }
467
+ return m;
468
+ }
469
+
470
+ GSL_TYPE(gsl_matrix)* FUNCTION(gsl_matrix,alloc_from_vector_sizes)(VALUE ary,
471
+ VALUE nn1, VALUE nn2)
472
+ {
473
+ size_t n1, n2;
474
+ GSL_TYPE(gsl_matrix) *m = NULL;
475
+ GSL_TYPE(gsl_vector) *v = NULL;
476
+ size_t i, j, k;
477
+ CHECK_VEC(ary);
478
+ CHECK_FIXNUM(nn1); CHECK_FIXNUM(nn2);
479
+ Data_Get_Struct(ary, GSL_TYPE(gsl_vector), v);
480
+ n1 = FIX2INT(nn1); n2 = FIX2INT(nn2);
481
+ m = FUNCTION(gsl_matrix,alloc)(n1, n2);
482
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_alloc failed");
483
+ k = 0;
484
+ for (i = 0; i < n1; i++) {
485
+ for (j = 0; j < n2; j++, k++) {
486
+ if (k >= v->size)
487
+ FUNCTION(gsl_matrix,set)(m, i, j, (BASE) 0);
488
+ else
489
+ FUNCTION(gsl_matrix,set)(m, i, j, FUNCTION(gsl_vector,get)(v, k));
490
+ }
491
+ }
492
+ return m;
493
+ }
494
+
495
+ static VALUE FUNCTION(rb_gsl_matrix,calloc)(VALUE klass, VALUE nn1, VALUE nn2)
496
+ {
497
+ GSL_TYPE(gsl_matrix) *m = NULL;
498
+ CHECK_FIXNUM(nn1); CHECK_FIXNUM(nn2);
499
+ m = FUNCTION(gsl_matrix,calloc)(FIX2INT(nn1), FIX2INT(nn2));
500
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_calloc failed");
501
+ return Data_Wrap_Struct(klass, 0, FUNCTION(gsl_matrix,free), m);
502
+ }
503
+
504
+ static VALUE FUNCTION(rb_gsl_matrix,diagonal_singleton)(int argc, VALUE *argv, VALUE klass)
505
+ {
506
+ GSL_TYPE(gsl_matrix) *m = NULL;
507
+ GSL_TYPE(gsl_vector) *v = NULL;
508
+ VALUE ary, tmp;
509
+ size_t len, i;
510
+ switch (argc) {
511
+ case 1:
512
+ switch (TYPE(argv[0])) {
513
+ case T_FIXNUM:
514
+ case T_FLOAT:
515
+ len = FIX2INT(argv[0]);
516
+ m = FUNCTION(gsl_matrix,alloc)(len, len);
517
+ for (i = 0; i < len; i++)
518
+ FUNCTION(gsl_matrix,set)(m, i, i, 1);
519
+ return Data_Wrap_Struct(klass, 0, FUNCTION(gsl_matrix,free), m);
520
+ break;
521
+ default:
522
+ /* do next */
523
+ break;
524
+ }
525
+ if (rb_obj_is_kind_of(argv[0], rb_cRange))
526
+ ary = rb_gsl_range2ary(argv[0]);
527
+ else ary = argv[0];
528
+ switch (TYPE(ary)) {
529
+ case T_ARRAY:
530
+ len = RARRAY_LEN(ary);
531
+ m = FUNCTION(gsl_matrix,calloc)(len, len);
532
+ for (i = 0; i < len; i++) {
533
+ tmp = rb_ary_entry(ary, i);
534
+ FUNCTION(gsl_matrix,set)(m, i, i, NUMCONV2(tmp));
535
+ }
536
+ break;
537
+ default:
538
+ CHECK_VEC(ary);
539
+ Data_Get_Struct(ary, GSL_TYPE(gsl_vector), v);
540
+ len = v->size;
541
+ m = FUNCTION(gsl_matrix,calloc)(len, len);
542
+ for (i = 0; i < len; i++) {
543
+ FUNCTION(gsl_matrix,set)(m, i, i, FUNCTION(gsl_vector,get)(v, i));
544
+ }
545
+ break;
546
+ }
547
+ break;
548
+ default:
549
+ m = FUNCTION(gsl_matrix,calloc)(argc, argc);
550
+ for (i = 0; i < argc; i++) {
551
+ FUNCTION(gsl_matrix,set)(m, i, i, NUMCONV2(argv[i]));
552
+ }
553
+ break;
554
+ }
555
+ return Data_Wrap_Struct(klass, 0, FUNCTION(gsl_matrix,free), m);
556
+ }
557
+
558
+ static VALUE FUNCTION(rb_gsl_matrix,eye)(int argc, VALUE *argv, VALUE klass)
559
+ {
560
+ GSL_TYPE(gsl_matrix) *m = NULL;
561
+ size_t n1, n2, n, i;
562
+ switch (argc) {
563
+ case 1:
564
+ CHECK_FIXNUM(argv[0]);
565
+ n = FIX2INT(argv[0]);
566
+ n1 = n2 = n;
567
+ break;
568
+ case 2:
569
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
570
+ n1 = FIX2INT(argv[0]); n2 = FIX2INT(argv[1]);
571
+ n = GSL_MIN_INT(n1, n2);
572
+ break;
573
+ default:
574
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
575
+ break;
576
+ }
577
+ m = FUNCTION(gsl_matrix,calloc)(n1, n2);
578
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_calloc failed");
579
+ for (i = 0; i < n; i++) {
580
+ FUNCTION(gsl_matrix,set)(m, i, i, 1);
581
+ }
582
+ return Data_Wrap_Struct(klass, 0, FUNCTION(gsl_matrix,free), m);
583
+ }
584
+
585
+ static VALUE FUNCTION(rb_gsl_matrix,ones)(int argc, VALUE *argv, VALUE klass)
586
+ {
587
+ GSL_TYPE(gsl_matrix) *m = NULL;
588
+ size_t n1, n2, n, i, j;
589
+ switch (argc) {
590
+ case 1:
591
+ CHECK_FIXNUM(argv[0]);
592
+ n = FIX2INT(argv[0]);
593
+ n1 = n2 = n;
594
+ break;
595
+ case 2:
596
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
597
+ n1 = FIX2INT(argv[0]); n2 = FIX2INT(argv[1]);
598
+ n = GSL_MIN_INT(n1, n2);
599
+ break;
600
+ default:
601
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
602
+ break;
603
+ }
604
+ m = FUNCTION(gsl_matrix,calloc)(n1, n2);
605
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_calloc failed");
606
+ for (i = 0; i < n1; i++) {
607
+ for (j = 0; j < n2; j++) {
608
+ FUNCTION(gsl_matrix,set)(m, i, j, (BASE) 1);
609
+ }
610
+ }
611
+ return Data_Wrap_Struct(klass, 0, FUNCTION(gsl_matrix,free), m);
612
+ }
613
+
614
+ static VALUE FUNCTION(rb_gsl_matrix,zeros)(int argc, VALUE *argv, VALUE klass)
615
+ {
616
+ GSL_TYPE(gsl_matrix) *m = NULL;
617
+ size_t n1, n2, n, i, j;
618
+ switch (argc) {
619
+ case 1:
620
+ CHECK_FIXNUM(argv[0]);
621
+ n = FIX2INT(argv[0]);
622
+ n1 = n2 = n;
623
+ break;
624
+ case 2:
625
+ CHECK_FIXNUM(argv[0]); CHECK_FIXNUM(argv[1]);
626
+ n1 = FIX2INT(argv[0]); n2 = FIX2INT(argv[1]);
627
+ n = GSL_MIN_INT(n1, n2);
628
+ break;
629
+ default:
630
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
631
+ break;
632
+ }
633
+ m = FUNCTION(gsl_matrix,calloc)(n1, n2);
634
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_calloc failed");
635
+ for (i = 0; i < n1; i++) {
636
+ for (j = 0; j < n2; j++) {
637
+ FUNCTION(gsl_matrix,set)(m, i, j, (BASE) 0);
638
+ }
639
+ }
640
+ return Data_Wrap_Struct(klass, 0, FUNCTION(gsl_matrix,free), m);
641
+ }
642
+
643
+ static VALUE FUNCTION(rb_gsl_matrix,identity)(VALUE klass, VALUE nn)
644
+ {
645
+ GSL_TYPE(gsl_matrix) *m = NULL;
646
+ size_t n, i;
647
+ CHECK_FIXNUM(nn);
648
+ n = FIX2INT(nn);
649
+ m = FUNCTION(gsl_matrix,calloc)(n, n);
650
+ if (m == NULL) rb_raise(rb_eNoMemError, "gsl_matrix_calloc failed");
651
+ for (i = 0; i < n; i++) {
652
+ FUNCTION(gsl_matrix,set)(m, i, i, 1);
653
+ }
654
+ return Data_Wrap_Struct(klass, 0, FUNCTION(gsl_matrix,free), m);
655
+ }
656
+
657
+ static VALUE FUNCTION(rb_gsl_matrix,size1)(VALUE obj)
658
+ {
659
+ GSL_TYPE(gsl_matrix) *m = NULL;
660
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
661
+ return INT2FIX(m->size1);
662
+ }
663
+
664
+ static VALUE FUNCTION(rb_gsl_matrix,size2)(VALUE obj)
665
+ {
666
+ GSL_TYPE(gsl_matrix) *m = NULL;
667
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
668
+ return INT2FIX(m->size2);
669
+ }
670
+
671
+ static VALUE FUNCTION(rb_gsl_matrix,shape)(VALUE obj)
672
+ {
673
+ GSL_TYPE(gsl_matrix) *m = NULL;
674
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
675
+ return rb_ary_new3(2, INT2FIX(m->size1), INT2FIX(m->size2));
676
+ }
677
+
678
+ static VALUE FUNCTION(rb_gsl_matrix,submatrix)(int argc, VALUE *argv, VALUE obj);
679
+ static VALUE FUNCTION(rb_gsl_matrix,get)(int argc, VALUE *argv, VALUE obj)
680
+ {
681
+ GSL_TYPE(gsl_matrix) *m = NULL;
682
+ VALUE retval;
683
+ int ii, ij;
684
+
685
+ if(argc == 2 && TYPE(argv[0]) == T_FIXNUM && TYPE(argv[1]) == T_FIXNUM) {
686
+ // m[i,j]
687
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
688
+ ii = FIX2INT(argv[0]);
689
+ ij = FIX2INT(argv[1]);
690
+ if(ii < 0) ii += m->size1;
691
+ if(ij < 0) ij += m->size2;
692
+ retval = C_TO_VALUE2(FUNCTION(gsl_matrix,get)(m, (size_t)ii, (size_t)ij));
693
+ } else if(argc == 1 && TYPE(argv[0]) == T_FIXNUM) {
694
+ // m[i]
695
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
696
+ ii = FIX2INT(argv[0]);
697
+ if(ii < 0) ii += m->size1 * m->size2;
698
+ retval = C_TO_VALUE2(FUNCTION(gsl_matrix,get)(m, (size_t)(ii / m->size2), (size_t)(ii % m->size2)));
699
+ } else if(argc == 1 && TYPE(argv[0]) == T_ARRAY) {
700
+ // m[[i,j]], to support m[m.max_index]
701
+ if(RARRAY_LEN(argv[0]) == 2) {
702
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
703
+ ii = FIX2INT(RARRAY_PTR(argv[0])[0]);
704
+ ij = FIX2INT(RARRAY_PTR(argv[0])[1]);
705
+ if(ii < 0) ii += m->size1;
706
+ if(ij < 0) ij += m->size2;
707
+ retval = C_TO_VALUE2(FUNCTION(gsl_matrix,get)(m, (size_t)ii, (size_t)ij));
708
+ } else {
709
+ rb_raise(rb_eArgError, "Array index must have length 2, not %d", (int) RARRAY_LEN(argv[0]));
710
+ }
711
+ } else {
712
+ retval = FUNCTION(rb_gsl_matrix,submatrix)(argc, argv, obj);
713
+ }
714
+
715
+ return retval;
716
+ }
717
+
718
+ void FUNCTION(rb_gsl_vector,set_subvector)(int argc, VALUE *argv, GSL_TYPE(gsl_vector) *v, VALUE other);
719
+ static VALUE FUNCTION(rb_gsl_matrix,set)(int argc, VALUE *argv, VALUE obj)
720
+ {
721
+ GSL_TYPE(gsl_matrix) *m, *mother;
722
+ QUALIFIED_VIEW(gsl_matrix,view) mv;
723
+ QUALIFIED_VIEW(gsl_vector,view) vv;
724
+ VALUE other, row, row_set_argv[2];
725
+ int ii, ij, step;
726
+ size_t i, j, k, n1, n2, nother;
727
+ BASE beg, end;
728
+
729
+ if(argc < 1 || argc > 5) {
730
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1-5)", argc);
731
+ }
732
+
733
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
734
+ other = argv[argc-1];
735
+
736
+ if(argc == 1 && TYPE(argv[0]) == T_ARRAY) {
737
+ // m.set([row0,row1,...])
738
+ n1 = RARRAY_LEN(argv[0]);
739
+ if(n1 > m->size1) n1 = m->size1;
740
+ row_set_argv[0] = INT2FIX(0);
741
+ // Each given row must have as manay elements as m has columns.
742
+ // The bounds check happens inside rb_gsl_vector*set_subvector().
743
+ row_set_argv[1] = INT2FIX(m->size2);
744
+ for(k = 0; k < n1 && k < m->size1; k++) {
745
+ vv = FUNCTION(gsl_matrix,row)(m, k);
746
+ FUNCTION(rb_gsl_vector,set_subvector)(2, row_set_argv, &vv.vector, rb_ary_entry(argv[0], k));
747
+ }
748
+ } else if(argc == 1) {
749
+ // m[] = x
750
+ FUNCTION(gsl_matrix,set_all)(m, NUMCONV2(other));
751
+ } else if(argc==2 && TYPE(argv[0]) == T_ARRAY && TYPE(argv[1]) != T_ARRAY) {
752
+ // m.set([i, j], x) or m[[i,j]] = x
753
+ ii = FIX2INT(rb_ary_entry(argv[0], 0));
754
+ ij = FIX2INT(rb_ary_entry(argv[0], 1));
755
+ if(ii < 0) ii += m->size1;
756
+ if(ij < 0) ij += m->size2;
757
+ FUNCTION(gsl_matrix,set)(m, (size_t)ii, (size_t)ij, NUMCONV2(argv[1]));
758
+ } else if(argc == 3 && TYPE(argv[0]) == T_FIXNUM && TYPE(argv[1]) == T_FIXNUM) {
759
+ // m[i,j] = x
760
+ ii = FIX2INT(argv[0]);
761
+ ij = FIX2INT(argv[1]);
762
+ if(ii < 0) ii += m->size1;
763
+ if(ij < 0) ij += m->size2;
764
+ FUNCTION(gsl_matrix,set)(m, (size_t)ii, (size_t)ij, NUMCONV2(other));
765
+ } else if(TYPE(argv[0]) == T_ARRAY) {
766
+ // m.set(row0,row1,...)
767
+ n1 = argc;
768
+ if(n1 > m->size1) n1 = m->size1;
769
+ row_set_argv[0] = INT2FIX(0);
770
+ row_set_argv[1] = INT2FIX(m->size2);
771
+ for(k = 0; k < n1 && k < m->size1; k++) {
772
+ vv = FUNCTION(gsl_matrix,row)(m, k);
773
+ FUNCTION(rb_gsl_vector,set_subvector)(2, row_set_argv, &vv.vector, argv[k]);
774
+ }
775
+ } else {
776
+ // x -> assignment to m.submatrix(i...)
777
+ parse_submatrix_args(argc-1, argv, m->size1, m->size2, &i, &j, &n1, &n2);
778
+ if(n1 == 0) n1 = 1;
779
+ if(n2 == 0) n2 = 1;
780
+ mv = FUNCTION(gsl_matrix,submatrix)(m, i, j, n1, n2);
781
+ if(rb_obj_is_kind_of(other, GSL_TYPE(cgsl_matrix))) {
782
+ // m[...] = m_other
783
+ Data_Get_Struct(other, GSL_TYPE(gsl_matrix), mother);
784
+ if(n1 * n2 != mother->size1 * mother->size2) {
785
+ rb_raise(rb_eRangeError, "sizes do not match (%d x %d != %d x %d)",
786
+ (int) n1, (int) n2, (int) mother->size1, (int) mother->size2);
787
+ }
788
+ // TODO Change to gsl_matrix_memmove if/when GSL has such a function
789
+ // because gsl_matrix_memcpy does not handle overlapping regions (e.g.
790
+ // Views) well.
791
+ FUNCTION(gsl_matrix,memcpy)(&mv.matrix, mother);
792
+ } else if(rb_obj_is_kind_of(other, rb_cArray)) {
793
+ row_set_argv[0] = INT2FIX(0);
794
+ row_set_argv[1] = INT2FIX(n2);
795
+
796
+ if(n1 == 1) {
797
+ // m[...] = [col0, ...] # single row
798
+ vv = FUNCTION(gsl_matrix,row)(&mv.matrix, 0);
799
+ FUNCTION(rb_gsl_vector,set_subvector)(2, row_set_argv, &vv.vector, other);
800
+ } else {
801
+ // m[...] = [[row0], [row1], ...] # multiple rows
802
+ if(n1 != RARRAY_LEN(other)) {
803
+ rb_raise(rb_eRangeError, "row counts do not match (%d != %d)",
804
+ (int) n1, (int) RARRAY_LEN(other));
805
+ }
806
+ for(k = 0; k < n1; k++) {
807
+ vv = FUNCTION(gsl_matrix,row)(&mv.matrix, k);
808
+ row = rb_ary_entry(other, k);
809
+ FUNCTION(rb_gsl_vector,set_subvector)(2, row_set_argv, &vv.vector, row);
810
+ }
811
+ }
812
+ } else if(rb_obj_is_kind_of(other, rb_cRange)) {
813
+ // m[...] = beg..end
814
+ FUNCTION(get_range,beg_en_n)(other, &beg, &end, &nother, &step);
815
+ if(n1 * n2 != nother) {
816
+ rb_raise(rb_eRangeError, "sizes do not match (%d x %d != %d)", (int) n1, (int) n2, (int) nother);
817
+ }
818
+ for(k = 0; k < nother; k++) {
819
+ FUNCTION(gsl_matrix,set)(&mv.matrix, k / n2, k % n2, beg);
820
+ beg += step;
821
+ }
822
+ } else {
823
+ // m[...] = x
824
+ FUNCTION(gsl_matrix,set_all)(&mv.matrix, NUMCONV2(other));
825
+ }
826
+ }
827
+
828
+ return obj;
829
+ }
830
+
831
+ static VALUE FUNCTION(rb_gsl_matrix,set_all)(VALUE obj, VALUE x)
832
+ {
833
+ GSL_TYPE(gsl_matrix) *m = NULL;
834
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
835
+ FUNCTION(gsl_matrix,set_all)(m, NUMCONV2(x));
836
+ return obj;
837
+ }
838
+
839
+ static VALUE FUNCTION(rb_gsl_matrix,set_zero)(VALUE obj)
840
+ {
841
+ return FUNCTION(rb_gsl_matrix,do_something)(obj, FUNCTION(gsl_matrix,set_zero));
842
+ }
843
+
844
+ static VALUE FUNCTION(rb_gsl_matrix,set_identity)(VALUE obj)
845
+ {
846
+ return FUNCTION(rb_gsl_matrix,do_something)(obj, FUNCTION(gsl_matrix,set_identity));
847
+ }
848
+
849
+ static VALUE FUNCTION(rb_gsl_matrix,print)(VALUE obj)
850
+ {
851
+ GSL_TYPE(gsl_matrix) *m = NULL;
852
+ size_t i, j;
853
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
854
+ printf("[ ");
855
+ for (i = 0; i < m->size1; i++) {
856
+ if (i != 0) printf(" ");
857
+ for (j = 0; j < m->size2; j++) {
858
+ printf(PRINTF_FORMAT, FUNCTION(gsl_matrix,get)(m, i, j));
859
+ }
860
+ if (i == m->size1 - 1) printf("]\n");
861
+ else printf("\n");
862
+ }
863
+ return Qnil;
864
+ }
865
+
866
+ #ifdef BASE_DOUBLE
867
+ #define SHOW_ELM 6
868
+ #else
869
+ #define SHOW_ELM 12
870
+ #endif
871
+
872
+ static VALUE FUNCTION(rb_gsl_matrix,to_s)(VALUE obj)
873
+ {
874
+ GSL_TYPE(gsl_matrix) *m = NULL;
875
+ char buf[32], format[32], format2[32];
876
+ size_t i, j;
877
+ VALUE str;
878
+ BASE x;
879
+ int dig = 8;
880
+ #ifdef BASE_INT
881
+ BASE min;
882
+ BASE max;
883
+ #endif
884
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
885
+ #ifdef BASE_INT
886
+ min = FUNCTION(gsl_matrix,min)(m);
887
+ max = gsl_matrix_int_max(m);
888
+ dig = (int) GSL_MAX(fabs(max),fabs(min));
889
+ if (dig > 0) dig = ceil(log10(dig+1e-10));
890
+ else dig = 1;
891
+ if (min < 0) dig += 1;
892
+ sprintf(format, "%%%dd ", (int) dig);
893
+ strcpy(format2, format);
894
+ #else
895
+ strcpy(format, PRINTF_FORMAT);
896
+ strcpy(format2, " "PRINTF_FORMAT);
897
+ #endif
898
+ str = rb_str_new2("[ ");
899
+ for (i = 0; i < m->size1; i++) {
900
+ if (i != 0) {
901
+ strcpy(buf, " ");
902
+ rb_str_cat(str, buf, strlen(buf));
903
+ }
904
+ for (j = 0; j < m->size2; j++) {
905
+ x = FUNCTION(gsl_matrix,get)(m, i, j);
906
+ if (x < 0)
907
+ sprintf(buf, format, x);
908
+ else
909
+ sprintf(buf, format2, x);
910
+ rb_str_cat(str, buf, strlen(buf));
911
+ if (j >= (55/dig)) {
912
+ strcpy(buf, "... ");
913
+ rb_str_cat(str, buf, strlen(buf));
914
+ break;
915
+ }
916
+ }
917
+ if (i >= 20) {
918
+ strcpy(buf, "\n ... ]");
919
+ rb_str_cat(str, buf, strlen(buf));
920
+ break;
921
+ }
922
+ if (i == m->size1 - 1) {
923
+ strcpy(buf, "]");
924
+ rb_str_cat(str, buf, strlen(buf));
925
+ } else {
926
+ strcpy(buf, "\n");
927
+ rb_str_cat(str, buf, strlen(buf));
928
+ }
929
+ }
930
+ return str;
931
+ }
932
+ #undef SHOW_ELM
933
+
934
+ static VALUE FUNCTION(rb_gsl_matrix,inspect)(VALUE obj)
935
+ {
936
+ VALUE str;
937
+ char buf[64];
938
+ sprintf(buf, "%s\n", rb_class2name(CLASS_OF(obj)));
939
+ str = rb_str_new2(buf);
940
+ return rb_str_concat(str, FUNCTION(rb_gsl_matrix,to_s)(obj));
941
+ }
942
+
943
+ #ifdef BASE_DOUBLE
944
+ #define PRINTF_FORMAT2 "%g"
945
+ #elif defined(BASE_INT)
946
+ #define PRINTF_FORMAT2 "%d"
947
+ #endif
948
+ static VALUE FUNCTION(rb_gsl_matrix,fprintf)(int argc, VALUE *argv, VALUE obj)
949
+ {
950
+ GSL_TYPE(gsl_matrix) *h = NULL;
951
+ FILE *fp = NULL;
952
+ int status, flag = 0;
953
+ if (argc != 1 && argc != 2) {
954
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
955
+ }
956
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), h);
957
+ fp = rb_gsl_open_writefile(argv[0], &flag);
958
+ if (argc == 2) {
959
+ Check_Type(argv[1], T_STRING);
960
+ status = FUNCTION(gsl_matrix,fprintf)(fp, h, STR2CSTR(argv[1]));
961
+ } else {
962
+ status = FUNCTION(gsl_matrix,fprintf)(fp, h, PRINTF_FORMAT2);
963
+ }
964
+ if (flag == 1) fclose(fp);
965
+ return INT2FIX(status);
966
+ }
967
+
968
+ static VALUE FUNCTION(rb_gsl_matrix,printf)(int argc, VALUE *argv, VALUE obj)
969
+ {
970
+ GSL_TYPE(gsl_matrix) *h = NULL;
971
+ int status;
972
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), h);
973
+ if (argc == 1) {
974
+ Check_Type(argv[0], T_STRING);
975
+ status = FUNCTION(gsl_matrix,fprintf)(stdout, h, STR2CSTR(argv[0]));
976
+ } else {
977
+ status = FUNCTION(gsl_matrix,fprintf)(stdout, h, PRINTF_FORMAT2);
978
+ }
979
+ return INT2FIX(status);
980
+ }
981
+
982
+ #undef PRINTF_FORMAT2
983
+ static VALUE FUNCTION(rb_gsl_matrix,fscanf)(VALUE obj, VALUE io)
984
+ {
985
+ GSL_TYPE(gsl_matrix) *h = NULL;
986
+ FILE *fp = NULL;
987
+ int status, flag = 0;
988
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), h);
989
+ fp = rb_gsl_open_readfile(io, &flag);
990
+ status = FUNCTION(gsl_matrix,fscanf)(fp, h);
991
+ if (flag == 1) fclose(fp);
992
+ return INT2FIX(status);
993
+ }
994
+
995
+ static VALUE FUNCTION(rb_gsl_matrix,set_diagonal)(VALUE obj, VALUE diag)
996
+ {
997
+ GSL_TYPE(gsl_matrix) *m = NULL;
998
+ GSL_TYPE(gsl_vector) *v;
999
+ size_t i, len;
1000
+ BASE x;
1001
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1002
+ switch (TYPE(diag)) {
1003
+ case T_FIXNUM: case T_BIGNUM: case T_FLOAT:
1004
+ x = (BASE) NUMCONV2(diag);
1005
+ for (i = 0; i < m->size1; i++) FUNCTION(gsl_matrix,set)(m, i, i, x);
1006
+ break;
1007
+ case T_ARRAY:
1008
+ len = GSL_MIN_INT(m->size1, RARRAY_LEN(diag));
1009
+ for (i = 0; i < len; i++) {
1010
+ FUNCTION(gsl_matrix,set)(m, i, i, NUMCONV2(rb_ary_entry(diag, i)));
1011
+ }
1012
+ break;
1013
+ default:
1014
+ if (VEC_P(diag)) {
1015
+ Data_Get_Struct(diag, GSL_TYPE(gsl_vector), v);
1016
+ len = GSL_MIN_INT(m->size1, v->size);
1017
+ for (i = 0; i < len; i++) {
1018
+ FUNCTION(gsl_matrix,set)(m, i, i, FUNCTION(gsl_vector,get)(v, i));
1019
+ }
1020
+ } else {
1021
+ rb_raise(rb_eTypeError, "wrong argument type %s (GSL::Vector or Array expected)",
1022
+ rb_class2name(CLASS_OF(diag)));
1023
+ }
1024
+ break;
1025
+ }
1026
+ return obj;
1027
+ }
1028
+
1029
+ static VALUE FUNCTION(rb_gsl_matrix,get_row)(VALUE obj, VALUE i)
1030
+ {
1031
+ GSL_TYPE(gsl_matrix) *m = NULL;
1032
+ GSL_TYPE(gsl_vector) *v = NULL;
1033
+ CHECK_FIXNUM(i);
1034
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1035
+ v = FUNCTION(gsl_vector,alloc)(m->size1);
1036
+ if (v == NULL) rb_raise(rb_eNoMemError, "gsl_vector_alloc failed");
1037
+ FUNCTION(gsl_matrix,get_row)(v, m, FIX2INT(i));
1038
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_vector), 0, FUNCTION(gsl_vector,free), v);
1039
+ }
1040
+
1041
+ static VALUE FUNCTION(rb_gsl_matrix,get_col)(VALUE obj, VALUE i)
1042
+ {
1043
+ GSL_TYPE(gsl_matrix) *m = NULL;
1044
+ GSL_TYPE(gsl_vector) *v = NULL;
1045
+ CHECK_FIXNUM(i);
1046
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1047
+ v = FUNCTION(gsl_vector,alloc)(m->size2);
1048
+ if (v == NULL) rb_raise(rb_eNoMemError, "gsl_vector_alloc failed");
1049
+ FUNCTION(gsl_matrix,get_col)(v, m, FIX2INT(i));
1050
+ // TODO This is NOT returning a view! Is there a macro more appropriate than
1051
+ // QUALIFIED_VIEW?
1052
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,col), 0, FUNCTION(gsl_vector,free), v);
1053
+ }
1054
+
1055
+ static VALUE FUNCTION(rb_gsl_matrix,set_row)(VALUE obj, VALUE i, VALUE vv)
1056
+ {
1057
+ GSL_TYPE(gsl_matrix) *m = NULL;
1058
+ GSL_TYPE(gsl_vector) *v = NULL;
1059
+ int flag = 0;
1060
+ size_t j;
1061
+ CHECK_FIXNUM(i);
1062
+ if (CLASS_OF(vv) == rb_cRange) vv = rb_gsl_range2ary(vv);
1063
+ if (TYPE(vv) == T_ARRAY) {
1064
+ v = FUNCTION(gsl_vector,alloc)(RARRAY_LEN(vv));
1065
+ for (j = 0; j < RARRAY_LEN(vv); j++) {
1066
+ FUNCTION(gsl_vector,set)(v, j, NUMCONV2(rb_ary_entry(vv, j)));
1067
+ }
1068
+ flag = 1;
1069
+ } else {
1070
+ CHECK_VEC(vv);
1071
+ Data_Get_Struct(vv, GSL_TYPE(gsl_vector), v);
1072
+ }
1073
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1074
+ FUNCTION(gsl_matrix,set_row)(m, FIX2INT(i), v);
1075
+ if (flag == 1) FUNCTION(gsl_vector,free)(v);
1076
+ return obj;
1077
+ }
1078
+
1079
+ static VALUE FUNCTION(rb_gsl_matrix,set_col)(VALUE obj, VALUE j, VALUE vv)
1080
+ {
1081
+ GSL_TYPE(gsl_matrix) *m = NULL;
1082
+ GSL_TYPE(gsl_vector) *v = NULL;
1083
+ int flag = 0;
1084
+ size_t i;
1085
+ CHECK_FIXNUM(j);
1086
+ if (CLASS_OF(vv) == rb_cRange) vv = rb_gsl_range2ary(vv);
1087
+ if (TYPE(vv) == T_ARRAY) {
1088
+ v = FUNCTION(gsl_vector,alloc)(RARRAY_LEN(vv));
1089
+ for (i = 0; i < RARRAY_LEN(vv); i++) {
1090
+ FUNCTION(gsl_vector,set)(v, i, NUMCONV2(rb_ary_entry(vv, i)));
1091
+ }
1092
+ flag = 1;
1093
+ } else {
1094
+ CHECK_VECTOR(vv);
1095
+ Data_Get_Struct(vv, GSL_TYPE(gsl_vector), v);
1096
+ }
1097
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1098
+ FUNCTION(gsl_matrix,set_col)(m, FIX2INT(j), v);
1099
+ if (flag == 1) FUNCTION(gsl_vector,free)(v);
1100
+ return obj;
1101
+ }
1102
+
1103
+ static VALUE FUNCTION(rb_gsl_matrix,clone)(VALUE obj)
1104
+ {
1105
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew = NULL;
1106
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1107
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
1108
+ FUNCTION(gsl_matrix,memcpy)(mnew, m);
1109
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1110
+ }
1111
+
1112
+ static VALUE FUNCTION(rb_gsl_matrix,memcpy)(VALUE obj, VALUE mm1, VALUE mm2)
1113
+ {
1114
+ GSL_TYPE(gsl_matrix) *m1 = NULL, *m2 = NULL;
1115
+ CHECK_MAT(mm1); CHECK_MAT(mm2);
1116
+ Data_Get_Struct(mm1, GSL_TYPE(gsl_matrix), m1);
1117
+ Data_Get_Struct(mm2, GSL_TYPE(gsl_matrix), m2);
1118
+ FUNCTION(gsl_matrix,memcpy)(m1, m2);
1119
+ return mm1;
1120
+ }
1121
+
1122
+ static VALUE FUNCTION(rb_gsl_matrix,isnull)(VALUE obj)
1123
+ {
1124
+ GSL_TYPE(gsl_matrix) *m = NULL;
1125
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1126
+ return INT2FIX(FUNCTION(gsl_matrix,isnull)(m));
1127
+ }
1128
+
1129
+ static VALUE FUNCTION(rb_gsl_matrix,isnull2)(VALUE obj)
1130
+ {
1131
+ GSL_TYPE(gsl_matrix) *m = NULL;
1132
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1133
+ if (FUNCTION(gsl_matrix,isnull)(m)) return Qtrue;
1134
+ else return Qfalse;
1135
+ }
1136
+
1137
+
1138
+ /* singleton */
1139
+ static VALUE FUNCTION(rb_gsl_matrix,swap)(VALUE obj, VALUE mm1, VALUE mm2)
1140
+ {
1141
+ GSL_TYPE(gsl_matrix) *m1 = NULL, *m2 = NULL;
1142
+ CHECK_MAT(mm1); CHECK_MAT(mm2);
1143
+ Data_Get_Struct(mm1, GSL_TYPE(gsl_matrix), m1);
1144
+ Data_Get_Struct(mm2, GSL_TYPE(gsl_matrix), m2);
1145
+ FUNCTION(gsl_matrix,swap)(m1, m2);
1146
+ return mm1;
1147
+ }
1148
+
1149
+ static VALUE FUNCTION(rb_gsl_matrix,swap_rows_bang)(VALUE obj, VALUE i, VALUE j)
1150
+ {
1151
+ GSL_TYPE(gsl_matrix) *m = NULL;
1152
+ CHECK_FIXNUM(i); CHECK_FIXNUM(j);
1153
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1154
+ FUNCTION(gsl_matrix,swap_rows)(m, FIX2INT(i), FIX2INT(j));
1155
+ return obj;
1156
+ }
1157
+
1158
+ static VALUE FUNCTION(rb_gsl_matrix,swap_rows)(VALUE obj, VALUE i, VALUE j)
1159
+ {
1160
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew;
1161
+ CHECK_FIXNUM(i); CHECK_FIXNUM(j);
1162
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1163
+ mnew = FUNCTION(make_matrix,clone)(m);
1164
+ FUNCTION(gsl_matrix,swap_rows)(mnew, FIX2INT(i), FIX2INT(j));
1165
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1166
+ }
1167
+
1168
+ static VALUE FUNCTION(rb_gsl_matrix,swap_columns_bang)(VALUE obj, VALUE i, VALUE j)
1169
+ {
1170
+ GSL_TYPE(gsl_matrix) *m = NULL;
1171
+ CHECK_FIXNUM(i); CHECK_FIXNUM(j);
1172
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1173
+ FUNCTION(gsl_matrix,swap_columns)(m, FIX2INT(i), FIX2INT(j));
1174
+ return obj;
1175
+ }
1176
+
1177
+ static VALUE FUNCTION(rb_gsl_matrix,swap_columns)(VALUE obj, VALUE i, VALUE j)
1178
+ {
1179
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew;
1180
+ CHECK_FIXNUM(i); CHECK_FIXNUM(j);
1181
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1182
+ mnew = FUNCTION(make_matrix,clone)(m);
1183
+ FUNCTION(gsl_matrix,swap_columns)(mnew, FIX2INT(i), FIX2INT(j));
1184
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1185
+ }
1186
+
1187
+ static VALUE FUNCTION(rb_gsl_matrix,swap_rowcol_bang)(VALUE obj, VALUE i, VALUE j)
1188
+ {
1189
+ GSL_TYPE(gsl_matrix) *m = NULL;
1190
+ CHECK_FIXNUM(i); CHECK_FIXNUM(j);
1191
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1192
+ FUNCTION(gsl_matrix,swap_rowcol)(m, FIX2INT(i), FIX2INT(j));
1193
+ return obj;
1194
+ }
1195
+
1196
+ static VALUE FUNCTION(rb_gsl_matrix,swap_rowcol)(VALUE obj, VALUE i, VALUE j)
1197
+ {
1198
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew = NULL;
1199
+ CHECK_FIXNUM(i); CHECK_FIXNUM(j);
1200
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1201
+ mnew = FUNCTION(make_matrix,clone)(m);
1202
+ FUNCTION(gsl_matrix,swap_rowcol)(mnew, FIX2INT(i), FIX2INT(j));
1203
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1204
+ }
1205
+
1206
+ static VALUE FUNCTION(rb_gsl_matrix,transpose_memcpy)(VALUE obj)
1207
+ {
1208
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew = NULL;
1209
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1210
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size2, m->size1);
1211
+ FUNCTION(gsl_matrix,transpose_memcpy)(mnew, m);
1212
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1213
+ }
1214
+
1215
+ static VALUE FUNCTION(rb_gsl_matrix,transpose_bang)(VALUE obj)
1216
+ {
1217
+ GSL_TYPE(gsl_matrix) *m = NULL;
1218
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1219
+ FUNCTION(gsl_matrix,transpose)(m);
1220
+ return obj;
1221
+ }
1222
+
1223
+ static VALUE FUNCTION(rb_gsl_matrix,max)(VALUE obj)
1224
+ {
1225
+ GSL_TYPE(gsl_matrix) *m = NULL;
1226
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1227
+ return C_TO_VALUE2(FUNCTION(gsl_matrix,max)(m));
1228
+ }
1229
+
1230
+ static VALUE FUNCTION(rb_gsl_matrix,min)(VALUE obj)
1231
+ {
1232
+ GSL_TYPE(gsl_matrix) *m = NULL;
1233
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1234
+ return C_TO_VALUE2(FUNCTION(gsl_matrix,min)(m));
1235
+ }
1236
+
1237
+ static VALUE FUNCTION(rb_gsl_matrix,minmax)(VALUE obj)
1238
+ {
1239
+ GSL_TYPE(gsl_matrix) *m = NULL;
1240
+ BASE min, max;
1241
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1242
+ FUNCTION(gsl_matrix,minmax)(m, &min, &max);
1243
+ return rb_ary_new3(2, C_TO_VALUE2(min), C_TO_VALUE2(max));
1244
+ }
1245
+
1246
+ static VALUE FUNCTION(rb_gsl_matrix,max_index)(VALUE obj)
1247
+ {
1248
+ GSL_TYPE(gsl_matrix) *m = NULL;
1249
+ size_t imax, jmax;
1250
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1251
+ FUNCTION(gsl_matrix,max_index)(m, &imax, &jmax);
1252
+ return rb_ary_new3(2, INT2FIX(imax), INT2FIX(jmax));
1253
+ }
1254
+
1255
+ static VALUE FUNCTION(rb_gsl_matrix,min_index)(VALUE obj)
1256
+ {
1257
+ GSL_TYPE(gsl_matrix) *m = NULL;
1258
+ size_t imin, jmin;
1259
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1260
+ FUNCTION(gsl_matrix,min_index)(m, &imin, &jmin);
1261
+ return rb_ary_new3(2, INT2FIX(imin), INT2FIX(jmin));
1262
+ }
1263
+
1264
+ static VALUE FUNCTION(rb_gsl_matrix,minmax_index)(VALUE obj)
1265
+ {
1266
+ GSL_TYPE(gsl_matrix) *m = NULL;
1267
+ size_t imin, jmin, imax, jmax;
1268
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1269
+ FUNCTION(gsl_matrix,minmax_index)(m, &imin, &jmin, &imax, &jmax);
1270
+ return rb_ary_new3(2, rb_ary_new3(2, INT2FIX(imin), INT2FIX(jmin)),
1271
+ rb_ary_new3(2, INT2FIX(imax), INT2FIX(jmax)));
1272
+ }
1273
+
1274
+ static VALUE FUNCTION(rb_gsl_matrix,fwrite)(VALUE obj, VALUE io)
1275
+ {
1276
+ GSL_TYPE(gsl_matrix) *h = NULL;
1277
+ FILE *f = NULL;
1278
+ int status, flag = 0;
1279
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), h);
1280
+ f = rb_gsl_open_writefile(io, &flag);
1281
+ status = FUNCTION(gsl_matrix,fwrite)(f, h);
1282
+ if (flag == 1) fclose(f);
1283
+ return INT2FIX(status);
1284
+ }
1285
+
1286
+ static VALUE FUNCTION(rb_gsl_matrix,fread)(VALUE obj, VALUE io)
1287
+ {
1288
+ GSL_TYPE(gsl_matrix) *h = NULL;
1289
+ FILE *f = NULL;
1290
+ int status, flag = 0;
1291
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), h);
1292
+ f = rb_gsl_open_readfile(io, &flag);
1293
+ status = FUNCTION(gsl_matrix,fread)(f, h);
1294
+ if (flag == 1) fclose(f);
1295
+ return INT2FIX(status);
1296
+ }
1297
+
1298
+ static VALUE FUNCTION(rb_gsl_matrix,trace)(VALUE obj)
1299
+ {
1300
+ GSL_TYPE(gsl_matrix) *m = NULL;
1301
+ size_t i;
1302
+ BASE trace = 0;
1303
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1304
+ for (i = 0; i < m->size1; i++) {
1305
+ trace += FUNCTION(gsl_matrix,get)(m, i, i);
1306
+ }
1307
+ return C_TO_VALUE2(trace);
1308
+ }
1309
+
1310
+
1311
+ static VALUE FUNCTION(rb_gsl_matrix,uplus)(VALUE obj)
1312
+ {
1313
+ return obj;
1314
+ }
1315
+
1316
+ static VALUE FUNCTION(rb_gsl_matrix,uminus)(VALUE obj)
1317
+ {
1318
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew = NULL;
1319
+ size_t i, j;
1320
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1321
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
1322
+ for (i = 0; i < m->size1; i++) {
1323
+ for (j = 0; j < m->size2; j++) {
1324
+ FUNCTION(gsl_matrix,set)(mnew, i, j, -FUNCTION(gsl_matrix,get)(m, i, j));
1325
+ }
1326
+ }
1327
+ return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
1328
+ }
1329
+
1330
+ VALUE FUNCTION(rb_gsl_matrix,power)(VALUE obj, VALUE bb)
1331
+ {
1332
+ GSL_TYPE(gsl_matrix) *m = NULL, *mtmp = NULL, *mnew = NULL;
1333
+ size_t i, b;
1334
+ CHECK_FIXNUM(bb);
1335
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1336
+ b = FIX2INT(bb);
1337
+ mtmp = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
1338
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
1339
+ FUNCTION(gsl_matrix,memcpy)(mnew, m);
1340
+ for (i = 1; i < b; i++) {
1341
+ FUNCTION(gsl_matrix,memcpy)(mtmp, mnew);
1342
+ #ifdef BASE_DOUBLE
1343
+ gsl_linalg_matmult(mtmp, m, mnew);
1344
+ #else
1345
+ gsl_linalg_matmult_int(mtmp, m, mnew);
1346
+ #endif
1347
+ }
1348
+ FUNCTION(gsl_matrix,free)(mtmp);
1349
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1350
+ }
1351
+
1352
+ static VALUE FUNCTION(rb_gsl_matrix,submatrix)(int argc, VALUE *argv, VALUE obj)
1353
+ {
1354
+ GSL_TYPE(gsl_matrix) *m = NULL;
1355
+ QUALIFIED_VIEW(gsl_matrix,view) *mv = NULL;
1356
+ QUALIFIED_VIEW(gsl_vector,view) *vv = NULL;
1357
+ size_t i, j, n1, n2;
1358
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1359
+ parse_submatrix_args(argc, argv, m->size1, m->size2, &i, &j, &n1, &n2);
1360
+ if(n1 == 0) {
1361
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1362
+ *vv = FUNCTION(gsl_matrix,subrow)(m, i, j, n2);
1363
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,view), 0, free, vv);
1364
+ }
1365
+ else if(n2 == 0) {
1366
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1367
+ *vv = FUNCTION(gsl_matrix,subcolumn)(m, j, i, n1);
1368
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,col_view), 0, free, vv);
1369
+ } else {
1370
+ mv = ALLOC(QUALIFIED_VIEW(gsl_matrix,view));
1371
+ *mv = FUNCTION(gsl_matrix,submatrix)(m, i, j, n1, n2);
1372
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_matrix,view), 0, free, mv);
1373
+ }
1374
+ }
1375
+
1376
+ static VALUE FUNCTION(rb_gsl_matrix,return_vector_view)(VALUE obj, VALUE index,
1377
+ QUALIFIED_VIEW(gsl_vector,view) (*f)(GSL_TYPE(gsl_matrix)*,
1378
+ size_t))
1379
+ {
1380
+ GSL_TYPE(gsl_matrix) *m = NULL;
1381
+ QUALIFIED_VIEW(gsl_vector,view) *vv = NULL;
1382
+ CHECK_FIXNUM(index);
1383
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1384
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1385
+ *vv = (*f)(m, FIX2INT(index));
1386
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,view), 0, free, vv);
1387
+ }
1388
+
1389
+ static VALUE FUNCTION(rb_gsl_matrix,row)(VALUE obj, VALUE i)
1390
+ {
1391
+ return FUNCTION(rb_gsl_matrix,return_vector_view)(obj, i, FUNCTION(gsl_matrix,row));
1392
+ }
1393
+
1394
+ static VALUE FUNCTION(rb_gsl_matrix,column)(VALUE obj, VALUE j)
1395
+ {
1396
+ GSL_TYPE(gsl_matrix) *m = NULL;
1397
+ QUALIFIED_VIEW(gsl_vector,view) *vv = NULL;
1398
+ CHECK_FIXNUM(j);
1399
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1400
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1401
+ *vv = FUNCTION(gsl_matrix,column)(m, FIX2INT(j));
1402
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,col_view), 0, free, vv);
1403
+ }
1404
+
1405
+ #ifdef GSL_1_10_LATER
1406
+ static VALUE FUNCTION(rb_gsl_matrix,subrow)(VALUE obj, VALUE i, VALUE offset,
1407
+ VALUE n)
1408
+ {
1409
+ GSL_TYPE(gsl_matrix) *m = NULL;
1410
+ QUALIFIED_VIEW(gsl_vector,view) *vv = NULL;
1411
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1412
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1413
+ *vv = FUNCTION(gsl_matrix,subrow)(m, FIX2INT(i), FIX2INT(offset), FIX2INT(n));
1414
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,view), 0, free, vv);
1415
+ }
1416
+ static VALUE FUNCTION(rb_gsl_matrix,subcolumn)(VALUE obj, VALUE j, VALUE offset,
1417
+ VALUE n)
1418
+ {
1419
+ GSL_TYPE(gsl_matrix) *m = NULL;
1420
+ QUALIFIED_VIEW(gsl_vector,view) *vv = NULL;
1421
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1422
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1423
+ *vv = FUNCTION(gsl_matrix,subcolumn)(m, FIX2INT(j), FIX2INT(offset), FIX2INT(n));
1424
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,col_view), 0, free, vv);
1425
+ }
1426
+ #endif
1427
+
1428
+ static VALUE FUNCTION(rb_gsl_matrix,diagonal)(VALUE obj)
1429
+ {
1430
+ GSL_TYPE(gsl_matrix) *m = NULL;
1431
+ QUALIFIED_VIEW(gsl_vector,view) *vv = NULL;
1432
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1433
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1434
+ *vv = FUNCTION(gsl_matrix,diagonal)(m);
1435
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,view), 0, free, vv);
1436
+ }
1437
+
1438
+ static VALUE FUNCTION(rb_gsl_matrix,subdiagonal)(VALUE obj, VALUE k)
1439
+ {
1440
+ return rb_gsl_matrix_return_vector_view(obj, k, gsl_matrix_subdiagonal);
1441
+ }
1442
+
1443
+ static VALUE FUNCTION(rb_gsl_matrix,superdiagonal)(VALUE obj, VALUE k)
1444
+ {
1445
+ return rb_gsl_matrix_return_vector_view(obj, k, gsl_matrix_superdiagonal);
1446
+ }
1447
+
1448
+ static VALUE FUNCTION(rb_gsl_matrix,vector_view)(VALUE obj)
1449
+ {
1450
+ GSL_TYPE(gsl_matrix) *m = NULL;
1451
+ QUALIFIED_VIEW(gsl_vector,view) *vv;
1452
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1453
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1454
+ vv->vector.size = m->size1*m->size2;
1455
+ vv->vector.owner = 0;
1456
+ vv->vector.stride = 1;
1457
+ vv->vector.data = m->data;
1458
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,view), 0, free, vv);
1459
+ }
1460
+
1461
+
1462
+ static VALUE FUNCTION(rb_gsl_matrix,each_row)(VALUE obj)
1463
+ {
1464
+ GSL_TYPE(gsl_matrix) *m = NULL;
1465
+ QUALIFIED_VIEW(gsl_vector,view) *vv;
1466
+ size_t i;
1467
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1468
+ for (i = 0; i < m->size1; i++) {
1469
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1470
+ *vv = FUNCTION(gsl_matrix,row)(m, i);
1471
+ #ifdef BASE_DOUBLE
1472
+ rb_yield(Data_Wrap_Struct(cgsl_vector_view, 0, free, vv));
1473
+ #else
1474
+ rb_yield(Data_Wrap_Struct(cgsl_vector_int_view, 0, free, vv));
1475
+ #endif
1476
+ }
1477
+ return obj;
1478
+ }
1479
+
1480
+ static VALUE FUNCTION(rb_gsl_matrix,each_col)(VALUE obj)
1481
+ {
1482
+ GSL_TYPE(gsl_matrix) *m = NULL;
1483
+ QUALIFIED_VIEW(gsl_vector,view) *vv;
1484
+ size_t i;
1485
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1486
+ for (i = 0; i < m->size2; i++) {
1487
+ vv = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1488
+ *vv = FUNCTION(gsl_matrix,column)(m, i);
1489
+ #ifdef BASE_DOUBLE
1490
+ rb_yield(Data_Wrap_Struct(cgsl_vector_col_view, 0, free, vv));
1491
+ #else
1492
+ rb_yield(Data_Wrap_Struct(cgsl_vector_int_col_view, 0, free, vv));
1493
+ #endif
1494
+ }
1495
+ return obj;
1496
+ }
1497
+
1498
+ static VALUE FUNCTION(rb_gsl_matrix,scale_bang)(VALUE obj, VALUE x)
1499
+ {
1500
+ GSL_TYPE(gsl_matrix) *m;
1501
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1502
+ FUNCTION(gsl_matrix,scale)(m, NUMCONV(x));
1503
+ return obj;
1504
+ }
1505
+
1506
+ static VALUE FUNCTION(rb_gsl_matrix,scale)(VALUE obj, VALUE b)
1507
+ {
1508
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew;
1509
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1510
+ mnew = FUNCTION(make_matrix,clone)(m);
1511
+ FUNCTION(gsl_matrix,scale)(mnew, NUMCONV(b));
1512
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1513
+ }
1514
+
1515
+ static VALUE FUNCTION(rb_gsl_matrix,add_constant_bang)(VALUE obj, VALUE x)
1516
+ {
1517
+ GSL_TYPE(gsl_matrix) *m;
1518
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1519
+ FUNCTION(gsl_matrix,add_constant)(m, NUMCONV(x));
1520
+ return obj;
1521
+ }
1522
+
1523
+ static VALUE FUNCTION(rb_gsl_matrix,add_constant)(VALUE obj, VALUE b)
1524
+ {
1525
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew;
1526
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1527
+ mnew = FUNCTION(make_matrix,clone)(m);
1528
+ FUNCTION(gsl_matrix,add_constant)(mnew, NUMCONV(b));
1529
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1530
+ }
1531
+
1532
+ static int FUNCTION(mygsl_matrix,equal)(GSL_TYPE(gsl_matrix) *a, GSL_TYPE(gsl_matrix) *b, double eps)
1533
+ {
1534
+ size_t i, j;
1535
+ BASE x, y;
1536
+ if (a->size1 != b->size1 || a->size2 != b->size2) return 0;
1537
+ for (i = 0; i < a->size1; i++) {
1538
+ for (j = 0; j < a->size2; j++) {
1539
+ x = FUNCTION(gsl_matrix,get)(a, i, j);
1540
+ y = FUNCTION(gsl_matrix,get)(b, i, j);
1541
+ if (fabs(x-y) > eps) return 0;
1542
+ }
1543
+ }
1544
+ return 1;
1545
+ }
1546
+
1547
+ #ifdef HAVE_TENSOR_TENSOR_H
1548
+ EXTERN VALUE cgsl_tensor, cgsl_tensor_int;
1549
+ VALUE rb_gsl_tensor_equal(int argc, VALUE *argv, VALUE obj);
1550
+ VALUE rb_gsl_tensor_int_equal(int argc, VALUE *argv, VALUE obj);
1551
+ #ifdef BASE_DOUBLE
1552
+ #define TEN_P(x) TENSOR_P(x)
1553
+ #else
1554
+ #define TEN_P(x) TENSOR_INT_P(x)
1555
+ #endif
1556
+ #endif
1557
+
1558
+ static VALUE FUNCTION(rb_gsl_matrix,equal)(int argc, VALUE *argv, VALUE obj)
1559
+ {
1560
+ GSL_TYPE(gsl_matrix) *a, *b;
1561
+ double eps = 1e-10;
1562
+ VALUE bb;
1563
+ switch (argc) {
1564
+ case 2:
1565
+ bb = argv[0];
1566
+ eps = NUM2DBL(argv[1]);
1567
+ break;
1568
+ case 1:
1569
+ bb = argv[0];
1570
+ break;
1571
+ default:
1572
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
1573
+ break;
1574
+ }
1575
+ #ifdef HAVE_TENSOR_TENSOR_H
1576
+ if (TEN_P(bb)) {
1577
+ return FUNCTION(rb_gsl_tensor,equal)(argc, argv, obj);
1578
+ }
1579
+ #endif
1580
+ CHECK_MAT(bb);
1581
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), a);
1582
+ Data_Get_Struct(bb, GSL_TYPE(gsl_matrix), b);
1583
+ if (FUNCTION(mygsl_matrix,equal)(a, b, eps) == 1) return Qtrue;
1584
+ else return Qfalse;
1585
+ }
1586
+
1587
+ #ifdef HAVE_TENSOR_TENSOR_H
1588
+ #ifdef TEN_P
1589
+ #undef TEN_P
1590
+ #endif
1591
+ #endif
1592
+
1593
+ static VALUE FUNCTION(rb_gsl_matrix,equal_singleton)(int argc, VALUE *argv, VALUE obj)
1594
+ {
1595
+ GSL_TYPE(gsl_matrix) *a, *b;
1596
+ VALUE aa, bb;
1597
+ double eps = 1e-10;
1598
+ BASE x, y;
1599
+ size_t i, j;
1600
+ switch (argc) {
1601
+ case 3:
1602
+ aa = argv[0];
1603
+ bb = argv[1];
1604
+ eps = NUM2DBL(argv[2]);
1605
+ break;
1606
+ case 2:
1607
+ aa = argv[0];
1608
+ bb = argv[1];
1609
+ break;
1610
+ default:
1611
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2 or 3)", argc);
1612
+ break;
1613
+ }
1614
+ CHECK_MAT(aa); CHECK_MAT(bb);
1615
+ Data_Get_Struct(aa, GSL_TYPE(gsl_matrix), a);
1616
+ Data_Get_Struct(bb, GSL_TYPE(gsl_matrix), b);
1617
+ if (a->size1 != b->size1 || a->size2 != b->size2) return Qfalse;
1618
+ for (i = 0; i < a->size1; i++) {
1619
+ for (j = 0; j < a->size2; j++) {
1620
+ x = FUNCTION(gsl_matrix,get)(a, i, j);
1621
+ y = FUNCTION(gsl_matrix,get)(b, i, j);
1622
+ if (fabs(x-y) > eps) return Qfalse;
1623
+ }
1624
+ }
1625
+ return Qtrue;
1626
+ }
1627
+
1628
+ #ifdef HAVE_TENSOR_TENSOR_H
1629
+ #include "rb_gsl_tensor.h"
1630
+ static VALUE FUNCTION(rb_gsl_matrix,to_tensor)(VALUE obj)
1631
+ {
1632
+ GSL_TYPE(gsl_matrix) *m;
1633
+ GSL_TYPE(rbgsl_tensor) *t;
1634
+ unsigned int rank;
1635
+ size_t dim;
1636
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1637
+ if (m->size1 != m->size2) rb_raise(rb_eRuntimeError, "matrix must have equal dimensions");
1638
+ rank = 2;
1639
+ dim = m->size1;
1640
+ t = FUNCTION(rbgsl_tensor,alloc)(rank, dim);
1641
+ memcpy(t->tensor->data, m->data, sizeof(BASE)*t->tensor->size);
1642
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_tensor), 0, FUNCTION(rbgsl_tensor,free), t);
1643
+ }
1644
+ #endif
1645
+
1646
+ static VALUE FUNCTION(rb_gsl_matrix,collect)(VALUE obj)
1647
+ {
1648
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew;
1649
+ size_t i, j;
1650
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1651
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
1652
+ for (i = 0; i < m->size1; i++) {
1653
+ for (j = 0; j < m->size2; j++) {
1654
+ FUNCTION(gsl_matrix,set)(mnew, i, j, NUMCONV(rb_yield(C_TO_VALUE(FUNCTION(gsl_matrix,get)(m, i, j)))));
1655
+ }
1656
+ }
1657
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1658
+ }
1659
+
1660
+ static VALUE FUNCTION(rb_gsl_matrix,collect_bang)(VALUE obj)
1661
+ {
1662
+ GSL_TYPE(gsl_matrix) *m = NULL;
1663
+ size_t i, j;
1664
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1665
+ for (i = 0; i < m->size1; i++) {
1666
+ for (j = 0; j < m->size2; j++) {
1667
+ FUNCTION(gsl_matrix,set)(m, i, j, NUMCONV(rb_yield(C_TO_VALUE(FUNCTION(gsl_matrix,get)(m, i, j)))));
1668
+ }
1669
+ }
1670
+ return obj;
1671
+ }
1672
+
1673
+ static VALUE FUNCTION(rb_gsl_matrix,upper)(VALUE obj)
1674
+ {
1675
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew;
1676
+ size_t i, j;
1677
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1678
+ mnew = FUNCTION(make_matrix,clone)(m);
1679
+ for (i = 0; i < m->size1; i++) {
1680
+ for (j = 0; j < i; j++) {
1681
+ FUNCTION(gsl_matrix,set)(mnew, i, j, 0);
1682
+ }
1683
+ }
1684
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1685
+ }
1686
+
1687
+ static VALUE FUNCTION(rb_gsl_matrix,lower)(VALUE obj)
1688
+ {
1689
+ GSL_TYPE(gsl_matrix) *m = NULL, *mnew;
1690
+ size_t i, j;
1691
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1692
+ mnew = FUNCTION(make_matrix,clone)(m);
1693
+ for (i = 0; i < m->size1; i++) {
1694
+ for (j = i+1; j < m->size2; j++) {
1695
+ FUNCTION(gsl_matrix,set)(mnew, i, j, 0);
1696
+ }
1697
+ }
1698
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1699
+ }
1700
+
1701
+ /* singleton method, creates Pascal matrix of dimension n */
1702
+ static VALUE FUNCTION(rb_gsl_matrix,pascal1)(VALUE obj, VALUE n)
1703
+ {
1704
+ GSL_TYPE(gsl_matrix) *m;
1705
+ BASE x;
1706
+ size_t i, j, dim;
1707
+ CHECK_FIXNUM(n);
1708
+ dim = (size_t) FIX2INT(n);
1709
+ m = FUNCTION(gsl_matrix,alloc)(dim, dim);
1710
+ for (j = 0; j < dim; j++) FUNCTION(gsl_matrix,set)(m, 0, j, (BASE) 1);
1711
+ for (i = 1; i < dim; i++) {
1712
+ FUNCTION(gsl_matrix,set)(m, i, 0, (BASE) 1);
1713
+ for (j = 1; j < dim; j++) {
1714
+ x=FUNCTION(gsl_matrix,get)(m,i-1,j)+FUNCTION(gsl_matrix,get)(m,i,j-1);
1715
+ FUNCTION(gsl_matrix,set)(m, i, j, x);
1716
+ }
1717
+ }
1718
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
1719
+ }
1720
+
1721
+ #ifdef BASE_DOUBLE
1722
+ static VALUE FUNCTION(rb_gsl_matrix,hilbert)(VALUE obj, VALUE n)
1723
+ {
1724
+ GSL_TYPE(gsl_matrix) *m;
1725
+ double x;
1726
+ size_t i, j, dim;
1727
+ CHECK_FIXNUM(n);
1728
+ dim = (size_t) FIX2INT(n);
1729
+ m = FUNCTION(gsl_matrix,alloc)(dim, dim);
1730
+ for (i = 0; i < dim; i++) {
1731
+ for (j = 0; j < dim; j++) {
1732
+ x = 1.0/(i + j + 1);
1733
+ FUNCTION(gsl_matrix,set)(m, i, j, (BASE) x);
1734
+ }
1735
+ }
1736
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
1737
+ }
1738
+
1739
+ double mygsl_binomial_coef(unsigned int n, unsigned int k);
1740
+ static VALUE FUNCTION(rb_gsl_matrix,invhilbert)(VALUE obj, VALUE n)
1741
+ {
1742
+ GSL_TYPE(gsl_matrix) *m;
1743
+ double x, y;
1744
+ size_t i, j, dim;
1745
+ CHECK_FIXNUM(n);
1746
+ dim = (size_t) FIX2INT(n);
1747
+ m = FUNCTION(gsl_matrix,alloc)(dim, dim);
1748
+ for (i = 0; i < dim; i++) {
1749
+ for (j = 0; j < dim; j++) {
1750
+ if ((i+j)%2 == 0) x = 1;
1751
+ else x = -1;
1752
+ x *= (i + j + 1);
1753
+ x *= mygsl_binomial_coef(dim + i, dim - j - 1);
1754
+ x *= mygsl_binomial_coef(dim + j, dim - i - 1);
1755
+ y = mygsl_binomial_coef(i + j, i);
1756
+ x *= y*y;
1757
+ FUNCTION(gsl_matrix,set)(m, i, j, (BASE) x);
1758
+ }
1759
+ }
1760
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
1761
+ }
1762
+ #endif
1763
+
1764
+ static void FUNCTION(mygsl_matrix,vandermonde)(GSL_TYPE(gsl_matrix) *m, GSL_TYPE(gsl_vector) *v)
1765
+ {
1766
+ size_t i, j;
1767
+
1768
+ for (i = 0; i < v->size; i++) {
1769
+ for (j = 0; j < v->size; j++) {
1770
+ FUNCTION(gsl_matrix,set)(m, i, j, (BASE) gsl_pow_int(FUNCTION(gsl_vector,get)(v, i), v->size-j-1));
1771
+ }
1772
+ }
1773
+ }
1774
+
1775
+ /* singleton */
1776
+ static VALUE FUNCTION(rb_gsl_matrix,vandermonde)(VALUE obj, VALUE vv)
1777
+ {
1778
+ GSL_TYPE(gsl_vector) *v;
1779
+ GSL_TYPE(gsl_matrix) *m;
1780
+ int flag = 0;
1781
+ if (TYPE(vv) == T_ARRAY) {
1782
+ v = FUNCTION(make_cvector,from_rarray)(vv);
1783
+ flag = 1;
1784
+ } else if (VEC_P(vv)) {
1785
+ Data_Get_Struct(vv, GSL_TYPE(gsl_vector), v);
1786
+ } else {
1787
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array or Vector expected)",
1788
+ rb_class2name(CLASS_OF(vv)));
1789
+ }
1790
+ m = FUNCTION(gsl_matrix,alloc)(v->size, v->size);
1791
+ FUNCTION(mygsl_matrix,vandermonde)(m, v);
1792
+ if (flag == 1) FUNCTION(gsl_vector,free)(v);
1793
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
1794
+ }
1795
+
1796
+ static void FUNCTION(mygsl_matrix,toeplitz)(GSL_TYPE(gsl_matrix) *m, GSL_TYPE(gsl_vector) *v)
1797
+ {
1798
+ size_t i, j;
1799
+ for (i = 0; i < v->size; i++) {
1800
+ for (j = 0; j < v->size; j++) {
1801
+ if (j >= i)
1802
+ FUNCTION(gsl_matrix,set)(m, i, j, FUNCTION(gsl_vector,get)(v, j-i));
1803
+ else
1804
+ FUNCTION(gsl_matrix,set)(m, i, j, FUNCTION(gsl_vector,get)(v, i-j));
1805
+ }
1806
+ }
1807
+ }
1808
+
1809
+ static VALUE FUNCTION(rb_gsl_matrix,toeplitz)(VALUE obj, VALUE vv)
1810
+ {
1811
+ GSL_TYPE(gsl_vector) *v;
1812
+ GSL_TYPE(gsl_matrix) *m;
1813
+ int flag = 0;
1814
+ if (TYPE(vv) == T_ARRAY) {
1815
+ v = FUNCTION(make_cvector,from_rarray)(vv);
1816
+ flag = 1;
1817
+ } else if (VEC_P(vv)) {
1818
+ Data_Get_Struct(vv, GSL_TYPE(gsl_vector), v);
1819
+ } else {
1820
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array or Vector expected)",
1821
+ rb_class2name(CLASS_OF(vv)));
1822
+ }
1823
+ m = FUNCTION(gsl_matrix,alloc)(v->size, v->size);
1824
+ FUNCTION(mygsl_matrix,toeplitz)(m, v);
1825
+ if (flag == 1) FUNCTION(gsl_vector,free)(v);
1826
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
1827
+ }
1828
+
1829
+ void FUNCTION(mygsl_vector,to_m_circulant)(GSL_TYPE(gsl_matrix) *m, GSL_TYPE(gsl_vector) *v);
1830
+ static VALUE FUNCTION(rb_gsl_matrix,circulant)(VALUE obj, VALUE vv)
1831
+ {
1832
+ GSL_TYPE(gsl_vector) *v;
1833
+ GSL_TYPE(gsl_matrix) *m;
1834
+ int flag = 0;
1835
+ if (TYPE(vv) == T_ARRAY) {
1836
+ v = FUNCTION(make_cvector,from_rarray)(vv);
1837
+ flag = 1;
1838
+ } else if (VEC_P(vv)) {
1839
+ Data_Get_Struct(vv, GSL_TYPE(gsl_vector), v);
1840
+ } else {
1841
+ rb_raise(rb_eTypeError, "wrong argument type %s (Array or Vector expected)",
1842
+ rb_class2name(CLASS_OF(vv)));
1843
+ }
1844
+ m = FUNCTION(gsl_matrix,alloc)(v->size, v->size);
1845
+ FUNCTION(mygsl_vector,to_m_circulant)(m, v);
1846
+ if (flag == 1) FUNCTION(gsl_vector,free)(v);
1847
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
1848
+ }
1849
+
1850
+ static void FUNCTION(mygsl_matrix,indgen)(GSL_TYPE(gsl_matrix) *m,
1851
+ BASE start, BASE step)
1852
+ {
1853
+ size_t i, j;
1854
+ BASE n;
1855
+ n = start;
1856
+ for (i = 0; i < m->size1; i++) {
1857
+ for (j = 0; j < m->size2; j++) {
1858
+ FUNCTION(gsl_matrix,set)(m, i, j, n);
1859
+ n += step;
1860
+ }
1861
+ }
1862
+ }
1863
+
1864
+ static VALUE FUNCTION(rb_gsl_matrix,indgen_singleton)(int argc, VALUE *argv, VALUE obj)
1865
+ {
1866
+ GSL_TYPE(gsl_matrix) *m;
1867
+ size_t n1, n2;
1868
+ BASE start = 0, step = 1;
1869
+ switch (argc) {
1870
+ case 4:
1871
+ step = NUMCONV2(argv[3]);
1872
+ /* no break */
1873
+ case 3:
1874
+ start = NUMCONV2(argv[2]);
1875
+ /* no break */
1876
+ case 2:
1877
+ n1 = NUM2INT(argv[0]);
1878
+ n2 = NUM2INT(argv[1]);
1879
+ break;
1880
+ default:
1881
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 2-4)", argc);
1882
+ break;
1883
+ }
1884
+ m = FUNCTION(gsl_matrix,alloc)(n1, n2);
1885
+ FUNCTION(mygsl_matrix,indgen)(m, start, step);
1886
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), m);
1887
+ }
1888
+
1889
+ static VALUE FUNCTION(rb_gsl_matrix,indgen)(int argc, VALUE *argv, VALUE obj)
1890
+ {
1891
+ GSL_TYPE(gsl_matrix) *m, *mnew;
1892
+ BASE start = 0, step = 1;
1893
+ switch (argc) {
1894
+ case 2:
1895
+ step = NUMCONV2(argv[1]);
1896
+ /* no break */
1897
+ case 1:
1898
+ start = NUMCONV2(argv[0]);
1899
+ break;
1900
+ case 0:
1901
+ break;
1902
+ default:
1903
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0-2)", argc);
1904
+ break;
1905
+ }
1906
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1907
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
1908
+ FUNCTION(mygsl_matrix,indgen)(mnew, start, step);
1909
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
1910
+ }
1911
+
1912
+ static VALUE FUNCTION(rb_gsl_matrix,indgen_bang)(int argc, VALUE *argv, VALUE obj)
1913
+ {
1914
+ GSL_TYPE(gsl_matrix) *m;
1915
+ BASE start = 0, step = 1;
1916
+ switch (argc) {
1917
+ case 2:
1918
+ step = NUMCONV2(argv[1]);
1919
+ /* no break */
1920
+ case 1:
1921
+ start = NUMCONV2(argv[0]);
1922
+ break;
1923
+ case 0:
1924
+ break;
1925
+ default:
1926
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0-2)", argc);
1927
+ break;
1928
+ }
1929
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1930
+ FUNCTION(mygsl_matrix,indgen)(m, start, step);
1931
+ return obj;
1932
+ }
1933
+
1934
+ static VALUE FUNCTION(rb_gsl_matrix,to_a)(VALUE obj)
1935
+ {
1936
+ GSL_TYPE(gsl_matrix) *m;
1937
+ VALUE ma, ra;
1938
+ size_t i, j;
1939
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1940
+ ma = rb_ary_new2(m->size1);
1941
+ for(i=0; i < m->size1; i++) {
1942
+ ra = rb_ary_new2(m->size2);
1943
+ rb_ary_store(ma, i, ra);
1944
+ for(j=0; j < m->size2; j++) {
1945
+ rb_ary_store(ra, j, C_TO_VALUE(FUNCTION(gsl_matrix,get)(m, i, j)));
1946
+ }
1947
+ }
1948
+ return ma;
1949
+ }
1950
+
1951
+ static VALUE FUNCTION(rb_gsl_matrix,to_v)(VALUE obj)
1952
+ {
1953
+ GSL_TYPE(gsl_matrix) *m;
1954
+ GSL_TYPE(gsl_vector) *v;
1955
+ size_t i, j, k;
1956
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1957
+ v = FUNCTION(gsl_vector,alloc)(m->size1*m->size2);
1958
+ // memcpy(v->data, m->data, sizeof(BASE)*v->size);
1959
+ for (i = 0, k = 0; i < m->size1; i++) {
1960
+ for (j = 0; j < m->size2; j++, k++) {
1961
+ FUNCTION(gsl_vector,set)(v, k, FUNCTION(gsl_matrix,get)(m, i, j));
1962
+ }
1963
+ }
1964
+
1965
+ if(m->size1 > 1 && m->size2 == 1) {
1966
+ return Data_Wrap_Struct(CONCAT2(GSL_TYPE(cgsl_vector),col), 0, FUNCTION(gsl_vector,free), v);
1967
+ } else {
1968
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_vector), 0, FUNCTION(gsl_vector,free), v);
1969
+ }
1970
+ }
1971
+
1972
+ static VALUE FUNCTION(rb_gsl_matrix,to_vview)(VALUE obj)
1973
+ {
1974
+ GSL_TYPE(gsl_matrix) *m;
1975
+ QUALIFIED_VIEW(gsl_vector,view) *v;
1976
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1977
+ v = ALLOC(QUALIFIED_VIEW(gsl_vector,view));
1978
+ v->vector.size = m->size1*m->size2;
1979
+ v->vector.stride = 1;
1980
+ v->vector.owner = 0;
1981
+ v->vector.data = m->data;
1982
+ return Data_Wrap_Struct(QUALIFIED_VIEW(cgsl_vector,view), 0, FUNCTION(gsl_vector,free), v);
1983
+ }
1984
+
1985
+ static VALUE FUNCTION(rb_gsl_matrix,norm)(VALUE obj)
1986
+ {
1987
+ GSL_TYPE(gsl_matrix) *m;
1988
+ size_t i, n;
1989
+ BASE x = 0;
1990
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
1991
+ n = m->size1*m->size2;
1992
+ for (i = 0; i < n; i++) x += m->data[i]*m->data[i];
1993
+ return rb_float_new(sqrt(x));
1994
+ }
1995
+
1996
+ static int FUNCTION(mygsl_matrix,reverse_columns)(GSL_TYPE(gsl_matrix) *dst,
1997
+ GSL_TYPE(gsl_matrix) *src)
1998
+ {
1999
+ size_t j;
2000
+ QUALIFIED_VIEW(gsl_vector,view) col;
2001
+ if (dst->size1 != src->size1 || dst->size2 != src->size2)
2002
+ rb_raise(rb_eRuntimeError, "matrix sizes are different.");
2003
+ for (j = 0; j < src->size2; j++) {
2004
+ col = FUNCTION(gsl_matrix,column)(src, j);
2005
+ FUNCTION(gsl_matrix,set_col)(dst, dst->size2-1-j, &col.vector);
2006
+ }
2007
+ return 0;
2008
+ }
2009
+
2010
+ static int FUNCTION(mygsl_matrix,reverse_rows)(GSL_TYPE(gsl_matrix) *dst,
2011
+ GSL_TYPE(gsl_matrix) *src)
2012
+ {
2013
+ size_t i;
2014
+ QUALIFIED_VIEW(gsl_vector,view) row;
2015
+ if (dst->size1 != src->size1 || dst->size2 != src->size2)
2016
+ rb_raise(rb_eRuntimeError, "matrix sizes are different.");
2017
+ for (i = 0; i < src->size1; i++) {
2018
+ row = FUNCTION(gsl_matrix,row)(src, i);
2019
+ FUNCTION(gsl_matrix,set_row)(dst, dst->size1-1-i, &row.vector);
2020
+ }
2021
+ return 0;
2022
+ }
2023
+
2024
+ static VALUE FUNCTION(rb_gsl_matrix,reverse_columns)(VALUE obj)
2025
+ {
2026
+ GSL_TYPE(gsl_matrix) *m, *mnew;
2027
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2028
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2029
+ FUNCTION(mygsl_matrix,reverse_columns)(mnew, m);
2030
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
2031
+ }
2032
+
2033
+ static VALUE FUNCTION(rb_gsl_matrix,reverse_columns_bang)(VALUE obj)
2034
+ {
2035
+ GSL_TYPE(gsl_matrix) *m, *mnew;
2036
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2037
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2038
+ FUNCTION(mygsl_matrix,reverse_columns)(mnew, m);
2039
+ FUNCTION(gsl_matrix,memcpy)(m, mnew);
2040
+ FUNCTION(gsl_matrix,free)(mnew);
2041
+ return obj;
2042
+ }
2043
+
2044
+ static VALUE FUNCTION(rb_gsl_matrix,reverse_rows)(VALUE obj)
2045
+ {
2046
+ GSL_TYPE(gsl_matrix) *m, *mnew;
2047
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2048
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2049
+ FUNCTION(mygsl_matrix,reverse_rows)(mnew, m);
2050
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
2051
+ }
2052
+
2053
+ static VALUE FUNCTION(rb_gsl_matrix,reverse_rows_bang)(VALUE obj)
2054
+ {
2055
+ GSL_TYPE(gsl_matrix) *m, *mnew;
2056
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2057
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2058
+ FUNCTION(mygsl_matrix,reverse_rows)(mnew, m);
2059
+ FUNCTION(gsl_matrix,memcpy)(m, mnew);
2060
+ FUNCTION(gsl_matrix,free)(mnew);
2061
+ return obj;
2062
+ }
2063
+
2064
+ static VALUE FUNCTION(rb_gsl_matrix,block)(VALUE obj)
2065
+ {
2066
+ GSL_TYPE(gsl_matrix) *m;
2067
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2068
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_block), 0, NULL, m->block);
2069
+ }
2070
+
2071
+ static VALUE FUNCTION(rb_gsl_matrix,info)(VALUE obj)
2072
+ {
2073
+ GSL_TYPE(gsl_matrix) *m;
2074
+ char buf[256];
2075
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2076
+ sprintf(buf, "Class: %s\n", rb_class2name(CLASS_OF(obj)));
2077
+ #ifdef RUBY_1_9_LATER
2078
+ sprintf(buf, "%sSuperClass: %s\n", buf, rb_class2name(RCLASS_SUPER(CLASS_OF(obj))));
2079
+ #else
2080
+ sprintf(buf, "%sSuperClass: %s\n", buf, rb_class2name(RCLASS(CLASS_OF(obj))->super));
2081
+ #endif
2082
+ sprintf(buf, "%sDimension: %dx%d\n", buf, (int) m->size1, (int) m->size2);
2083
+ sprintf(buf, "%sSize: %d\n", buf, (int) (m->size1*m->size2));
2084
+ return rb_str_new2(buf);
2085
+ }
2086
+
2087
+ static VALUE FUNCTION(rb_gsl_matrix,any)(VALUE obj)
2088
+ {
2089
+ GSL_TYPE(gsl_matrix) *m;
2090
+ QUALIFIED_VIEW(gsl_vector,view) vv;
2091
+ GSL_TYPE(gsl_vector) *v;
2092
+ gsl_vector_int *vnew;
2093
+ size_t j;
2094
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2095
+ vnew = gsl_vector_int_alloc(m->size2);
2096
+ for (j = 0; j < m->size2; j++) {
2097
+ vv = FUNCTION(gsl_matrix,column)(m, j);
2098
+ v = &vv.vector;
2099
+ if (FUNCTION(gsl_vector,isnull(v))) gsl_vector_int_set(vnew, j, 0);
2100
+ else gsl_vector_int_set(vnew, j, 1);
2101
+ }
2102
+ return Data_Wrap_Struct(cgsl_vector_int, 0, gsl_vector_int_free, vnew);
2103
+ }
2104
+
2105
+ static VALUE FUNCTION(rb_gsl_matrix,all)(VALUE obj)
2106
+ {
2107
+ GSL_TYPE(gsl_matrix) *m;
2108
+ QUALIFIED_VIEW(gsl_vector,view) vv;
2109
+ GSL_TYPE(gsl_vector) *v;
2110
+ gsl_vector_int *vnew;
2111
+ size_t i, j;
2112
+ int flag = 0;
2113
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2114
+ vnew = gsl_vector_int_alloc(m->size2);
2115
+ for (j = 0; j < m->size2; j++) {
2116
+ vv = FUNCTION(gsl_matrix,column)(m, j);
2117
+ v = &vv.vector;
2118
+ /* if (FUNCTION(gsl_vector,isnull(v))) gsl_vector_int_set(vnew, j, 0);
2119
+ else gsl_vector_int_set(vnew, j, 1);*/
2120
+ for (i = 0; i < v->size; i++) {
2121
+ if (FUNCTION(gsl_vector,get)(v, i) == (BASE) 0) {
2122
+ gsl_vector_int_set(vnew, j, 0);
2123
+ flag = 0;
2124
+ break;
2125
+ } else {
2126
+ flag = 1;
2127
+ }
2128
+ }
2129
+ if (flag == 1) gsl_vector_int_set(vnew, j, 1);
2130
+ }
2131
+ return Data_Wrap_Struct(cgsl_vector_int, 0, gsl_vector_int_free, vnew);
2132
+ }
2133
+
2134
+ static VALUE FUNCTION(rb_gsl_matrix,rot90)(int argc, VALUE *argv, VALUE obj)
2135
+ {
2136
+ GSL_TYPE(gsl_matrix) *m, *mnew, *mtmp;
2137
+ int p;
2138
+ switch (argc) {
2139
+ case 0:
2140
+ p = 1;
2141
+ break;
2142
+ case 1:
2143
+ p = FIX2INT(argv[0])%4;
2144
+ break;
2145
+ default:
2146
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
2147
+ break;
2148
+ }
2149
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2150
+ switch (p) {
2151
+ case 0:
2152
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2153
+ // FUNCTION(gsl_matrix,memcpy(mnew, m));
2154
+ FUNCTION(gsl_matrix,memcpy)(mnew, m);
2155
+ break;
2156
+ case 1:
2157
+ case -3:
2158
+ mtmp = FUNCTION(gsl_matrix,alloc)(m->size2, m->size1);
2159
+ FUNCTION(gsl_matrix,transpose_memcpy(mtmp, m));
2160
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size2, m->size1);
2161
+ FUNCTION(mygsl_matrix,reverse_rows)(mnew, mtmp);
2162
+ FUNCTION(gsl_matrix,free)(mtmp);
2163
+ break;
2164
+ case 2:
2165
+ case -2:
2166
+ mtmp = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2167
+ FUNCTION(mygsl_matrix,reverse_rows)(mtmp,m);
2168
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2169
+ FUNCTION(mygsl_matrix,reverse_columns)(mnew, mtmp);
2170
+ FUNCTION(gsl_matrix,free)(mtmp);
2171
+ break;
2172
+ case 3:
2173
+ case -1:
2174
+ mtmp = FUNCTION(gsl_matrix,alloc)(m->size2, m->size1);
2175
+ FUNCTION(gsl_matrix,transpose_memcpy(mtmp, m));
2176
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size2, m->size1);
2177
+ FUNCTION(mygsl_matrix,reverse_columns)(mnew, mtmp);
2178
+ FUNCTION(gsl_matrix,free)(mtmp);
2179
+ break;
2180
+ default:
2181
+ return Qnil;
2182
+ break;
2183
+ }
2184
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
2185
+ }
2186
+
2187
+ void FUNCTION(mygsl_vector,diff)(GSL_TYPE(gsl_vector) *vdst,
2188
+ GSL_TYPE(gsl_vector) *vsrc, size_t n);
2189
+
2190
+ static VALUE FUNCTION(rb_gsl_matrix,diff)(int argc, VALUE *argv, VALUE obj)
2191
+ {
2192
+ GSL_TYPE(gsl_matrix) *m, *mnew;
2193
+ QUALIFIED_VIEW(gsl_vector,view) v1, v2;
2194
+ size_t n, j;
2195
+ switch (argc) {
2196
+ case 0:
2197
+ n = 1;
2198
+ break;
2199
+ case 1:
2200
+ n = FIX2INT(argv[0]);
2201
+ break;
2202
+ default:
2203
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0 or 1)", argc);
2204
+ break;
2205
+ }
2206
+ if (n <= 0) return obj;
2207
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2208
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1-n, m->size2);
2209
+ if (m->size1 <= n) return obj;
2210
+ for (j = 0; j < m->size2; j++) {
2211
+ v1 = FUNCTION(gsl_matrix,column)(m, j);
2212
+ v2 = FUNCTION(gsl_matrix,column)(mnew, j);
2213
+ FUNCTION(mygsl_vector,diff)(&v2.vector, &v1.vector, n);
2214
+ }
2215
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
2216
+ }
2217
+
2218
+ static VALUE FUNCTION(rb_gsl_matrix,test)(VALUE obj, int (*f)(const double))
2219
+ {
2220
+ GSL_TYPE(gsl_matrix) *m;
2221
+ gsl_matrix_int *mi;
2222
+ size_t i, j;
2223
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2224
+ mi = gsl_matrix_int_alloc(m->size1, m->size2);
2225
+ for (i = 0; i < m->size1; i++) {
2226
+ for (j = 0; j < m->size2; j++) {
2227
+ gsl_matrix_int_set(mi, i, j, (*f)(FUNCTION(gsl_matrix,get)(m, i, j)));
2228
+ }
2229
+ }
2230
+ return Data_Wrap_Struct(cgsl_matrix_int, 0, gsl_matrix_int_free, mi);
2231
+ }
2232
+
2233
+ static VALUE FUNCTION(rb_gsl_matrix,isnan)(VALUE obj)
2234
+ {
2235
+ return FUNCTION(rb_gsl_matrix,test)(obj, gsl_isnan);
2236
+ }
2237
+
2238
+ static VALUE FUNCTION(rb_gsl_matrix,isinf)(VALUE obj)
2239
+ {
2240
+ return FUNCTION(rb_gsl_matrix,test)(obj, gsl_isinf);
2241
+ }
2242
+
2243
+ static VALUE FUNCTION(rb_gsl_matrix,finite)(VALUE obj)
2244
+ {
2245
+ return FUNCTION(rb_gsl_matrix,test)(obj, gsl_finite);
2246
+ }
2247
+
2248
+ static VALUE FUNCTION(rb_gsl_matrix,sgn)(VALUE obj)
2249
+ {
2250
+ GSL_TYPE(gsl_matrix) *m, *mnew;
2251
+ BASE x;
2252
+ size_t i, j;
2253
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2254
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2255
+ for (i = 0; i < m->size1; i++) {
2256
+ for (j = 0; j < m->size2; j++) {
2257
+ x = FUNCTION(gsl_matrix,get)(m, i, j);
2258
+ FUNCTION(gsl_matrix,set)(mnew, i, j, (BASE)(x>0 ? 1 : (x<0 ? -1 : 0)));
2259
+ }
2260
+ }
2261
+
2262
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
2263
+ }
2264
+
2265
+ static VALUE FUNCTION(rb_gsl_matrix,abs)(VALUE obj)
2266
+ {
2267
+ GSL_TYPE(gsl_matrix) *m, *mnew;
2268
+ size_t i, j;
2269
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2270
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2271
+ for (i = 0; i < m->size1; i++) {
2272
+ for (j = 0; j < m->size2; j++) {
2273
+ FUNCTION(gsl_matrix,set)(mnew, i, j, (BASE) fabs(FUNCTION(gsl_matrix,get)(m, i, j)));
2274
+ }
2275
+ }
2276
+
2277
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
2278
+ }
2279
+
2280
+ static VALUE FUNCTION(rb_gsl_matrix,horzcat)(VALUE obj, VALUE mm2)
2281
+ {
2282
+ GSL_TYPE(gsl_matrix) *m, *m2, *mnew;
2283
+ QUALIFIED_VIEW(gsl_vector,view) v;
2284
+ size_t j, k;
2285
+ CHECK_MAT(mm2);
2286
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2287
+ Data_Get_Struct(mm2, GSL_TYPE(gsl_matrix), m2);
2288
+ if (m->size1 != m2->size1)
2289
+ rb_raise(rb_eRuntimeError, "Different number of rows (%d and %d).",
2290
+ (int) m->size1, (int) m2->size1);
2291
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2 + m2->size2);
2292
+ for (j = 0, k = 0; j < m->size2; j++, k++) {
2293
+ v = FUNCTION(gsl_matrix,column)(m, j);
2294
+ FUNCTION(gsl_matrix,set_col)(mnew, k, &v.vector);
2295
+ }
2296
+ for (j = 0; j < m2->size2; j++, k++) {
2297
+ v = FUNCTION(gsl_matrix,column)(m2, j);
2298
+ FUNCTION(gsl_matrix,set_col)(mnew, k, &v.vector);
2299
+ }
2300
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
2301
+ }
2302
+
2303
+ static VALUE FUNCTION(rb_gsl_matrix,horzcat_singleton)(VALUE klass, VALUE mm, VALUE mm2)
2304
+ {
2305
+ CHECK_MAT(mm);
2306
+ return FUNCTION(rb_gsl_matrix,horzcat)(mm, mm2);
2307
+ }
2308
+
2309
+
2310
+ static VALUE FUNCTION(rb_gsl_matrix,vertcat)(VALUE obj, VALUE mm2)
2311
+ {
2312
+ GSL_TYPE(gsl_matrix) *m, *m2, *mnew;
2313
+ QUALIFIED_VIEW(gsl_vector,view) v;
2314
+ size_t i, k;
2315
+ CHECK_MAT(mm2);
2316
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2317
+ Data_Get_Struct(mm2, GSL_TYPE(gsl_matrix), m2);
2318
+ if (m->size2 != m2->size2)
2319
+ rb_raise(rb_eRuntimeError, "Different number of columns (%d and %d).",
2320
+ (int) m->size2, (int) m2->size2);
2321
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1 + m2->size1, m->size2);
2322
+ for (i = 0, k = 0; i < m->size1; i++, k++) {
2323
+ v = FUNCTION(gsl_matrix,row)(m, i);
2324
+ FUNCTION(gsl_matrix,set_row)(mnew, k, &v.vector);
2325
+ }
2326
+ for (i = 0; i < m2->size1; i++, k++) {
2327
+ v = FUNCTION(gsl_matrix,row)(m2, i);
2328
+ FUNCTION(gsl_matrix,set_row)(mnew, k, &v.vector);
2329
+ }
2330
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
2331
+ }
2332
+
2333
+ static VALUE FUNCTION(rb_gsl_matrix,vertcat_singleton)(VALUE klass, VALUE mm, VALUE mm2)
2334
+ {
2335
+ CHECK_MAT(mm);
2336
+ return FUNCTION(rb_gsl_matrix,vertcat)(mm, mm2);
2337
+ }
2338
+
2339
+ #ifdef GSL_1_9_LATER
2340
+ static VALUE FUNCTION(rb_gsl_matrix,property)(VALUE obj,
2341
+ int (*f)(const GSL_TYPE(gsl_matrix)*)) {
2342
+ GSL_TYPE(gsl_matrix) *m;
2343
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2344
+ return INT2FIX((*f)(m));
2345
+ }
2346
+
2347
+ static VALUE FUNCTION(rb_gsl_matrix,property2)(VALUE obj,
2348
+ int (*f)(const GSL_TYPE(gsl_matrix) *)) {
2349
+ GSL_TYPE(gsl_matrix) *m;
2350
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2351
+ if ((*f)(m)) return Qtrue;
2352
+ else return Qfalse;
2353
+ }
2354
+ static VALUE FUNCTION(rb_gsl_matrix,ispos)(VALUE obj)
2355
+ {
2356
+ return FUNCTION(rb_gsl_matrix,property)(obj, FUNCTION(gsl_matrix,ispos));
2357
+ }
2358
+ static VALUE FUNCTION(rb_gsl_matrix,ispos2)(VALUE obj)
2359
+ {
2360
+ return FUNCTION(rb_gsl_matrix,property2)(obj, FUNCTION(gsl_matrix,ispos));
2361
+ }
2362
+ static VALUE FUNCTION(rb_gsl_matrix,isneg)(VALUE obj)
2363
+ {
2364
+ return FUNCTION(rb_gsl_matrix,property)(obj, FUNCTION(gsl_matrix,isneg));
2365
+ }
2366
+ static VALUE FUNCTION(rb_gsl_matrix,isneg2)(VALUE obj)
2367
+ {
2368
+ return FUNCTION(rb_gsl_matrix,property2)(obj, FUNCTION(gsl_matrix,isneg));
2369
+ }
2370
+ #endif
2371
+
2372
+ #ifdef GSL_1_10_LATER
2373
+ static VALUE FUNCTION(rb_gsl_matrix,isnonneg)(VALUE obj)
2374
+ {
2375
+ return FUNCTION(rb_gsl_matrix,property)(obj, FUNCTION(gsl_matrix,isnonneg));
2376
+ }
2377
+ static VALUE FUNCTION(rb_gsl_matrix,isnonneg2)(VALUE obj)
2378
+ {
2379
+ return FUNCTION(rb_gsl_matrix,property2)(obj, FUNCTION(gsl_matrix,isnonneg));
2380
+ }
2381
+ #endif
2382
+
2383
+ static VALUE FUNCTION(rb_gsl_matrix,symmetrize)(VALUE obj)
2384
+ {
2385
+ GSL_TYPE(gsl_matrix) *m, *mnew;
2386
+ size_t i, j;
2387
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2388
+ if (m->size1 != m->size2)
2389
+ rb_raise(rb_eRuntimeError, "symmetrize: not a square matrix.\n");
2390
+ mnew = FUNCTION(gsl_matrix,alloc)(m->size1, m->size2);
2391
+ for (i = 0; i < m->size1; i++) {
2392
+ for (j = i; j < m->size2; j++) {
2393
+ FUNCTION(gsl_matrix,set)(mnew, i, j, FUNCTION(gsl_matrix,get)(m, i, j));
2394
+ }
2395
+ for (j = 0; j < i; j++) {
2396
+ FUNCTION(gsl_matrix,set)(mnew, i, j, FUNCTION(gsl_matrix,get)(m, j, i));
2397
+ }
2398
+ }
2399
+ return Data_Wrap_Struct(GSL_TYPE(cgsl_matrix), 0, FUNCTION(gsl_matrix,free), mnew);
2400
+ }
2401
+
2402
+ static VALUE FUNCTION(rb_gsl_matrix,symmetrize_bang)(VALUE obj)
2403
+ {
2404
+ GSL_TYPE(gsl_matrix) *m;
2405
+ size_t i, j;
2406
+ Data_Get_Struct(obj, GSL_TYPE(gsl_matrix), m);
2407
+ if (m->size1 != m->size2)
2408
+ rb_raise(rb_eRuntimeError, "symmetrize: not a square matrix.\n");
2409
+ for (i = 0; i < m->size1; i++) {
2410
+ for (j = 0; j < i; j++) {
2411
+ FUNCTION(gsl_matrix,set)(m, i, j, FUNCTION(gsl_matrix,get)(m, j, i));
2412
+ }
2413
+ }
2414
+ return obj;
2415
+ }
2416
+
2417
+ void FUNCTION(Init_gsl_matrix,init)(VALUE module)
2418
+ {
2419
+ /* rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "new", FUNCTION(rb_gsl_matrix,alloc), -1);*/
2420
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "[]", FUNCTION(rb_gsl_matrix,alloc), -1);
2421
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "alloc", FUNCTION(rb_gsl_matrix,alloc), -1);
2422
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "calloc", FUNCTION(rb_gsl_matrix,calloc), 2);
2423
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "eye", FUNCTION(rb_gsl_matrix,eye), -1);
2424
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "ones", FUNCTION(rb_gsl_matrix,ones), -1);
2425
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "zeros", FUNCTION(rb_gsl_matrix,zeros), -1);
2426
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "diagonal",
2427
+ FUNCTION(rb_gsl_matrix,diagonal_singleton), -1);
2428
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "diag",
2429
+ FUNCTION(rb_gsl_matrix,diagonal_singleton), -1);
2430
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "identity",
2431
+ FUNCTION(rb_gsl_matrix,identity), 1);
2432
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "scalar",
2433
+ FUNCTION(rb_gsl_matrix,identity), 1);
2434
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "unit",
2435
+ FUNCTION(rb_gsl_matrix,identity), 1);
2436
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "I",
2437
+ FUNCTION(rb_gsl_matrix,identity), 1);
2438
+
2439
+ /*****/
2440
+ rb_define_method(GSL_TYPE(cgsl_matrix), "size1",
2441
+ FUNCTION(rb_gsl_matrix,size1), 0);
2442
+ rb_define_method(GSL_TYPE(cgsl_matrix), "size2",
2443
+ FUNCTION(rb_gsl_matrix,size2), 0);
2444
+ rb_define_method(GSL_TYPE(cgsl_matrix), "shape",
2445
+ FUNCTION(rb_gsl_matrix,shape), 0);
2446
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "size", "shape");
2447
+
2448
+ rb_define_method(GSL_TYPE(cgsl_matrix), "get", FUNCTION(rb_gsl_matrix,get), -1);
2449
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "[]", "get");
2450
+ rb_define_method(GSL_TYPE(cgsl_matrix), "set", FUNCTION(rb_gsl_matrix,set), -1);
2451
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "[]=", "set");
2452
+
2453
+ rb_define_method(GSL_TYPE(cgsl_matrix), "set_all",
2454
+ FUNCTION(rb_gsl_matrix,set_all), 1);
2455
+ rb_define_method(GSL_TYPE(cgsl_matrix), "set_zero",
2456
+ FUNCTION(rb_gsl_matrix,set_zero), 0);
2457
+ rb_define_method(GSL_TYPE(cgsl_matrix), "set_identity",
2458
+ FUNCTION(rb_gsl_matrix,set_identity), 0);
2459
+
2460
+ rb_define_method(GSL_TYPE(cgsl_matrix), "print",
2461
+ FUNCTION(rb_gsl_matrix,print), 0);
2462
+ rb_define_method(GSL_TYPE(cgsl_matrix), "inspect",
2463
+ FUNCTION(rb_gsl_matrix,inspect), 0);
2464
+ rb_define_method(GSL_TYPE(cgsl_matrix), "to_s",
2465
+ FUNCTION(rb_gsl_matrix,to_s), 0);
2466
+
2467
+ rb_define_method(GSL_TYPE(cgsl_matrix), "set_diagonal",
2468
+ FUNCTION(rb_gsl_matrix,set_diagonal), 1);
2469
+
2470
+ rb_define_method(GSL_TYPE(cgsl_matrix), "get_row",
2471
+ FUNCTION(rb_gsl_matrix,get_row), 1);
2472
+ rb_define_method(GSL_TYPE(cgsl_matrix), "get_column",
2473
+ FUNCTION(rb_gsl_matrix,get_col), 1);
2474
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "get_col", "get_column");
2475
+ rb_define_method(GSL_TYPE(cgsl_matrix), "set_column",
2476
+ FUNCTION(rb_gsl_matrix,set_col), 2);
2477
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "set_col", "set_column");
2478
+ rb_define_method(GSL_TYPE(cgsl_matrix), "set_row",
2479
+ FUNCTION(rb_gsl_matrix,set_row), 2);
2480
+
2481
+ rb_define_method(GSL_TYPE(cgsl_matrix), "clone",
2482
+ FUNCTION(rb_gsl_matrix,clone), 0);
2483
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "duplicate", "clone");
2484
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "dup", "clone");
2485
+ rb_define_method(GSL_TYPE(cgsl_matrix), "isnull",
2486
+ FUNCTION(rb_gsl_matrix,isnull), 0);
2487
+ rb_define_method(GSL_TYPE(cgsl_matrix), "isnull?",
2488
+ FUNCTION(rb_gsl_matrix,isnull2), 0);
2489
+
2490
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "memcpy",
2491
+ FUNCTION(rb_gsl_matrix,memcpy), 2);
2492
+
2493
+ rb_define_method(GSL_TYPE(cgsl_matrix), "swap_rows",
2494
+ FUNCTION(rb_gsl_matrix,swap_rows), 2);
2495
+ rb_define_method(GSL_TYPE(cgsl_matrix), "swap_rows!",
2496
+ FUNCTION(rb_gsl_matrix,swap_rows_bang), 2);
2497
+ rb_define_method(GSL_TYPE(cgsl_matrix), "swap_columns",
2498
+ FUNCTION(rb_gsl_matrix,swap_columns), 2);
2499
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "swap_cols", "swap_columns");
2500
+ rb_define_method(GSL_TYPE(cgsl_matrix), "swap_columns!",
2501
+ FUNCTION(rb_gsl_matrix,swap_columns_bang), 2);
2502
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "swap_cols!", "swap_columns!");
2503
+ rb_define_method(GSL_TYPE(cgsl_matrix), "swap_rowcol",
2504
+ FUNCTION(rb_gsl_matrix,swap_rowcol), 2);
2505
+ rb_define_method(GSL_TYPE(cgsl_matrix), "swap_rowcol!",
2506
+ FUNCTION(rb_gsl_matrix,swap_rowcol_bang), 2);
2507
+ rb_define_method(GSL_TYPE(cgsl_matrix), "transpose_memcpy",
2508
+ FUNCTION(rb_gsl_matrix,transpose_memcpy), 0);
2509
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "transpose", "transpose_memcpy");
2510
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "trans", "transpose_memcpy");
2511
+ rb_define_method(GSL_TYPE(cgsl_matrix), "transpose!",
2512
+ FUNCTION(rb_gsl_matrix,transpose_bang), 0);
2513
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "trans!", "transpose!");
2514
+ rb_define_method(GSL_TYPE(cgsl_matrix), "reverse_columns",
2515
+ FUNCTION(rb_gsl_matrix,reverse_columns), 0);
2516
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "fliplr", "reverse_columns");
2517
+ rb_define_method(GSL_TYPE(cgsl_matrix), "reverse_columns!",
2518
+ FUNCTION(rb_gsl_matrix,reverse_columns_bang), 0);
2519
+ rb_define_method(GSL_TYPE(cgsl_matrix), "reverse_rows",
2520
+ FUNCTION(rb_gsl_matrix,reverse_rows), 0);
2521
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "flipud", "reverse_rows");
2522
+ rb_define_method(GSL_TYPE(cgsl_matrix), "reverse_rows!",
2523
+ FUNCTION(rb_gsl_matrix,reverse_rows_bang), 0);
2524
+ /*****/
2525
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "swap",
2526
+ FUNCTION(rb_gsl_matrix,swap), 2);
2527
+
2528
+ rb_define_method(GSL_TYPE(cgsl_matrix), "max", FUNCTION(rb_gsl_matrix,max), 0);
2529
+ rb_define_method(GSL_TYPE(cgsl_matrix), "min", FUNCTION(rb_gsl_matrix,min), 0);
2530
+ rb_define_method(GSL_TYPE(cgsl_matrix), "minmax",
2531
+ FUNCTION(rb_gsl_matrix,minmax), 0);
2532
+ rb_define_method(GSL_TYPE(cgsl_matrix), "max_index",
2533
+ FUNCTION(rb_gsl_matrix,max_index), 0);
2534
+ rb_define_method(GSL_TYPE(cgsl_matrix), "min_index",
2535
+ FUNCTION(rb_gsl_matrix,min_index), 0);
2536
+ rb_define_method(GSL_TYPE(cgsl_matrix), "minmax_index",
2537
+ FUNCTION(rb_gsl_matrix,minmax_index), 0);
2538
+
2539
+ rb_define_method(GSL_TYPE(cgsl_matrix), "fwrite",
2540
+ FUNCTION(rb_gsl_matrix,fwrite), 1);
2541
+ rb_define_method(GSL_TYPE(cgsl_matrix), "fread",
2542
+ FUNCTION(rb_gsl_matrix,fread), 1);
2543
+ rb_define_method(GSL_TYPE(cgsl_matrix), "fprintf",
2544
+ FUNCTION(rb_gsl_matrix,fprintf), -1);
2545
+ rb_define_method(GSL_TYPE(cgsl_matrix), "printf",
2546
+ FUNCTION(rb_gsl_matrix,printf), -1);
2547
+ rb_define_method(GSL_TYPE(cgsl_matrix), "fscanf",
2548
+ FUNCTION(rb_gsl_matrix,fscanf), 1);
2549
+
2550
+ rb_define_method(GSL_TYPE(cgsl_matrix), "trace",
2551
+ FUNCTION(rb_gsl_matrix,trace), 0);
2552
+
2553
+ rb_define_method(GSL_TYPE(cgsl_matrix), "-@",
2554
+ FUNCTION(rb_gsl_matrix,uminus), 0);
2555
+ rb_define_method(GSL_TYPE(cgsl_matrix), "+@",
2556
+ FUNCTION(rb_gsl_matrix,uplus), 0);
2557
+
2558
+
2559
+ /*****/
2560
+ rb_define_method(GSL_TYPE(cgsl_matrix), "submatrix",
2561
+ FUNCTION(rb_gsl_matrix,submatrix), -1);
2562
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "view", "submatrix");
2563
+
2564
+ rb_define_method(GSL_TYPE(cgsl_matrix), "row", FUNCTION(rb_gsl_matrix,row), 1);
2565
+ /* rb_define_alias(GSL_TYPE(cgsl_matrix), "[]", "row");*/
2566
+ rb_define_method(GSL_TYPE(cgsl_matrix), "column",
2567
+ FUNCTION(rb_gsl_matrix,column), 1);
2568
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "col", "column");
2569
+
2570
+ #ifdef GSL_1_10_LATER
2571
+ rb_define_method(GSL_TYPE(cgsl_matrix), "subrow",
2572
+ FUNCTION(rb_gsl_matrix,subrow), 3);
2573
+ rb_define_method(GSL_TYPE(cgsl_matrix), "subcolumn",
2574
+ FUNCTION(rb_gsl_matrix,subcolumn), 3);
2575
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "subcol", "subcolumn");
2576
+ #endif
2577
+
2578
+ rb_define_method(GSL_TYPE(cgsl_matrix), "diagonal",
2579
+ FUNCTION(rb_gsl_matrix,diagonal), 0);
2580
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "diag", "diagonal");
2581
+
2582
+ rb_define_method(GSL_TYPE(cgsl_matrix), "subdiagonal",
2583
+ FUNCTION(rb_gsl_matrix,subdiagonal), 1);
2584
+ rb_define_method(GSL_TYPE(cgsl_matrix), "superdiagonal",
2585
+ FUNCTION(rb_gsl_matrix,superdiagonal), 1);
2586
+ rb_define_method(GSL_TYPE(cgsl_matrix), "vector_view",
2587
+ FUNCTION(rb_gsl_matrix,vector_view), 0);
2588
+
2589
+ rb_define_method(GSL_TYPE(cgsl_matrix), "each_row",
2590
+ FUNCTION(rb_gsl_matrix,each_row), 0);
2591
+ rb_define_method(GSL_TYPE(cgsl_matrix), "each_col",
2592
+ FUNCTION(rb_gsl_matrix,each_col), 0);
2593
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "each_column", "each_col");
2594
+
2595
+ rb_define_method(GSL_TYPE(cgsl_matrix), "scale",
2596
+ FUNCTION(rb_gsl_matrix,scale), 1);
2597
+ rb_define_method(GSL_TYPE(cgsl_matrix), "scale!",
2598
+ FUNCTION(rb_gsl_matrix,scale_bang), 1);
2599
+ rb_define_method(GSL_TYPE(cgsl_matrix), "add_constant",
2600
+ FUNCTION(rb_gsl_matrix,add_constant), 1);
2601
+ rb_define_method(GSL_TYPE(cgsl_matrix), "add_constant!",
2602
+ FUNCTION(rb_gsl_matrix,add_constant_bang), 1);
2603
+
2604
+ rb_define_method(GSL_TYPE(cgsl_matrix), "equal?",
2605
+ FUNCTION(rb_gsl_matrix,equal), -1);
2606
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "==", "equal?");
2607
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "equal?",
2608
+ FUNCTION(rb_gsl_matrix,equal_singleton), -1);
2609
+
2610
+ rb_define_method(GSL_TYPE(cgsl_matrix), "power",
2611
+ FUNCTION(rb_gsl_matrix,power), 1);
2612
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "**", "power");
2613
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "^", "power");
2614
+
2615
+ rb_define_method(GSL_TYPE(cgsl_matrix), "collect",
2616
+ FUNCTION(rb_gsl_matrix,collect), 0);
2617
+ rb_define_method(GSL_TYPE(cgsl_matrix), "collect!",
2618
+ FUNCTION(rb_gsl_matrix,collect_bang), 0);
2619
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "map", "collect");
2620
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "map!", "collect!");
2621
+ #ifdef HAVE_TENSOR_TENSOR_H
2622
+ rb_define_method(GSL_TYPE(cgsl_matrix), "to_tensor",
2623
+ FUNCTION(rb_gsl_matrix,to_tensor), 0);
2624
+ #endif
2625
+
2626
+ /*****/
2627
+
2628
+ rb_define_method(GSL_TYPE(cgsl_matrix), "to_a",
2629
+ FUNCTION(rb_gsl_matrix,to_a), 0);
2630
+ rb_define_method(GSL_TYPE(cgsl_matrix), "to_v",
2631
+ FUNCTION(rb_gsl_matrix,to_v), 0);
2632
+ rb_define_method(GSL_TYPE(cgsl_matrix), "to_vview",
2633
+ FUNCTION(rb_gsl_matrix,to_vview), 0);
2634
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "data", "to_vview");
2635
+ rb_define_method(GSL_TYPE(cgsl_matrix), "norm",
2636
+ FUNCTION(rb_gsl_matrix,norm), 0);
2637
+
2638
+ rb_define_method(GSL_TYPE(cgsl_matrix), "upper",
2639
+ FUNCTION(rb_gsl_matrix,upper), 0);
2640
+ rb_define_method(GSL_TYPE(cgsl_matrix), "lower",
2641
+ FUNCTION(rb_gsl_matrix,lower), 0);
2642
+ rb_define_method(GSL_TYPE(cgsl_matrix), "block",
2643
+ FUNCTION(rb_gsl_matrix,block), 0);
2644
+
2645
+ /***** Special matrices *****/
2646
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "pascal",
2647
+ FUNCTION(rb_gsl_matrix,pascal1), 1);
2648
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "vandermonde",
2649
+ FUNCTION(rb_gsl_matrix,vandermonde), 1);
2650
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "vander",
2651
+ FUNCTION(rb_gsl_matrix,vandermonde), 1);
2652
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "toeplitz",
2653
+ FUNCTION(rb_gsl_matrix,toeplitz), 1);
2654
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "circulant",
2655
+ FUNCTION(rb_gsl_matrix,circulant), 1);
2656
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "indgen",
2657
+ FUNCTION(rb_gsl_matrix,indgen_singleton), -1);
2658
+ rb_define_method(GSL_TYPE(cgsl_matrix), "indgen",
2659
+ FUNCTION(rb_gsl_matrix,indgen), -1);
2660
+ rb_define_method(GSL_TYPE(cgsl_matrix), "indgen!",
2661
+ FUNCTION(rb_gsl_matrix,indgen_bang), -1);
2662
+
2663
+ rb_define_method(GSL_TYPE(cgsl_matrix), "info",
2664
+ FUNCTION(rb_gsl_matrix,info), 0);
2665
+ #ifdef BASE_DOUBLE
2666
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "hilbert",
2667
+ FUNCTION(rb_gsl_matrix,hilbert), 1);
2668
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "hilb",
2669
+ FUNCTION(rb_gsl_matrix,hilbert), 1);
2670
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "invhilbert",
2671
+ FUNCTION(rb_gsl_matrix,invhilbert), 1);
2672
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "invhilb",
2673
+ FUNCTION(rb_gsl_matrix,invhilbert), 1);
2674
+ #endif
2675
+
2676
+ rb_define_method(GSL_TYPE(cgsl_matrix), "any",
2677
+ FUNCTION(rb_gsl_matrix,any), 0);
2678
+ rb_define_method(GSL_TYPE(cgsl_matrix), "all",
2679
+ FUNCTION(rb_gsl_matrix,all), 0);
2680
+ rb_define_method(GSL_TYPE(cgsl_matrix), "rot90",
2681
+ FUNCTION(rb_gsl_matrix,rot90), -1);
2682
+
2683
+ rb_define_method(GSL_TYPE(cgsl_matrix), "diff",
2684
+ FUNCTION(rb_gsl_matrix,diff), -1);
2685
+ rb_define_method(GSL_TYPE(cgsl_matrix), "isnan", FUNCTION(rb_gsl_matrix,isnan), 0);
2686
+ rb_define_method(GSL_TYPE(cgsl_matrix), "isinf", FUNCTION(rb_gsl_matrix,isinf), 0);
2687
+ rb_define_method(GSL_TYPE(cgsl_matrix), "finite", FUNCTION(rb_gsl_matrix,finite), 0);
2688
+
2689
+ rb_define_method(GSL_TYPE(cgsl_matrix), "sgn", FUNCTION(rb_gsl_matrix,sgn), 0);
2690
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "signum", "sgn");
2691
+ rb_define_method(GSL_TYPE(cgsl_matrix), "abs", FUNCTION(rb_gsl_matrix,abs), 0);
2692
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "fabs", "abs");
2693
+
2694
+ rb_define_method(GSL_TYPE(cgsl_matrix), "horzcat", FUNCTION(rb_gsl_matrix,horzcat), 1);
2695
+ rb_define_alias(GSL_TYPE(cgsl_matrix), "cat", "horzcat");
2696
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "horzcat", FUNCTION(rb_gsl_matrix,horzcat_singleton), 2);
2697
+
2698
+ rb_define_method(GSL_TYPE(cgsl_matrix), "vertcat", FUNCTION(rb_gsl_matrix,vertcat), 1);
2699
+ rb_define_singleton_method(GSL_TYPE(cgsl_matrix), "vertcat", FUNCTION(rb_gsl_matrix,vertcat_singleton), 2);
2700
+
2701
+ #ifdef GSL_1_9_LATER
2702
+ rb_define_method(GSL_TYPE(cgsl_matrix), "ispos", FUNCTION(rb_gsl_matrix,ispos), 0);
2703
+ rb_define_method(GSL_TYPE(cgsl_matrix), "ispos?", FUNCTION(rb_gsl_matrix,ispos2), 0);
2704
+ rb_define_method(GSL_TYPE(cgsl_matrix), "isneg", FUNCTION(rb_gsl_matrix,isneg), 0);
2705
+ rb_define_method(GSL_TYPE(cgsl_matrix), "isneg?", FUNCTION(rb_gsl_matrix,isneg2), 0);
2706
+ #endif
2707
+
2708
+ #ifdef GSL_1_10_LATER
2709
+ rb_define_method(GSL_TYPE(cgsl_matrix), "isnonneg", FUNCTION(rb_gsl_matrix,isnonneg), 0);
2710
+ rb_define_method(GSL_TYPE(cgsl_matrix), "isnonneg?", FUNCTION(rb_gsl_matrix,isnonneg2), 0);
2711
+ #endif
2712
+
2713
+ rb_define_method(GSL_TYPE(cgsl_matrix), "symmetrize", FUNCTION(rb_gsl_matrix,symmetrize), 0);
2714
+ rb_define_method(GSL_TYPE(cgsl_matrix), "symmetrize!", FUNCTION(rb_gsl_matrix,symmetrize_bang), 0);
2715
+ }
2716
+
2717
+ #undef NUMCONV
2718
+ #undef NUMCONV2
2719
+ #undef PRINTF_FORMAT
2720
+ #undef VEC_ROW_COL
2721
+ #undef VEC_P
2722
+ #undef C_TO_VALUE
2723
+ #undef C_TO_VALUE2
2724
+ #undef VEC_COL_P
2725
+ #undef VEC_ROW_P
2726
+ #undef CHECK_VEC
2727
+ #undef VEC_VIEW_P
2728
+
2729
+ #undef MAT_ROW_COL
2730
+ #undef MAT_P
2731
+ #undef MAT_COL_P
2732
+ #undef MAT_ROW_P
2733
+ #undef CHECK_MAT
2734
+ #undef MAT_VIEW_P