numo-linalg-alt 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -0
  3. data/ext/numo/linalg/blas/dot.c +61 -61
  4. data/ext/numo/linalg/blas/dot_sub.c +60 -60
  5. data/ext/numo/linalg/blas/gemm.c +161 -152
  6. data/ext/numo/linalg/blas/gemv.c +135 -131
  7. data/ext/numo/linalg/blas/nrm2.c +54 -54
  8. data/ext/numo/linalg/lapack/gebal.c +87 -0
  9. data/ext/numo/linalg/lapack/gebal.h +15 -0
  10. data/ext/numo/linalg/lapack/gees.c +243 -224
  11. data/ext/numo/linalg/lapack/geev.c +131 -114
  12. data/ext/numo/linalg/lapack/gelsd.c +85 -74
  13. data/ext/numo/linalg/lapack/geqrf.c +56 -55
  14. data/ext/numo/linalg/lapack/gerqf.c +70 -0
  15. data/ext/numo/linalg/lapack/gerqf.h +15 -0
  16. data/ext/numo/linalg/lapack/gesdd.c +100 -90
  17. data/ext/numo/linalg/lapack/gesv.c +84 -82
  18. data/ext/numo/linalg/lapack/gesvd.c +144 -133
  19. data/ext/numo/linalg/lapack/getrf.c +55 -54
  20. data/ext/numo/linalg/lapack/getri.c +68 -67
  21. data/ext/numo/linalg/lapack/getrs.c +96 -92
  22. data/ext/numo/linalg/lapack/gges.c +214 -0
  23. data/ext/numo/linalg/lapack/gges.h +15 -0
  24. data/ext/numo/linalg/lapack/heev.c +56 -54
  25. data/ext/numo/linalg/lapack/heevd.c +56 -54
  26. data/ext/numo/linalg/lapack/heevr.c +111 -100
  27. data/ext/numo/linalg/lapack/hegv.c +79 -76
  28. data/ext/numo/linalg/lapack/hegvd.c +79 -76
  29. data/ext/numo/linalg/lapack/hegvx.c +134 -122
  30. data/ext/numo/linalg/lapack/hetrf.c +56 -52
  31. data/ext/numo/linalg/lapack/lange.c +49 -48
  32. data/ext/numo/linalg/lapack/orgqr.c +65 -64
  33. data/ext/numo/linalg/lapack/orgrq.c +78 -0
  34. data/ext/numo/linalg/lapack/orgrq.h +15 -0
  35. data/ext/numo/linalg/lapack/potrf.c +53 -52
  36. data/ext/numo/linalg/lapack/potri.c +53 -52
  37. data/ext/numo/linalg/lapack/potrs.c +78 -76
  38. data/ext/numo/linalg/lapack/syev.c +56 -54
  39. data/ext/numo/linalg/lapack/syevd.c +56 -54
  40. data/ext/numo/linalg/lapack/syevr.c +109 -100
  41. data/ext/numo/linalg/lapack/sygv.c +79 -75
  42. data/ext/numo/linalg/lapack/sygvd.c +79 -75
  43. data/ext/numo/linalg/lapack/sygvx.c +134 -122
  44. data/ext/numo/linalg/lapack/sytrf.c +58 -54
  45. data/ext/numo/linalg/lapack/trtrs.c +83 -79
  46. data/ext/numo/linalg/lapack/ungqr.c +65 -64
  47. data/ext/numo/linalg/lapack/ungrq.c +78 -0
  48. data/ext/numo/linalg/lapack/ungrq.h +15 -0
  49. data/ext/numo/linalg/linalg.c +24 -13
  50. data/ext/numo/linalg/linalg.h +5 -0
  51. data/ext/numo/linalg/util.c +8 -0
  52. data/ext/numo/linalg/util.h +1 -0
  53. data/lib/numo/linalg/version.rb +1 -1
  54. data/lib/numo/linalg.rb +235 -3
  55. metadata +12 -2
