nmatrix 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/nmatrix/data/complex.h +20 -55
- data/ext/nmatrix/data/data.cpp +11 -44
- data/ext/nmatrix/data/data.h +174 -311
- data/ext/nmatrix/data/meta.h +1 -7
- data/ext/nmatrix/data/ruby_object.h +3 -85
- data/ext/nmatrix/extconf.rb +2 -73
- data/ext/nmatrix/math.cpp +170 -813
- data/ext/nmatrix/math/asum.h +2 -25
- data/ext/nmatrix/math/{inc.h → cblas_enums.h} +11 -22
- data/ext/nmatrix/math/cblas_templates_core.h +507 -0
- data/ext/nmatrix/math/gemm.h +2 -32
- data/ext/nmatrix/math/gemv.h +1 -35
- data/ext/nmatrix/math/getrf.h +21 -6
- data/ext/nmatrix/math/getrs.h +0 -8
- data/ext/nmatrix/math/imax.h +0 -22
- data/ext/nmatrix/math/long_dtype.h +0 -3
- data/ext/nmatrix/math/math.h +11 -337
- data/ext/nmatrix/math/nrm2.h +2 -23
- data/ext/nmatrix/math/rot.h +1 -25
- data/ext/nmatrix/math/rotg.h +4 -13
- data/ext/nmatrix/math/scal.h +0 -22
- data/ext/nmatrix/math/trsm.h +0 -55
- data/ext/nmatrix/math/util.h +148 -0
- data/ext/nmatrix/nmatrix.cpp +0 -14
- data/ext/nmatrix/nmatrix.h +92 -84
- data/ext/nmatrix/ruby_constants.cpp +0 -2
- data/ext/nmatrix/ruby_constants.h +0 -2
- data/ext/nmatrix/ruby_nmatrix.c +86 -45
- data/ext/nmatrix/storage/dense/dense.cpp +1 -7
- data/ext/nmatrix/storage/storage.h +0 -1
- data/ext/nmatrix/ttable_helper.rb +0 -6
- data/ext/nmatrix/util/io.cpp +1 -1
- data/lib/nmatrix.rb +1 -19
- data/lib/nmatrix/blas.rb +33 -11
- data/lib/nmatrix/io/market.rb +3 -3
- data/lib/nmatrix/lapack_core.rb +181 -0
- data/lib/nmatrix/lapack_plugin.rb +44 -0
- data/lib/nmatrix/math.rb +382 -131
- data/lib/nmatrix/monkeys.rb +2 -3
- data/lib/nmatrix/nmatrix.rb +166 -13
- data/lib/nmatrix/shortcuts.rb +72 -7
- data/lib/nmatrix/version.rb +2 -2
- data/spec/00_nmatrix_spec.rb +154 -5
- data/spec/02_slice_spec.rb +2 -6
- data/spec/03_nmatrix_monkeys_spec.rb +7 -1
- data/spec/blas_spec.rb +60 -33
- data/spec/homogeneous_spec.rb +10 -10
- data/spec/lapack_core_spec.rb +482 -0
- data/spec/math_spec.rb +436 -52
- data/spec/shortcuts_spec.rb +28 -4
- data/spec/spec_helper.rb +14 -2
- data/spec/utm5940.mtx +83844 -0
- metadata +49 -76
- data/.gitignore +0 -27
- data/.rspec +0 -2
- data/.travis.yml +0 -15
- data/CONTRIBUTING.md +0 -82
- data/Gemfile +0 -2
- data/History.txt +0 -677
- data/LICENSE.txt +0 -23
- data/Manifest.txt +0 -92
- data/README.rdoc +0 -150
- data/Rakefile +0 -216
- data/ext/nmatrix/data/rational.h +0 -440
- data/ext/nmatrix/math/geev.h +0 -82
- data/ext/nmatrix/math/ger.h +0 -96
- data/ext/nmatrix/math/gesdd.h +0 -80
- data/ext/nmatrix/math/gesvd.h +0 -78
- data/ext/nmatrix/math/getf2.h +0 -86
- data/ext/nmatrix/math/getri.h +0 -108
- data/ext/nmatrix/math/potrs.h +0 -129
- data/ext/nmatrix/math/swap.h +0 -52
- data/lib/nmatrix/lapack.rb +0 -240
- data/nmatrix.gemspec +0 -55
- data/scripts/mac-brew-gcc.sh +0 -50
- data/scripts/mac-mavericks-brew-gcc.sh +0 -22
- data/spec/lapack_spec.rb +0 -459
data/ext/nmatrix/math/potrs.h
DELETED
@@ -1,129 +0,0 @@
|
|
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 POTRS_H
|
60
|
-
#define POTRS_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
|
-
* Solves a system of linear equations A*X = B with a symmetric positive definite matrix A using the Cholesky factorization computed by POTRF.
|
74
|
-
*
|
75
|
-
* From ATLAS 3.8.0.
|
76
|
-
*/
|
77
|
-
template <typename DType, bool is_complex>
|
78
|
-
int potrs(const enum CBLAS_ORDER Order, const enum CBLAS_UPLO Uplo, const int N, const int NRHS, const DType* A,
|
79
|
-
const int lda, DType* B, const int ldb)
|
80
|
-
{
|
81
|
-
// enum CBLAS_DIAG Lunit, Uunit; // These aren't used. Not sure why they're declared in ATLAS' src.
|
82
|
-
|
83
|
-
CBLAS_TRANSPOSE MyTrans = is_complex ? CblasConjTrans : CblasTrans;
|
84
|
-
|
85
|
-
if (!N || !NRHS) return 0;
|
86
|
-
|
87
|
-
const DType ONE = 1;
|
88
|
-
|
89
|
-
if (Order == CblasColMajor) {
|
90
|
-
if (Uplo == CblasUpper) {
|
91
|
-
nm::math::trsm<DType>(Order, CblasLeft, CblasUpper, MyTrans, CblasNonUnit, 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, CblasLower, CblasNoTrans, CblasNonUnit, N, NRHS, ONE, A, lda, B, ldb);
|
95
|
-
nm::math::trsm<DType>(Order, CblasLeft, CblasLower, MyTrans, CblasNonUnit, N, NRHS, ONE, A, lda, B, ldb);
|
96
|
-
}
|
97
|
-
} else {
|
98
|
-
// There's some kind of scaling operation that normally happens here in ATLAS. Not sure what it does, so we'll only
|
99
|
-
// worry if something breaks. It probably has to do with their non-templated code and doesn't apply to us.
|
100
|
-
|
101
|
-
if (Uplo == CblasUpper) {
|
102
|
-
nm::math::trsm<DType>(Order, CblasRight, CblasUpper, CblasNoTrans, CblasNonUnit, NRHS, N, ONE, A, lda, B, ldb);
|
103
|
-
nm::math::trsm<DType>(Order, CblasRight, CblasUpper, MyTrans, CblasNonUnit, NRHS, N, ONE, A, lda, B, ldb);
|
104
|
-
} else {
|
105
|
-
nm::math::trsm<DType>(Order, CblasRight, CblasLower, MyTrans, CblasNonUnit, 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 potrs functions as directly as possible.
|
115
|
-
*
|
116
|
-
* For documentation: http://www.netlib.org/lapack/double/dpotrs.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, bool is_complex>
|
121
|
-
inline int clapack_potrs(const enum CBLAS_ORDER order, const enum CBLAS_UPLO uplo, const int n, const int nrhs,
|
122
|
-
const void* a, const int lda, void* b, const int ldb) {
|
123
|
-
return potrs<DType,is_complex>(order, uplo, n, nrhs, reinterpret_cast<const DType*>(a), lda, reinterpret_cast<DType*>(b), ldb);
|
124
|
-
}
|
125
|
-
|
126
|
-
|
127
|
-
} } // end nm::math
|
128
|
-
|
129
|
-
#endif // POTRS_H
|
data/ext/nmatrix/math/swap.h
DELETED
@@ -1,52 +0,0 @@
|
|
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
|
-
// == swap.h
|
25
|
-
//
|
26
|
-
// BLAS level 2 swap function in native C++.
|
27
|
-
//
|
28
|
-
|
29
|
-
#ifndef SWAP_H
|
30
|
-
#define SWAP_H
|
31
|
-
|
32
|
-
namespace nm { namespace math {
|
33
|
-
// This is the old BLAS version of this function. ATLAS has an optimized version, but
|
34
|
-
// it's going to be tough to translate.
|
35
|
-
template <typename DType>
|
36
|
-
static void swap(const int N, DType* X, const int incX, DType* Y, const int incY) {
|
37
|
-
if (N > 0) {
|
38
|
-
int ix = 0, iy = 0;
|
39
|
-
for (int i = 0; i < N; ++i) {
|
40
|
-
DType temp = X[i];
|
41
|
-
X[i] = Y[i];
|
42
|
-
Y[i] = temp;
|
43
|
-
|
44
|
-
ix += incX;
|
45
|
-
iy += incY;
|
46
|
-
}
|
47
|
-
}
|
48
|
-
}
|
49
|
-
|
50
|
-
}} // end nm::math
|
51
|
-
|
52
|
-
#endif
|
data/lib/nmatrix/lapack.rb
DELETED
@@ -1,240 +0,0 @@
|
|
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
|
-
# == lapack.rb
|
25
|
-
#
|
26
|
-
# This file contains LAPACK functions accessible in their C versions,
|
27
|
-
# e.g., NMatrix::LAPACK::clapack_func. There are some exceptions,
|
28
|
-
# such as clapack_gesv, which is implemented in Ruby but calls
|
29
|
-
# clapack_getrf and clapack_getrs.
|
30
|
-
#
|
31
|
-
# Note: most of these functions are borrowed from ATLAS, which is available under a BSD-
|
32
|
-
# style license.
|
33
|
-
#++
|
34
|
-
|
35
|
-
class NMatrix
|
36
|
-
module LAPACK
|
37
|
-
class << self
|
38
|
-
#
|
39
|
-
# call-seq:
|
40
|
-
# clapack_gesv(order, n, nrhs, a, lda, ipiv, b, ldb) -> NMatrix
|
41
|
-
#
|
42
|
-
# Computes the solution to a system of linear equations
|
43
|
-
# A * X = B,
|
44
|
-
# where A is an N-by-N matrix and X and B are N-by-NRHS matrices.
|
45
|
-
#
|
46
|
-
# The LU factorization used to factor A is dependent on the +order+
|
47
|
-
# parameter, as detailed in the leading comments of clapack_getrf.
|
48
|
-
#
|
49
|
-
# The factored form of A is then used solve the system of equations
|
50
|
-
# A * X = B.
|
51
|
-
#
|
52
|
-
# A is overwritten with the appropriate LU factorization, and B, which
|
53
|
-
# contains B on input, is overwritten with the solution X on output.
|
54
|
-
#
|
55
|
-
# From ATLAS 3.8.0.
|
56
|
-
#
|
57
|
-
# Note: Because this function is implemented in Ruby, the ATLAS lib
|
58
|
-
# version is never called! For float32, float64, complex64, and
|
59
|
-
# complex128, the ATLAS lib versions of getrf and getrs *will* be called.
|
60
|
-
#
|
61
|
-
# * *Arguments* :
|
62
|
-
# - +order+ ->
|
63
|
-
# - +n+ ->
|
64
|
-
# - +nrhs+ ->
|
65
|
-
# - +a+ ->
|
66
|
-
# - +lda+ ->
|
67
|
-
# - +b+ ->
|
68
|
-
# - +ldb+ ->
|
69
|
-
# - +ipiv+ -> A pivot array (if nil, one will be generated with +clapack_getrf+)
|
70
|
-
# * *Returns* :
|
71
|
-
# -
|
72
|
-
# * *Raises* :
|
73
|
-
# - ++ ->
|
74
|
-
#
|
75
|
-
def clapack_gesv(order, n, nrhs, a, lda, b, ldb, ipiv=nil)
|
76
|
-
ipiv ||= clapack_getrf(order, n, n, a, lda)
|
77
|
-
clapack_getrs(order, :no_transpose, n, nrhs, a, lda, ipiv, b, ldb)
|
78
|
-
end
|
79
|
-
|
80
|
-
#
|
81
|
-
# call-seq:
|
82
|
-
# clapack_posv(order, uplo, n ,nrhs, a, lda, b, ldb) -> ...
|
83
|
-
#
|
84
|
-
# TODO Complete this description.
|
85
|
-
#
|
86
|
-
# Computes the solution to a real system of linear equations
|
87
|
-
# A * X = B,
|
88
|
-
# where A is an N-by-N symmetric positive definite matrix and X and B
|
89
|
-
# are N-by-NRHS matrices.
|
90
|
-
#
|
91
|
-
# The Cholesky decomposition is used to factor A as
|
92
|
-
# A = U**T* U, if UPLO = 'U', or
|
93
|
-
# A = L * L**T, if UPLO = 'L',
|
94
|
-
# where U is an upper triangular matrix and L is a lower triangular
|
95
|
-
# matrix. The factored form of A is then used to solve the system of
|
96
|
-
# equations A * X = B.
|
97
|
-
#
|
98
|
-
# From ATLAS 3.8.0.
|
99
|
-
#
|
100
|
-
# Note: Because this function is implemented in Ruby, the ATLAS lib
|
101
|
-
# version is never called! For float32, float64, complex64, and
|
102
|
-
# complex128, the ATLAS lib versions of potrf and potrs *will* be called.
|
103
|
-
#
|
104
|
-
# * *Arguments* :
|
105
|
-
# - +order+ ->
|
106
|
-
# - +uplo+ ->
|
107
|
-
# - +n+ ->
|
108
|
-
# - +nrhs+ ->
|
109
|
-
# - +a+ ->
|
110
|
-
# - +lda+ ->
|
111
|
-
# - +b+ ->
|
112
|
-
# - +ldb+ ->
|
113
|
-
# * *Returns* :
|
114
|
-
# -
|
115
|
-
# * *Raises* :
|
116
|
-
# - ++ ->
|
117
|
-
#
|
118
|
-
def clapack_posv(order, uplo, n, nrhs, a, lda, b, ldb)
|
119
|
-
clapack_potrf(order, uplo, n, a, lda)
|
120
|
-
clapack_potrs(order, uplo, n, nrhs, a, lda, b, ldb)
|
121
|
-
end
|
122
|
-
|
123
|
-
# laswp(matrix, ipiv) -> NMatrix
|
124
|
-
#
|
125
|
-
# Permute the columns of a matrix (in-place) according to the Array +ipiv+.
|
126
|
-
#
|
127
|
-
def laswp(matrix, ipiv)
|
128
|
-
raise(ArgumentError, "expected NMatrix for argument 0") unless matrix.is_a?(NMatrix)
|
129
|
-
raise(StorageTypeError, "LAPACK functions only work on :dense NMatrix instances") unless matrix.stype == :dense
|
130
|
-
raise(ArgumentError, "expected Array ipiv to have no more entries than NMatrix a has columns") if ipiv.size > matrix.shape[1]
|
131
|
-
|
132
|
-
clapack_laswp(matrix.shape[0], matrix, matrix.shape[1], 0, ipiv.size-1, ipiv, 1)
|
133
|
-
end
|
134
|
-
|
135
|
-
def alloc_svd_result(matrix)
|
136
|
-
[
|
137
|
-
NMatrix.new(matrix.shape[0], dtype: matrix.dtype),
|
138
|
-
NMatrix.new([matrix.shape[0],1], dtype: matrix.dtype),
|
139
|
-
NMatrix.new(matrix.shape[1], dtype: matrix.dtype)
|
140
|
-
]
|
141
|
-
end
|
142
|
-
|
143
|
-
|
144
|
-
#
|
145
|
-
# call-seq:
|
146
|
-
# gesvd(matrix) -> [u, sigma, v_transpose]
|
147
|
-
# gesvd(matrix) -> [u, sigma, v_conjugate_transpose] # complex
|
148
|
-
#
|
149
|
-
# Compute the singular value decomposition of a matrix using LAPACK's GESVD function.
|
150
|
-
#
|
151
|
-
# Optionally accepts a +workspace_size+ parameter, which will be honored only if it is larger than what LAPACK
|
152
|
-
# requires.
|
153
|
-
#
|
154
|
-
def gesvd(matrix, workspace_size=1)
|
155
|
-
result = alloc_svd_result(matrix)
|
156
|
-
NMatrix::LAPACK::lapack_gesvd(:a, :a, matrix.shape[0], matrix.shape[1], matrix, matrix.shape[0], result[1], result[0], matrix.shape[0], result[2], matrix.shape[1], workspace_size)
|
157
|
-
result
|
158
|
-
end
|
159
|
-
|
160
|
-
#
|
161
|
-
# call-seq:
|
162
|
-
# gesdd(matrix) -> [u, sigma, v_transpose]
|
163
|
-
# gesdd(matrix) -> [u, sigma, v_conjugate_transpose] # complex
|
164
|
-
#
|
165
|
-
# Compute the singular value decomposition of a matrix using LAPACK's GESDD function. This uses a divide-and-conquer
|
166
|
-
# strategy. See also #gesvd.
|
167
|
-
#
|
168
|
-
# Optionally accepts a +workspace_size+ parameter, which will be honored only if it is larger than what LAPACK
|
169
|
-
# requires.
|
170
|
-
#
|
171
|
-
def gesdd(matrix, workspace_size=nil)
|
172
|
-
min_workspace_size = matrix.shape.min * (6 + 4 * matrix.shape.min) + matrix.shape.max
|
173
|
-
workspace_size = min_workspace_size if workspace_size.nil? || workspace_size < min_workspace_size
|
174
|
-
result = alloc_svd_result(matrix)
|
175
|
-
NMatrix::LAPACK::lapack_gesdd(:a, matrix.shape[0], matrix.shape[1], matrix, matrix.shape[0], result[1], result[0], matrix.shape[0], result[2], matrix.shape[1], workspace_size)
|
176
|
-
result
|
177
|
-
end
|
178
|
-
|
179
|
-
def alloc_evd_result(matrix)
|
180
|
-
[
|
181
|
-
NMatrix.new(matrix.shape[0], dtype: matrix.dtype),
|
182
|
-
NMatrix.new(matrix.shape[0], dtype: matrix.dtype),
|
183
|
-
NMatrix.new([matrix.shape[0],1], dtype: matrix.dtype),
|
184
|
-
NMatrix.new([matrix.shape[0],1], dtype: matrix.dtype),
|
185
|
-
]
|
186
|
-
end
|
187
|
-
|
188
|
-
|
189
|
-
#
|
190
|
-
# call-seq:
|
191
|
-
# geev(matrix) -> [eigenvalues, left_eigenvectors, right_eigenvectors]
|
192
|
-
# geev(matrix, :left) -> [eigenvalues, left_eigenvectors]
|
193
|
-
# geev(matrix, :right) -> [eigenvalues, right_eigenvectors]
|
194
|
-
#
|
195
|
-
# Perform eigenvalue decomposition on a matrix using LAPACK's xGEEV function.
|
196
|
-
#
|
197
|
-
def geev(matrix, which=:both)
|
198
|
-
jobvl = (which == :both || which == :left) ? :left : false
|
199
|
-
jobvr = (which == :both || which == :right) ? :right : false
|
200
|
-
|
201
|
-
# Copy the matrix so it doesn't get overwritten.
|
202
|
-
temporary_matrix = matrix.clone
|
203
|
-
|
204
|
-
# Outputs
|
205
|
-
real_eigenvalues = NMatrix.new([matrix.shape[0], 1], dtype: matrix.dtype)
|
206
|
-
imag_eigenvalues = NMatrix.new([matrix.shape[0], 1], dtype: matrix.dtype)
|
207
|
-
|
208
|
-
left_output = jobvl == :left ? matrix.clone_structure : NMatrix.new(1, dtype: matrix.dtype)
|
209
|
-
right_output = jobvr == :right ? matrix.clone_structure : NMatrix.new(1, dtype: matrix.dtype)
|
210
|
-
|
211
|
-
NMatrix::LAPACK::lapack_geev(jobvl, # compute left eigenvectors of A?
|
212
|
-
jobvr, # compute right eigenvectors of A? (left eigenvectors of A**T)
|
213
|
-
matrix.shape[0], # order of the matrix
|
214
|
-
temporary_matrix,# input matrix (used as work)
|
215
|
-
matrix.shape[0], # leading dimension of matrix
|
216
|
-
real_eigenvalues,# real part of computed eigenvalues
|
217
|
-
imag_eigenvalues,# imag part of computed eigenvalues
|
218
|
-
left_output, # left eigenvectors, if applicable
|
219
|
-
left_output.shape[0], # leading dimension of left_output
|
220
|
-
right_output, # right eigenvectors, if applicable
|
221
|
-
right_output.shape[0], # leading dimension of right_output
|
222
|
-
2*matrix.shape[0])
|
223
|
-
|
224
|
-
# Put the real and imaginary parts together
|
225
|
-
eigenvalues = real_eigenvalues.to_a.flatten.map.with_index do |real,i|
|
226
|
-
imag_eigenvalues[i] != 0 ? Complex(real, imag_eigenvalues[i]) : real
|
227
|
-
end
|
228
|
-
|
229
|
-
if which == :both
|
230
|
-
return [eigenvalues, left_output.transpose, right_output.transpose]
|
231
|
-
elsif which == :left
|
232
|
-
return [eigenvalues, left_output.transpose]
|
233
|
-
else
|
234
|
-
return [eigenvalues, right_output]
|
235
|
-
end
|
236
|
-
end
|
237
|
-
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|
data/nmatrix.gemspec
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
lib = File.expand_path('../lib/', __FILE__)
|
2
|
-
$:.unshift lib unless $:.include?(lib)
|
3
|
-
|
4
|
-
require 'nmatrix/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |gem|
|
7
|
-
gem.name = "nmatrix"
|
8
|
-
gem.version = NMatrix::VERSION::STRING
|
9
|
-
gem.summary = "NMatrix is a linear algebra library for Ruby"
|
10
|
-
gem.description = "NMatrix is a linear algebra library for Ruby, written mostly in C and C++."
|
11
|
-
gem.homepage = 'http://sciruby.com'
|
12
|
-
gem.authors = ['John Woods', 'Chris Wailes', 'Aleksey Timin']
|
13
|
-
gem.email = ['john.o.woods@gmail.com']
|
14
|
-
gem.license = 'BSD 3-clause'
|
15
|
-
gem.post_install_message = <<-EOF
|
16
|
-
***********************************************************
|
17
|
-
Welcome to SciRuby: Tools for Scientific Computing in Ruby!
|
18
|
-
|
19
|
-
NMatrix requires a C compiler, and has been tested only
|
20
|
-
with GCC 4.6+. We are happy to accept contributions
|
21
|
-
which improve the portability of this project.
|
22
|
-
|
23
|
-
Also required is ATLAS. Most Linux distributions and Mac
|
24
|
-
versions include ATLAS, but you may wish to compile it
|
25
|
-
yourself. The Ubuntu/Debian apt package for ATLAS WILL
|
26
|
-
NOT WORK with NMatrix if LAPACK is also installed.
|
27
|
-
|
28
|
-
More explicit instructions for NMatrix and SciRuby should
|
29
|
-
be available on the SciRuby website, sciruby.com, or
|
30
|
-
through our mailing list (which can be found on our web-
|
31
|
-
site).
|
32
|
-
|
33
|
-
Thanks for trying out NMatrix! Happy coding!
|
34
|
-
|
35
|
-
***********************************************************
|
36
|
-
EOF
|
37
|
-
|
38
|
-
gem.files = `git ls-files`.split("\n")
|
39
|
-
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
40
|
-
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
41
|
-
gem.extensions = ['ext/nmatrix/extconf.rb']
|
42
|
-
gem.require_paths = ["lib"]
|
43
|
-
|
44
|
-
gem.required_ruby_version = '>= 1.9'
|
45
|
-
|
46
|
-
gem.add_dependency 'rdoc', '~>4.0', '>=4.0.1'
|
47
|
-
gem.add_dependency 'packable', '~> 1.3', '>= 1.3.5'
|
48
|
-
gem.add_development_dependency 'rake', '~>10.3'
|
49
|
-
gem.add_development_dependency 'bundler', '~>1.6'
|
50
|
-
gem.add_development_dependency 'rspec', '~>2.14'
|
51
|
-
gem.add_development_dependency 'rspec-longrun', '~>1.0'
|
52
|
-
gem.add_development_dependency 'pry', '~>0.10'
|
53
|
-
gem.add_development_dependency 'rake-compiler', '~>0.8'
|
54
|
-
end
|
55
|
-
|