numo-linalg-alt 0.2.0 → 0.4.0

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 +10 -1
  3. data/README.md +3 -1
  4. data/ext/numo/linalg/blas/dot.c +59 -59
  5. data/ext/numo/linalg/blas/dot_sub.c +58 -58
  6. data/ext/numo/linalg/blas/gemm.c +157 -148
  7. data/ext/numo/linalg/blas/gemv.c +131 -127
  8. data/ext/numo/linalg/blas/nrm2.c +50 -50
  9. data/ext/numo/linalg/lapack/gees.c +276 -0
  10. data/ext/numo/linalg/lapack/gees.h +15 -0
  11. data/ext/numo/linalg/lapack/geev.c +127 -110
  12. data/ext/numo/linalg/lapack/gelsd.c +81 -70
  13. data/ext/numo/linalg/lapack/geqrf.c +52 -51
  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 +96 -86
  17. data/ext/numo/linalg/lapack/gesv.c +80 -78
  18. data/ext/numo/linalg/lapack/gesvd.c +140 -129
  19. data/ext/numo/linalg/lapack/getrf.c +51 -50
  20. data/ext/numo/linalg/lapack/getri.c +64 -63
  21. data/ext/numo/linalg/lapack/getrs.c +92 -88
  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 +54 -52
  25. data/ext/numo/linalg/lapack/heevd.c +54 -52
  26. data/ext/numo/linalg/lapack/heevr.c +109 -98
  27. data/ext/numo/linalg/lapack/hegv.c +77 -74
  28. data/ext/numo/linalg/lapack/hegvd.c +77 -74
  29. data/ext/numo/linalg/lapack/hegvx.c +132 -120
  30. data/ext/numo/linalg/lapack/hetrf.c +54 -50
  31. data/ext/numo/linalg/lapack/lange.c +45 -44
  32. data/ext/numo/linalg/lapack/orgqr.c +63 -62
  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 +49 -48
  36. data/ext/numo/linalg/lapack/potri.c +49 -48
  37. data/ext/numo/linalg/lapack/potrs.c +74 -72
  38. data/ext/numo/linalg/lapack/syev.c +54 -52
  39. data/ext/numo/linalg/lapack/syevd.c +54 -52
  40. data/ext/numo/linalg/lapack/syevr.c +107 -98
  41. data/ext/numo/linalg/lapack/sygv.c +77 -73
  42. data/ext/numo/linalg/lapack/sygvd.c +77 -73
  43. data/ext/numo/linalg/lapack/sygvx.c +132 -120
  44. data/ext/numo/linalg/lapack/sytrf.c +54 -50
  45. data/ext/numo/linalg/lapack/trtrs.c +79 -75
  46. data/ext/numo/linalg/lapack/ungqr.c +63 -62
  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 +21 -10
  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 +322 -0
  55. metadata +14 -4
@@ -5,76 +5,87 @@ struct _gelsd_option {
5
5
  double rcond;
6
6
  };
7
7
 