@@ -4,56 +4,57 @@ struct _getrf_option {
4
4
  int matrix_layout;
5
5
  };
6
6
 
7
- #define DEF_LINALG_FUNC(tDType, tNAryClass, fLapackFunc) \
8
- static void _iter_##fLapackFunc(na_loop_t* const lp) { \
9
- tDType* a = (tDType*)NDL_PTR(lp, 0); \
10
- int* ipiv = (int*)NDL_PTR(lp, 1); \
11
- int* info = (int*)NDL_PTR(lp, 2); \
12
- struct _getrf_option* opt = (struct _getrf_option*)(lp->opt_ptr); \
13
- const lapack_int m = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
14
- const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[1]; \
15
- const lapack_int lda = n; \
16
- const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, m, n, a, lda, ipiv); \
17
- *info = (int)i; \
18
- } \
19
- \
20
- static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
21
- VALUE a_vnary = Qnil; \
22
- VALUE kw_args = Qnil; \
23
- rb_scan_args(argc, argv, "1:", &a_vnary, &kw_args); \
24
- ID kw_table[1] = { rb_intern("order") }; \
25
- VALUE kw_values[1] = { Qundef }; \
26
- rb_get_kwargs(kw_args, kw_table, 0, 1, kw_values); \
27
- const int matrix_layout = kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
28
- \
29
- if (CLASS_OF(a_vnary) != tNAryClass) { \
30
- a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
31
- } \
32
- if (!RTEST(nary_check_contiguous(a_vnary))) { \
33
- a_vnary = nary_dup(a_vnary); \
34
- } \
35
- \
36
- narray_t* a_nary = NULL; \
37
- GetNArray(a_vnary, a_nary); \
38
- const int n_dims = NA_NDIM(a_nary); \
39
- if (n_dims != 2) { \
40
- rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
41
- return Qnil; \
42
- } \
43
- \
44
- size_t m = NA_SHAPE(a_nary)[0]; \
45
- size_t n = NA_SHAPE(a_nary)[1]; \
46
- size_t shape[1] = { m < n ? m : n }; \
47
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } }; \
48
- ndfunc_arg_out_t aout[2] = { { numo_cInt32, 1, shape }, { numo_cInt32, 0 } }; \
49
- ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 2, ain, aout }; \
50
- struct _getrf_option opt = { matrix_layout }; \
51
- VALUE res = na_ndloop3(&ndf, &opt, 1, a_vnary); \
52
- \
53
- VALUE ret = rb_ary_concat(rb_ary_new3(1, a_vnary), res); \
54
- \
55
- RB_GC_GUARD(a_vnary); \
56
- return ret; \
7
+ #define DEF_LINALG_FUNC(tDType, tNAryClass, fLapackFunc) \
8
+ static void _iter_##fLapackFunc(na_loop_t* const lp) { \
9
+ tDType* a = (tDType*)NDL_PTR(lp, 0); \
10
+ int* ipiv = (int*)NDL_PTR(lp, 1); \
11
+ int* info = (int*)NDL_PTR(lp, 2); \
12
+ struct _getrf_option* opt = (struct _getrf_option*)(lp->opt_ptr); \
13
+ const lapack_int m = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
14
+ const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[1]; \
15
+ const lapack_int lda = n; \
16
+ const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, m, n, a, lda, ipiv); \
17
+ *info = (int)i; \
18
+ } \
19
+ \
20
+ static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
21
+ VALUE a_vnary = Qnil; \
22
+ VALUE kw_args = Qnil; \
23
+ rb_scan_args(argc, argv, "1:", &a_vnary, &kw_args); \
24
+ ID kw_table[1] = { rb_intern("order") }; \
25
+ VALUE kw_values[1] = { Qundef }; \
26
+ rb_get_kwargs(kw_args, kw_table, 0, 1, kw_values); \
27
+ const int matrix_layout = \
28
+ kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
29
+ \
30
+ if (CLASS_OF(a_vnary) != tNAryClass) { \
31
+ a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
32
+ } \
33
+ if (!RTEST(nary_check_contiguous(a_vnary))) { \
34
+ a_vnary = nary_dup(a_vnary); \
35
+ } \
36
+ \
37
+ narray_t* a_nary = NULL; \
38
+ GetNArray(a_vnary, a_nary); \
39
+ const int n_dims = NA_NDIM(a_nary); \
40
+ if (n_dims != 2) { \
41
+ rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
42
+ return Qnil; \
43
+ } \
44
+ \
45
+ size_t m = NA_SHAPE(a_nary)[0]; \
46
+ size_t n = NA_SHAPE(a_nary)[1]; \
47
+ size_t shape[1] = { m < n ? m : n }; \
48
+ ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } }; \
49
+ ndfunc_arg_out_t aout[2] = { { numo_cInt32, 1, shape }, { numo_cInt32, 0 } }; \
50
+ ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 2, ain, aout }; \
51
+ struct _getrf_option opt = { matrix_layout }; \
52
+ VALUE res = na_ndloop3(&ndf, &opt, 1, a_vnary); \
53
+ \
54
+ VALUE ret = rb_ary_concat(rb_ary_new3(1, a_vnary), res); \
55
+ \
56
+ RB_GC_GUARD(a_vnary); \
57
+ return ret; \
57
58
  }
