nmatrix-gemv 0.0.3
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/.gitignore +29 -0
- data/.rspec +2 -0
- data/.travis.yml +14 -0
- data/Gemfile +7 -0
- data/README.md +29 -0
- data/Rakefile +225 -0
- data/ext/nmatrix_gemv/binary_format.txt +53 -0
- data/ext/nmatrix_gemv/data/complex.h +399 -0
- data/ext/nmatrix_gemv/data/data.cpp +298 -0
- data/ext/nmatrix_gemv/data/data.h +771 -0
- data/ext/nmatrix_gemv/data/meta.h +70 -0
- data/ext/nmatrix_gemv/data/rational.h +436 -0
- data/ext/nmatrix_gemv/data/ruby_object.h +471 -0
- data/ext/nmatrix_gemv/extconf.rb +254 -0
- data/ext/nmatrix_gemv/math.cpp +1639 -0
- data/ext/nmatrix_gemv/math/asum.h +143 -0
- data/ext/nmatrix_gemv/math/geev.h +82 -0
- data/ext/nmatrix_gemv/math/gemm.h +271 -0
- data/ext/nmatrix_gemv/math/gemv.h +212 -0
- data/ext/nmatrix_gemv/math/ger.h +96 -0
- data/ext/nmatrix_gemv/math/gesdd.h +80 -0
- data/ext/nmatrix_gemv/math/gesvd.h +78 -0
- data/ext/nmatrix_gemv/math/getf2.h +86 -0
- data/ext/nmatrix_gemv/math/getrf.h +240 -0
- data/ext/nmatrix_gemv/math/getri.h +108 -0
- data/ext/nmatrix_gemv/math/getrs.h +129 -0
- data/ext/nmatrix_gemv/math/idamax.h +86 -0
- data/ext/nmatrix_gemv/math/inc.h +47 -0
- data/ext/nmatrix_gemv/math/laswp.h +165 -0
- data/ext/nmatrix_gemv/math/long_dtype.h +52 -0
- data/ext/nmatrix_gemv/math/math.h +1069 -0
- data/ext/nmatrix_gemv/math/nrm2.h +181 -0
- data/ext/nmatrix_gemv/math/potrs.h +129 -0
- data/ext/nmatrix_gemv/math/rot.h +141 -0
- data/ext/nmatrix_gemv/math/rotg.h +115 -0
- data/ext/nmatrix_gemv/math/scal.h +73 -0
- data/ext/nmatrix_gemv/math/swap.h +73 -0
- data/ext/nmatrix_gemv/math/trsm.h +387 -0
- data/ext/nmatrix_gemv/nm_memory.h +60 -0
- data/ext/nmatrix_gemv/nmatrix_gemv.cpp +90 -0
- data/ext/nmatrix_gemv/nmatrix_gemv.h +374 -0
- data/ext/nmatrix_gemv/ruby_constants.cpp +153 -0
- data/ext/nmatrix_gemv/ruby_constants.h +107 -0
- data/ext/nmatrix_gemv/ruby_nmatrix.c +84 -0
- data/ext/nmatrix_gemv/ttable_helper.rb +122 -0
- data/ext/nmatrix_gemv/types.h +54 -0
- data/ext/nmatrix_gemv/util/util.h +78 -0
- data/lib/nmatrix-gemv.rb +43 -0
- data/lib/nmatrix_gemv/blas.rb +85 -0
- data/lib/nmatrix_gemv/nmatrix_gemv.rb +35 -0
- data/lib/nmatrix_gemv/rspec.rb +75 -0
- data/nmatrix-gemv.gemspec +31 -0
- data/spec/blas_spec.rb +154 -0
- data/spec/spec_helper.rb +128 -0
- metadata +186 -0
@@ -0,0 +1,86 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == getf2.h
|
25
|
+
//
|
26
|
+
// LAPACK getf2 function in native C++.
|
27
|
+
//
|
28
|
+
|
29
|
+
#ifndef GETF2_H
|
30
|
+
#define GETF2_H
|
31
|
+
|
32
|
+
namespace nm { namespace math {
|
33
|
+
|
34
|
+
template <typename DType>
|
35
|
+
inline int getf2(const int m, const int n, DType* a, const int lda, int *ipiv) {
|
36
|
+
|
37
|
+
/* Function Body */
|
38
|
+
if (m < 0) return -1; // error
|
39
|
+
else if (n < 0) return -2; // error
|
40
|
+
else if (lda < std::max(1,m)) return -4; // error
|
41
|
+
|
42
|
+
|
43
|
+
if (m == 0 || n == 0) return 0; /* Quick return if possible */
|
44
|
+
|
45
|
+
for (size_t j = 0; j < std::min(m,n); ++j) { // changed
|
46
|
+
|
47
|
+
/* Find pivot and test for singularity. */
|
48
|
+
|
49
|
+
int jp = j - 1 + idamax<DType>(m-j+1, &a[j + j * lda], 1);
|
50
|
+
|
51
|
+
ipiv[j] = jp;
|
52
|
+
|
53
|
+
|
54
|
+
if (a[jp + j*lda] != 0) {
|
55
|
+
|
56
|
+
/* Apply the interchange to columns 1:N. */
|
57
|
+
// (Don't swap two columns that are the same.)
|
58
|
+
if (jp != j) swap<DType>(n, &a[j], lda, &a[jp], lda);
|
59
|
+
|
60
|
+
/* Compute elements J+1:M of J-th column. */
|
61
|
+
|
62
|
+
if (j < m-1) {
|
63
|
+
if (std::abs(a[j+j*lda]) >= std::numeric_limits<DType>::min()) {
|
64
|
+
scal<DType>(m-j, 1.0 / a[j+j*lda], &a[j+1+j*lda], 1);
|
65
|
+
} else {
|
66
|
+
for (size_t i = 0; i < m-j; ++i) { // changed
|
67
|
+
a[j+i+j*lda] /= a[j+j*lda];
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
} else { // singular matrix
|
73
|
+
return j; // U(j,j) is exactly zero, div by zero if answer is used to solve a system of equations.
|
74
|
+
}
|
75
|
+
|
76
|
+
if (j < std::min(m,n)-1) /* Update trailing submatrix. */
|
77
|
+
ger<DType>(m-j, n-j, -1.0, &a[j+1+j*lda], 1, &a[j+(j+1)*lda], lda, &a[j+1+(j+1)*lda], lda);
|
78
|
+
|
79
|
+
}
|
80
|
+
return 0;
|
81
|
+
} /* dgetf2_ */
|
82
|
+
|
83
|
+
|
84
|
+
}} // end of namespace nm::math
|
85
|
+
|
86
|
+
#endif // GETF2
|
@@ -0,0 +1,240 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == getrf.h
|
25
|
+
//
|
26
|
+
// getrf function in native C++.
|
27
|
+
//
|
28
|
+
|
29
|
+
/*
|
30
|
+
* Automatically Tuned Linear Algebra Software v3.8.4
|
31
|
+
* (C) Copyright 1999 R. Clint Whaley
|
32
|
+
*
|
33
|
+
* Redistribution and use in source and binary forms, with or without
|
34
|
+
* modification, are permitted provided that the following conditions
|
35
|
+
* are met:
|
36
|
+
* 1. Redistributions of source code must retain the above copyright
|
37
|
+
* notice, this list of conditions and the following disclaimer.
|
38
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
39
|
+
* notice, this list of conditions, and the following disclaimer in the
|
40
|
+
* documentation and/or other materials provided with the distribution.
|
41
|
+
* 3. The name of the ATLAS group or the names of its contributers may
|
42
|
+
* not be used to endorse or promote products derived from this
|
43
|
+
* software without specific written permission.
|
44
|
+
*
|
45
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
46
|
+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
47
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
48
|
+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ATLAS GROUP OR ITS CONTRIBUTORS
|
49
|
+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
50
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
51
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
52
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
53
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
54
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
55
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
56
|
+
*
|
57
|
+
*/
|
58
|
+
|
59
|
+
#ifndef GETRF_H
|
60
|
+
#define GETRF_H
|
61
|
+
|
62
|
+
namespace nm { namespace math {
|
63
|
+
|
64
|
+
/* Numeric inverse -- usually just 1 / f, but a little more complicated for complex. */
|
65
|
+
template <typename DType>
|
66
|
+
inline DType numeric_inverse(const DType& n) {
|
67
|
+
return n.inverse();
|
68
|
+
}
|
69
|
+
template <> inline float numeric_inverse(const float& n) { return 1 / n; }
|
70
|
+
template <> inline double numeric_inverse(const double& n) { return 1 / n; }
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
/*
|
75
|
+
* Templated version of row-order and column-order getrf, derived from ATL_getrfR.c (from ATLAS 3.8.0).
|
76
|
+
*
|
77
|
+
* 1. Row-major factorization of form
|
78
|
+
* A = L * U * P
|
79
|
+
* where P is a column-permutation matrix, L is lower triangular (lower
|
80
|
+
* trapazoidal if M > N), and U is upper triangular with unit diagonals (upper
|
81
|
+
* trapazoidal if M < N). This is the recursive Level 3 BLAS version.
|
82
|
+
*
|
83
|
+
* 2. Column-major factorization of form
|
84
|
+
* A = P * L * U
|
85
|
+
* where P is a row-permutation matrix, L is lower triangular with unit diagonal
|
86
|
+
* elements (lower trapazoidal if M > N), and U is upper triangular (upper
|
87
|
+
* trapazoidal if M < N). This is the recursive Level 3 BLAS version.
|
88
|
+
*
|
89
|
+
* Template argument determines whether 1 or 2 is utilized.
|
90
|
+
*/
|
91
|
+
template <bool RowMajor, typename DType>
|
92
|
+
inline int getrf_nothrow(const int M, const int N, DType* A, const int lda, int* ipiv) {
|
93
|
+
const int MN = std::min(M, N);
|
94
|
+
int ierr = 0;
|
95
|
+
|
96
|
+
// Symbols used by ATLAS in the several versions of this function:
|
97
|
+
// Row Col Us
|
98
|
+
// Nup Nleft N_ul
|
99
|
+
// Ndown Nright N_dr
|
100
|
+
// We're going to use N_ul, N_dr
|
101
|
+
|
102
|
+
DType neg_one = -1, one = 1;
|
103
|
+
|
104
|
+
if (MN > 1) {
|
105
|
+
int N_ul = MN >> 1;
|
106
|
+
|
107
|
+
// FIXME: Figure out how ATLAS #defines NB
|
108
|
+
#ifdef NB
|
109
|
+
if (N_ul > NB) N_ul = ATL_MulByNB(ATL_DivByNB(N_ul));
|
110
|
+
#endif
|
111
|
+
|
112
|
+
int N_dr = M - N_ul;
|
113
|
+
|
114
|
+
int i = RowMajor ? getrf_nothrow<true,DType>(N_ul, N, A, lda, ipiv) : getrf_nothrow<false,DType>(M, N_ul, A, lda, ipiv);
|
115
|
+
|
116
|
+
if (i) if (!ierr) ierr = i;
|
117
|
+
|
118
|
+
DType *Ar, *Ac, *An;
|
119
|
+
if (RowMajor) {
|
120
|
+
Ar = &(A[N_ul * lda]),
|
121
|
+
Ac = &(A[N_ul]);
|
122
|
+
An = &(Ar[N_ul]);
|
123
|
+
|
124
|
+
nm::math::laswp<DType>(N_dr, Ar, lda, 0, N_ul, ipiv, 1);
|
125
|
+
|
126
|
+
nm::math::trsm<DType>(CblasRowMajor, CblasRight, CblasUpper, CblasNoTrans, CblasUnit, N_dr, N_ul, one, A, lda, Ar, lda);
|
127
|
+
nm::math::gemm<DType>(CblasRowMajor, CblasNoTrans, CblasNoTrans, N_dr, N-N_ul, N_ul, &neg_one, Ar, lda, Ac, lda, &one, An, lda);
|
128
|
+
|
129
|
+
i = getrf_nothrow<true,DType>(N_dr, N-N_ul, An, lda, ipiv+N_ul);
|
130
|
+
} else {
|
131
|
+
Ar = NULL;
|
132
|
+
Ac = &(A[N_ul * lda]);
|
133
|
+
An = &(Ac[N_ul]);
|
134
|
+
|
135
|
+
nm::math::laswp<DType>(N_dr, Ac, lda, 0, N_ul, ipiv, 1);
|
136
|
+
|
137
|
+
nm::math::trsm<DType>(CblasColMajor, CblasLeft, CblasLower, CblasNoTrans, CblasUnit, N_ul, N_dr, one, A, lda, Ac, lda);
|
138
|
+
nm::math::gemm<DType>(CblasColMajor, CblasNoTrans, CblasNoTrans, M-N_ul, N_dr, N_ul, &neg_one, An, lda, Ac, lda, &one, An, lda);
|
139
|
+
|
140
|
+
i = getrf_nothrow<false,DType>(M-N_ul, N_dr, An, lda, ipiv+N_ul);
|
141
|
+
}
|
142
|
+
|
143
|
+
if (i) if (!ierr) ierr = N_ul + i;
|
144
|
+
|
145
|
+
for (i = N_ul; i != MN; i++) {
|
146
|
+
ipiv[i] += N_ul;
|
147
|
+
}
|
148
|
+
|
149
|
+
nm::math::laswp<DType>(N_ul, A, lda, N_ul, MN, ipiv, 1); /* apply pivots */
|
150
|
+
|
151
|
+
} else if (MN == 1) { // there's another case for the colmajor version, but i don't know that it's that critical. Calls ATLAS LU2, who knows what that does.
|
152
|
+
|
153
|
+
int i = *ipiv = nm::math::idamax<DType>(N, A, 1); // cblas_iamax(N, A, 1);
|
154
|
+
|
155
|
+
DType tmp = A[i];
|
156
|
+
if (tmp != 0) {
|
157
|
+
|
158
|
+
nm::math::scal<DType>((RowMajor ? N : M), nm::math::numeric_inverse(tmp), A, 1);
|
159
|
+
A[i] = *A;
|
160
|
+
*A = tmp;
|
161
|
+
|
162
|
+
} else ierr = 1;
|
163
|
+
|
164
|
+
}
|
165
|
+
return(ierr);
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
/*
|
170
|
+
* From ATLAS 3.8.0:
|
171
|
+
*
|
172
|
+
* Computes one of two LU factorizations based on the setting of the Order
|
173
|
+
* parameter, as follows:
|
174
|
+
* ----------------------------------------------------------------------------
|
175
|
+
* Order == CblasColMajor
|
176
|
+
* Column-major factorization of form
|
177
|
+
* A = P * L * U
|
178
|
+
* where P is a row-permutation matrix, L is lower triangular with unit
|
179
|
+
* diagonal elements (lower trapazoidal if M > N), and U is upper triangular
|
180
|
+
* (upper trapazoidal if M < N).
|
181
|
+
*
|
182
|
+
* ----------------------------------------------------------------------------
|
183
|
+
* Order == CblasRowMajor
|
184
|
+
* Row-major factorization of form
|
185
|
+
* A = P * L * U
|
186
|
+
* where P is a column-permutation matrix, L is lower triangular (lower
|
187
|
+
* trapazoidal if M > N), and U is upper triangular with unit diagonals (upper
|
188
|
+
* trapazoidal if M < N).
|
189
|
+
*
|
190
|
+
* ============================================================================
|
191
|
+
* Let IERR be the return value of the function:
|
192
|
+
* If IERR == 0, successful exit.
|
193
|
+
* If (IERR < 0) the -IERR argument had an illegal value
|
194
|
+
* If (IERR > 0 && Order == CblasColMajor)
|
195
|
+
* U(i-1,i-1) is exactly zero. The factorization has been completed,
|
196
|
+
* but the factor U is exactly singular, and division by zero will
|
197
|
+
* occur if it is used to solve a system of equations.
|
198
|
+
* If (IERR > 0 && Order == CblasRowMajor)
|
199
|
+
* L(i-1,i-1) is exactly zero. The factorization has been completed,
|
200
|
+
* but the factor L is exactly singular, and division by zero will
|
201
|
+
* occur if it is used to solve a system of equations.
|
202
|
+
*/
|
203
|
+
template <typename DType>
|
204
|
+
inline int getrf(const enum CBLAS_ORDER Order, const int M, const int N, DType* A, int lda, int* ipiv) {
|
205
|
+
if (Order == CblasRowMajor) {
|
206
|
+
if (lda < std::max(1,N)) {
|
207
|
+
rb_raise(rb_eArgError, "GETRF: lda must be >= MAX(N,1): lda=%d N=%d", lda, N);
|
208
|
+
return -6;
|
209
|
+
}
|
210
|
+
|
211
|
+
return getrf_nothrow<true,DType>(M, N, A, lda, ipiv);
|
212
|
+
} else {
|
213
|
+
if (lda < std::max(1,M)) {
|
214
|
+
rb_raise(rb_eArgError, "GETRF: lda must be >= MAX(M,1): lda=%d M=%d", lda, M);
|
215
|
+
return -6;
|
216
|
+
}
|
217
|
+
|
218
|
+
return getrf_nothrow<false,DType>(M, N, A, lda, ipiv);
|
219
|
+
//rb_raise(rb_eNotImpError, "column major getrf not implemented");
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
/*
|
226
|
+
* Function signature conversion for calling LAPACK's getrf functions as directly as possible.
|
227
|
+
*
|
228
|
+
* For documentation: http://www.netlib.org/lapack/double/dgetrf.f
|
229
|
+
*
|
230
|
+
* This function should normally go in math.cpp, but we need it to be available to nmatrix.cpp.
|
231
|
+
*/
|
232
|
+
template <typename DType>
|
233
|
+
inline int clapack_getrf(const enum CBLAS_ORDER order, const int m, const int n, void* a, const int lda, int* ipiv) {
|
234
|
+
return getrf<DType>(order, m, n, reinterpret_cast<DType*>(a), lda, ipiv);
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
} } // end nm::math
|
239
|
+
|
240
|
+
#endif
|
@@ -0,0 +1,108 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == getri.h
|
25
|
+
//
|
26
|
+
// getri function in native C++.
|
27
|
+
//
|
28
|
+
|
29
|
+
/*
|
30
|
+
* Automatically Tuned Linear Algebra Software v3.8.4
|
31
|
+
* (C) Copyright 1999 R. Clint Whaley
|
32
|
+
*
|
33
|
+
* Redistribution and use in source and binary forms, with or without
|
34
|
+
* modification, are permitted provided that the following conditions
|
35
|
+
* are met:
|
36
|
+
* 1. Redistributions of source code must retain the above copyright
|
37
|
+
* notice, this list of conditions and the following disclaimer.
|
38
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
39
|
+
* notice, this list of conditions, and the following disclaimer in the
|
40
|
+
* documentation and/or other materials provided with the distribution.
|
41
|
+
* 3. The name of the ATLAS group or the names of its contributers may
|
42
|
+
* not be used to endorse or promote products derived from this
|
43
|
+
* software without specific written permission.
|
44
|
+
*
|
45
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
46
|
+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
47
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
48
|
+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ATLAS GROUP OR ITS CONTRIBUTORS
|
49
|
+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
50
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
51
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
52
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
53
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
54
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
55
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
56
|
+
*
|
57
|
+
*/
|
58
|
+
|
59
|
+
#ifndef GETRI_H
|
60
|
+
#define GETRI_H
|
61
|
+
|
62
|
+
|
63
|
+
namespace nm { namespace math {
|
64
|
+
|
65
|
+
template <typename DType>
|
66
|
+
inline int getri(const enum CBLAS_ORDER order, const int n, DType* a, const int lda, const int* ipiv) {
|
67
|
+
rb_raise(rb_eNotImpError, "getri not yet implemented for non-BLAS dtypes");
|
68
|
+
return 0;
|
69
|
+
}
|
70
|
+
|
71
|
+
#ifdef HAVE_CLAPACK_H
|
72
|
+
template <>
|
73
|
+
inline int getri(const enum CBLAS_ORDER order, const int n, float* a, const int lda, const int* ipiv) {
|
74
|
+
return clapack_sgetri(order, n, a, lda, ipiv);
|
75
|
+
}
|
76
|
+
|
77
|
+
template <>
|
78
|
+
inline int getri(const enum CBLAS_ORDER order, const int n, double* a, const int lda, const int* ipiv) {
|
79
|
+
return clapack_dgetri(order, n, a, lda, ipiv);
|
80
|
+
}
|
81
|
+
|
82
|
+
template <>
|
83
|
+
inline int getri(const enum CBLAS_ORDER order, const int n, Complex64* a, const int lda, const int* ipiv) {
|
84
|
+
return clapack_cgetri(order, n, reinterpret_cast<void*>(a), lda, ipiv);
|
85
|
+
}
|
86
|
+
|
87
|
+
template <>
|
88
|
+
inline int getri(const enum CBLAS_ORDER order, const int n, Complex128* a, const int lda, const int* ipiv) {
|
89
|
+
return clapack_zgetri(order, n, reinterpret_cast<void*>(a), lda, ipiv);
|
90
|
+
}
|
91
|
+
#endif
|
92
|
+
|
93
|
+
/*
|
94
|
+
* Function signature conversion for calling LAPACK's getri functions as directly as possible.
|
95
|
+
*
|
96
|
+
* For documentation: http://www.netlib.org/lapack/double/dgetri.f
|
97
|
+
*
|
98
|
+
* This function should normally go in math.cpp, but we need it to be available to nmatrix.cpp.
|
99
|
+
*/
|
100
|
+
template <typename DType>
|
101
|
+
inline int clapack_getri(const enum CBLAS_ORDER order, const int n, void* a, const int lda, const int* ipiv) {
|
102
|
+
return getri<DType>(order, n, reinterpret_cast<DType*>(a), lda, ipiv);
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
} } // end nm::math
|
107
|
+
|
108
|
+
#endif // GETRI_H
|
@@ -0,0 +1,129 @@
|
|
1
|
+
/////////////////////////////////////////////////////////////////////
|
2
|
+
// = NMatrix
|
3
|
+
//
|
4
|
+
// A linear algebra library for scientific computation in Ruby.
|
5
|
+
// NMatrix is part of SciRuby.
|
6
|
+
//
|
7
|
+
// NMatrix was originally inspired by and derived from NArray, by
|
8
|
+
// Masahiro Tanaka: http://narray.rubyforge.org
|
9
|
+
//
|
10
|
+
// == Copyright Information
|
11
|
+
//
|
12
|
+
// SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
|
14
|
+
//
|
15
|
+
// Please see LICENSE.txt for additional copyright notices.
|
16
|
+
//
|
17
|
+
// == Contributing
|
18
|
+
//
|
19
|
+
// By contributing source code to SciRuby, you agree to be bound by
|
20
|
+
// our Contributor Agreement:
|
21
|
+
//
|
22
|
+
// * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
23
|
+
//
|
24
|
+
// == getrs.h
|
25
|
+
//
|
26
|
+
// getrs function in native C++.
|
27
|
+
//
|
28
|
+
|
29
|
+
/*
|
30
|
+
* Automatically Tuned Linear Algebra Software v3.8.4
|
31
|
+
* (C) Copyright 1999 R. Clint Whaley
|
32
|
+
*
|
33
|
+
* Redistribution and use in source and binary forms, with or without
|
34
|
+
* modification, are permitted provided that the following conditions
|
35
|
+
* are met:
|
36
|
+
* 1. Redistributions of source code must retain the above copyright
|
37
|
+
* notice, this list of conditions and the following disclaimer.
|
38
|
+
* 2. Redistributions in binary form must reproduce the above copyright
|
39
|
+
* notice, this list of conditions, and the following disclaimer in the
|
40
|
+
* documentation and/or other materials provided with the distribution.
|
41
|
+
* 3. The name of the ATLAS group or the names of its contributers may
|
42
|
+
* not be used to endorse or promote products derived from this
|
43
|
+
* software without specific written permission.
|
44
|
+
*
|
45
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
46
|
+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
47
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
48
|
+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ATLAS GROUP OR ITS CONTRIBUTORS
|
49
|
+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
50
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
51
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
52
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
53
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
54
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
55
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
56
|
+
*
|
57
|
+
*/
|
58
|
+
|
59
|
+
#ifndef GETRS_H
|
60
|
+
#define GETRS_H
|
61
|
+
|
62
|
+
extern "C" {
|
63
|
+
#if defined HAVE_CBLAS_H
|
64
|
+
#include <cblas.h>
|
65
|
+
#elif defined HAVE_ATLAS_CBLAS_H
|
66
|
+
#include <atlas/cblas.h>
|
67
|
+
#endif
|
68
|
+
}
|
69
|
+
|
70
|
+
namespace nm { namespace math {
|
71
|
+
|
72
|
+
|
73
|
+
/*
|
74
|
+
* Solves a system of linear equations A*X = B with a general NxN matrix A using the LU factorization computed by GETRF.
|
75
|
+
*
|
76
|
+
* From ATLAS 3.8.0.
|
77
|
+
*/
|
78
|
+
template <typename DType>
|
79
|
+
int getrs(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE Trans, const int N, const int NRHS, const DType* A,
|
80
|
+
const int lda, const int* ipiv, DType* B, const int ldb)
|
81
|
+
{
|
82
|
+
// enum CBLAS_DIAG Lunit, Uunit; // These aren't used. Not sure why they're declared in ATLAS' src.
|
83
|
+
|
84
|
+
if (!N || !NRHS) return 0;
|
85
|
+
|
86
|
+
const DType ONE = 1;
|
87
|
+
|
88
|
+
if (Order == CblasColMajor) {
|
89
|
+
if (Trans == CblasNoTrans) {
|
90
|
+
nm::math::laswp<DType>(NRHS, B, ldb, 0, N, ipiv, 1);
|
91
|
+
nm::math::trsm<DType>(Order, CblasLeft, CblasLower, CblasNoTrans, CblasUnit, N, NRHS, ONE, A, lda, B, ldb);
|
92
|
+
nm::math::trsm<DType>(Order, CblasLeft, CblasUpper, CblasNoTrans, CblasNonUnit, N, NRHS, ONE, A, lda, B, ldb);
|
93
|
+
} else {
|
94
|
+
nm::math::trsm<DType>(Order, CblasLeft, CblasUpper, Trans, CblasNonUnit, N, NRHS, ONE, A, lda, B, ldb);
|
95
|
+
nm::math::trsm<DType>(Order, CblasLeft, CblasLower, Trans, CblasUnit, N, NRHS, ONE, A, lda, B, ldb);
|
96
|
+
nm::math::laswp<DType>(NRHS, B, ldb, 0, N, ipiv, -1);
|
97
|
+
}
|
98
|
+
} else {
|
99
|
+
if (Trans == CblasNoTrans) {
|
100
|
+
nm::math::trsm<DType>(Order, CblasRight, CblasLower, CblasTrans, CblasNonUnit, NRHS, N, ONE, A, lda, B, ldb);
|
101
|
+
nm::math::trsm<DType>(Order, CblasRight, CblasUpper, CblasTrans, CblasUnit, NRHS, N, ONE, A, lda, B, ldb);
|
102
|
+
nm::math::laswp<DType>(NRHS, B, ldb, 0, N, ipiv, -1);
|
103
|
+
} else {
|
104
|
+
nm::math::laswp<DType>(NRHS, B, ldb, 0, N, ipiv, 1);
|
105
|
+
nm::math::trsm<DType>(Order, CblasRight, CblasUpper, CblasNoTrans, CblasUnit, NRHS, N, ONE, A, lda, B, ldb);
|
106
|
+
nm::math::trsm<DType>(Order, CblasRight, CblasLower, CblasNoTrans, CblasNonUnit, NRHS, N, ONE, A, lda, B, ldb);
|
107
|
+
}
|
108
|
+
}
|
109
|
+
return 0;
|
110
|
+
}
|
111
|
+
|
112
|
+
|
113
|
+
/*
|
114
|
+
* Function signature conversion for calling LAPACK's getrs functions as directly as possible.
|
115
|
+
*
|
116
|
+
* For documentation: http://www.netlib.org/lapack/double/dgetrs.f
|
117
|
+
*
|
118
|
+
* This function should normally go in math.cpp, but we need it to be available to nmatrix.cpp.
|
119
|
+
*/
|
120
|
+
template <typename DType>
|
121
|
+
inline int clapack_getrs(const enum CBLAS_ORDER order, const enum CBLAS_TRANSPOSE trans, const int n, const int nrhs,
|
122
|
+
const void* a, const int lda, const int* ipiv, void* b, const int ldb) {
|
123
|
+
return getrs<DType>(order, trans, n, nrhs, reinterpret_cast<const DType*>(a), lda, ipiv, reinterpret_cast<DType*>(b), ldb);
|
124
|
+
}
|
125
|
+
|
126
|
+
|
127
|
+
} } // end nm::math
|
128
|
+
|
129
|
+
#endif // GETRS_H
|