8
- #define DEF_LINALG_FUNC(tDType, tRtDType, tNAryClass, tRtNAryClass, fLapackFunc) \
9
- static void _iter_##fLapackFunc(na_loop_t* const lp) { \
10
- tDType* a = (tDType*)NDL_PTR(lp, 0); \
11
- tDType* b = (tDType*)NDL_PTR(lp, 1); \
12
- tRtDType* s = (tRtDType*)NDL_PTR(lp, 2); \
13
- int* rank = (int*)NDL_PTR(lp, 3); \
14
- int* info = (int*)NDL_PTR(lp, 4); \
15
- struct _gelsd_option* opt = (struct _gelsd_option*)(lp->opt_ptr); \
16
- const lapack_int m = (lapack_int)(opt->matrix_layout == LAPACK_ROW_MAJOR ? NDL_SHAPE(lp, 0)[0] : NDL_SHAPE(lp, 0)[1]); \
17
- const lapack_int n = (lapack_int)(opt->matrix_layout == LAPACK_ROW_MAJOR ? NDL_SHAPE(lp, 0)[1] : NDL_SHAPE(lp, 0)[0]); \
18
- const lapack_int nrhs = lp->args[1].ndim == 1 ? 1 : (lapack_int)NDL_SHAPE(lp, 1)[1]; \
19
- const lapack_int lda = n; \
20
- const lapack_int ldb = nrhs; \
21
- lapack_int r = 0; \
22
- lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, m, n, nrhs, a, lda, b, ldb, s, (tRtDType)(opt->rcond), &r); \
23
- *rank = (int)r; \
24
- *info = (int)i; \
25
- } \
26
- \
27
- static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
28
- VALUE a_vnary = Qnil; \
29
- VALUE b_vnary = Qnil; \
30
- VALUE kw_args = Qnil; \
31
- rb_scan_args(argc, argv, "2:", &a_vnary, &b_vnary, &kw_args); \
32
- ID kw_table[2] = { rb_intern("matrix_layout"), rb_intern("rcond") }; \
33
- VALUE kw_values[2] = { Qundef, Qundef }; \
34
- rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
35
- const int matrix_layout = kw_values[0] != Qundef && kw_values[0] != Qnil ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
36
- const double rcond = kw_values[1] != Qundef && kw_values[1] != Qnil ? NUM2DBL(kw_values[1]) : -1.0; \
37
- \
38
- if (CLASS_OF(a_vnary) != tNAryClass) { \
39
- a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
40
- } \
41
- if (!RTEST(nary_check_contiguous(a_vnary))) { \
42
- a_vnary = nary_dup(a_vnary); \
43
- } \
44
- if (CLASS_OF(b_vnary) != tNAryClass) { \
45
- b_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, b_vnary); \
46
- } \
47
- if (!RTEST(nary_check_contiguous(b_vnary))) { \
48
- b_vnary = nary_dup(b_vnary); \
49
- } \
50
- \
51
- narray_t* a_nary = NULL; \
52
- GetNArray(a_vnary, a_nary); \
53
- const int n_dims = NA_NDIM(a_nary); \
54
- if (n_dims != 2) { \
55
- rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
56
- return Qnil; \
57
- } \
58
- narray_t* b_nary = NULL; \
59
- GetNArray(b_vnary, b_nary); \
60
- const int b_n_dims = NA_NDIM(b_nary); \
61
- if (b_n_dims != 1 && b_n_dims != 2) { \
62
- rb_raise(rb_eArgError, "input array b must be 1 or 2-dimensional"); \
63
- return Qnil; \
64
- } \
65
- \
66
- const size_t m = NA_SHAPE(a_nary)[0]; \
67
- const size_t n = NA_SHAPE(a_nary)[1]; \
68
- size_t shape_s[1] = { m < n ? m : n }; \
69
- ndfunc_arg_in_t ain[2] = { { tNAryClass, 2 }, { OVERWRITE, b_n_dims } }; \
70
- ndfunc_arg_out_t aout[3] = { { tRtNAryClass, 1, shape_s }, { numo_cInt32, 0 }, { numo_cInt32, 0 } }; \
71
- ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 2, 3, ain, aout }; \
72
- struct _gelsd_option opt = { matrix_layout, rcond }; \
73
- VALUE ret = na_ndloop3(&ndf, &opt, 2, a_vnary, b_vnary); \
74
- \
75
- RB_GC_GUARD(a_vnary); \
76
- RB_GC_GUARD(b_vnary); \
77
- return ret; \
8
+ #define DEF_LINALG_FUNC(tDType, tRtDType, tNAryClass, tRtNAryClass, fLapackFunc) \
9
+ static void _iter_##fLapackFunc(na_loop_t* const lp) { \
10
+ tDType* a = (tDType*)NDL_PTR(lp, 0); \
11
+ tDType* b = (tDType*)NDL_PTR(lp, 1); \
12
+ tRtDType* s = (tRtDType*)NDL_PTR(lp, 2); \
13
+ int* rank = (int*)NDL_PTR(lp, 3); \
14
+ int* info = (int*)NDL_PTR(lp, 4); \
15
+ struct _gelsd_option* opt = (struct _gelsd_option*)(lp->opt_ptr); \
16
+ const lapack_int m = \
17
+ (lapack_int)(opt->matrix_layout == LAPACK_ROW_MAJOR ? NDL_SHAPE(lp, 0)[0] \
18
+ : NDL_SHAPE(lp, 0)[1]); \
19
+ const lapack_int n = \
20
+ (lapack_int)(opt->matrix_layout == LAPACK_ROW_MAJOR ? NDL_SHAPE(lp, 0)[1] \
21
+ : NDL_SHAPE(lp, 0)[0]); \
22
+ const lapack_int nrhs = lp->args[1].ndim == 1 ? 1 : (lapack_int)NDL_SHAPE(lp, 1)[1]; \
23
+ const lapack_int lda = n; \
24
+ const lapack_int ldb = nrhs; \
25
+ lapack_int r = 0; \
26
+ lapack_int i = LAPACKE_##fLapackFunc( \
27
+ opt->matrix_layout, m, n, nrhs, a, lda, b, ldb, s, (tRtDType)(opt->rcond), &r \
28
+ ); \
29
+ *rank = (int)r; \
30
+ *info = (int)i; \
31
+ } \
32
+ \
33
+ static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
34
+ VALUE a_vnary = Qnil; \
35
+ VALUE b_vnary = Qnil; \
36
+ VALUE kw_args = Qnil; \
37
+ rb_scan_args(argc, argv, "2:", &a_vnary, &b_vnary, &kw_args); \
38
+ ID kw_table[2] = { rb_intern("matrix_layout"), rb_intern("rcond") }; \
39
+ VALUE kw_values[2] = { Qundef, Qundef }; \
40
+ rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
41
+ const int matrix_layout = kw_values[0] != Qundef && kw_values[0] != Qnil \
42
+ ? get_matrix_layout(kw_values[0]) \
43
+ : LAPACK_ROW_MAJOR; \
44
+ const double rcond = \
45
+ kw_values[1] != Qundef && kw_values[1] != Qnil ? NUM2DBL(kw_values[1]) : -1.0; \
46
+ \
47
+ if (CLASS_OF(a_vnary) != tNAryClass) { \
48
+ a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
49
+ } \
50
+ if (!RTEST(nary_check_contiguous(a_vnary))) { \
51
+ a_vnary = nary_dup(a_vnary); \
52
+ } \
53
+ if (CLASS_OF(b_vnary) != tNAryClass) { \
54
+ b_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, b_vnary); \
55
+ } \
56
+ if (!RTEST(nary_check_contiguous(b_vnary))) { \
57
+ b_vnary = nary_dup(b_vnary); \
58
+ } \
59
+ \
60
+ narray_t* a_nary = NULL; \
61
+ GetNArray(a_vnary, a_nary); \
62
+ const int n_dims = NA_NDIM(a_nary); \
63
+ if (n_dims != 2) { \
64
+ rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
65
+ return Qnil; \
66
+ } \
67
+ narray_t* b_nary = NULL; \
68
+ GetNArray(b_vnary, b_nary); \
69
+ const int b_n_dims = NA_NDIM(b_nary); \
70
+ if (b_n_dims != 1 && b_n_dims != 2) { \
71
+ rb_raise(rb_eArgError, "input array b must be 1 or 2-dimensional"); \
72
+ return Qnil; \
73
+ } \
74
+ \
75
+ const size_t m = NA_SHAPE(a_nary)[0]; \
76
+ const size_t n = NA_SHAPE(a_nary)[1]; \
77
+ size_t shape_s[1] = { m < n ? m : n }; \
78
+ ndfunc_arg_in_t ain[2] = { { tNAryClass, 2 }, { OVERWRITE, b_n_dims } }; \
79
+ ndfunc_arg_out_t aout[3] = { { tRtNAryClass, 1, shape_s }, \
80
+ { numo_cInt32, 0 }, \
81
+ { numo_cInt32, 0 } }; \
82
+ ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 2, 3, ain, aout }; \
83
+ struct _gelsd_option opt = { matrix_layout, rcond }; \
84
+ VALUE ret = na_ndloop3(&ndf, &opt, 2, a_vnary, b_vnary); \
85
+ \
86
+ RB_GC_GUARD(a_vnary); \
87
+ RB_GC_GUARD(b_vnary); \
88
+ return ret; \
78
89
  }