58
59
 
59
60
  DEF_LINALG_FUNC(double, numo_cDFloat, dgetrf)
@@ -64,8 +65,8 @@ DEF_LINALG_FUNC(lapack_complex_float, numo_cSComplex, cgetrf)
64
65
  #undef DEF_LINALG_FUNC
65
66
 
66
67
  void define_linalg_lapack_getrf(VALUE mLapack) {
67
- rb_define_module_function(mLapack, "dgetrf", RUBY_METHOD_FUNC(_linalg_lapack_dgetrf), -1);
68
- rb_define_module_function(mLapack, "sgetrf", RUBY_METHOD_FUNC(_linalg_lapack_sgetrf), -1);
69
- rb_define_module_function(mLapack, "zgetrf", RUBY_METHOD_FUNC(_linalg_lapack_zgetrf), -1);
70
- rb_define_module_function(mLapack, "cgetrf", RUBY_METHOD_FUNC(_linalg_lapack_cgetrf), -1);
68
+ rb_define_module_function(mLapack, "dgetrf", _linalg_lapack_dgetrf, -1);
69
+ rb_define_module_function(mLapack, "sgetrf", _linalg_lapack_sgetrf, -1);
70
+ rb_define_module_function(mLapack, "zgetrf", _linalg_lapack_zgetrf, -1);
71
+ rb_define_module_function(mLapack, "cgetrf", _linalg_lapack_cgetrf, -1);
71
72
  }
@@ -4,69 +4,70 @@ struct _getri_option {
4
4
  int matrix_layout;
5
5
  };
6
6
 
