pnmatrix 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/nmatrix/binary_format.txt +53 -0
- data/ext/nmatrix/data/complex.h +388 -0
- data/ext/nmatrix/data/data.cpp +274 -0
- data/ext/nmatrix/data/data.h +651 -0
- data/ext/nmatrix/data/meta.h +64 -0
- data/ext/nmatrix/data/ruby_object.h +386 -0
- data/ext/nmatrix/extconf.rb +70 -0
- data/ext/nmatrix/math/asum.h +99 -0
- data/ext/nmatrix/math/cblas_enums.h +36 -0
- data/ext/nmatrix/math/cblas_templates_core.h +507 -0
- data/ext/nmatrix/math/gemm.h +241 -0
- data/ext/nmatrix/math/gemv.h +178 -0
- data/ext/nmatrix/math/getrf.h +255 -0
- data/ext/nmatrix/math/getrs.h +121 -0
- data/ext/nmatrix/math/imax.h +82 -0
- data/ext/nmatrix/math/laswp.h +165 -0
- data/ext/nmatrix/math/long_dtype.h +62 -0
- data/ext/nmatrix/math/magnitude.h +54 -0
- data/ext/nmatrix/math/math.h +751 -0
- data/ext/nmatrix/math/nrm2.h +165 -0
- data/ext/nmatrix/math/rot.h +117 -0
- data/ext/nmatrix/math/rotg.h +106 -0
- data/ext/nmatrix/math/scal.h +71 -0
- data/ext/nmatrix/math/trsm.h +336 -0
- data/ext/nmatrix/math/util.h +162 -0
- data/ext/nmatrix/math.cpp +1368 -0
- data/ext/nmatrix/nm_memory.h +60 -0
- data/ext/nmatrix/nmatrix.cpp +285 -0
- data/ext/nmatrix/nmatrix.h +476 -0
- data/ext/nmatrix/ruby_constants.cpp +151 -0
- data/ext/nmatrix/ruby_constants.h +106 -0
- data/ext/nmatrix/ruby_nmatrix.c +3130 -0
- data/ext/nmatrix/storage/common.cpp +77 -0
- data/ext/nmatrix/storage/common.h +183 -0
- data/ext/nmatrix/storage/dense/dense.cpp +1096 -0
- data/ext/nmatrix/storage/dense/dense.h +129 -0
- data/ext/nmatrix/storage/list/list.cpp +1628 -0
- data/ext/nmatrix/storage/list/list.h +138 -0
- data/ext/nmatrix/storage/storage.cpp +730 -0
- data/ext/nmatrix/storage/storage.h +99 -0
- data/ext/nmatrix/storage/yale/class.h +1139 -0
- data/ext/nmatrix/storage/yale/iterators/base.h +143 -0
- data/ext/nmatrix/storage/yale/iterators/iterator.h +131 -0
- data/ext/nmatrix/storage/yale/iterators/row.h +450 -0
- data/ext/nmatrix/storage/yale/iterators/row_stored.h +140 -0
- data/ext/nmatrix/storage/yale/iterators/row_stored_nd.h +169 -0
- data/ext/nmatrix/storage/yale/iterators/stored_diagonal.h +124 -0
- data/ext/nmatrix/storage/yale/math/transpose.h +110 -0
- data/ext/nmatrix/storage/yale/yale.cpp +2074 -0
- data/ext/nmatrix/storage/yale/yale.h +203 -0
- data/ext/nmatrix/types.h +55 -0
- data/ext/nmatrix/util/io.cpp +279 -0
- data/ext/nmatrix/util/io.h +115 -0
- data/ext/nmatrix/util/sl_list.cpp +627 -0
- data/ext/nmatrix/util/sl_list.h +144 -0
- data/ext/nmatrix/util/util.h +78 -0
- data/lib/nmatrix/blas.rb +378 -0
- data/lib/nmatrix/cruby/math.rb +744 -0
- data/lib/nmatrix/enumerate.rb +253 -0
- data/lib/nmatrix/homogeneous.rb +241 -0
- data/lib/nmatrix/io/fortran_format.rb +138 -0
- data/lib/nmatrix/io/harwell_boeing.rb +221 -0
- data/lib/nmatrix/io/market.rb +263 -0
- data/lib/nmatrix/io/point_cloud.rb +189 -0
- data/lib/nmatrix/jruby/decomposition.rb +24 -0
- data/lib/nmatrix/jruby/enumerable.rb +13 -0
- data/lib/nmatrix/jruby/error.rb +4 -0
- data/lib/nmatrix/jruby/math.rb +501 -0
- data/lib/nmatrix/jruby/nmatrix_java.rb +840 -0
- data/lib/nmatrix/jruby/operators.rb +283 -0
- data/lib/nmatrix/jruby/slice.rb +264 -0
- data/lib/nmatrix/lapack_core.rb +181 -0
- data/lib/nmatrix/lapack_plugin.rb +44 -0
- data/lib/nmatrix/math.rb +953 -0
- data/lib/nmatrix/mkmf.rb +100 -0
- data/lib/nmatrix/monkeys.rb +137 -0
- data/lib/nmatrix/nmatrix.rb +1172 -0
- data/lib/nmatrix/rspec.rb +75 -0
- data/lib/nmatrix/shortcuts.rb +1163 -0
- data/lib/nmatrix/version.rb +39 -0
- data/lib/nmatrix/yale_functions.rb +118 -0
- data/lib/nmatrix.rb +28 -0
- data/spec/00_nmatrix_spec.rb +892 -0
- data/spec/01_enum_spec.rb +196 -0
- data/spec/02_slice_spec.rb +407 -0
- data/spec/03_nmatrix_monkeys_spec.rb +80 -0
- data/spec/2x2_dense_double.mat +0 -0
- data/spec/4x4_sparse.mat +0 -0
- data/spec/4x5_dense.mat +0 -0
- data/spec/blas_spec.rb +215 -0
- data/spec/elementwise_spec.rb +311 -0
- data/spec/homogeneous_spec.rb +100 -0
- data/spec/io/fortran_format_spec.rb +88 -0
- data/spec/io/harwell_boeing_spec.rb +98 -0
- data/spec/io/test.rua +9 -0
- data/spec/io_spec.rb +159 -0
- data/spec/lapack_core_spec.rb +482 -0
- data/spec/leakcheck.rb +16 -0
- data/spec/math_spec.rb +1363 -0
- data/spec/nmatrix_yale_resize_test_associations.yaml +2802 -0
- data/spec/nmatrix_yale_spec.rb +286 -0
- data/spec/rspec_monkeys.rb +56 -0
- data/spec/rspec_spec.rb +35 -0
- data/spec/shortcuts_spec.rb +474 -0
- data/spec/slice_set_spec.rb +162 -0
- data/spec/spec_helper.rb +172 -0
- data/spec/stat_spec.rb +214 -0
- data/spec/test.pcd +20 -0
- data/spec/utm5940.mtx +83844 -0
- metadata +295 -0
@@ -0,0 +1,144 @@
|
|
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
|
+
// == sl_list.h
|
25
|
+
//
|
26
|
+
// Singly-linked list implementation used for List Storage.
|
27
|
+
|
28
|
+
#ifndef SL_LIST_H
|
29
|
+
#define SL_LIST_H
|
30
|
+
|
31
|
+
|
32
|
+
/*
|
33
|
+
* Standard Includes
|
34
|
+
*/
|
35
|
+
|
36
|
+
#include <ruby.h>
|
37
|
+
#include <type_traits>
|
38
|
+
#include <cstdlib>
|
39
|
+
|
40
|
+
/*
|
41
|
+
* Project Includes
|
42
|
+
*/
|
43
|
+
|
44
|
+
#include "types.h"
|
45
|
+
|
46
|
+
#include "data/data.h"
|
47
|
+
|
48
|
+
#include "nmatrix.h"
|
49
|
+
|
50
|
+
namespace nm { namespace list {
|
51
|
+
|
52
|
+
/*
|
53
|
+
* Macros
|
54
|
+
*/
|
55
|
+
|
56
|
+
/*
|
57
|
+
* Types
|
58
|
+
*/
|
59
|
+
|
60
|
+
/*
|
61
|
+
* Data
|
62
|
+
*/
|
63
|
+
|
64
|
+
|
65
|
+
/*
|
66
|
+
* Functions
|
67
|
+
*/
|
68
|
+
|
69
|
+
////////////////
|
70
|
+
// Lifecycle //
|
71
|
+
///////////////
|
72
|
+
|
73
|
+
LIST* create(void);
|
74
|
+
void del(LIST* list, size_t recursions);
|
75
|
+
void mark(LIST* list, size_t recursions);
|
76
|
+
|
77
|
+
///////////////
|
78
|
+
// Accessors //
|
79
|
+
///////////////
|
80
|
+
|
81
|
+
NODE* insert(LIST* list, bool replace, size_t key, void* val);
|
82
|
+
NODE* insert_copy(LIST *list, bool replace, size_t key, void *val, size_t size);
|
83
|
+
NODE* insert_first_node(LIST* list, size_t key, void* val, size_t val_size);
|
84
|
+
NODE* insert_first_list(LIST* list, size_t key, LIST* l);
|
85
|
+
NODE* insert_after(NODE* node, size_t key, void* val);
|
86
|
+
NODE* replace_insert_after(NODE* node, size_t key, void* val, bool copy, size_t copy_size);
|
87
|
+
void* remove(LIST* list, size_t key);
|
88
|
+
void* remove_by_node(LIST* list, NODE* prev, NODE* rm);
|
89
|
+
bool remove_recursive(LIST* list, const size_t* coords, const size_t* offset, const size_t* lengths, size_t r, const size_t& dim);
|
90
|
+
bool node_is_within_slice(NODE* n, size_t coord, size_t len);
|
91
|
+
|
92
|
+
template <typename Type>
|
93
|
+
inline NODE* insert_helper(LIST* list, NODE* node, size_t key, Type val) {
|
94
|
+
Type* val_mem = NM_ALLOC(Type);
|
95
|
+
*val_mem = val;
|
96
|
+
|
97
|
+
if (node == NULL) {
|
98
|
+
return insert(list, false, key, val_mem);
|
99
|
+
|
100
|
+
} else {
|
101
|
+
return insert_after(node, key, val_mem);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
template <typename Type>
|
106
|
+
inline NODE* insert_helper(LIST* list, NODE* node, size_t key, Type* ptr) {
|
107
|
+
if (node == NULL) {
|
108
|
+
return insert(list, false, key, ptr);
|
109
|
+
|
110
|
+
} else {
|
111
|
+
return insert_after(node, key, ptr);
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
///////////
|
116
|
+
// Tests //
|
117
|
+
///////////
|
118
|
+
|
119
|
+
|
120
|
+
/////////////
|
121
|
+
// Utility //
|
122
|
+
/////////////
|
123
|
+
|
124
|
+
NODE* find(LIST* list, size_t key);
|
125
|
+
NODE* find_preceding_from_node(NODE* prev, size_t key);
|
126
|
+
NODE* find_preceding_from_list(LIST* l, size_t key);
|
127
|
+
NODE* find_nearest(LIST* list, size_t key);
|
128
|
+
NODE* find_nearest_from(NODE* prev, size_t key);
|
129
|
+
|
130
|
+
/////////////////////////
|
131
|
+
// Copying and Casting //
|
132
|
+
/////////////////////////
|
133
|
+
|
134
|
+
template <typename LDType, typename RDType>
|
135
|
+
void cast_copy_contents(LIST* lhs, const LIST* rhs, size_t recursions);
|
136
|
+
|
137
|
+
}} // end of namespace nm::list
|
138
|
+
|
139
|
+
extern "C" {
|
140
|
+
void nm_list_cast_copy_contents(LIST* lhs, const LIST* rhs, nm::dtype_t lhs_dtype, nm::dtype_t rhs_dtype, size_t recursions);
|
141
|
+
VALUE nm_list_copy_to_hash(const LIST* l, const nm::dtype_t dtype, size_t recursions, VALUE default_value);
|
142
|
+
} // end of extern "C" block
|
143
|
+
|
144
|
+
#endif // SL_LIST_H
|
@@ -0,0 +1,78 @@
|
|
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
|
+
// == util.h
|
25
|
+
//
|
26
|
+
// Header file for utility functions and data.
|
27
|
+
|
28
|
+
#ifndef UTIL_H
|
29
|
+
#define UTIL_H
|
30
|
+
|
31
|
+
/*
|
32
|
+
* Standard Includes
|
33
|
+
*/
|
34
|
+
|
35
|
+
/*
|
36
|
+
* Project Includes
|
37
|
+
*/
|
38
|
+
|
39
|
+
#include "types.h"
|
40
|
+
|
41
|
+
/*
|
42
|
+
* Macros
|
43
|
+
*/
|
44
|
+
|
45
|
+
/*
|
46
|
+
* Types
|
47
|
+
*/
|
48
|
+
|
49
|
+
/*
|
50
|
+
* Data
|
51
|
+
*/
|
52
|
+
|
53
|
+
/*
|
54
|
+
* Functions
|
55
|
+
*/
|
56
|
+
namespace nm {
|
57
|
+
template <typename Type>
|
58
|
+
inline Type gcf(Type x, Type y) {
|
59
|
+
Type t;
|
60
|
+
|
61
|
+
if (x < 0) x = -x;
|
62
|
+
if (y < 0) y = -y;
|
63
|
+
|
64
|
+
if (x == 0) return y;
|
65
|
+
if (y == 0) return x;
|
66
|
+
|
67
|
+
while (x > 0) {
|
68
|
+
t = x;
|
69
|
+
x = y % x;
|
70
|
+
y = t;
|
71
|
+
}
|
72
|
+
|
73
|
+
return y;
|
74
|
+
}
|
75
|
+
} // end of namespace nm
|
76
|
+
|
77
|
+
|
78
|
+
#endif // UTIL_H
|
data/lib/nmatrix/blas.rb
ADDED
@@ -0,0 +1,378 @@
|
|
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 - 2016, Ruby Science Foundation
|
13
|
+
# NMatrix is Copyright (c) 2012 - 2016, 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
|
+
# == blas.rb
|
25
|
+
#
|
26
|
+
# This file contains the safer accessors for the BLAS functions
|
27
|
+
# supported by NMatrix.
|
28
|
+
#++
|
29
|
+
|
30
|
+
module NMatrix::BLAS
|
31
|
+
|
32
|
+
#Add functions from C extension to main BLAS module
|
33
|
+
class << self
|
34
|
+
if jruby?
|
35
|
+
# BLAS functionalities for JRuby need to be implemented
|
36
|
+
else
|
37
|
+
NMatrix::Internal::BLAS.singleton_methods.each do |m|
|
38
|
+
define_method m, NMatrix::Internal::BLAS.method(m).to_proc
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class << self
|
44
|
+
#
|
45
|
+
# call-seq:
|
46
|
+
# gemm(a, b) -> NMatrix
|
47
|
+
# gemm(a, b, c) -> NMatrix
|
48
|
+
# gemm(a, b, c, alpha, beta) -> NMatrix
|
49
|
+
#
|
50
|
+
# Updates the value of C via the matrix multiplication
|
51
|
+
# C = (alpha * A * B) + (beta * C)
|
52
|
+
# where +alpha+ and +beta+ are scalar values.
|
53
|
+
#
|
54
|
+
# * *Arguments* :
|
55
|
+
# - +a+ -> Matrix A.
|
56
|
+
# - +b+ -> Matrix B.
|
57
|
+
# - +c+ -> Matrix C.
|
58
|
+
# - +alpha+ -> A scalar value that multiplies A * B.
|
59
|
+
# - +beta+ -> A scalar value that multiplies C.
|
60
|
+
# - +transpose_a+ ->
|
61
|
+
# - +transpose_b+ ->
|
62
|
+
# - +m+ ->
|
63
|
+
# - +n+ ->
|
64
|
+
# - +k+ ->
|
65
|
+
# - +lda+ ->
|
66
|
+
# - +ldb+ ->
|
67
|
+
# - +ldc+ ->
|
68
|
+
# * *Returns* :
|
69
|
+
# - A NMatrix equal to (alpha * A * B) + (beta * C).
|
70
|
+
# * *Raises* :
|
71
|
+
# - +ArgumentError+ -> +a+ and +b+ must be dense matrices.
|
72
|
+
# - +ArgumentError+ -> +c+ must be +nil+ or a dense matrix.
|
73
|
+
# - +ArgumentError+ -> The dtype of the matrices must be equal.
|
74
|
+
#
|
75
|
+
def gemm(a, b, c = nil, alpha = 1.0, beta = 0.0,
|
76
|
+
transpose_a = false, transpose_b = false, m = nil,
|
77
|
+
n = nil, k = nil, lda = nil, ldb = nil, ldc = nil)
|
78
|
+
|
79
|
+
raise(ArgumentError, 'Expected dense NMatrices as first two arguments.') \
|
80
|
+
unless a.is_a?(NMatrix) and b.is_a? \
|
81
|
+
(NMatrix) and a.stype == :dense and b.stype == :dense
|
82
|
+
|
83
|
+
raise(ArgumentError, 'Expected nil or dense NMatrix as third argument.') \
|
84
|
+
unless c.nil? or (c.is_a?(NMatrix) \
|
85
|
+
and c.stype == :dense)
|
86
|
+
raise(ArgumentError, 'NMatrix dtype mismatch.') \
|
87
|
+
unless a.dtype == b.dtype and (c ? a.dtype == c.dtype : true)
|
88
|
+
|
89
|
+
# First, set m, n, and k, which depend on whether we're taking the
|
90
|
+
# transpose of a and b.
|
91
|
+
if c
|
92
|
+
m ||= c.shape[0]
|
93
|
+
n ||= c.shape[1]
|
94
|
+
k ||= transpose_a ? a.shape[0] : a.shape[1]
|
95
|
+
|
96
|
+
else
|
97
|
+
if transpose_a
|
98
|
+
# Either :transpose or :complex_conjugate.
|
99
|
+
m ||= a.shape[1]
|
100
|
+
k ||= a.shape[0]
|
101
|
+
|
102
|
+
else
|
103
|
+
# No transpose.
|
104
|
+
m ||= a.shape[0]
|
105
|
+
k ||= a.shape[1]
|
106
|
+
end
|
107
|
+
|
108
|
+
n ||= transpose_b ? b.shape[0] : b.shape[1]
|
109
|
+
c = NMatrix.new([m, n], dtype: a.dtype)
|
110
|
+
end
|
111
|
+
|
112
|
+
# I think these are independent of whether or not a transpose occurs.
|
113
|
+
lda ||= a.shape[1]
|
114
|
+
ldb ||= b.shape[1]
|
115
|
+
ldc ||= c.shape[1]
|
116
|
+
|
117
|
+
# NM_COMPLEX64 and NM_COMPLEX128 both require complex alpha and beta.
|
118
|
+
if a.dtype == :complex64 or a.dtype == :complex128
|
119
|
+
alpha = Complex(1.0, 0.0) if alpha == 1.0
|
120
|
+
beta = Complex(0.0, 0.0) if beta == 0.0
|
121
|
+
end
|
122
|
+
|
123
|
+
# For argument descriptions, see: http://www.netlib.org/blas/dgemm.f
|
124
|
+
::NMatrix::BLAS.cblas_gemm(:row, transpose_a, transpose_b,
|
125
|
+
m, n, k, alpha, a, lda, b, ldb, beta, c, ldc)
|
126
|
+
|
127
|
+
return c
|
128
|
+
end
|
129
|
+
|
130
|
+
#
|
131
|
+
# call-seq:
|
132
|
+
# gemv(a, x) -> NMatrix
|
133
|
+
# gemv(a, x, y) -> NMatrix
|
134
|
+
# gemv(a, x, y, alpha, beta) -> NMatrix
|
135
|
+
#
|
136
|
+
# Implements matrix-vector product via
|
137
|
+
# y = (alpha * A * x) + (beta * y)
|
138
|
+
# where +alpha+ and +beta+ are scalar values.
|
139
|
+
#
|
140
|
+
# * *Arguments* :
|
141
|
+
# - +a+ -> Matrix A.
|
142
|
+
# - +x+ -> Vector x.
|
143
|
+
# - +y+ -> Vector y.
|
144
|
+
# - +alpha+ -> A scalar value that multiplies A * x.
|
145
|
+
# - +beta+ -> A scalar value that multiplies y.
|
146
|
+
# - +transpose_a+ ->
|
147
|
+
# - +m+ ->
|
148
|
+
# - +n+ ->
|
149
|
+
# - +lda+ ->
|
150
|
+
# - +incx+ ->
|
151
|
+
# - +incy+ ->
|
152
|
+
# * *Returns* :
|
153
|
+
# -
|
154
|
+
# * *Raises* :
|
155
|
+
# - ++ ->
|
156
|
+
#
|
157
|
+
def gemv(a, x, y = nil, alpha = 1.0, beta = 0.0,
|
158
|
+
transpose_a = false, m = nil, n = nil, lda = nil,
|
159
|
+
incx = nil, incy = nil)
|
160
|
+
raise(ArgumentError, 'Expected dense NMatrices as first two arguments.') \
|
161
|
+
unless a.is_a?(NMatrix) and x.is_a?(NMatrix) and \
|
162
|
+
a.stype == :dense and x.stype == :dense
|
163
|
+
|
164
|
+
raise(ArgumentError, 'Expected nil or dense NMatrix as third argument.') \
|
165
|
+
unless y.nil? or (y.is_a?(NMatrix) and y.stype == :dense)
|
166
|
+
|
167
|
+
raise(ArgumentError, 'NMatrix dtype mismatch.') \
|
168
|
+
unless a.dtype == x.dtype and (y ? a.dtype == y.dtype : true)
|
169
|
+
|
170
|
+
m ||= transpose_a == :transpose ? a.shape[1] : a.shape[0]
|
171
|
+
n ||= transpose_a == :transpose ? a.shape[0] : a.shape[1]
|
172
|
+
raise(ArgumentError, "dimensions don't match") \
|
173
|
+
unless x.shape[0] == n && x.shape[1] == 1
|
174
|
+
|
175
|
+
if y
|
176
|
+
raise(ArgumentError, "dimensions don't match") \
|
177
|
+
unless y.shape[0] == m && y.shape[1] == 1
|
178
|
+
else
|
179
|
+
y = NMatrix.new([m,1], dtype: a.dtype)
|
180
|
+
end
|
181
|
+
|
182
|
+
lda ||= a.shape[1]
|
183
|
+
incx ||= 1
|
184
|
+
incy ||= 1
|
185
|
+
|
186
|
+
::NMatrix::BLAS.cblas_gemv(transpose_a, m, n,
|
187
|
+
alpha, a, lda, x, incx, beta, y, incy)
|
188
|
+
|
189
|
+
return y
|
190
|
+
end
|
191
|
+
|
192
|
+
#
|
193
|
+
# call-seq:
|
194
|
+
# rot(x, y, c, s) -> [NMatrix, NMatrix]
|
195
|
+
#
|
196
|
+
# Apply plane rotation.
|
197
|
+
#
|
198
|
+
# * *Arguments* :
|
199
|
+
# - +x+ -> NMatrix
|
200
|
+
# - +y+ -> NMatrix
|
201
|
+
# - +c+ -> cosine of the angle of rotation
|
202
|
+
# - +s+ -> sine of the angle of rotation
|
203
|
+
# - +incx+ -> stride of NMatrix +x+
|
204
|
+
# - +incy+ -> stride of NMatrix +y+
|
205
|
+
# - +n+ -> number of elements to consider in x and y
|
206
|
+
# - +in_place+ -> true if it's okay to modify the supplied
|
207
|
+
# +x+ and +y+ parameters directly;
|
208
|
+
# false if not. Default is false.
|
209
|
+
# * *Returns* :
|
210
|
+
# - Array with the results, in the format [xx, yy]
|
211
|
+
# * *Raises* :
|
212
|
+
# - +ArgumentError+ -> Expected dense NMatrices as first two arguments.
|
213
|
+
# - +ArgumentError+ -> NMatrix dtype mismatch.
|
214
|
+
# - +ArgumentError+ -> Need to supply n for non-standard incx,
|
215
|
+
# incy values.
|
216
|
+
#
|
217
|
+
def rot(x, y, c, s, incx = 1, incy = 1, n = nil, in_place=false)
|
218
|
+
raise(ArgumentError, 'Expected dense NMatrices as first two arguments.') \
|
219
|
+
unless x.is_a?(NMatrix) and y.is_a?(NMatrix) \
|
220
|
+
and x.stype == :dense and y.stype == :dense
|
221
|
+
|
222
|
+
raise(ArgumentError, 'NMatrix dtype mismatch.') \
|
223
|
+
unless x.dtype == y.dtype
|
224
|
+
|
225
|
+
raise(ArgumentError, 'Need to supply n for non-standard incx, incy values') \
|
226
|
+
if n.nil? && incx != 1 && incx != -1 && incy != 1 && incy != -1
|
227
|
+
|
228
|
+
n ||= [x.size/incx.abs, y.size/incy.abs].min
|
229
|
+
|
230
|
+
if in_place
|
231
|
+
::NMatrix::BLAS.cblas_rot(n, x, incx, y, incy, c, s)
|
232
|
+
return [x,y]
|
233
|
+
else
|
234
|
+
xx = x.clone
|
235
|
+
yy = y.clone
|
236
|
+
|
237
|
+
::NMatrix::BLAS.cblas_rot(n, xx, incx, yy, incy, c, s)
|
238
|
+
|
239
|
+
return [xx,yy]
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
#
|
245
|
+
# call-seq:
|
246
|
+
# rot!(x, y, c, s) -> [NMatrix, NMatrix]
|
247
|
+
#
|
248
|
+
# Apply plane rotation directly to +x+ and +y+.
|
249
|
+
#
|
250
|
+
# See rot for arguments.
|
251
|
+
def rot!(x, y, c, s, incx = 1, incy = 1, n = nil)
|
252
|
+
rot(x,y,c,s,incx,incy,n,true)
|
253
|
+
end
|
254
|
+
|
255
|
+
|
256
|
+
#
|
257
|
+
# call-seq:
|
258
|
+
# rotg(ab) -> [Numeric, Numeric]
|
259
|
+
#
|
260
|
+
# Apply givens plane rotation to the coordinates (a,b),
|
261
|
+
# returning the cosine and sine of the angle theta.
|
262
|
+
#
|
263
|
+
# Since the givens rotation includes a square root,
|
264
|
+
# integers are disallowed.
|
265
|
+
#
|
266
|
+
# * *Arguments* :
|
267
|
+
# - +ab+ -> NMatrix with two elements
|
268
|
+
# * *Returns* :
|
269
|
+
# - Array with the results, in the format [cos(theta), sin(theta)]
|
270
|
+
# * *Raises* :
|
271
|
+
# - +ArgumentError+ -> Expected dense NMatrix of size 2
|
272
|
+
#
|
273
|
+
def rotg(ab)
|
274
|
+
raise(ArgumentError, "Expected dense NMatrix of shape [2,1] or [1,2]") \
|
275
|
+
unless ab.is_a?(NMatrix) && ab.stype == :dense && ab.size == 2
|
276
|
+
|
277
|
+
::NMatrix::BLAS.cblas_rotg(ab)
|
278
|
+
end
|
279
|
+
|
280
|
+
|
281
|
+
#
|
282
|
+
# call-seq:
|
283
|
+
# asum(x, incx, n) -> Numeric
|
284
|
+
#
|
285
|
+
# Calculate the sum of absolute values of the entries of a
|
286
|
+
# vector +x+ of size +n+
|
287
|
+
#
|
288
|
+
# * *Arguments* :
|
289
|
+
# - +x+ -> an NMatrix (will also allow an NMatrix,
|
290
|
+
# but will treat it as if it's a vector )
|
291
|
+
# - +incx+ -> the skip size (defaults to 1)
|
292
|
+
# - +n+ -> the size of +x+ (defaults to +x.size / incx+)
|
293
|
+
# * *Returns* :
|
294
|
+
# - The sum
|
295
|
+
# * *Raises* :
|
296
|
+
# - +ArgumentError+ -> Expected dense NMatrix for arg 0
|
297
|
+
# - +RangeError+ -> n out of range
|
298
|
+
#
|
299
|
+
def asum(x, incx = 1, n = nil)
|
300
|
+
n ||= x.size / incx
|
301
|
+
raise(ArgumentError, "Expected dense NMatrix for arg 0") \
|
302
|
+
unless x.is_a?(NMatrix)
|
303
|
+
|
304
|
+
raise(RangeError, "n out of range") \
|
305
|
+
if n*incx > x.size || n*incx <= 0 || n <= 0
|
306
|
+
::NMatrix::BLAS.cblas_asum(n, x, incx)
|
307
|
+
end
|
308
|
+
|
309
|
+
#
|
310
|
+
# call-seq:
|
311
|
+
# nrm2(x, incx, n)
|
312
|
+
#
|
313
|
+
# Calculate the 2-norm of a vector +x+ of size +n+
|
314
|
+
#
|
315
|
+
# * *Arguments* :
|
316
|
+
# - +x+ -> an NMatrix (will also allow an
|
317
|
+
# NMatrix, but will treat it as if it's a vector )
|
318
|
+
# - +incx+ -> the skip size (defaults to 1)
|
319
|
+
# - +n+ -> the size of +x+ (defaults to +x.size / incx+)
|
320
|
+
# * *Returns* :
|
321
|
+
# - The 2-norm
|
322
|
+
# * *Raises* :
|
323
|
+
# - +ArgumentError+ -> Expected dense NMatrix for arg 0
|
324
|
+
# - +RangeError+ -> n out of range
|
325
|
+
#
|
326
|
+
def nrm2(x, incx = 1, n = nil)
|
327
|
+
n ||= x.size / incx
|
328
|
+
raise(ArgumentError, "Expected dense NMatrix for arg 0") \
|
329
|
+
unless x.is_a?(NMatrix)
|
330
|
+
|
331
|
+
raise(RangeError, "n out of range") \
|
332
|
+
if n*incx > x.size || n*incx <= 0 || n <= 0
|
333
|
+
::NMatrix::BLAS.cblas_nrm2(n, x, incx)
|
334
|
+
end
|
335
|
+
|
336
|
+
#
|
337
|
+
# call-seq:
|
338
|
+
# scal(alpha, vector, incx, n)
|
339
|
+
#
|
340
|
+
# Scale a matrix by a given scaling factor
|
341
|
+
#
|
342
|
+
# * *Arguments* :
|
343
|
+
# - +alpha+ -> a scaling factor
|
344
|
+
# - +vector+ -> an NMatrix
|
345
|
+
# - +incx+ -> the skip size (defaults to 1)
|
346
|
+
# - +n+ -> the size of +x+ (defaults to +x.size / incx+)
|
347
|
+
# * *Returns* :
|
348
|
+
# - The scaling result
|
349
|
+
# * *Raises* :
|
350
|
+
# - +ArgumentError+ -> Expected dense NMatrix for arg 0
|
351
|
+
# - +RangeError+ -> n out of range
|
352
|
+
#
|
353
|
+
def scal(alpha, vector, incx=1, n=nil)
|
354
|
+
n ||= vector.size / incx
|
355
|
+
raise(ArgumentError, "Expected dense NMatrix for arg 0") unless vector.is_a?(NMatrix)
|
356
|
+
raise(RangeError, "n out of range") if n*incx > vector.size || n*incx <= 0 || n <= 0
|
357
|
+
::NMatrix::BLAS.cblas_scal(n, alpha, vector, incx)
|
358
|
+
end
|
359
|
+
|
360
|
+
# The following are functions that used to be implemented in C, but
|
361
|
+
# now require nmatrix-atlas or nmatrix-lapcke to run properly, so we can just
|
362
|
+
# implemented their stubs in Ruby.
|
363
|
+
def cblas_trmm(order, side, uplo, trans_a, diag, m, n, alpha, a, lda, b, ldb)
|
364
|
+
raise(NotImplementedError,"cblas_trmm requires either the
|
365
|
+
nmatrix-lapacke or nmatrix-atlas gem")
|
366
|
+
end
|
367
|
+
|
368
|
+
def cblas_syrk(order, uplo, trans, n, k, alpha, a, lda, beta, c, ldc)
|
369
|
+
raise(NotImplementedError,"cblas_syrk requires either the
|
370
|
+
nmatrix-lapacke or nmatrix-atlas gem")
|
371
|
+
end
|
372
|
+
|
373
|
+
def cblas_herk(order, uplo, trans, n, k, alpha, a, lda, beta, c, ldc)
|
374
|
+
raise(NotImplementedError,"cblas_herk requires either the
|
375
|
+
nmatrix-lapacke or nmatrix-atlas gem")
|
376
|
+
end
|
377
|
+
end
|
378
|
+
end
|