79
90
 
80
91
  DEF_LINALG_FUNC(double, double, numo_cDFloat, numo_cDFloat, dgelsd)
@@ -4,57 +4,58 @@ struct _geqrf_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
- tDType* tau = (tDType*)NDL_PTR(lp, 1); \
11
- int* info = (int*)NDL_PTR(lp, 2); \
12
- struct _geqrf_option* opt = (struct _geqrf_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, tau); \
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] = { { tNAryClass, 1, shape }, { numo_cInt32, 0 } }; \
49
- ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 2, ain, aout }; \
50
- struct _geqrf_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
- \
57
- 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
+ tDType* tau = (tDType*)NDL_PTR(lp, 1); \
11
+ int* info = (int*)NDL_PTR(lp, 2); \
12
+ struct _geqrf_option* opt = (struct _geqrf_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, tau); \
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] = { { tNAryClass, 1, shape }, { numo_cInt32, 0 } }; \
50
+ ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 2, ain, aout }; \
51
+ struct _geqrf_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
+ \
58
+ return ret; \
58
59
  }
59
60
 
60
61
  DEF_LINALG_FUNC(double, numo_cDFloat, dgeqrf)
@@ -0,0 +1,70 @@
1
+ #include "gerqf.h"
2
+
3
+ struct _gerqf_option {
4
+ int matrix_layout;
5
+ };
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
+ tDType* tau = (tDType*)NDL_PTR(lp, 1); \
11
+ int* info = (int*)NDL_PTR(lp, 2); \
12
+ struct _gerqf_option* opt = (struct _gerqf_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, tau); \
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; \
38
+ GetNArray(a_vnary, a_nary); \
39
+ if (NA_NDIM(a_nary) != 2) { \
40
+ rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
41
+ return Qnil; \
42
+ } \
43
+ \
44
+ const size_t m = NA_SHAPE(a_nary)[0]; \
45
+ const 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] = { { tNAryClass, 1, shape }, { numo_cInt32, 0 } }; \
49
+ ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 2, ain, aout }; \
50
+ struct _gerqf_option opt = { matrix_layout }; \
51
+ VALUE res = na_ndloop3(&ndf, &opt, 1, a_vnary); \
52
+ VALUE ret = rb_ary_concat(rb_ary_new3(1, a_vnary), res); \
53
+ \
54
+ RB_GC_GUARD(a_vnary); \
55
+ return ret; \
56
+ }
57
+
58
+ DEF_LINALG_FUNC(double, numo_cDFloat, dgerqf)
59
+ DEF_LINALG_FUNC(float, numo_cSFloat, sgerqf)
60
+ DEF_LINALG_FUNC(lapack_complex_double, numo_cDComplex, zgerqf)
61
+ DEF_LINALG_FUNC(lapack_complex_float, numo_cSComplex, cgerqf)
62
+
63
+ #undef DEF_LINALG_FUNC
64
+
65
+ void define_linalg_lapack_gerqf(VALUE mLapack) {
66
+ rb_define_module_function(mLapack, "dgerqf", RUBY_METHOD_FUNC(_linalg_lapack_dgerqf), -1);
67
+ rb_define_module_function(mLapack, "sgerqf", RUBY_METHOD_FUNC(_linalg_lapack_sgerqf), -1);
68
+ rb_define_module_function(mLapack, "zgerqf", RUBY_METHOD_FUNC(_linalg_lapack_zgerqf), -1);
69
+ rb_define_module_function(mLapack, "cgerqf", RUBY_METHOD_FUNC(_linalg_lapack_cgerqf), -1);
70
+ }
@@ -0,0 +1,15 @@
1
+ #ifndef NUMO_LINALG_ALT_LAPACK_GERQF_H
2
+ #define NUMO_LINALG_ALT_LAPACK_GERQF_H 1
3
+
4
+ #include <lapacke.h>
5
+
6
+ #include <ruby.h>
7
+
8
+ #include <numo/narray.h>
9
+ #include <numo/template.h>
10
+
11
+ #include "../util.h"
12
+
13
+ void define_linalg_lapack_gerqf(VALUE mLapack);
14
+
15
+ #endif /* NUMO_LINALG_ALT_LAPACK_GERQF_H */
@@ -5,92 +5,102 @@ struct _gesdd_option {
5
5
  char jobz;
6
6
  };