7
- #define DEF_LINALG_FUNC(tDType, tNAryClass, fLapackFunc) \
8
- static void _iter_##fLapackFunc(na_loop_t* const lp) { \
9
- tDType* a = (tDType*)NDL_PTR(lp, 0); \
10
- lapack_int* ipiv = (lapack_int*)NDL_PTR(lp, 1); \
11
- int* info = (int*)NDL_PTR(lp, 2); \
12
- struct _getri_option* opt = (struct _getri_option*)(lp->opt_ptr); \
13
- const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
14
- const lapack_int lda = n; \
15
- const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, n, a, lda, ipiv); \
16
- *info = (int)i; \
17
- } \
18
- \
19
- static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
20
- VALUE a_vnary = Qnil; \
21
- VALUE ipiv_vnary = Qnil; \
22
- VALUE kw_args = Qnil; \
23
- rb_scan_args(argc, argv, "2:", &a_vnary, &ipiv_vnary, &kw_args); \
24
- ID kw_table[1] = { rb_intern("order") }; \
25
- VALUE kw_values[1] = { Qundef }; \
26
- rb_get_kwargs(kw_args, kw_table, 0, 1, kw_values); \
27
- const int matrix_layout = kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
28
- \
29
- if (CLASS_OF(a_vnary) != tNAryClass) { \
30
- a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
31
- } \
32
- if (!RTEST(nary_check_contiguous(a_vnary))) { \
33
- a_vnary = nary_dup(a_vnary); \
34
- } \
35
- if (CLASS_OF(ipiv_vnary) != numo_cInt32) { \
36
- ipiv_vnary = rb_funcall(numo_cInt32, rb_intern("cast"), 1, ipiv_vnary); \
37
- } \
38
- if (!RTEST(nary_check_contiguous(ipiv_vnary))) { \
39
- ipiv_vnary = nary_dup(ipiv_vnary); \
40
- } \
41
- \
42
- narray_t* a_nary = NULL; \
43
- GetNArray(a_vnary, a_nary); \
44
- if (NA_NDIM(a_nary) != 2) { \
45
- rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
46
- return Qnil; \
47
- } \
48
- if (NA_SHAPE(a_nary)[0] != NA_SHAPE(a_nary)[1]) { \
49
- rb_raise(rb_eArgError, "input array a must be square"); \
50
- return Qnil; \
51
- } \
52
- narray_t* ipiv_nary = NULL; \
53
- GetNArray(ipiv_vnary, ipiv_nary); \
54
- if (NA_NDIM(ipiv_nary) != 1) { \
55
- rb_raise(rb_eArgError, "input array ipiv must be 1-dimensional"); \
56
- return Qnil; \
57
- } \
58
- \
59
- ndfunc_arg_in_t ain[2] = { { OVERWRITE, 2 }, { numo_cInt32, 1 } }; \
60
- ndfunc_arg_out_t aout[1] = { { numo_cInt32, 0 } }; \
61
- ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 2, 1, ain, aout }; \
62
- struct _getri_option opt = { matrix_layout }; \
63
- VALUE res = na_ndloop3(&ndf, &opt, 2, a_vnary, ipiv_vnary); \
64
- \
65
- VALUE ret = rb_ary_new3(2, a_vnary, res); \
66
- \
67
- RB_GC_GUARD(a_vnary); \
68
- RB_GC_GUARD(ipiv_vnary); \
69
- return ret; \
7
+ #define DEF_LINALG_FUNC(tDType, tNAryClass, fLapackFunc) \
8
+ static void _iter_##fLapackFunc(na_loop_t* const lp) { \
9
+ tDType* a = (tDType*)NDL_PTR(lp, 0); \
10
+ lapack_int* ipiv = (lapack_int*)NDL_PTR(lp, 1); \
11
+ int* info = (int*)NDL_PTR(lp, 2); \
12
+ struct _getri_option* opt = (struct _getri_option*)(lp->opt_ptr); \
13
+ const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
14
+ const lapack_int lda = n; \
15
+ const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, n, a, lda, ipiv); \
16
+ *info = (int)i; \
17
+ } \
18
+ \
19
+ static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
20
+ VALUE a_vnary = Qnil; \
21
+ VALUE ipiv_vnary = Qnil; \
22
+ VALUE kw_args = Qnil; \
23
+ rb_scan_args(argc, argv, "2:", &a_vnary, &ipiv_vnary, &kw_args); \
24
+ ID kw_table[1] = { rb_intern("order") }; \
25
+ VALUE kw_values[1] = { Qundef }; \
26
+ rb_get_kwargs(kw_args, kw_table, 0, 1, kw_values); \
27
+ const int matrix_layout = \
28
+ kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
29
+ \
30
+ if (CLASS_OF(a_vnary) != tNAryClass) { \
31
+ a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
32
+ } \
33
+ if (!RTEST(nary_check_contiguous(a_vnary))) { \
34
+ a_vnary = nary_dup(a_vnary); \
35
+ } \
36
+ if (CLASS_OF(ipiv_vnary) != numo_cInt32) { \
37
+ ipiv_vnary = rb_funcall(numo_cInt32, rb_intern("cast"), 1, ipiv_vnary); \
38
+ } \
39
+ if (!RTEST(nary_check_contiguous(ipiv_vnary))) { \
40
+ ipiv_vnary = nary_dup(ipiv_vnary); \
41
+ } \
42
+ \
43
+ narray_t* a_nary = NULL; \
44
+ GetNArray(a_vnary, a_nary); \
45
+ if (NA_NDIM(a_nary) != 2) { \
46
+ rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
47
+ return Qnil; \
48
+ } \
49
+ if (NA_SHAPE(a_nary)[0] != NA_SHAPE(a_nary)[1]) { \
50
+ rb_raise(rb_eArgError, "input array a must be square"); \
51
+ return Qnil; \
52
+ } \
53
+ narray_t* ipiv_nary = NULL; \
54
+ GetNArray(ipiv_vnary, ipiv_nary); \
55
+ if (NA_NDIM(ipiv_nary) != 1) { \
56
+ rb_raise(rb_eArgError, "input array ipiv must be 1-dimensional"); \
57
+ return Qnil; \
58
+ } \
59
+ \
60
+ ndfunc_arg_in_t ain[2] = { { OVERWRITE, 2 }, { numo_cInt32, 1 } }; \
61
+ ndfunc_arg_out_t aout[1] = { { numo_cInt32, 0 } }; \
62
+ ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 2, 1, ain, aout }; \
63
+ struct _getri_option opt = { matrix_layout }; \
64
+ VALUE res = na_ndloop3(&ndf, &opt, 2, a_vnary, ipiv_vnary); \
65
+ \
66
+ VALUE ret = rb_ary_new3(2, a_vnary, res); \
67
+ \
68
+ RB_GC_GUARD(a_vnary); \
69
+ RB_GC_GUARD(ipiv_vnary); \
70
+ return ret; \
70
71
  }
