nmatrix 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/Gemfile +5 -0
- data/History.txt +97 -0
- data/Manifest.txt +34 -7
- data/README.rdoc +13 -13
- data/Rakefile +36 -26
- data/ext/nmatrix/data/data.cpp +15 -2
- data/ext/nmatrix/data/data.h +4 -0
- data/ext/nmatrix/data/ruby_object.h +5 -14
- data/ext/nmatrix/extconf.rb +3 -2
- data/ext/nmatrix/{util/math.cpp → math.cpp} +296 -6
- data/ext/nmatrix/math/asum.h +143 -0
- data/ext/nmatrix/math/geev.h +82 -0
- data/ext/nmatrix/math/gemm.h +267 -0
- data/ext/nmatrix/math/gemv.h +208 -0
- data/ext/nmatrix/math/ger.h +96 -0
- data/ext/nmatrix/math/gesdd.h +80 -0
- data/ext/nmatrix/math/gesvd.h +78 -0
- data/ext/nmatrix/math/getf2.h +86 -0
- data/ext/nmatrix/math/getrf.h +240 -0
- data/ext/nmatrix/math/getri.h +107 -0
- data/ext/nmatrix/math/getrs.h +125 -0
- data/ext/nmatrix/math/idamax.h +86 -0
- data/ext/nmatrix/{util → math}/lapack.h +60 -356
- data/ext/nmatrix/math/laswp.h +165 -0
- data/ext/nmatrix/math/long_dtype.h +52 -0
- data/ext/nmatrix/math/math.h +1154 -0
- data/ext/nmatrix/math/nrm2.h +181 -0
- data/ext/nmatrix/math/potrs.h +125 -0
- data/ext/nmatrix/math/rot.h +141 -0
- data/ext/nmatrix/math/rotg.h +115 -0
- data/ext/nmatrix/math/scal.h +73 -0
- data/ext/nmatrix/math/swap.h +73 -0
- data/ext/nmatrix/math/trsm.h +383 -0
- data/ext/nmatrix/nmatrix.cpp +176 -152
- data/ext/nmatrix/nmatrix.h +1 -2
- data/ext/nmatrix/ruby_constants.cpp +9 -4
- data/ext/nmatrix/ruby_constants.h +1 -0
- data/ext/nmatrix/storage/dense.cpp +57 -41
- data/ext/nmatrix/storage/list.cpp +52 -50
- data/ext/nmatrix/storage/storage.cpp +59 -43
- data/ext/nmatrix/storage/yale.cpp +352 -333
- data/ext/nmatrix/storage/yale.h +4 -0
- data/lib/nmatrix.rb +2 -2
- data/lib/nmatrix/blas.rb +4 -4
- data/lib/nmatrix/enumerate.rb +241 -0
- data/lib/nmatrix/lapack.rb +54 -1
- data/lib/nmatrix/math.rb +462 -0
- data/lib/nmatrix/nmatrix.rb +210 -486
- data/lib/nmatrix/nvector.rb +0 -62
- data/lib/nmatrix/rspec.rb +75 -0
- data/lib/nmatrix/shortcuts.rb +136 -108
- data/lib/nmatrix/version.rb +1 -1
- data/spec/blas_spec.rb +20 -12
- data/spec/elementwise_spec.rb +22 -13
- data/spec/io_spec.rb +1 -0
- data/spec/lapack_spec.rb +197 -0
- data/spec/nmatrix_spec.rb +39 -38
- data/spec/nvector_spec.rb +3 -9
- data/spec/rspec_monkeys.rb +29 -0
- data/spec/rspec_spec.rb +34 -0
- data/spec/shortcuts_spec.rb +14 -16
- data/spec/slice_spec.rb +242 -186
- data/spec/spec_helper.rb +19 -0
- metadata +33 -5
- data/ext/nmatrix/util/math.h +0 -2612
@@ -0,0 +1,181 @@
|
|
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 - 2013, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2013, 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
|
+
// == nrm2.h
|
25
|
+
//
|
26
|
+
// CBLAS nrm2 function
|
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 NRM2_H
|
60
|
+
# define NRM2_H
|
61
|
+
|
62
|
+
|
63
|
+
namespace nm { namespace math {
|
64
|
+
|
65
|
+
/*
|
66
|
+
* Level 1 BLAS routine which returns the 2-norm of an n-vector x.
|
67
|
+
#
|
68
|
+
* Based on input types, these are the valid return types:
|
69
|
+
* int -> int
|
70
|
+
* float -> float or double
|
71
|
+
* double -> double
|
72
|
+
* complex64 -> float or double
|
73
|
+
* complex128 -> double
|
74
|
+
* rational -> rational
|
75
|
+
*/
|
76
|
+
template <typename ReturnDType, typename DType>
|
77
|
+
ReturnDType nrm2(const int N, const DType* X, const int incX) {
|
78
|
+
const DType ONE = 1, ZERO = 0;
|
79
|
+
typename LongDType<DType>::type scale = 0, ssq = 1, absxi, temp;
|
80
|
+
|
81
|
+
|
82
|
+
if ((N < 1) || (incX < 1)) return ZERO;
|
83
|
+
else if (N == 1) return std::abs(X[0]);
|
84
|
+
|
85
|
+
for (int i = 0; i < N; ++i) {
|
86
|
+
absxi = std::abs(X[i*incX]);
|
87
|
+
if (scale < absxi) {
|
88
|
+
temp = scale / absxi;
|
89
|
+
scale = absxi;
|
90
|
+
ssq = ONE + ssq * (temp * temp);
|
91
|
+
} else {
|
92
|
+
temp = absxi / scale;
|
93
|
+
ssq += temp * temp;
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
return scale * std::sqrt( ssq );
|
98
|
+
}
|
99
|
+
|
100
|
+
|
101
|
+
#ifdef HAVE_CBLAS_H
|
102
|
+
template <>
|
103
|
+
inline float nrm2(const int N, const float* X, const int incX) {
|
104
|
+
return cblas_snrm2(N, X, incX);
|
105
|
+
}
|
106
|
+
|
107
|
+
template <>
|
108
|
+
inline double nrm2(const int N, const double* X, const int incX) {
|
109
|
+
return cblas_dnrm2(N, X, incX);
|
110
|
+
}
|
111
|
+
|
112
|
+
template <>
|
113
|
+
inline float nrm2(const int N, const Complex64* X, const int incX) {
|
114
|
+
return cblas_scnrm2(N, X, incX);
|
115
|
+
}
|
116
|
+
|
117
|
+
template <>
|
118
|
+
inline double nrm2(const int N, const Complex128* X, const int incX) {
|
119
|
+
return cblas_dznrm2(N, X, incX);
|
120
|
+
}
|
121
|
+
#else
|
122
|
+
template <typename FloatDType>
|
123
|
+
static inline void nrm2_complex_helper(const FloatDType& xr, const FloatDType& xi, double& scale, double& ssq) {
|
124
|
+
double absx = std::abs(xr);
|
125
|
+
if (scale < absx) {
|
126
|
+
double temp = scale / absx;
|
127
|
+
scale = absx;
|
128
|
+
ssq = 1.0 + ssq * (temp * temp);
|
129
|
+
} else {
|
130
|
+
double temp = absx / scale;
|
131
|
+
ssq += temp * temp;
|
132
|
+
}
|
133
|
+
|
134
|
+
absx = std::abs(xi);
|
135
|
+
if (scale < absx) {
|
136
|
+
double temp = scale / absx;
|
137
|
+
scale = absx;
|
138
|
+
ssq = 1.0 + ssq * (temp * temp);
|
139
|
+
} else {
|
140
|
+
double temp = absx / scale;
|
141
|
+
ssq += temp * temp;
|
142
|
+
}
|
143
|
+
}
|
144
|
+
|
145
|
+
template <>
|
146
|
+
float nrm2(const int N, const Complex64* X, const int incX) {
|
147
|
+
double scale = 0, ssq = 1, temp;
|
148
|
+
|
149
|
+
if ((N < 1) || (incX < 1)) return 0.0;
|
150
|
+
|
151
|
+
for (int i = 0; i < N; ++i) {
|
152
|
+
nrm2_complex_helper<float>(X[i*incX].r, X[i*incX].i, scale, temp);
|
153
|
+
}
|
154
|
+
|
155
|
+
return scale * std::sqrt( ssq );
|
156
|
+
}
|
157
|
+
|
158
|
+
template <>
|
159
|
+
double nrm2(const int N, const Complex128* X, const int incX) {
|
160
|
+
double scale = 0, ssq = 1, temp;
|
161
|
+
|
162
|
+
if ((N < 1) || (incX < 1)) return 0.0;
|
163
|
+
|
164
|
+
for (int i = 0; i < N; ++i) {
|
165
|
+
nrm2_complex_helper<double>(X[i*incX].r, X[i*incX].i, scale, temp);
|
166
|
+
}
|
167
|
+
|
168
|
+
return scale * std::sqrt( ssq );
|
169
|
+
}
|
170
|
+
#endif
|
171
|
+
|
172
|
+
template <typename ReturnDType, typename DType>
|
173
|
+
inline void cblas_nrm2(const int N, const void* X, const int incX, void* result) {
|
174
|
+
*reinterpret_cast<ReturnDType*>( result ) = nrm2<ReturnDType, DType>( N, reinterpret_cast<const DType*>(X), incX );
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
}} // end of namespace nm::math
|
180
|
+
|
181
|
+
#endif // NRM2_H
|
@@ -0,0 +1,125 @@
|
|
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 - 2013, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2013, 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 POTRS_H
|
60
|
+
#define POTRS_H
|
61
|
+
|
62
|
+
extern "C" {
|
63
|
+
#include <cblas.h>
|
64
|
+
}
|
65
|
+
|
66
|
+
namespace nm { namespace math {
|
67
|
+
|
68
|
+
/*
|
69
|
+
* Solves a system of linear equations A*X = B with a symmetric positive definite matrix A using the Cholesky factorization computed by POTRF.
|
70
|
+
*
|
71
|
+
* From ATLAS 3.8.0.
|
72
|
+
*/
|
73
|
+
template <typename DType, bool is_complex>
|
74
|
+
int potrs(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const int N, const int NRHS, const DType* A,
|
75
|
+
const int lda, DType* B, const int ldb)
|
76
|
+
{
|
77
|
+
// enum CBLAS_DIAG Lunit, Uunit; // These aren't used. Not sure why they're declared in ATLAS' src.
|
78
|
+
|
79
|
+
CBLAS_TRANSPOSE MyTrans = is_complex ? CblasConjTrans : CblasTrans;
|
80
|
+
|
81
|
+
if (!N || !NRHS) return 0;
|
82
|
+
|
83
|
+
const DType ONE = 1;
|
84
|
+
|
85
|
+
if (Order == CblasColMajor) {
|
86
|
+
if (Uplo == CblasUpper) {
|
87
|
+
nm::math::trsm<DType>(Order, CblasLeft, CblasUpper, MyTrans, CblasNonUnit, N, NRHS, ONE, A, lda, B, ldb);
|
88
|
+
nm::math::trsm<DType>(Order, CblasLeft, CblasUpper, CblasNoTrans, CblasNonUnit, N, NRHS, ONE, A, lda, B, ldb);
|
89
|
+
} else {
|
90
|
+
nm::math::trsm<DType>(Order, CblasLeft, CblasLower, CblasNoTrans, CblasNonUnit, N, NRHS, ONE, A, lda, B, ldb);
|
91
|
+
nm::math::trsm<DType>(Order, CblasLeft, CblasLower, MyTrans, CblasNonUnit, N, NRHS, ONE, A, lda, B, ldb);
|
92
|
+
}
|
93
|
+
} else {
|
94
|
+
// There's some kind of scaling operation that normally happens here in ATLAS. Not sure what it does, so we'll only
|
95
|
+
// worry if something breaks. It probably has to do with their non-templated code and doesn't apply to us.
|
96
|
+
|
97
|
+
if (Uplo == CblasUpper) {
|
98
|
+
nm::math::trsm<DType>(Order, CblasRight, CblasUpper, CblasNoTrans, CblasNonUnit, NRHS, N, ONE, A, lda, B, ldb);
|
99
|
+
nm::math::trsm<DType>(Order, CblasRight, CblasUpper, MyTrans, CblasNonUnit, NRHS, N, ONE, A, lda, B, ldb);
|
100
|
+
} else {
|
101
|
+
nm::math::trsm<DType>(Order, CblasRight, CblasLower, MyTrans, CblasNonUnit, NRHS, N, ONE, A, lda, B, ldb);
|
102
|
+
nm::math::trsm<DType>(Order, CblasRight, CblasLower, CblasNoTrans, CblasNonUnit, NRHS, N, ONE, A, lda, B, ldb);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
return 0;
|
106
|
+
}
|
107
|
+
|
108
|
+
|
109
|
+
/*
|
110
|
+
* Function signature conversion for calling LAPACK's potrs functions as directly as possible.
|
111
|
+
*
|
112
|
+
* For documentation: http://www.netlib.org/lapack/double/dpotrs.f
|
113
|
+
*
|
114
|
+
* This function should normally go in math.cpp, but we need it to be available to nmatrix.cpp.
|
115
|
+
*/
|
116
|
+
template <typename DType, bool is_complex>
|
117
|
+
inline int clapack_potrs(const enum CBLAS_ORDER order, const enum CBLAS_UPLO uplo, const int n, const int nrhs,
|
118
|
+
const void* a, const int lda, void* b, const int ldb) {
|
119
|
+
return potrs<DType,is_complex>(order, uplo, n, nrhs, reinterpret_cast<const DType*>(a), lda, reinterpret_cast<DType*>(b), ldb);
|
120
|
+
}
|
121
|
+
|
122
|
+
|
123
|
+
} } // end nm::math
|
124
|
+
|
125
|
+
#endif // POTRS_H
|
@@ -0,0 +1,141 @@
|
|
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 - 2013, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2013, 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
|
+
// == rot.h
|
25
|
+
//
|
26
|
+
// BLAS rot 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 ROT_H
|
60
|
+
# define ROT_H
|
61
|
+
|
62
|
+
namespace nm { namespace math {
|
63
|
+
|
64
|
+
// FIXME: This is not working properly for rational numbers. Do we need some kind of symbolic
|
65
|
+
// type to handle square roots?
|
66
|
+
|
67
|
+
|
68
|
+
// TODO: Test this to see if it works properly on complex. ATLAS has a separate algorithm for complex, which looks like
|
69
|
+
// TODO: it may actually be the same one.
|
70
|
+
//
|
71
|
+
// This function is called ATL_rot in ATLAS 3.8.4.
|
72
|
+
template <typename DType>
|
73
|
+
inline void rot_helper(const int N, DType* X, const int incX, DType* Y, const int incY, const DType c, const DType s) {
|
74
|
+
if (c != 1 || s != 0) {
|
75
|
+
if (incX == 1 && incY == 1) {
|
76
|
+
for (int i = 0; i != N; ++i) {
|
77
|
+
DType tmp = X[i] * c + Y[i] * s;
|
78
|
+
Y[i] = Y[i] * c - X[i] * s;
|
79
|
+
X[i] = tmp;
|
80
|
+
}
|
81
|
+
} else {
|
82
|
+
for (int i = N; i > 0; --i, Y += incY, X += incX) {
|
83
|
+
DType tmp = *X * c + *Y * s;
|
84
|
+
*Y = *Y * c - *X * s;
|
85
|
+
*X = tmp;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
/* Applies a plane rotation. From ATLAS 3.8.4. */
|
93
|
+
template <typename DType, typename CSDType>
|
94
|
+
inline void rot(const int N, DType* X, const int incX, DType* Y, const int incY, const CSDType c, const CSDType s) {
|
95
|
+
int incx = incX, incy = incY;
|
96
|
+
DType *x = X, *y = Y;
|
97
|
+
|
98
|
+
if (N > 0) {
|
99
|
+
if (incX < 0) {
|
100
|
+
if (incY < 0) { incx = -incx; incy = -incy; }
|
101
|
+
else x += -incX * (N-1);
|
102
|
+
} else if (incY < 0) {
|
103
|
+
incy = -incy;
|
104
|
+
incx = -incx;
|
105
|
+
x += (N-1) * incX;
|
106
|
+
}
|
107
|
+
rot_helper<DType>(N, x, incx, y, incy, c, s);
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
template <>
|
112
|
+
inline void rot(const int N, float* X, const int incX, float* Y, const int incY, const float c, const float s) {
|
113
|
+
cblas_srot(N, X, incX, Y, incY, (float)c, (float)s);
|
114
|
+
}
|
115
|
+
|
116
|
+
template <>
|
117
|
+
inline void rot(const int N, double* X, const int incX, double* Y, const int incY, const double c, const double s) {
|
118
|
+
cblas_drot(N, X, incX, Y, incY, c, s);
|
119
|
+
}
|
120
|
+
|
121
|
+
template <>
|
122
|
+
inline void rot(const int N, Complex64* X, const int incX, Complex64* Y, const int incY, const float c, const float s) {
|
123
|
+
cblas_csrot(N, X, incX, Y, incY, c, s);
|
124
|
+
}
|
125
|
+
|
126
|
+
template <>
|
127
|
+
inline void rot(const int N, Complex128* X, const int incX, Complex128* Y, const int incY, const double c, const double s) {
|
128
|
+
cblas_zdrot(N, X, incX, Y, incY, c, s);
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
template <typename DType, typename CSDType>
|
133
|
+
inline void cblas_rot(const int N, void* X, const int incX, void* Y, const int incY, const void* c, const void* s) {
|
134
|
+
rot<DType,CSDType>(N, reinterpret_cast<DType*>(X), incX, reinterpret_cast<DType*>(Y), incY,
|
135
|
+
*reinterpret_cast<const CSDType*>(c), *reinterpret_cast<const CSDType*>(s));
|
136
|
+
}
|
137
|
+
|
138
|
+
|
139
|
+
} } //nm::math
|
140
|
+
|
141
|
+
#endif // ROT_H
|
@@ -0,0 +1,115 @@
|
|
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 - 2013, Ruby Science Foundation
|
13
|
+
// NMatrix is Copyright (c) 2013, 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
|
+
// == rotg.h
|
25
|
+
//
|
26
|
+
// BLAS rotg 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 ROTG_H
|
60
|
+
# define ROTG_H
|
61
|
+
|
62
|
+
namespace nm { namespace math {
|
63
|
+
|
64
|
+
/* Givens plane rotation. From ATLAS 3.8.4. */
|
65
|
+
// FIXME: Not working properly for Ruby objects.
|
66
|
+
template <typename DType>
|
67
|
+
inline void rotg(DType* a, DType* b, DType* c, DType* s) {
|
68
|
+
DType aa = std::abs(*a), ab = std::abs(*b);
|
69
|
+
DType roe = aa > ab ? *a : *b;
|
70
|
+
DType scal = aa + ab;
|
71
|
+
|
72
|
+
if (scal == 0) {
|
73
|
+
*c = 1;
|
74
|
+
*s = *a = *b = 0;
|
75
|
+
} else {
|
76
|
+
DType t0 = aa / scal, t1 = ab / scal;
|
77
|
+
DType r = scal * std::sqrt(t0 * t0 + t1 * t1);
|
78
|
+
if (roe < 0) r = -r;
|
79
|
+
*c = *a / r;
|
80
|
+
*s = *b / r;
|
81
|
+
DType z = (*c != 0) ? (1 / *c) : DType(1);
|
82
|
+
*a = r;
|
83
|
+
*b = z;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
template <>
|
88
|
+
inline void rotg(float* a, float* b, float* c, float* s) {
|
89
|
+
cblas_srotg(a, b, c, s);
|
90
|
+
}
|
91
|
+
|
92
|
+
template <>
|
93
|
+
inline void rotg(double* a, double* b, double* c, double* s) {
|
94
|
+
cblas_drotg(a, b, c, s);
|
95
|
+
}
|
96
|
+
|
97
|
+
template <>
|
98
|
+
inline void rotg(Complex64* a, Complex64* b, Complex64* c, Complex64* s) {
|
99
|
+
cblas_crotg(reinterpret_cast<void*>(a), reinterpret_cast<void*>(b), reinterpret_cast<void*>(c), reinterpret_cast<void*>(s));
|
100
|
+
}
|
101
|
+
|
102
|
+
template <>
|
103
|
+
inline void rotg(Complex128* a, Complex128* b, Complex128* c, Complex128* s) {
|
104
|
+
cblas_zrotg(reinterpret_cast<void*>(a), reinterpret_cast<void*>(b), reinterpret_cast<void*>(c), reinterpret_cast<void*>(s));
|
105
|
+
}
|
106
|
+
|
107
|
+
template <typename DType>
|
108
|
+
inline void cblas_rotg(void* a, void* b, void* c, void* s) {
|
109
|
+
rotg<DType>(reinterpret_cast<DType*>(a), reinterpret_cast<DType*>(b), reinterpret_cast<DType*>(c), reinterpret_cast<DType*>(s));
|
110
|
+
}
|
111
|
+
|
112
|
+
|
113
|
+
} } //nm::math
|
114
|
+
|
115
|
+
#endif // ROTG_H
|