numo-linalg-alt 0.2.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +27 -0
- data/README.md +106 -0
- data/ext/numo/linalg/blas/dot.c +72 -0
- data/ext/numo/linalg/blas/dot.h +13 -0
- data/ext/numo/linalg/blas/dot_sub.c +71 -0
- data/ext/numo/linalg/blas/dot_sub.h +13 -0
- data/ext/numo/linalg/blas/gemm.c +184 -0
- data/ext/numo/linalg/blas/gemm.h +16 -0
- data/ext/numo/linalg/blas/gemv.c +161 -0
- data/ext/numo/linalg/blas/gemv.h +16 -0
- data/ext/numo/linalg/blas/nrm2.c +67 -0
- data/ext/numo/linalg/blas/nrm2.h +13 -0
- data/ext/numo/linalg/converter.c +67 -0
- data/ext/numo/linalg/converter.h +23 -0
- data/ext/numo/linalg/extconf.rb +99 -0
- data/ext/numo/linalg/lapack/geev.c +152 -0
- data/ext/numo/linalg/lapack/geev.h +15 -0
- data/ext/numo/linalg/lapack/gelsd.c +92 -0
- data/ext/numo/linalg/lapack/gelsd.h +15 -0
- data/ext/numo/linalg/lapack/geqrf.c +72 -0
- data/ext/numo/linalg/lapack/geqrf.h +15 -0
- data/ext/numo/linalg/lapack/gesdd.c +108 -0
- data/ext/numo/linalg/lapack/gesdd.h +15 -0
- data/ext/numo/linalg/lapack/gesv.c +99 -0
- data/ext/numo/linalg/lapack/gesv.h +15 -0
- data/ext/numo/linalg/lapack/gesvd.c +152 -0
- data/ext/numo/linalg/lapack/gesvd.h +15 -0
- data/ext/numo/linalg/lapack/getrf.c +71 -0
- data/ext/numo/linalg/lapack/getrf.h +15 -0
- data/ext/numo/linalg/lapack/getri.c +82 -0
- data/ext/numo/linalg/lapack/getri.h +15 -0
- data/ext/numo/linalg/lapack/getrs.c +110 -0
- data/ext/numo/linalg/lapack/getrs.h +15 -0
- data/ext/numo/linalg/lapack/heev.c +71 -0
- data/ext/numo/linalg/lapack/heev.h +15 -0
- data/ext/numo/linalg/lapack/heevd.c +71 -0
- data/ext/numo/linalg/lapack/heevd.h +15 -0
- data/ext/numo/linalg/lapack/heevr.c +111 -0
- data/ext/numo/linalg/lapack/heevr.h +15 -0
- data/ext/numo/linalg/lapack/hegv.c +94 -0
- data/ext/numo/linalg/lapack/hegv.h +15 -0
- data/ext/numo/linalg/lapack/hegvd.c +94 -0
- data/ext/numo/linalg/lapack/hegvd.h +15 -0
- data/ext/numo/linalg/lapack/hegvx.c +133 -0
- data/ext/numo/linalg/lapack/hegvx.h +15 -0
- data/ext/numo/linalg/lapack/hetrf.c +68 -0
- data/ext/numo/linalg/lapack/hetrf.h +15 -0
- data/ext/numo/linalg/lapack/lange.c +66 -0
- data/ext/numo/linalg/lapack/lange.h +15 -0
- data/ext/numo/linalg/lapack/orgqr.c +79 -0
- data/ext/numo/linalg/lapack/orgqr.h +15 -0
- data/ext/numo/linalg/lapack/potrf.c +70 -0
- data/ext/numo/linalg/lapack/potrf.h +15 -0
- data/ext/numo/linalg/lapack/potri.c +70 -0
- data/ext/numo/linalg/lapack/potri.h +15 -0
- data/ext/numo/linalg/lapack/potrs.c +94 -0
- data/ext/numo/linalg/lapack/potrs.h +15 -0
- data/ext/numo/linalg/lapack/syev.c +71 -0
- data/ext/numo/linalg/lapack/syev.h +15 -0
- data/ext/numo/linalg/lapack/syevd.c +71 -0
- data/ext/numo/linalg/lapack/syevd.h +15 -0
- data/ext/numo/linalg/lapack/syevr.c +111 -0
- data/ext/numo/linalg/lapack/syevr.h +15 -0
- data/ext/numo/linalg/lapack/sygv.c +93 -0
- data/ext/numo/linalg/lapack/sygv.h +15 -0
- data/ext/numo/linalg/lapack/sygvd.c +93 -0
- data/ext/numo/linalg/lapack/sygvd.h +15 -0
- data/ext/numo/linalg/lapack/sygvx.c +133 -0
- data/ext/numo/linalg/lapack/sygvx.h +15 -0
- data/ext/numo/linalg/lapack/sytrf.c +72 -0
- data/ext/numo/linalg/lapack/sytrf.h +15 -0
- data/ext/numo/linalg/lapack/trtrs.c +99 -0
- data/ext/numo/linalg/lapack/trtrs.h +15 -0
- data/ext/numo/linalg/lapack/ungqr.c +79 -0
- data/ext/numo/linalg/lapack/ungqr.h +15 -0
- data/ext/numo/linalg/linalg.c +290 -0
- data/ext/numo/linalg/linalg.h +85 -0
- data/ext/numo/linalg/util.c +95 -0
- data/ext/numo/linalg/util.h +17 -0
- data/lib/numo/linalg/version.rb +10 -0
- data/lib/numo/linalg.rb +1309 -0
- data/vendor/tmp/.gitkeep +0 -0
- metadata +146 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef NUMO_LINALG_ALT_LAPACK_HETRF_H
|
2
|
+
#define NUMO_LINALG_ALT_LAPACK_HETRF_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_hetrf(VALUE mLapack);
|
14
|
+
|
15
|
+
#endif /* NUMO_LINALG_ALT_LAPACK_HETRF_H */
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#include "lange.h"
|
2
|
+
|
3
|
+
struct _lange_option {
|
4
|
+
int matrix_layout;
|
5
|
+
char norm;
|
6
|
+
};
|
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
|
+
tDType* d = (tDType*)NDL_PTR(lp, 1); \
|
12
|
+
struct _lange_option* opt = (struct _lange_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
|
+
*d = LAPACKE_##fLapackFunc(opt->matrix_layout, opt->norm, m, n, a, lda); \
|
17
|
+
} \
|
18
|
+
\
|
19
|
+
static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
|
20
|
+
VALUE a_vnary = Qnil; \
|
21
|
+
VALUE kw_args = Qnil; \
|
22
|
+
rb_scan_args(argc, argv, "1:", &a_vnary, &kw_args); \
|
23
|
+
ID kw_table[2] = { rb_intern("order"), rb_intern("norm") }; \
|
24
|
+
VALUE kw_values[2] = { Qundef, Qundef }; \
|
25
|
+
rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
|
26
|
+
const int matrix_layout = kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
|
27
|
+
const char norm = kw_values[1] != Qundef ? NUM2CHR(kw_values[1]) : 'F'; \
|
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
|
+
if (NA_NDIM(a_nary) != 2) { \
|
39
|
+
rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
|
40
|
+
return Qnil; \
|
41
|
+
} \
|
42
|
+
\
|
43
|
+
ndfunc_arg_in_t ain[1] = { { tNAryClass, 2 } }; \
|
44
|
+
size_t shape_out[1] = { 1 }; \
|
45
|
+
ndfunc_arg_out_t aout[1] = { { tNAryClass, 0, shape_out } }; \
|
46
|
+
ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 1, ain, aout }; \
|
47
|
+
struct _lange_option opt = { matrix_layout, norm }; \
|
48
|
+
VALUE ret = na_ndloop3(&ndf, &opt, 1, a_vnary); \
|
49
|
+
\
|
50
|
+
RB_GC_GUARD(a_vnary); \
|
51
|
+
return ret; \
|
52
|
+
}
|
53
|
+
|
54
|
+
DEF_LINALG_FUNC(double, numo_cDFloat, dlange)
|
55
|
+
DEF_LINALG_FUNC(float, numo_cSFloat, slange)
|
56
|
+
DEF_LINALG_FUNC(lapack_complex_double, numo_cDComplex, zlange)
|
57
|
+
DEF_LINALG_FUNC(lapack_complex_float, numo_cSComplex, clange)
|
58
|
+
|
59
|
+
#undef DEF_LINALG_FUNC
|
60
|
+
|
61
|
+
void define_linalg_lapack_lange(VALUE mLapack) {
|
62
|
+
rb_define_module_function(mLapack, "dlange", RUBY_METHOD_FUNC(_linalg_lapack_dlange), -1);
|
63
|
+
rb_define_module_function(mLapack, "slange", RUBY_METHOD_FUNC(_linalg_lapack_slange), -1);
|
64
|
+
rb_define_module_function(mLapack, "zlange", RUBY_METHOD_FUNC(_linalg_lapack_zlange), -1);
|
65
|
+
rb_define_module_function(mLapack, "clange", RUBY_METHOD_FUNC(_linalg_lapack_clange), -1);
|
66
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef NUMO_LINALG_ALT_LAPACK_LANGE_H
|
2
|
+
#define NUMO_LINALG_ALT_LAPACK_LANGE_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_lange(VALUE mLapack);
|
14
|
+
|
15
|
+
#endif /* NUMO_LINALG_ALT_LAPACK_LANGE_H */
|
@@ -0,0 +1,79 @@
|
|
1
|
+
#include "orgqr.h"
|
2
|
+
|
3
|
+
struct _orgqr_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 _orgqr_option* opt = (struct _orgqr_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 k = (lapack_int)NDL_SHAPE(lp, 1)[0]; \
|
16
|
+
const lapack_int lda = n; \
|
17
|
+
const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, m, n, k, a, lda, tau); \
|
18
|
+
*info = (int)i; \
|
19
|
+
} \
|
20
|
+
\
|
21
|
+
static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
|
22
|
+
VALUE a_vnary = Qnil; \
|
23
|
+
VALUE tau_vnary = Qnil; \
|
24
|
+
VALUE kw_args = Qnil; \
|
25
|
+
rb_scan_args(argc, argv, "2:", &a_vnary, &tau_vnary, &kw_args); \
|
26
|
+
ID kw_table[1] = { rb_intern("order") }; \
|
27
|
+
VALUE kw_values[1] = { Qundef }; \
|
28
|
+
rb_get_kwargs(kw_args, kw_table, 0, 1, kw_values); \
|
29
|
+
const int matrix_layout = kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
|
30
|
+
\
|
31
|
+
if (CLASS_OF(a_vnary) != tNAryClass) { \
|
32
|
+
a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
|
33
|
+
} \
|
34
|
+
if (!RTEST(nary_check_contiguous(a_vnary))) { \
|
35
|
+
a_vnary = nary_dup(a_vnary); \
|
36
|
+
} \
|
37
|
+
if (CLASS_OF(tau_vnary) != tNAryClass) { \
|
38
|
+
tau_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, tau_vnary); \
|
39
|
+
} \
|
40
|
+
if (!RTEST(nary_check_contiguous(tau_vnary))) { \
|
41
|
+
tau_vnary = nary_dup(tau_vnary); \
|
42
|
+
} \
|
43
|
+
\
|
44
|
+
narray_t* a_nary = NULL; \
|
45
|
+
GetNArray(a_vnary, a_nary); \
|
46
|
+
if (NA_NDIM(a_nary) != 2) { \
|
47
|
+
rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
|
48
|
+
return Qnil; \
|
49
|
+
} \
|
50
|
+
narray_t* tau_nary = NULL; \
|
51
|
+
GetNArray(tau_vnary, tau_nary); \
|
52
|
+
if (NA_NDIM(tau_nary) != 1) { \
|
53
|
+
rb_raise(rb_eArgError, "input array tau must be 1-dimensional"); \
|
54
|
+
return Qnil; \
|
55
|
+
} \
|
56
|
+
\
|
57
|
+
ndfunc_arg_in_t ain[2] = { { OVERWRITE, 2 }, { tNAryClass, 1 } }; \
|
58
|
+
ndfunc_arg_out_t aout[1] = { { numo_cInt32, 0 } }; \
|
59
|
+
ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 2, 1, ain, aout }; \
|
60
|
+
struct _orgqr_option opt = { matrix_layout }; \
|
61
|
+
VALUE res = na_ndloop3(&ndf, &opt, 2, a_vnary, tau_vnary); \
|
62
|
+
\
|
63
|
+
VALUE ret = rb_ary_new3(2, a_vnary, res); \
|
64
|
+
\
|
65
|
+
RB_GC_GUARD(a_vnary); \
|
66
|
+
RB_GC_GUARD(tau_vnary); \
|
67
|
+
\
|
68
|
+
return ret; \
|
69
|
+
}
|
70
|
+
|
71
|
+
DEF_LINALG_FUNC(double, numo_cDFloat, dorgqr)
|
72
|
+
DEF_LINALG_FUNC(float, numo_cSFloat, sorgqr)
|
73
|
+
|
74
|
+
#undef DEF_LINALG_FUNC
|
75
|
+
|
76
|
+
void define_linalg_lapack_orgqr(VALUE mLapack) {
|
77
|
+
rb_define_module_function(mLapack, "dorgqr", RUBY_METHOD_FUNC(_linalg_lapack_dorgqr), -1);
|
78
|
+
rb_define_module_function(mLapack, "sorgqr", RUBY_METHOD_FUNC(_linalg_lapack_sorgqr), -1);
|
79
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef NUMO_LINALG_ALT_LAPACK_ORGQR_H
|
2
|
+
#define NUMO_LINALG_ALT_LAPACK_ORGQR_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_orgqr(VALUE mLapack);
|
14
|
+
|
15
|
+
#endif /* NUMO_LINALG_ALT_LAPACK_ORGQR_H */
|
@@ -0,0 +1,70 @@
|
|
1
|
+
#include "potrf.h"
|
2
|
+
|
3
|
+
struct _potrf_option {
|
4
|
+
int matrix_layout;
|
5
|
+
char uplo;
|
6
|
+
};
|
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* info = (int*)NDL_PTR(lp, 1); \
|
12
|
+
struct _potrf_option* opt = (struct _potrf_option*)(lp->opt_ptr); \
|
13
|
+
const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
|
14
|
+
const lapack_int lda = (lapack_int)NDL_SHAPE(lp, 0)[1]; \
|
15
|
+
const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, opt->uplo, n, a, lda); \
|
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 kw_args = Qnil; \
|
22
|
+
rb_scan_args(argc, argv, "1:", &a_vnary, &kw_args); \
|
23
|
+
ID kw_table[2] = { rb_intern("order"), rb_intern("uplo") }; \
|
24
|
+
VALUE kw_values[2] = { Qundef, Qundef }; \
|
25
|
+
rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
|
26
|
+
const int matrix_layout = kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
|
27
|
+
const char uplo = kw_values[1] != Qundef ? get_uplo(kw_values[1]) : 'U'; \
|
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
|
+
if (NA_NDIM(a_nary) != 2) { \
|
39
|
+
rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
|
40
|
+
return Qnil; \
|
41
|
+
} \
|
42
|
+
if (NA_SHAPE(a_nary)[0] != NA_SHAPE(a_nary)[1]) { \
|
43
|
+
rb_raise(rb_eArgError, "input array a must be square"); \
|
44
|
+
return Qnil; \
|
45
|
+
} \
|
46
|
+
\
|
47
|
+
ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } }; \
|
48
|
+
ndfunc_arg_out_t aout[1] = { { numo_cInt32, 0 } }; \
|
49
|
+
ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 1, ain, aout }; \
|
50
|
+
struct _potrf_option opt = { matrix_layout, uplo }; \
|
51
|
+
VALUE res = na_ndloop3(&ndf, &opt, 1, a_vnary); \
|
52
|
+
VALUE ret = rb_ary_new3(2, a_vnary, res); \
|
53
|
+
\
|
54
|
+
RB_GC_GUARD(a_vnary); \
|
55
|
+
return ret; \
|
56
|
+
}
|
57
|
+
|
58
|
+
DEF_LINALG_FUNC(double, numo_cDFloat, dpotrf)
|
59
|
+
DEF_LINALG_FUNC(float, numo_cSFloat, spotrf)
|
60
|
+
DEF_LINALG_FUNC(lapack_complex_double, numo_cDComplex, zpotrf)
|
61
|
+
DEF_LINALG_FUNC(lapack_complex_float, numo_cSComplex, cpotrf)
|
62
|
+
|
63
|
+
#undef DEF_LINALG_FUNC
|
64
|
+
|
65
|
+
void define_linalg_lapack_potrf(VALUE mLapack) {
|
66
|
+
rb_define_module_function(mLapack, "dpotrf", RUBY_METHOD_FUNC(_linalg_lapack_dpotrf), -1);
|
67
|
+
rb_define_module_function(mLapack, "spotrf", RUBY_METHOD_FUNC(_linalg_lapack_spotrf), -1);
|
68
|
+
rb_define_module_function(mLapack, "zpotrf", RUBY_METHOD_FUNC(_linalg_lapack_zpotrf), -1);
|
69
|
+
rb_define_module_function(mLapack, "cpotrf", RUBY_METHOD_FUNC(_linalg_lapack_cpotrf), -1);
|
70
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef NUMO_LINALG_ALT_LAPACK_POTRF_H
|
2
|
+
#define NUMO_LINALG_ALT_LAPACK_POTRF_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_potrf(VALUE mLapack);
|
14
|
+
|
15
|
+
#endif /* NUMO_LINALG_ALT_LAPACK_POTRF_H */
|
@@ -0,0 +1,70 @@
|
|
1
|
+
#include "potri.h"
|
2
|
+
|
3
|
+
struct _potri_option {
|
4
|
+
int matrix_layout;
|
5
|
+
char uplo;
|
6
|
+
};
|
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* info = (int*)NDL_PTR(lp, 1); \
|
12
|
+
struct _potri_option* opt = (struct _potri_option*)(lp->opt_ptr); \
|
13
|
+
const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
|
14
|
+
const lapack_int lda = (lapack_int)NDL_SHAPE(lp, 0)[1]; \
|
15
|
+
const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, opt->uplo, n, a, lda); \
|
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 kw_args = Qnil; \
|
22
|
+
rb_scan_args(argc, argv, "1:", &a_vnary, &kw_args); \
|
23
|
+
ID kw_table[2] = { rb_intern("order"), rb_intern("uplo") }; \
|
24
|
+
VALUE kw_values[2] = { Qundef, Qundef }; \
|
25
|
+
rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
|
26
|
+
const int matrix_layout = kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
|
27
|
+
const char uplo = kw_values[1] != Qundef ? get_uplo(kw_values[1]) : 'U'; \
|
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
|
+
if (NA_NDIM(a_nary) != 2) { \
|
39
|
+
rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
|
40
|
+
return Qnil; \
|
41
|
+
} \
|
42
|
+
if (NA_SHAPE(a_nary)[0] != NA_SHAPE(a_nary)[1]) { \
|
43
|
+
rb_raise(rb_eArgError, "input array a must be square"); \
|
44
|
+
return Qnil; \
|
45
|
+
} \
|
46
|
+
\
|
47
|
+
ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } }; \
|
48
|
+
ndfunc_arg_out_t aout[1] = { { numo_cInt32, 0 } }; \
|
49
|
+
ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 1, ain, aout }; \
|
50
|
+
struct _potri_option opt = { matrix_layout, uplo }; \
|
51
|
+
VALUE res = na_ndloop3(&ndf, &opt, 1, a_vnary); \
|
52
|
+
VALUE ret = rb_ary_new3(2, a_vnary, res); \
|
53
|
+
\
|
54
|
+
RB_GC_GUARD(a_vnary); \
|
55
|
+
return ret; \
|
56
|
+
}
|
57
|
+
|
58
|
+
DEF_LINALG_FUNC(double, numo_cDFloat, dpotri)
|
59
|
+
DEF_LINALG_FUNC(float, numo_cSFloat, spotri)
|
60
|
+
DEF_LINALG_FUNC(lapack_complex_double, numo_cDComplex, zpotri)
|
61
|
+
DEF_LINALG_FUNC(lapack_complex_float, numo_cSComplex, cpotri)
|
62
|
+
|
63
|
+
#undef DEF_LINALG_FUNC
|
64
|
+
|
65
|
+
void define_linalg_lapack_potri(VALUE mLapack) {
|
66
|
+
rb_define_module_function(mLapack, "dpotri", RUBY_METHOD_FUNC(_linalg_lapack_dpotri), -1);
|
67
|
+
rb_define_module_function(mLapack, "spotri", RUBY_METHOD_FUNC(_linalg_lapack_spotri), -1);
|
68
|
+
rb_define_module_function(mLapack, "zpotri", RUBY_METHOD_FUNC(_linalg_lapack_zpotri), -1);
|
69
|
+
rb_define_module_function(mLapack, "cpotri", RUBY_METHOD_FUNC(_linalg_lapack_cpotri), -1);
|
70
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef NUMO_LINALG_ALT_LAPACK_POTRI_H
|
2
|
+
#define NUMO_LINALG_ALT_LAPACK_POTRI_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_potri(VALUE mLapack);
|
14
|
+
|
15
|
+
#endif /* NUMO_LINALG_ALT_LAPACK_POTRI_H */
|
@@ -0,0 +1,94 @@
|
|
1
|
+
#include "potrs.h"
|
2
|
+
|
3
|
+
struct _potrs_option {
|
4
|
+
int matrix_layout;
|
5
|
+
char uplo;
|
6
|
+
};
|
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
|
+
tDType* b = (tDType*)NDL_PTR(lp, 1); \
|
12
|
+
int* info = (int*)NDL_PTR(lp, 2); \
|
13
|
+
struct _potrs_option* opt = (struct _potrs_option*)(lp->opt_ptr); \
|
14
|
+
const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
|
15
|
+
const lapack_int nrhs = lp->args[1].ndim == 1 ? 1 : (lapack_int)NDL_SHAPE(lp, 1)[1]; \
|
16
|
+
const lapack_int lda = n; \
|
17
|
+
const lapack_int ldb = nrhs; \
|
18
|
+
const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, opt->uplo, n, nrhs, a, lda, b, ldb); \
|
19
|
+
*info = (int)i; \
|
20
|
+
} \
|
21
|
+
\
|
22
|
+
static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
|
23
|
+
VALUE a_vnary = Qnil; \
|
24
|
+
VALUE b_vnary = Qnil; \
|
25
|
+
VALUE kw_args = Qnil; \
|
26
|
+
rb_scan_args(argc, argv, "2:", &a_vnary, &b_vnary, &kw_args); \
|
27
|
+
ID kw_table[2] = { rb_intern("order"), rb_intern("uplo") }; \
|
28
|
+
VALUE kw_values[2] = { Qundef, Qundef }; \
|
29
|
+
rb_get_kwargs(kw_args, kw_table, 0, 2, kw_values); \
|
30
|
+
const int matrix_layout = kw_values[0] != Qundef ? get_matrix_layout(kw_values[0]) : LAPACK_ROW_MAJOR; \
|
31
|
+
const char uplo = kw_values[1] != Qundef ? get_uplo(kw_values[1]) : 'U'; \
|
32
|
+
\
|
33
|
+
if (CLASS_OF(a_vnary) != tNAryClass) { \
|
34
|
+
a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
|
35
|
+
} \
|
36
|
+
if (!RTEST(nary_check_contiguous(a_vnary))) { \
|
37
|
+
a_vnary = nary_dup(a_vnary); \
|
38
|
+
} \
|
39
|
+
if (CLASS_OF(b_vnary) != tNAryClass) { \
|
40
|
+
b_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, b_vnary); \
|
41
|
+
} \
|
42
|
+
if (!RTEST(nary_check_contiguous(b_vnary))) { \
|
43
|
+
b_vnary = nary_dup(b_vnary); \
|
44
|
+
} \
|
45
|
+
\
|
46
|
+
narray_t* a_nary = NULL; \
|
47
|
+
GetNArray(a_vnary, a_nary); \
|
48
|
+
if (NA_NDIM(a_nary) != 2) { \
|
49
|
+
rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
|
50
|
+
return Qnil; \
|
51
|
+
} \
|
52
|
+
if (NA_SHAPE(a_nary)[0] != NA_SHAPE(a_nary)[1]) { \
|
53
|
+
rb_raise(rb_eArgError, "input array a must be square"); \
|
54
|
+
return Qnil; \
|
55
|
+
} \
|
56
|
+
narray_t* b_nary = NULL; \
|
57
|
+
GetNArray(b_vnary, b_nary); \
|
58
|
+
const int b_n_dims = NA_NDIM(b_nary); \
|
59
|
+
if (b_n_dims != 1 && b_n_dims != 2) { \
|
60
|
+
rb_raise(rb_eArgError, "input array b must be 1- or 2-dimensional"); \
|
61
|
+
return Qnil; \
|
62
|
+
} \
|
63
|
+
\
|
64
|
+
lapack_int n = (lapack_int)NA_SHAPE(a_nary)[0]; \
|
65
|
+
lapack_int nb = (lapack_int)NA_SHAPE(b_nary)[0]; \
|
66
|
+
if (n != nb) { \
|
67
|
+
rb_raise(nary_eShapeError, "shape1[0](=%d) != shape2[0](=%d)", n, nb); \
|
68
|
+
} \
|
69
|
+
\
|
70
|
+
ndfunc_arg_in_t ain[2] = { { tNAryClass, 2 }, { OVERWRITE, b_n_dims } }; \
|
71
|
+
ndfunc_arg_out_t aout[1] = { { numo_cInt32, 0 } }; \
|
72
|
+
ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 2, 1, ain, aout }; \
|
73
|
+
struct _potrs_option opt = { matrix_layout, uplo }; \
|
74
|
+
VALUE res = na_ndloop3(&ndf, &opt, 2, a_vnary, b_vnary); \
|
75
|
+
VALUE ret = rb_ary_new3(2, b_vnary, res); \
|
76
|
+
\
|
77
|
+
RB_GC_GUARD(a_vnary); \
|
78
|
+
RB_GC_GUARD(b_vnary); \
|
79
|
+
return ret; \
|
80
|
+
}
|
81
|
+
|
82
|
+
DEF_LINALG_FUNC(double, numo_cDFloat, dpotrs)
|
83
|
+
DEF_LINALG_FUNC(float, numo_cSFloat, spotrs)
|
84
|
+
DEF_LINALG_FUNC(lapack_complex_double, numo_cDComplex, zpotrs)
|
85
|
+
DEF_LINALG_FUNC(lapack_complex_float, numo_cSComplex, cpotrs)
|
86
|
+
|
87
|
+
#undef DEF_LINALG_FUNC
|
88
|
+
|
89
|
+
void define_linalg_lapack_potrs(VALUE mLapack) {
|
90
|
+
rb_define_module_function(mLapack, "dpotrs", RUBY_METHOD_FUNC(_linalg_lapack_dpotrs), -1);
|
91
|
+
rb_define_module_function(mLapack, "spotrs", RUBY_METHOD_FUNC(_linalg_lapack_spotrs), -1);
|
92
|
+
rb_define_module_function(mLapack, "zpotrs", RUBY_METHOD_FUNC(_linalg_lapack_zpotrs), -1);
|
93
|
+
rb_define_module_function(mLapack, "cpotrs", RUBY_METHOD_FUNC(_linalg_lapack_cpotrs), -1);
|
94
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef NUMO_LINALG_ALT_LAPACK_POTRS_H
|
2
|
+
#define NUMO_LINALG_ALT_LAPACK_POTRS_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_potrs(VALUE mLapack);
|
14
|
+
|
15
|
+
#endif /* NUMO_LINALG_ALT_LAPACK_POTRS_H */
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#include "syev.h"
|
2
|
+
|
3
|
+
struct _syev_option {
|
4
|
+
int matrix_layout;
|
5
|
+
char jobz;
|
6
|
+
char uplo;
|
7
|
+
};
|
8
|
+
|
9
|
+
#define DEF_LINALG_FUNC(tDType, tNAryClass, fLapackFunc) \
|
10
|
+
static void _iter_##fLapackFunc(na_loop_t* const lp) { \
|
11
|
+
tDType* a = (tDType*)NDL_PTR(lp, 0); \
|
12
|
+
tDType* w = (tDType*)NDL_PTR(lp, 1); \
|
13
|
+
int* info = (int*)NDL_PTR(lp, 2); \
|
14
|
+
struct _syev_option* opt = (struct _syev_option*)(lp->opt_ptr); \
|
15
|
+
const lapack_int n = (lapack_int)NDL_SHAPE(lp, 0)[1]; \
|
16
|
+
const lapack_int lda = (lapack_int)NDL_SHAPE(lp, 0)[0]; \
|
17
|
+
const lapack_int i = LAPACKE_##fLapackFunc(opt->matrix_layout, opt->jobz, opt->uplo, n, a, lda, w); \
|
18
|
+
*info = (int)i; \
|
19
|
+
} \
|
20
|
+
\
|
21
|
+
static VALUE _linalg_lapack_##fLapackFunc(int argc, VALUE* argv, VALUE self) { \
|
22
|
+
VALUE a_vnary = Qnil; \
|
23
|
+
VALUE kw_args = Qnil; \
|
24
|
+
rb_scan_args(argc, argv, "1:", &a_vnary, &kw_args); \
|
25
|
+
ID kw_table[3] = { rb_intern("jobz"), rb_intern("uplo"), rb_intern("order") }; \
|
26
|
+
VALUE kw_values[3] = { Qundef, Qundef, Qundef }; \
|
27
|
+
rb_get_kwargs(kw_args, kw_table, 0, 3, kw_values); \
|
28
|
+
const char jobz = kw_values[0] != Qundef ? get_jobz(kw_values[0]) : 'V'; \
|
29
|
+
const char uplo = kw_values[1] != Qundef ? get_uplo(kw_values[1]) : 'U'; \
|
30
|
+
const int matrix_layout = kw_values[2] != Qundef ? get_matrix_layout(kw_values[2]) : LAPACK_ROW_MAJOR; \
|
31
|
+
\
|
32
|
+
if (CLASS_OF(a_vnary) != tNAryClass) { \
|
33
|
+
a_vnary = rb_funcall(tNAryClass, rb_intern("cast"), 1, a_vnary); \
|
34
|
+
} \
|
35
|
+
if (!RTEST(nary_check_contiguous(a_vnary))) { \
|
36
|
+
a_vnary = nary_dup(a_vnary); \
|
37
|
+
} \
|
38
|
+
\
|
39
|
+
narray_t* a_nary = NULL; \
|
40
|
+
GetNArray(a_vnary, a_nary); \
|
41
|
+
if (NA_NDIM(a_nary) != 2) { \
|
42
|
+
rb_raise(rb_eArgError, "input array a must be 2-dimensional"); \
|
43
|
+
return Qnil; \
|
44
|
+
} \
|
45
|
+
if (NA_SHAPE(a_nary)[0] != NA_SHAPE(a_nary)[1]) { \
|
46
|
+
rb_raise(rb_eArgError, "input array a must be square"); \
|
47
|
+
return Qnil; \
|
48
|
+
} \
|
49
|
+
\
|
50
|
+
const size_t n = NA_SHAPE(a_nary)[1]; \
|
51
|
+
size_t shape[1] = { n }; \
|
52
|
+
ndfunc_arg_in_t ain[1] = { { OVERWRITE, 2 } }; \
|
53
|
+
ndfunc_arg_out_t aout[2] = { { tNAryClass, 1, shape }, { numo_cInt32, 0 } }; \
|
54
|
+
ndfunc_t ndf = { _iter_##fLapackFunc, NO_LOOP | NDF_EXTRACT, 1, 2, ain, aout }; \
|
55
|
+
struct _syev_option opt = { matrix_layout, jobz, uplo }; \
|
56
|
+
VALUE res = na_ndloop3(&ndf, &opt, 1, a_vnary); \
|
57
|
+
VALUE ret = rb_ary_new3(3, a_vnary, rb_ary_entry(res, 0), rb_ary_entry(res, 1)); \
|
58
|
+
\
|
59
|
+
RB_GC_GUARD(a_vnary); \
|
60
|
+
return ret; \
|
61
|
+
}
|
62
|
+
|
63
|
+
DEF_LINALG_FUNC(double, numo_cDFloat, dsyev)
|
64
|
+
DEF_LINALG_FUNC(float, numo_cSFloat, ssyev)
|
65
|
+
|
66
|
+
#undef DEFINE_LINALG_FUNC
|
67
|
+
|
68
|
+
void define_linalg_lapack_syev(VALUE mLapack) {
|
69
|
+
rb_define_module_function(mLapack, "dsyev", RUBY_METHOD_FUNC(_linalg_lapack_dsyev), -1);
|
70
|
+
rb_define_module_function(mLapack, "ssyev", RUBY_METHOD_FUNC(_linalg_lapack_ssyev), -1);
|
71
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef NUMO_LINALG_ALT_LAPACK_SYEV_H
|
2
|
+
#define NUMO_LINALG_ALT_LAPACK_SYEV_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_syev(VALUE mLapack);
|
14
|
+
|
15
|
+
#endif /* NUMO_LINALG_ALT_LAPACK_SYEV_H */
|