71
72
 
72
73
  DEF_LINALG_FUNC(double, numo_cDFloat, dgetri)
@@ -75,8 +76,8 @@ DEF_LINALG_FUNC(lapack_complex_double, numo_cDComplex, zgetri)
75
76
  DEF_LINALG_FUNC(lapack_complex_float, numo_cSComplex, cgetri)
76
77
 
77
78
  void define_linalg_lapack_getri(VALUE mLapack) {
78
- rb_define_module_function(mLapack, "dgetri", RUBY_METHOD_FUNC(_linalg_lapack_dgetri), -1);
79
- rb_define_module_function(mLapack, "sgetri", RUBY_METHOD_FUNC(_linalg_lapack_sgetri), -1);
80
- rb_define_module_function(mLapack, "zgetri", RUBY_METHOD_FUNC(_linalg_lapack_zgetri), -1);
81
- rb_define_module_function(mLapack, "cgetri", RUBY_METHOD_FUNC(_linalg_lapack_cgetri), -1);
79
+ rb_define_module_function(mLapack, "dgetri", _linalg_lapack_dgetri, -1);
80
+ rb_define_module_function(mLapack, "sgetri", _linalg_lapack_sgetri, -1);
81
+ rb_define_module_function(mLapack, "zgetri", _linalg_lapack_zgetri, -1);
82
+ rb_define_module_function(mLapack, "cgetri", _linalg_lapack_cgetri, -1);
82
83
  }
@@ -5,94 +5,98 @@ struct _getrs_option {
5
5
  char trans;
6
6
  };
7
7
 
