nmatrix 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +27 -0
- data/.rspec +2 -0
- data/Gemfile +3 -5
- data/Guardfile +6 -0
- data/History.txt +33 -0
- data/Manifest.txt +41 -38
- data/README.rdoc +88 -11
- data/Rakefile +35 -53
- data/ext/nmatrix/data/complex.h +372 -0
- data/ext/nmatrix/data/data.cpp +275 -0
- data/ext/nmatrix/data/data.h +707 -0
- data/ext/nmatrix/data/rational.h +421 -0
- data/ext/nmatrix/data/ruby_object.h +446 -0
- data/ext/nmatrix/extconf.rb +101 -51
- data/ext/nmatrix/new_extconf.rb +56 -0
- data/ext/nmatrix/nmatrix.cpp +1609 -0
- data/ext/nmatrix/nmatrix.h +265 -849
- data/ext/nmatrix/ruby_constants.cpp +134 -0
- data/ext/nmatrix/ruby_constants.h +103 -0
- data/ext/nmatrix/storage/common.cpp +70 -0
- data/ext/nmatrix/storage/common.h +170 -0
- data/ext/nmatrix/storage/dense.cpp +665 -0
- data/ext/nmatrix/storage/dense.h +116 -0
- data/ext/nmatrix/storage/list.cpp +1088 -0
- data/ext/nmatrix/storage/list.h +129 -0
- data/ext/nmatrix/storage/storage.cpp +658 -0
- data/ext/nmatrix/storage/storage.h +99 -0
- data/ext/nmatrix/storage/yale.cpp +1601 -0
- data/ext/nmatrix/storage/yale.h +208 -0
- data/ext/nmatrix/ttable_helper.rb +126 -0
- data/ext/nmatrix/{yale/smmp1_header.template.c → types.h} +36 -9
- data/ext/nmatrix/util/io.cpp +295 -0
- data/ext/nmatrix/util/io.h +117 -0
- data/ext/nmatrix/util/lapack.h +1175 -0
- data/ext/nmatrix/util/math.cpp +557 -0
- data/ext/nmatrix/util/math.h +1363 -0
- data/ext/nmatrix/util/sl_list.cpp +475 -0
- data/ext/nmatrix/util/sl_list.h +255 -0
- data/ext/nmatrix/util/util.h +78 -0
- data/lib/nmatrix/blas.rb +70 -0
- data/lib/nmatrix/io/mat5_reader.rb +567 -0
- data/lib/nmatrix/io/mat_reader.rb +162 -0
- data/lib/{string.rb → nmatrix/monkeys.rb} +49 -2
- data/lib/nmatrix/nmatrix.rb +199 -0
- data/lib/nmatrix/nvector.rb +103 -0
- data/lib/nmatrix/version.rb +27 -0
- data/lib/nmatrix.rb +22 -230
- data/nmatrix.gemspec +59 -0
- data/scripts/mac-brew-gcc.sh +47 -0
- data/spec/4x4_sparse.mat +0 -0
- data/spec/4x5_dense.mat +0 -0
- data/spec/blas_spec.rb +47 -0
- data/spec/elementwise_spec.rb +164 -0
- data/spec/io_spec.rb +60 -0
- data/spec/lapack_spec.rb +52 -0
- data/spec/math_spec.rb +96 -0
- data/spec/nmatrix_spec.rb +93 -89
- data/spec/nmatrix_yale_spec.rb +52 -36
- data/spec/nvector_spec.rb +1 -1
- data/spec/slice_spec.rb +257 -0
- data/spec/spec_helper.rb +51 -0
- data/spec/utm5940.mtx +83844 -0
- metadata +113 -71
- data/.autotest +0 -23
- data/.gemtest +0 -0
- data/ext/nmatrix/cblas.c +0 -150
- data/ext/nmatrix/dense/blas_header.template.c +0 -52
- data/ext/nmatrix/dense/elementwise.template.c +0 -107
- data/ext/nmatrix/dense/gemm.template.c +0 -159
- data/ext/nmatrix/dense/gemv.template.c +0 -130
- data/ext/nmatrix/dense/rationalmath.template.c +0 -68
- data/ext/nmatrix/dense.c +0 -307
- data/ext/nmatrix/depend +0 -18
- data/ext/nmatrix/generator/syntax_tree.rb +0 -481
- data/ext/nmatrix/generator.rb +0 -594
- data/ext/nmatrix/list.c +0 -774
- data/ext/nmatrix/nmatrix.c +0 -1977
- data/ext/nmatrix/rational.c +0 -98
- data/ext/nmatrix/yale/complexmath.template.c +0 -71
- data/ext/nmatrix/yale/elementwise.template.c +0 -46
- data/ext/nmatrix/yale/elementwise_op.template.c +0 -73
- data/ext/nmatrix/yale/numbmm.template.c +0 -94
- data/ext/nmatrix/yale/smmp1.template.c +0 -21
- data/ext/nmatrix/yale/smmp2.template.c +0 -43
- data/ext/nmatrix/yale/smmp2_header.template.c +0 -46
- data/ext/nmatrix/yale/sort_columns.template.c +0 -56
- data/ext/nmatrix/yale/symbmm.template.c +0 -54
- data/ext/nmatrix/yale/transp.template.c +0 -68
- data/ext/nmatrix/yale.c +0 -726
- data/lib/array.rb +0 -67
- data/spec/syntax_tree_spec.rb +0 -46
data/lib/nmatrix.rb
CHANGED
@@ -22,242 +22,34 @@
|
|
22
22
|
#
|
23
23
|
# == nmatrix.rb
|
24
24
|
#
|
25
|
-
# This file loads the C extension for NMatrix
|
26
|
-
#
|
27
|
-
# Also provided is NVector, which represents a rank-1 NMatrix in
|
28
|
-
# vector operations.
|
25
|
+
# This file loads the C extension for NMatrix and adds an autoload for the
|
26
|
+
# NMatrix and NVector classes.
|
29
27
|
|
30
|
-
# For some reason nmatrix.so ends up in a different place during gem build
|
31
|
-
if File.exist? "lib/nmatrix/nmatrix.so"
|
32
|
-
require File.join(File.dirname(__FILE__), "nmatrix/nmatrix.so") # development
|
33
|
-
else
|
34
|
-
require File.join(File.dirname(__FILE__), "nmatrix.so") # gem
|
35
|
-
end
|
36
|
-
require File.join(File.dirname(__FILE__), "array.rb") # Load Array extensions
|
37
|
-
|
38
|
-
|
39
|
-
class NMatrix
|
40
|
-
VERSION = '0.0.1'
|
41
|
-
|
42
|
-
#def inspect
|
43
|
-
#
|
44
|
-
#end
|
45
|
-
|
46
|
-
# TODO: Make this actually pretty.
|
47
|
-
def pretty_print
|
48
|
-
raise(NotImplementedError, "can only print rank 2 matrices") unless rank == 2
|
49
|
-
(0...shape[0]).each do |i|
|
50
|
-
arr = []
|
51
|
-
(0...shape[1]).each do |j|
|
52
|
-
arr << (self[i,j].nil? ? "nil" : self[i,j])
|
53
|
-
end
|
54
|
-
puts arr.join(" ")
|
55
|
-
end
|
56
|
-
nil
|
57
|
-
end
|
58
|
-
|
59
|
-
|
60
|
-
def inspect
|
61
|
-
original_inspect = super
|
62
|
-
original_inspect = original_inspect[0...original_inspect.size-1]
|
63
|
-
original_inspect + inspect_helper.join(" ") + ">"
|
64
|
-
end
|
65
|
-
|
66
|
-
def __yale_ary__to_s(sym)
|
67
|
-
ary = self.send("__yale_#{sym.to_s}__".to_sym)
|
68
|
-
"[" + ary.collect { |a| a.nil? ? "nil" : a }.join(',') + "]"
|
69
|
-
end
|
70
|
-
|
71
|
-
class << self
|
72
|
-
|
73
|
-
# Helper function for loading a file in the first sparse format given here:
|
74
|
-
# http://math.nist.gov/MatrixMarket/formats.html
|
75
|
-
#
|
76
|
-
# Override type specifier (e.g., 'real') using :read_with => :to_f (or any other string-to-numeric conversion
|
77
|
-
# function), and with :dtype => :float32 or :dtype => :int8 to force storage in a lesser type.
|
78
|
-
def load_matrix_matrix_coordinate_file filename, options = {}
|
79
|
-
f = File.new(filename, "r")
|
80
|
-
|
81
|
-
func = options.has_key?(:read_with) ? options[:read_with] : nil
|
82
|
-
dtype = options.has_key?(:dtype) ? options[:dtype] : nil
|
83
|
-
|
84
|
-
line = f.gets
|
85
|
-
raise(IOError, "incorrect file type specifier") unless line =~ /^%%MatrixMarket\ matrix\ coordinate/
|
86
|
-
spec = line.split
|
87
|
-
case spec[3]
|
88
|
-
when 'real'
|
89
|
-
func ||= :to_f
|
90
|
-
dtype ||= :float64
|
91
|
-
when 'integer'
|
92
|
-
func ||= :to_i
|
93
|
-
dtype ||= :int64
|
94
|
-
when 'complex'
|
95
|
-
func ||= :to_complex
|
96
|
-
dtype ||= :complex128
|
97
|
-
when 'rational'
|
98
|
-
func = :to_rational
|
99
|
-
dtype ||= :rational128
|
100
|
-
else
|
101
|
-
raise ArgumentError, "Unrecognized dtype"
|
102
|
-
end unless !func.nil? && !dtype.nil?
|
103
|
-
|
104
|
-
line = f.gets
|
105
|
-
while line =~ /^%/
|
106
|
-
line = f.gets
|
107
|
-
end
|
108
|
-
|
109
|
-
rows, cols, entries = line.split.collect { |x| x.to_i }
|
110
|
-
|
111
|
-
matrix = NMatrix.new(:yale, [rows, cols], entries, dtype)
|
112
|
-
|
113
|
-
entries.times do
|
114
|
-
i,j,v = line.split
|
115
|
-
matrix[i.to_i-1,j.to_i-1] = v.send(func)
|
116
|
-
end
|
117
|
-
|
118
|
-
matrix
|
119
|
-
end
|
120
|
-
|
121
|
-
|
122
|
-
def cblas_gemm a, b, c=nil, alpha=1.0, beta=0.0, transpose_a=false, transpose_b=false, m=nil, n=nil, k=nil, lda=nil, ldb=nil, ldc=nil
|
123
|
-
raise(ArgumentError, "expected dense NMatrices as first two arguments") unless a.is_a?(NMatrix) && b.is_a?(NMatrix) && a.stype == :dense && b.stype == :dense
|
124
|
-
raise(ArgumentError, "expected nil or dense NMatrix as third argument") unless c.nil? || (c.is_a?(NMatrix) && c.stype == :dense)
|
125
|
-
raise(ArgumentError, "NMatrix dtype mismatch") unless a.dtype == b.dtype && (c.nil? ? true : a.dtype == c.dtype)
|
126
|
-
|
127
|
-
# First, set m, n, and k, which depend on whether we're taking the transpose of a and b.
|
128
|
-
if c.nil?
|
129
|
-
if transpose_a # either :transpose or :complex_conjugate
|
130
|
-
m ||= a.shape[1]
|
131
|
-
k ||= a.shape[0]
|
132
|
-
else # no transpose
|
133
|
-
m ||= a.shape[0]
|
134
|
-
k ||= a.shape[1]
|
135
|
-
end
|
136
|
-
n ||= transpose_b ? b.shape[0] : b.shape[1]
|
137
|
-
c = NMatrix.new([m, n], a.dtype)
|
138
|
-
else
|
139
|
-
m ||= c.shape[0]
|
140
|
-
n ||= c.shape[1]
|
141
|
-
k ||= transpose_a ? a.shape[0] : a.shape[1]
|
142
|
-
end
|
143
|
-
|
144
|
-
# I think these are independent of whether or not a transpose occurs.
|
145
|
-
lda ||= a.shape[1]
|
146
|
-
ldb ||= b.shape[1]
|
147
|
-
ldc ||= c.shape[1]
|
148
28
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
end
|
29
|
+
#############
|
30
|
+
# Autoloads #
|
31
|
+
#############
|
153
32
|
|
154
|
-
# For argument descriptions, see: http://www.netlib.org/blas/dgemm.f
|
155
|
-
NMatrix.__cblas_gemm__(transpose_a, transpose_b, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc)
|
156
33
|
|
157
|
-
|
158
|
-
|
34
|
+
############
|
35
|
+
# Requires #
|
36
|
+
############
|
159
37
|
|
38
|
+
# NMatrix
|
160
39
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
lda ||= a.shape[1]
|
166
|
-
incx ||= 1
|
167
|
-
incy ||= 1
|
168
|
-
|
169
|
-
if a.dtype == :complex64 || a.dtype == :complex128 # NM_COMPLEX64 and NM_COMPLEX128 both require complex alpha and beta
|
170
|
-
alpha = Complex.new(1.0, 0.0) if alpha == 1.0
|
171
|
-
beta = Complex.new(0.0, 0.0) if beta == 0.0
|
172
|
-
end
|
173
|
-
|
174
|
-
NMatrix.__cblas_gemv__(transpose_a, m, n, alpha, a, lda, x, incx, beta, y, incy)
|
175
|
-
|
176
|
-
return y
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
protected
|
181
|
-
def inspect_helper
|
182
|
-
ary = []
|
183
|
-
ary << "shape:[#{shape.join(',')}]" << "dtype:#{dtype}" << "stype:#{stype}"
|
184
|
-
|
185
|
-
if stype == :yale
|
186
|
-
ary << "capacity:#{capacity}" << "ija:#{__yale_ary__to_s(:ija)}" << "ia:#{__yale_ary__to_s(:ia)}" <<
|
187
|
-
"ja:#{__yale_ary__to_s(:ja)}" << "a:#{__yale_ary__to_s(:a)}" << "d:#{__yale_ary__to_s(:d)}" <<
|
188
|
-
"lu:#{__yale_ary__to_s(:lu)}" << "yale_size:#{__yale_size__}"
|
189
|
-
end
|
190
|
-
|
191
|
-
ary
|
192
|
-
end
|
40
|
+
require 'nmatrix/nmatrix.rb'
|
41
|
+
require 'nmatrix/version.rb'
|
42
|
+
require 'nmatrix/nvector.rb'
|
43
|
+
require 'nmatrix/blas.rb'
|
193
44
|
|
45
|
+
# For some reason nmatrix.so ends up in a different place during gem build.
|
46
|
+
if File.exist? 'lib/nmatrix/nmatrix.so'
|
47
|
+
# Development
|
48
|
+
require 'nmatrix/nmatrix.so'
|
49
|
+
else
|
50
|
+
# Gem
|
51
|
+
require 'nmatrix.so'
|
194
52
|
end
|
195
53
|
|
196
|
-
|
197
|
-
|
198
|
-
# This is a specific type of NMatrix in which only one dimension is not 1. Although it is stored as a rank-2, n x 1,
|
199
|
-
# matrix, it acts as a rank-1 vector of size n. If the @orientation flag is set to :row, it is stored as 1 x n instead
|
200
|
-
# of n x 1.
|
201
|
-
class NVector < NMatrix
|
202
|
-
def initialize length, *args
|
203
|
-
super :dense, [length,1], *args
|
204
|
-
end
|
205
|
-
|
206
|
-
# Orientation defaults to column (e.g., [3,1] is a column of length 3). It may also be row, e.g., for [1,5].
|
207
|
-
def orientation
|
208
|
-
defined?(@orientation) && !@orientation.nil? ? @orientation : :column
|
209
|
-
end
|
210
|
-
|
211
|
-
def transpose
|
212
|
-
t = super
|
213
|
-
t.send :eval, "@orientation = @orientation == :row ? :column : :row"
|
214
|
-
t
|
215
|
-
end
|
216
|
-
|
217
|
-
def transpose!
|
218
|
-
super
|
219
|
-
@orientation = @orientation == :row ? :column : :row
|
220
|
-
self
|
221
|
-
end
|
222
|
-
|
223
|
-
def multiply m
|
224
|
-
v = super(m)
|
225
|
-
v.send :eval, "@orientation = @orientation == :row ? :column : :row"
|
226
|
-
v
|
227
|
-
end
|
228
|
-
|
229
|
-
def multiply! m
|
230
|
-
super
|
231
|
-
@orientation = @orientation == :row ? :column : :row
|
232
|
-
self
|
233
|
-
end
|
234
|
-
|
235
|
-
def [] i
|
236
|
-
@orientation == :row ? super(0,i) : super(i,0)
|
237
|
-
end
|
238
|
-
|
239
|
-
def []= i,val
|
240
|
-
@orientation == :row ? super(0,i,val) : super(i,0,val)
|
241
|
-
end
|
242
|
-
|
243
|
-
def rank; 1; end
|
244
|
-
|
245
|
-
# TODO: Make this actually pretty.
|
246
|
-
def pretty_print
|
247
|
-
dim = @orientation == :row ? 1 : 0
|
248
|
-
arr = []
|
249
|
-
(0...shape[dim]).each do |i|
|
250
|
-
arr << self[i]
|
251
|
-
end
|
252
|
-
puts arr.join(" ")
|
253
|
-
nil
|
254
|
-
end
|
255
|
-
|
256
|
-
protected
|
257
|
-
def inspect_helper
|
258
|
-
ary = super
|
259
|
-
ary << "orientation:#{(@orientation || 'column').to_s}"
|
260
|
-
ary
|
261
|
-
end
|
262
|
-
|
263
|
-
end
|
54
|
+
# Monkey patches.
|
55
|
+
require 'nmatrix/monkeys'
|
data/nmatrix.gemspec
ADDED
@@ -0,0 +1,59 @@
|
|
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
|
9
|
+
gem.summary = "NMatrix is an experimental linear algebra library for Ruby, written mostly in C."
|
10
|
+
gem.description = "NMatrix is an experimental linear algebra library for Ruby, written mostly in 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.post_install_message = <<-EOF
|
15
|
+
***********************************************************
|
16
|
+
Welcome to SciRuby: Tools for Scientific Computing in Ruby!
|
17
|
+
|
18
|
+
*** WARNING ***
|
19
|
+
Please be aware that NMatrix is in ALPHA status. If you're
|
20
|
+
thinking of using NMatrix to write mission critical code,
|
21
|
+
such as for driving a car or flying a space shuttle, you
|
22
|
+
may wish to choose other software (for now).
|
23
|
+
|
24
|
+
NMatrix requires a C compiler, and has been tested only
|
25
|
+
with GCC 4.6.1. We are happy to accept contributions
|
26
|
+
which improve the portability of this project.
|
27
|
+
|
28
|
+
Also required is ATLAS. Most Linux distributions and Mac
|
29
|
+
versions include ATLAS, but you may wish to compile it
|
30
|
+
yourself.
|
31
|
+
|
32
|
+
More explicit instructions for NMatrix and SciRuby should
|
33
|
+
be available on the SciRuby website, sciruby.com, or
|
34
|
+
through our mailing list (which can be found on our web-
|
35
|
+
site).
|
36
|
+
|
37
|
+
Thanks for trying out NMatrix! Happy coding!
|
38
|
+
|
39
|
+
***********************************************************
|
40
|
+
EOF
|
41
|
+
|
42
|
+
gem.files = `git ls-files`.split("\n")
|
43
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
44
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
45
|
+
gem.extensions = ['ext/nmatrix/extconf.rb']
|
46
|
+
gem.require_paths = ["lib"]
|
47
|
+
|
48
|
+
gem.required_ruby_version = '>= 1.9.2'
|
49
|
+
|
50
|
+
#gem.add_dependency 'csquare', '~>0.0.2'
|
51
|
+
|
52
|
+
gem.add_development_dependency 'rake', '~>0.9'
|
53
|
+
gem.add_development_dependency 'bundler'
|
54
|
+
gem.add_development_dependency 'rspec', '~>2.9.0'
|
55
|
+
gem.add_development_dependency 'pry', '~>0.9.9'
|
56
|
+
gem.add_development_dependency 'guard-rspec', '~>0.7.0'
|
57
|
+
gem.add_development_dependency 'rake-compiler', '~>0.8.1'
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
VERSION="4.7.1"
|
3
|
+
PREFIX="/usr/gcc-${VERSION}"
|
4
|
+
LANGUAGES="c,c++,fortran"
|
5
|
+
MAKE="make -j 4"
|
6
|
+
|
7
|
+
brew-path() { brew info $1 | head -n3 | tail -n1 | cut -d' ' -f1; }
|
8
|
+
|
9
|
+
# Prerequisites
|
10
|
+
|
11
|
+
brew install gmp
|
12
|
+
brew install mpfr
|
13
|
+
brew install libmpc
|
14
|
+
|
15
|
+
# Download & install the latest GCC
|
16
|
+
|
17
|
+
mkdir -p $PREFIX
|
18
|
+
mkdir temp-gcc
|
19
|
+
cd temp-gcc
|
20
|
+
wget ftp://ftp.gnu.org/gnu/gcc/gcc-$VERSION/gcc-$VERSION.tar.gz
|
21
|
+
tar xfz gcc-$VERSION.tar.gz
|
22
|
+
rm gcc-$VERSION.tar.gz
|
23
|
+
cd gcc-$VERSION
|
24
|
+
|
25
|
+
mkdir build
|
26
|
+
cd build
|
27
|
+
|
28
|
+
../configure \
|
29
|
+
--prefix=$PREFIX \
|
30
|
+
--with-gmp=$(brew-path gmp) \
|
31
|
+
--with-mpfr=$(brew-path mpfr) \
|
32
|
+
--with-mpc=$(brew-path libmpc) \
|
33
|
+
--program-suffix=-$VERSION \
|
34
|
+
--enable-languages=$LANGUAGES \
|
35
|
+
--with-system-zlib \
|
36
|
+
--enable-stage1-checking \
|
37
|
+
--enable-plugin \
|
38
|
+
--enable-lto \
|
39
|
+
--disable-multilib
|
40
|
+
|
41
|
+
$MAKE bootstrap
|
42
|
+
|
43
|
+
make install
|
44
|
+
|
45
|
+
# Uncomment for cleanup …
|
46
|
+
# cd ../../..
|
47
|
+
# rm -r temp-gcc
|
data/spec/4x4_sparse.mat
ADDED
Binary file
|
data/spec/4x5_dense.mat
ADDED
Binary file
|
data/spec/blas_spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# = NMatrix
|
2
|
+
#
|
3
|
+
# A linear algebra library for scientific computation in Ruby.
|
4
|
+
# NMatrix is part of SciRuby.
|
5
|
+
#
|
6
|
+
# NMatrix was originally inspired by and derived from NArray, by
|
7
|
+
# Masahiro Tanaka: http://narray.rubyforge.org
|
8
|
+
#
|
9
|
+
# == Copyright Information
|
10
|
+
#
|
11
|
+
# SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012, Ruby Science Foundation
|
13
|
+
#
|
14
|
+
# Please see LICENSE.txt for additional copyright notices.
|
15
|
+
#
|
16
|
+
# == Contributing
|
17
|
+
#
|
18
|
+
# By contributing source code to SciRuby, you agree to be bound by
|
19
|
+
# our Contributor Agreement:
|
20
|
+
#
|
21
|
+
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
22
|
+
#
|
23
|
+
# == blas_spec.rb
|
24
|
+
#
|
25
|
+
# Tests for properly exposed BLAS functions.
|
26
|
+
#
|
27
|
+
|
28
|
+
# Can we use require_relative here instead?
|
29
|
+
require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
30
|
+
|
31
|
+
describe NMatrix::BLAS do
|
32
|
+
[:rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
|
33
|
+
context dtype do
|
34
|
+
it "exposes cblas trsm, with B as a vector" do
|
35
|
+
a = NMatrix.new(:dense, 3, [5,4,-1,0,10,-3,0,0,1], dtype)
|
36
|
+
b = NVector.new(3, [0,11,3], dtype)
|
37
|
+
alpha = 1
|
38
|
+
NMatrix::BLAS::cblas_trsm(:row, :left, :upper, :no_transpose, :nonunit, b.shape[0], b.shape[1], alpha, a, 3, b, 1)
|
39
|
+
b[0].should == -1
|
40
|
+
b[1].should == 2
|
41
|
+
b[2].should == 3
|
42
|
+
end
|
43
|
+
|
44
|
+
it "exposes cblas trsm, with B as a matrix"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
# = NMatrix
|
2
|
+
#
|
3
|
+
# A linear algebra library for scientific computation in Ruby.
|
4
|
+
# NMatrix is part of SciRuby.
|
5
|
+
#
|
6
|
+
# NMatrix was originally inspired by and derived from NArray, by
|
7
|
+
# Masahiro Tanaka: http://narray.rubyforge.org
|
8
|
+
#
|
9
|
+
# == Copyright Information
|
10
|
+
#
|
11
|
+
# SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012, Ruby Science Foundation
|
13
|
+
#
|
14
|
+
# Please see LICENSE.txt for additional copyright notices.
|
15
|
+
#
|
16
|
+
# == Contributing
|
17
|
+
#
|
18
|
+
# By contributing source code to SciRuby, you agree to be bound by
|
19
|
+
# our Contributor Agreement:
|
20
|
+
#
|
21
|
+
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
22
|
+
#
|
23
|
+
# == nmatrix_spec.rb
|
24
|
+
#
|
25
|
+
# Element-wise operation tests.
|
26
|
+
#
|
27
|
+
|
28
|
+
# Can we use require_relative here instead?
|
29
|
+
require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
30
|
+
|
31
|
+
describe NMatrix do
|
32
|
+
|
33
|
+
context "list" do
|
34
|
+
before :each do
|
35
|
+
@n = NMatrix.new(:list, 2, 0, :int64)
|
36
|
+
@m = NMatrix.new(:list, 2, 0, :int64)
|
37
|
+
@n[0,0] = 52
|
38
|
+
@m[0,0] = -48
|
39
|
+
@n[1,1] = 40
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should perform element-wise addition" do
|
43
|
+
r = NMatrix.new(:dense, 2, [4, 0, 0, 40], :int64).cast(:list, :int64)
|
44
|
+
(@n+@m).should == r
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should perform element-wise subtraction" do
|
48
|
+
r = NMatrix.new(:dense, 2, [100, 0, 0, 40], :int64).cast(:list, :int64)
|
49
|
+
(@n-@m).should == r
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should perform element-wise multiplication" do
|
53
|
+
r = NMatrix.new(:dense, 2, [-2496, 0, 0, 0], :int64).cast(:list, :int64)
|
54
|
+
(@n*@m).should == r
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should perform element-wise division" do
|
58
|
+
m = NMatrix.new(:list, 2, 1, :int64)
|
59
|
+
m[1,1] = 2
|
60
|
+
r = NMatrix.new(:dense, 2, [52, 0, 0, 20], :int64).cast(:list, :int64)
|
61
|
+
(@n/@m).should == r
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should perform element-wise modulo"
|
65
|
+
|
66
|
+
it "equals" do
|
67
|
+
(@n =~ @m).cast(:dense, :byte).should == NMatrix.new(:dense, 2, [0, 1, 1, 0], :byte)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "is not equal to" do
|
71
|
+
(@n !~ @m).cast(:dense, :byte).should == NMatrix.new(:dense, 2, [1, 0, 0, 1], :byte)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "is less than" do
|
75
|
+
(@n < @m).should == NMatrix.new(:list, 2, 0, :byte)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "is greater than" do
|
79
|
+
(@n > @m).should == NMatrix.new(:dense, 2, [1, 0, 0, 1], :byte).cast(:list, :byte)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "is greater than or equal to" do
|
83
|
+
(@n >= @m).cast(:dense, :byte).should == NMatrix.new(:dense, 2, [1, 1, 1, 1], :byte)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "is less than or equal to" do
|
87
|
+
(@n <= @m).cast(:dense, :byte).should == NMatrix.new(:dense, 2, [0, 1, 1, 0], :byte)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "dense" do
|
92
|
+
context "elementwise arithmetic" do
|
93
|
+
before :each do
|
94
|
+
@n = NMatrix.new(:dense, 2, [1,2,3,4], :int64)
|
95
|
+
@m = NMatrix.new(:dense, 2, [-4,-1,0,66], :int64)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "adds" do
|
99
|
+
r = @n+@m
|
100
|
+
r.should == NMatrix.new(:dense, [2,2], [-3, 1, 3, 70], :int64)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "subtracts" do
|
104
|
+
r = @n-@m
|
105
|
+
r.should == NMatrix.new(:dense, [2,2], [5, 3, 3, -62], :int64)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "multiplies" do
|
109
|
+
r = @n*@m
|
110
|
+
r.should == NMatrix.new(:dense, [2,2], [-4, -2, 0, 264], :int64)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "divides in the Ruby way" do
|
114
|
+
m = @m.clone
|
115
|
+
m[1,0] = 3
|
116
|
+
r = @n/m
|
117
|
+
r.should == NMatrix.new(:dense, [2,2], [-1, -2, 1, 0], :int64)
|
118
|
+
end
|
119
|
+
|
120
|
+
it "modulo" do
|
121
|
+
r = @n % @m
|
122
|
+
r.should == NMatrix.new(:dense, [2,2], [0, 1, 2, 3], :int64)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "elementwise comparisons" do
|
127
|
+
before :each do
|
128
|
+
@n = NMatrix.new(:dense, 2, [1,2,3,4], :int64)
|
129
|
+
@m = NMatrix.new(:dense, 2, [-4,-1,3,2], :int64)
|
130
|
+
end
|
131
|
+
|
132
|
+
it "equals" do
|
133
|
+
r = @n =~ @m
|
134
|
+
r.should == NMatrix.new(:dense, [2,2], [0, 0, 1, 0], :byte)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "is not equal" do
|
138
|
+
r = @n !~ @m
|
139
|
+
r.should == NMatrix.new(:dense, [2,2], [1, 1, 0, 1], :byte)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "is less than" do
|
143
|
+
r = @n < @m
|
144
|
+
r.should == NMatrix.new(:dense, [2,2], 0, :byte)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "is greater than" do
|
148
|
+
r = @n > @m
|
149
|
+
r.should == NMatrix.new(:dense, [2,2], [1, 1, 0, 1], :byte)
|
150
|
+
end
|
151
|
+
|
152
|
+
it "is less than or equal to" do
|
153
|
+
r = @n <= @m
|
154
|
+
r.should == NMatrix.new(:dense, [2,2], [0, 0, 1, 0], :byte)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "is greater than or equal to" do
|
158
|
+
n = NMatrix.new(:dense, [2,2], [ 1, 2, 2, 4], :int64)
|
159
|
+
r = n >= @m
|
160
|
+
r.should == NMatrix.new(:dense, [2,2], [1, 1, 0, 1], :byte)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
data/spec/io_spec.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# = NMatrix
|
2
|
+
#
|
3
|
+
# A linear algebra library for scientific computation in Ruby.
|
4
|
+
# NMatrix is part of SciRuby.
|
5
|
+
#
|
6
|
+
# NMatrix was originally inspired by and derived from NArray, by
|
7
|
+
# Masahiro Tanaka: http://narray.rubyforge.org
|
8
|
+
#
|
9
|
+
# == Copyright Information
|
10
|
+
#
|
11
|
+
# SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012, Ruby Science Foundation
|
13
|
+
#
|
14
|
+
# Please see LICENSE.txt for additional copyright notices.
|
15
|
+
#
|
16
|
+
# == Contributing
|
17
|
+
#
|
18
|
+
# By contributing source code to SciRuby, you agree to be bound by
|
19
|
+
# our Contributor Agreement:
|
20
|
+
#
|
21
|
+
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
22
|
+
#
|
23
|
+
# == io_spec.rb
|
24
|
+
#
|
25
|
+
# Basic tests for NMatrix::IO.
|
26
|
+
#
|
27
|
+
require "./lib/nmatrix"
|
28
|
+
|
29
|
+
describe NMatrix::IO do
|
30
|
+
it "repacks a string" do
|
31
|
+
NMatrix::IO::Matlab.repack("hello", :miUINT8, :dtype => :byte).should == "hello"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "creates yale from internal byte-string function" do
|
35
|
+
n = NMatrix.new(:yale, [4,4], :byte, "\0\1\3\3\4", "\0\1\3\0\0\0\0\0\0\0\0", "\2\3\5\4", :byte)
|
36
|
+
n[0,0].should == 2
|
37
|
+
n[1,1].should == 3
|
38
|
+
n[1,3].should == 5
|
39
|
+
n[3,0].should == 4
|
40
|
+
n[2,2].should == 0
|
41
|
+
n[3,3].should == 0
|
42
|
+
end
|
43
|
+
|
44
|
+
it "reads MATLAB .mat file containing a single square sparse matrix" do
|
45
|
+
# Note: same matrix as above
|
46
|
+
n = NMatrix::IO::Matlab.load_mat("spec/4x4_sparse.mat")
|
47
|
+
n[0,0].should == 2
|
48
|
+
n[1,1].should == 3
|
49
|
+
n[1,3].should == 5
|
50
|
+
n[3,0].should == 4
|
51
|
+
n[2,2].should == 0
|
52
|
+
n[3,3].should == 0
|
53
|
+
end
|
54
|
+
|
55
|
+
it "reads MATLAB .mat file containing a single dense matrix" do
|
56
|
+
n = NMatrix::IO::Matlab.load_mat("spec/4x5_dense.mat")
|
57
|
+
m = NMatrix.new(:dense, [4,5], [16,17,18,19,20,15,14,13,12,11,6,7,8,9,10,5,4,3,2,1])
|
58
|
+
n.should == m
|
59
|
+
end
|
60
|
+
end
|
data/spec/lapack_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# = NMatrix
|
2
|
+
#
|
3
|
+
# A linear algebra library for scientific computation in Ruby.
|
4
|
+
# NMatrix is part of SciRuby.
|
5
|
+
#
|
6
|
+
# NMatrix was originally inspired by and derived from NArray, by
|
7
|
+
# Masahiro Tanaka: http://narray.rubyforge.org
|
8
|
+
#
|
9
|
+
# == Copyright Information
|
10
|
+
#
|
11
|
+
# SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
|
12
|
+
# NMatrix is Copyright (c) 2012, Ruby Science Foundation
|
13
|
+
#
|
14
|
+
# Please see LICENSE.txt for additional copyright notices.
|
15
|
+
#
|
16
|
+
# == Contributing
|
17
|
+
#
|
18
|
+
# By contributing source code to SciRuby, you agree to be bound by
|
19
|
+
# our Contributor Agreement:
|
20
|
+
#
|
21
|
+
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
22
|
+
#
|
23
|
+
# == lapack_spec.rb
|
24
|
+
#
|
25
|
+
# Tests for properly exposed LAPACK functions.
|
26
|
+
#
|
27
|
+
|
28
|
+
# Can we use require_relative here instead?
|
29
|
+
require File.join(File.dirname(__FILE__), "spec_helper.rb")
|
30
|
+
|
31
|
+
describe NMatrix::LAPACK do
|
32
|
+
[:rational32, :rational64, :rational128, :float32, :float64, :complex64, :complex128].each do |dtype|
|
33
|
+
context dtype do
|
34
|
+
it "exposes clapack getrf" do
|
35
|
+
a = NMatrix.new(:dense, 3, [4,9,2,3,5,7,8,1,6], dtype)
|
36
|
+
NMatrix::LAPACK::clapack_getrf(:row, 3, 3, a, 3)
|
37
|
+
a[0,0].should == 8
|
38
|
+
a[0,1].should == 1
|
39
|
+
a[0,2].should == 6
|
40
|
+
a[1,0].should == 0.5
|
41
|
+
a[1,1].should == 8.5
|
42
|
+
a[1,2].should == -1
|
43
|
+
a[2,0].should == 0.375
|
44
|
+
# FIXME: these are rounded, == won't work
|
45
|
+
#a[2,1].should == 0.544118
|
46
|
+
#a[2,2].should == 5.294118
|
47
|
+
end
|
48
|
+
|
49
|
+
it "exposes cblas trsm, with B as a matrix"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|