7
7
 
8
- #define DEF_LINALG_FUNC(tDType, tRtDType, tNAryClass, tRtNAryClass, fLapackFunc) \
9
- static void _iter_##fLapackFunc(na_loop_t* const lp) { \
10
- tDType* a = (tDType*)NDL_PTR(lp, 0); \
11
- tRtDType* s = (tRtDType*)NDL_PTR(lp, 1); \
12
- tDType* u = (tDType*)NDL_PTR(lp, 2); \
13
- tDType* vt = (tDType*)NDL_PTR(lp, 3); \
14
- int* info = (int*)NDL_PTR(lp, 4); \
15
- struct _gesdd_option* opt = (struct _gesdd_option*)(lp->opt_ptr); \
16
- \
17
- const lapack_int m = (lapack_int)(opt->matrix_order == LAPACK_ROW_MAJOR ? NDL_SHAPE(lp, 0)[0] : NDL_SHAPE(lp, 0)[1]); \
18
- const lapack_int n = (lapack_int)(opt->matrix_order == LAPACK_ROW_MAJOR ? NDL_SHAPE(lp, 0)[1] : NDL_SHAPE(lp, 0)[0]); \
19
- const lapack_int min_mn = m < n ? m : n; \
20
- const lapack_int lda = n; \
21
- const lapack_int ldu = opt->jobz == 'S' ? min_mn : m; \
22
- const lapack_int ldvt = opt->jobz == 'S' ? min_mn : n; \
23
- \
24
- lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_order, opt->jobz, m, n, a, lda, s, u, ldu, vt, ldvt); \
25
- *info = (int)i; \
26
- } \
27
- \
28
- static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
29
- VALUE a_vnary = Qnil; \
30
- VALUE kw_args = Qnil; \
31
- \
32
- rb_scan_args(argc, argv, "1:", &a_vnary, &kw_args); \
33
- \
34
- ID kw_table[2] = { rb_intern("jobz"), rb_intern("order") }; \
35
- VALUE kw_values[2] = { Qundef, Qundef }; \
36
- \
37
- rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
38
- \
39
- const char jobz = kw_values[0] == Qundef ? 'A' : StringValueCStr(kw_values[0])[0]; \
40
- const char order = kw_values[1] == Qundef ? 'R' : StringValueCStr(kw_values[1])[0]; \
41
- \
42
- if (CLASS_OF(a_vnary) != tNAryClass) { \
43
- rb_raise(rb_eTypeError, "type of input array is invalid for overwriting"); \
44
- return Qnil; \
45
- } \
46
- if (!RTEST(nary_check_contiguous(a_vnary))) { \
47
- a_vnary = nary_dup(a_vnary); \
48
- } \
49
- \
50
- narray_t* a_nary = NULL; \
51
- GetNArray(a_vnary, a_nary); \
52
- const int n_dims = NA_NDIM(a_nary); \
53
- if (n_dims != 2) { \
54
- rb_raise(rb_eArgError, "input array must be 2-dimensional"); \
55
- return Qnil; \
56
- } \
57
- \
58
- const int matrix_order = order == 'C' ? LAPACK_COL_MAJOR : LAPACK_ROW_MAJOR; \
59
- const size_t m = matrix_order == LAPACK_ROW_MAJOR ? NA_SHAPE(a_nary)[0] : NA_SHAPE(a_nary)[1]; \
60
- const size_t n = matrix_order == LAPACK_ROW_MAJOR ? NA_SHAPE(a_nary)[1] : NA_SHAPE(a_nary)[0]; \
61
- \
62
- const size_t min_mn = m < n ? m : n; \
63
- size_t shape_s[1] = { min_mn }; \
64
- size_t shape_u[2] = { m, m }; \
65
- size_t shape_vt[2] = { n, n }; \
66
- \
67
- ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } }; \
68
- ndfunc_arg_out_t aout[4] = { { tRtNAryClass, 1, shape_s }, { tNAryClass, 2, shape_u }, { tNAryClass, 2, shape_vt }, { numo_cInt32, 0 } }; \
69
- \
70
- switch (jobz) { \
71
- case 'A': \
72
- break; \
73
- case 'S': \
74
- shape_u[matrix_order == LAPACK_ROW_MAJOR ? 1 : 0] = min_mn; \
75
- shape_vt[matrix_order == LAPACK_ROW_MAJOR ? 0 : 1] = min_mn; \
76
- break; \
77
- case 'O': \
78
- break; \
79
- case 'N': \
80
- aout[1].dim = 0; \
81
- aout[2].dim = 0; \
82
- break; \
83
- default: \
84
- rb_raise(rb_eArgError, "jobz must be one of 'A', 'S', 'O', or 'N'"); \
85
- return Qnil; \
86
- } \
87
- \
88
- ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 4, ain, aout }; \
89
- struct _gesdd_option opt = { matrix_order, jobz }; \
90
- VALUE ret = na_ndloop3(&ndf, &opt, 1, a_vnary); \
91
- \
92
- RB_GC_GUARD(a_vnary); \
93
- return ret; \
8
+ #define DEF_LINALG_FUNC(tDType, tRtDType, tNAryClass, tRtNAryClass, fLapackFunc) \
9
+ static void _iter_##fLapackFunc(na_loop_t* const lp) { \
10
+ tDType* a = (tDType*)NDL_PTR(lp, 0); \
11
+ tRtDType* s = (tRtDType*)NDL_PTR(lp, 1); \
12
+ tDType* u = (tDType*)NDL_PTR(lp, 2); \
13
+ tDType* vt = (tDType*)NDL_PTR(lp, 3); \
14
+ int* info = (int*)NDL_PTR(lp, 4); \
15
+ struct _gesdd_option* opt = (struct _gesdd_option*)(lp->opt_ptr); \
16
+ \
17
+ const lapack_int m = \
18
+ (lapack_int)(opt->matrix_order == LAPACK_ROW_MAJOR ? NDL_SHAPE(lp, 0)[0] \
19
+ : NDL_SHAPE(lp, 0)[1]); \
20
+ const lapack_int n = \
21
+ (lapack_int)(opt->matrix_order == LAPACK_ROW_MAJOR ? NDL_SHAPE(lp, 0)[1] \
22
+ : NDL_SHAPE(lp, 0)[0]); \
23
+ const lapack_int min_mn = m < n ? m : n; \
24
+ const lapack_int lda = n; \
25
+ const lapack_int ldu = opt->jobz == 'S' ? min_mn : m; \
26
+ const lapack_int ldvt = opt->jobz == 'S' ? min_mn : n; \
27
+ \
28
+ lapack_int i = \
29
+ LAPACKE_##fLapackFunc(opt->matrix_order, opt->jobz, m, n, a, lda, s, u, ldu, vt, ldvt); \
30
+ *info = (int)i; \
31
+ } \
32
+ \
33
+ static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
34
+ VALUE a_vnary = Qnil; \
35
+ VALUE kw_args = Qnil; \
36
+ \
37
+ rb_scan_args(argc, argv, "1:", &a_vnary, &kw_args); \
38
+ \
39
+ ID kw_table[2] = { rb_intern("jobz"), rb_intern("order") }; \
40
+ VALUE kw_values[2] = { Qundef, Qundef }; \
41
+ \
42
+ rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
43
+ \
44
+ const char jobz = kw_values[0] == Qundef ? 'A' : StringValueCStr(kw_values[0])[0]; \
45
+ const char order = kw_values[1] == Qundef ? 'R' : StringValueCStr(kw_values[1])[0]; \
46
+ \
47
+ if (CLASS_OF(a_vnary) != tNAryClass) { \
48
+ rb_raise(rb_eTypeError, "type of input array is invalid for overwriting"); \
49
+ return Qnil; \
50
+ } \
51
+ if (!RTEST(nary_check_contiguous(a_vnary))) { \
52
+ a_vnary = nary_dup(a_vnary); \
53
+ } \
54
+ \
55
+ narray_t* a_nary = NULL; \
56
+ GetNArray(a_vnary, a_nary); \
57
+ const int n_dims = NA_NDIM(a_nary); \
58
+ if (n_dims != 2) { \
59
+ rb_raise(rb_eArgError, "input array must be 2-dimensional"); \
60
+ return Qnil; \
61
+ } \
62
+ \
63
+ const int matrix_order = order == 'C' ? LAPACK_COL_MAJOR : LAPACK_ROW_MAJOR; \
64
+ const size_t m = \
65
+ matrix_order == LAPACK_ROW_MAJOR ? NA_SHAPE(a_nary)[0] : NA_SHAPE(a_nary)[1]; \
66
+ const size_t n = \
67
+ matrix_order == LAPACK_ROW_MAJOR ? NA_SHAPE(a_nary)[1] : NA_SHAPE(a_nary)[0]; \
68
+ \
69
+ const size_t min_mn = m < n ? m : n; \
70
+ size_t shape_s[1] = { min_mn }; \
71
+ size_t shape_u[2] = { m, m }; \
72
+ size_t shape_vt[2] = { n, n }; \
73
+ \
74
+ ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } }; \
75
+ ndfunc_arg_out_t aout[4] = { { tRtNAryClass, 1, shape_s }, \
76
+ { tNAryClass, 2, shape_u }, \
77
+ { tNAryClass, 2, shape_vt }, \
78
+ { numo_cInt32, 0 } }; \
79
+ \
80
+ switch (jobz) { \
81
+ case 'A': \
82
+ break; \
83
+ case 'S': \
84
+ shape_u[matrix_order == LAPACK_ROW_MAJOR ? 1 : 0] = min_mn; \
85
+ shape_vt[matrix_order == LAPACK_ROW_MAJOR ? 0 : 1] = min_mn; \
86
+ break; \
87
+ case 'O': \
88
+ break; \
89
+ case 'N': \
90
+ aout[1].dim = 0; \
91
+ aout[2].dim = 0; \
92
+ break; \
93
+ default: \
94
+ rb_raise(rb_eArgError, "jobz must be one of 'A', 'S', 'O', or 'N'"); \
95
+ return Qnil; \
96
+ } \
97
+ \
98
+ ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 4, ain, aout }; \
99
+ struct _gesdd_option opt = { matrix_order, jobz }; \
100
+ VALUE ret = na_ndloop3(&ndf, &opt, 1, a_vnary); \
101
+ \
102
+ RB_GC_GUARD(a_vnary); \
103
+ return ret; \
94
104
  }
95
105
 
96
106
  DEF_LINALG_FUNC(double, double, numo_cDFloat, numo_cDFloat, dgesdd)