8
- #define DEF_LINALG_FUNC(tDType, tNAryClass, fLapackFunc) \
9
- static void _iter_##fLapackFunc(na_loop_t* const lp) { \
10
- tDType* a = (tDType*)NDL_PTR(lp, 0); \
11
- int* ipiv = (int*)NDL_PTR(lp, 1); \
12
- tDType* b = (tDType*)NDL_PTR(lp, 2); \
13
- int* info = (int*)NDL_PTR(lp, 3); \
14
- struct _getrs_option* opt = (struct _getrs_option*)(lp->opt_ptr); \
15
- const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
16
- const lapack_int nrhs = lp->args[2].ndim == 1 ? 1 : (lapack_int)NDL_SHAPE(lp, 2)[1]; \
17
- const lapack_int lda = n; \
18
- const lapack_int ldb = nrhs; \
19
- const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, opt->trans, n, nrhs, a, lda, ipiv, b, ldb); \
20
- *info = (int)i; \
21
- } \
22
- \
23
- static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
24
- VALUE a_vnary = Qnil; \
25
- VALUE ipiv_vnary = Qnil; \
26
- VALUE b_vnary = Qnil; \
27
- VALUE kw_args = Qnil; \
28
- rb_scan_args(argc, argv, "3:", &a_vnary, &ipiv_vnary, &b_vnary, &kw_args); \
29
- ID kw_table[2] = { rb_intern("order"), rb_intern("trans") }; \
30
- VALUE kw_values[2] = { Qundef, Qundef }; \
31
- rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
32
- const int matrix_layout = kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
33
- const char trans = kw_values[1] != Qundef ? NUM2CHR(kw_values[1]) : 'N'; \
34
- \
35
- if (CLASS_OF(a_vnary) != tNAryClass) { \
36
- a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
37
- } \
38
- if (!RTEST(nary_check_contiguous(a_vnary))) { \
39
- a_vnary = nary_dup(a_vnary); \
40
- } \
41
- if (CLASS_OF(ipiv_vnary) != numo_cInt32) { \
42
- ipiv_vnary = rb_funcall(numo_cInt32, rb_intern("cast"), 1, ipiv_vnary); \
43
- } \
44
- if (!RTEST(nary_check_contiguous(ipiv_vnary))) { \
45
- ipiv_vnary = nary_dup(ipiv_vnary); \
46
- } \
47
- if (CLASS_OF(b_vnary) != tNAryClass) { \
48
- b_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, b_vnary); \
49
- } \
50
- if (!RTEST(nary_check_contiguous(b_vnary))) { \
51
- b_vnary = nary_dup(b_vnary); \
52
- } \
53
- \
54
- narray_t* a_nary = NULL; \
55
- GetNArray(a_vnary, a_nary); \
56
- const int n_dims = NA_NDIM(a_nary); \
57
- if (n_dims != 2) { \
58
- rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
59
- return Qnil; \
60
- } \
61
- if (NA_SHAPE(a_nary)[0] != NA_SHAPE(a_nary)[1]) { \
62
- rb_raise(rb_eArgError, "input array a must be square"); \
63
- return Qnil; \
64
- } \
65
- narray_t* ipiv_nary = NULL; \
66
- GetNArray(ipiv_vnary, ipiv_nary); \
67
- const int ipiv_n_dims = NA_NDIM(ipiv_nary); \
68
- if (ipiv_n_dims != 1) { \
69
- rb_raise(rb_eArgError, "input array ipiv must be 1-dimensional"); \
70
- return Qnil; \
71
- } \
72
- narray_t* b_nary = NULL; \
73
- GetNArray(b_vnary, b_nary); \
74
- const int b_n_dims = NA_NDIM(b_nary); \
75
- if (b_n_dims != 1 && b_n_dims != 2) { \
76
- rb_raise(rb_eArgError, "input array b must be 1 or 2-dimensional"); \
77
- return Qnil; \
78
- } \
79
- lapack_int n = (lapack_int)NA_SHAPE(a_nary)[0]; \
80
- lapack_int nb = (lapack_int)NA_SHAPE(b_nary)[0]; \
81
- if (n != nb) { \
82
- rb_raise(nary_eShapeError, "shape1[0](=%d) != shape2[0](=%d)", n, nb); \
83
- } \
84
- \
85
- ndfunc_arg_in_t ain[3] = { { tNAryClass, 2 }, { numo_cInt32, 1 }, { OVERWRITE, b_n_dims } }; \
86
- ndfunc_arg_out_t aout[1] = { { numo_cInt32, 0 } }; \
87
- ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 3, 1, ain, aout }; \
88
- struct _getrs_option opt = { matrix_layout, trans }; \
89
- VALUE info = na_ndloop3(&ndf, &opt, 3, a_vnary, ipiv_vnary, b_vnary); \
90
- VALUE ret = rb_ary_new3(2, b_vnary, info); \
91
- \
92
- RB_GC_GUARD(a_vnary); \
93
- RB_GC_GUARD(ipiv_vnary); \
94
- RB_GC_GUARD(b_vnary); \
95
- return ret; \
8
+ #define DEF_LINALG_FUNC(tDType, tNAryClass, fLapackFunc) \
9
+ static void _iter_##fLapackFunc(na_loop_t* const lp) { \
10
+ tDType* a = (tDType*)NDL_PTR(lp, 0); \
11
+ int* ipiv = (int*)NDL_PTR(lp, 1); \
12
+ tDType* b = (tDType*)NDL_PTR(lp, 2); \
13
+ int* info = (int*)NDL_PTR(lp, 3); \
14
+ struct _getrs_option* opt = (struct _getrs_option*)(lp->opt_ptr); \
15
+ const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
16
+ const lapack_int nrhs = lp->args[2].ndim == 1 ? 1 : (lapack_int)NDL_SHAPE(lp, 2)[1]; \
17
+ const lapack_int lda = n; \
18
+ const lapack_int ldb = nrhs; \
19
+ const lapack_int i = \
20
+ LAPACKE_##fLapackFunc(opt->matrix_layout, opt->trans, n, nrhs, a, lda, ipiv, b, ldb); \
21
+ *info = (int)i; \
22
+ } \
23
+ \
24
+ static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
25
+ VALUE a_vnary = Qnil; \
26
+ VALUE ipiv_vnary = Qnil; \
27
+ VALUE b_vnary = Qnil; \
28
+ VALUE kw_args = Qnil; \
29
+ rb_scan_args(argc, argv, "3:", &a_vnary, &ipiv_vnary, &b_vnary, &kw_args); \
30
+ ID kw_table[2] = { rb_intern("order"), rb_intern("trans") }; \
31
+ VALUE kw_values[2] = { Qundef, Qundef }; \
32
+ rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
33
+ const int matrix_layout = \
34
+ kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
35
+ const char trans = kw_values[1] != Qundef ? NUM2CHR(kw_values[1]) : 'N'; \
36
+ \
37
+ if (CLASS_OF(a_vnary) != tNAryClass) { \
38
+ a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
39
+ } \
40
+ if (!RTEST(nary_check_contiguous(a_vnary))) { \
41
+ a_vnary = nary_dup(a_vnary); \
42
+ } \
43
+ if (CLASS_OF(ipiv_vnary) != numo_cInt32) { \
44
+ ipiv_vnary = rb_funcall(numo_cInt32, rb_intern("cast"), 1, ipiv_vnary); \
45
+ } \
46
+ if (!RTEST(nary_check_contiguous(ipiv_vnary))) { \
47
+ ipiv_vnary = nary_dup(ipiv_vnary); \
48
+ } \
49
+ if (CLASS_OF(b_vnary) != tNAryClass) { \
50
+ b_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, b_vnary); \
51
+ } \
52
+ if (!RTEST(nary_check_contiguous(b_vnary))) { \
53
+ b_vnary = nary_dup(b_vnary); \
54
+ } \
55
+ \
56
+ narray_t* a_nary = NULL; \
57
+ GetNArray(a_vnary, a_nary); \
58
+ const int n_dims = NA_NDIM(a_nary); \
59
+ if (n_dims != 2) { \
60
+ rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
61
+ return Qnil; \
62
+ } \
63
+ if (NA_SHAPE(a_nary)[0] != NA_SHAPE(a_nary)[1]) { \
64
+ rb_raise(rb_eArgError, "input array a must be square"); \
65
+ return Qnil; \
66
+ } \
67
+ narray_t* ipiv_nary = NULL; \
68
+ GetNArray(ipiv_vnary, ipiv_nary); \
69
+ const int ipiv_n_dims = NA_NDIM(ipiv_nary); \
70
+ if (ipiv_n_dims != 1) { \
71
+ rb_raise(rb_eArgError, "input array ipiv must be 1-dimensional"); \
72
+ return Qnil; \
73
+ } \
74
+ narray_t* b_nary = NULL; \
75
+ GetNArray(b_vnary, b_nary); \
76
+ const int b_n_dims = NA_NDIM(b_nary); \
77
+ if (b_n_dims != 1 && b_n_dims != 2) { \
78
+ rb_raise(rb_eArgError, "input array b must be 1 or 2-dimensional"); \
79
+ return Qnil; \
80
+ } \
81
+ lapack_int n = (lapack_int)NA_SHAPE(a_nary)[0]; \
82
+ lapack_int nb = (lapack_int)NA_SHAPE(b_nary)[0]; \
83
+ if (n != nb) { \
84
+ rb_raise(nary_eShapeError, "shape1[0](=%d) != shape2[0](=%d)", n, nb); \
85
+ } \
86
+ \
87
+ ndfunc_arg_in_t ain[3] = { { tNAryClass, 2 }, \
88
+ { numo_cInt32, 1 }, \
89
+ { OVERWRITE, b_n_dims } }; \
90
+ ndfunc_arg_out_t aout[1] = { { numo_cInt32, 0 } }; \
91
+ ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 3, 1, ain, aout }; \
92
+ struct _getrs_option opt = { matrix_layout, trans }; \
93
+ VALUE info = na_ndloop3(&ndf, &opt, 3, a_vnary, ipiv_vnary, b_vnary); \
94
+ VALUE ret = rb_ary_new3(2, b_vnary, info); \
95
+ \
96
+ RB_GC_GUARD(a_vnary); \
97
+ RB_GC_GUARD(ipiv_vnary); \
98
+ RB_GC_GUARD(b_vnary); \
99
+ return ret; \
96
100
  }
97
101
 
98
102
  DEF_LINALG_FUNC(double, numo_cDFloat, dgetrs)
@@ -103,8 +107,8 @@ DEF_LINALG_FUNC(lapack_complex_float, numo_cSComplex, cgetrs)
103
107
  #undef DEF_LINALG_FUNC
104
108
 
105
109
  void define_linalg_lapack_getrs(VALUE mLapack) {
106
- rb_define_module_function(mLapack, "dgetrs", RUBY_METHOD_FUNC(_linalg_lapack_dgetrs), -1);
107
- rb_define_module_function(mLapack, "sgetrs", RUBY_METHOD_FUNC(_linalg_lapack_sgetrs), -1);
108
- rb_define_module_function(mLapack, "zgetrs", RUBY_METHOD_FUNC(_linalg_lapack_zgetrs), -1);
109
- rb_define_module_function(mLapack, "cgetrs", RUBY_METHOD_FUNC(_linalg_lapack_cgetrs), -1);
110
+ rb_define_module_function(mLapack, "dgetrs", _linalg_lapack_dgetrs, -1);
111
+ rb_define_module_function(mLapack, "sgetrs", _linalg_lapack_sgetrs, -1);
112
+ rb_define_module_function(mLapack, "zgetrs", _linalg_lapack_zgetrs, -1);
113
+ rb_define_module_function(mLapack, "cgetrs", _linalg_lapack_cgetrs, -1);
110
114
  }