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
@@ -0,0 +1,162 @@
|
|
1
|
+
require "packable"
|
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 - 2012, Ruby Science Foundation
|
13
|
+
# NMatrix is Copyright (c) 2012, 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
|
+
# == io/matlab/mat_reader.rb
|
25
|
+
#
|
26
|
+
# Base class for .mat file reading (Matlab files).
|
27
|
+
#
|
28
|
+
|
29
|
+
require 'packable'
|
30
|
+
|
31
|
+
module NMatrix::IO::Matlab
|
32
|
+
# Class for parsing a .mat file stream.
|
33
|
+
#
|
34
|
+
# The full format of .mat files is available here:
|
35
|
+
# * http://www.mathworks.com/help/pdf_doc/matlab/matfile_format.pdf
|
36
|
+
#
|
37
|
+
class MatReader
|
38
|
+
MDTYPE_UNPACK_ARGS = {
|
39
|
+
:miINT8 => [Integer, {:signed => true, :bytes => 1}],
|
40
|
+
:miUINT8 => [Integer, {:signed => false, :bytes => 1}],
|
41
|
+
:miINT16 => [Integer, {:signed => true, :bytes => 2}],
|
42
|
+
:miUINT16 => [Integer, {:signed => false, :bytes => 2}],
|
43
|
+
:miINT32 => [Integer, {:signed => true, :bytes => 4}],
|
44
|
+
:miUINT32 => [Integer, {:signed => false, :bytes => 4}],
|
45
|
+
:miSINGLE => [Float, {:precision => :single, :bytes => 4}],
|
46
|
+
:miDOUBLE => [Float, {:precision => :double, :bytes => 8}],
|
47
|
+
:miINT64 => [Integer, {:signed => true, :bytes => 8}],
|
48
|
+
:miUINT64 => [Integer, {:signed => false, :bytes => 8}]
|
49
|
+
}
|
50
|
+
|
51
|
+
DTYPE_PACK_ARGS = {
|
52
|
+
:byte => [Integer, {:signed => false, :bytes => 1}],
|
53
|
+
:int8 => [Integer, {:signed => true, :bytes => 1}],
|
54
|
+
:int16 => [Integer, {:signed => true, :bytes => 2}],
|
55
|
+
:int32 => [Integer, {:signed => true, :bytes => 4}],
|
56
|
+
:int64 => [Integer, {:signed => true, :bytes => 8}],
|
57
|
+
:float32 => [Float, {:precision => :single, :bytes => 4}],
|
58
|
+
:float64 => [Float, {:precision => :double, :bytes => 8}],
|
59
|
+
:complex64 => [Float, {:precision => :single, :bytes => 4}], #2x
|
60
|
+
:complex128 => [Float, {:precision => :double, :bytes => 8}]
|
61
|
+
}
|
62
|
+
|
63
|
+
ITYPE_PACK_ARGS = {
|
64
|
+
:uint8 => [Integer, {:signed => false, :bytes => 1}],
|
65
|
+
:uint16 => [Integer, {:signed => false, :bytes => 2}],
|
66
|
+
:uint32 => [Integer, {:signed => false, :bytes => 4}],
|
67
|
+
:uint64 => [Integer, {:signed => false, :bytes => 8}],
|
68
|
+
}
|
69
|
+
|
70
|
+
NO_REPACK = [:miINT8, :miUINT8, :miINT16, :miINT32, :miSINGLE, :miDOUBLE, :miINT64]
|
71
|
+
|
72
|
+
# Convert from MATLAB dtype to NMatrix dtype.
|
73
|
+
MDTYPE_TO_DTYPE = {
|
74
|
+
:miUINT8 => :byte,
|
75
|
+
:miINT8 => :int8,
|
76
|
+
:miINT16 => :int16,
|
77
|
+
:miUINT16 => :int16,
|
78
|
+
:miINT32 => :int32,
|
79
|
+
:miUINT32 => :int32,
|
80
|
+
:miINT64 => :int64,
|
81
|
+
:miUINT64 => :int64,
|
82
|
+
:miSINGLE => :float32,
|
83
|
+
:miDOUBLE => :float64
|
84
|
+
}
|
85
|
+
|
86
|
+
MDTYPE_TO_ITYPE = {
|
87
|
+
:miUINT8 => :uint8,
|
88
|
+
:miINT8 => :uint8,
|
89
|
+
:miINT16 => :uint16,
|
90
|
+
:miUINT16 => :uint16,
|
91
|
+
:miINT32 => :uint32,
|
92
|
+
:miUINT32 => :uint32,
|
93
|
+
:miINT64 => :uint64,
|
94
|
+
:miUINT64 => :uint64
|
95
|
+
}
|
96
|
+
|
97
|
+
# Before release v7.1 (release 14) matlab (TM) used the system
|
98
|
+
# default character encoding scheme padded out to 16-bits. Release 14
|
99
|
+
# and later use Unicode. When saving character data, R14 checks if it
|
100
|
+
# can be encoded in 7-bit ascii, and saves in that format if so.
|
101
|
+
MDTYPES = [
|
102
|
+
nil,
|
103
|
+
:miINT8,
|
104
|
+
:miUINT8,
|
105
|
+
:miINT16,
|
106
|
+
:miUINT16,
|
107
|
+
:miINT32,
|
108
|
+
:miUINT32,
|
109
|
+
:miSINGLE,
|
110
|
+
nil,
|
111
|
+
:miDOUBLE,
|
112
|
+
nil,
|
113
|
+
nil,
|
114
|
+
:miINT64,
|
115
|
+
:miUINT64,
|
116
|
+
:miMATRIX,
|
117
|
+
:miCOMPRESSED,
|
118
|
+
:miUTF8,
|
119
|
+
:miUTF16,
|
120
|
+
:miUTF32
|
121
|
+
]
|
122
|
+
|
123
|
+
MCLASSES = [
|
124
|
+
nil,
|
125
|
+
:mxCELL,
|
126
|
+
:mxSTRUCT,
|
127
|
+
:mxOBJECT,
|
128
|
+
:mxCHAR,
|
129
|
+
:mxSPARSE,
|
130
|
+
:mxDOUBLE,
|
131
|
+
:mxSINGLE,
|
132
|
+
:mxINT8,
|
133
|
+
:mxUINT8,
|
134
|
+
:mxINT16,
|
135
|
+
:mxUINT16,
|
136
|
+
:mxINT32,
|
137
|
+
:mxUINT32,
|
138
|
+
:mxINT64,
|
139
|
+
:mxUINT64,
|
140
|
+
:mxFUNCTION,
|
141
|
+
:mxOPAQUE,
|
142
|
+
:mxOBJECT_CLASS_FROM_MATRIX_H
|
143
|
+
]
|
144
|
+
|
145
|
+
attr_reader :byte_order
|
146
|
+
|
147
|
+
def initialize(stream, options = {})
|
148
|
+
raise ArgumentError, 'First arg must be IO.' unless stream.is_a?(::IO)
|
149
|
+
|
150
|
+
@stream = stream
|
151
|
+
@byte_order = options[:byte_order] || guess_byte_order
|
152
|
+
end
|
153
|
+
|
154
|
+
def guess_byte_order
|
155
|
+
# Assume native, since we don't know what type of file we have.
|
156
|
+
:native
|
157
|
+
end
|
158
|
+
|
159
|
+
protected
|
160
|
+
attr_reader :stream
|
161
|
+
end
|
162
|
+
end
|
@@ -20,10 +20,57 @@
|
|
20
20
|
#
|
21
21
|
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
|
22
22
|
#
|
23
|
-
# ==
|
23
|
+
# == monkeys.rb
|
24
24
|
#
|
25
25
|
# Ruby core extensions for NMatrix.
|
26
26
|
|
27
|
+
#######################
|
28
|
+
# Classes and Modules #
|
29
|
+
#######################
|
30
|
+
|
31
|
+
class Array
|
32
|
+
# Convert a Ruby Array to an NMatrix.
|
33
|
+
#
|
34
|
+
# You must provide a shape for the matrix as the first argument.
|
35
|
+
#
|
36
|
+
# == Arguments:
|
37
|
+
# <tt>shape</tt> :: Array describing matrix dimensions (or Fixnum for square) -- REQUIRED!
|
38
|
+
# <tt>dtype</tt> :: Override data type (e.g., to store a Float as :float32 instead of :float64) -- optional.
|
39
|
+
# <tt>stype</tt> :: Optional storage type (defaults to :dense)
|
40
|
+
def to_nm(shape, dtype = nil, stype = :dense)
|
41
|
+
dtype ||=
|
42
|
+
case self[0]
|
43
|
+
when Fixnum then :int64
|
44
|
+
when Float then :float64
|
45
|
+
when Rational then :rational128
|
46
|
+
when Complex then :complex128
|
47
|
+
end
|
48
|
+
|
49
|
+
matrix = NMatrix.new(:dense, shape, self, dtype)
|
50
|
+
|
51
|
+
if stype != :dense then matrix.cast(stype, dtype) else matrix end
|
52
|
+
end
|
53
|
+
|
54
|
+
unless method_defined?(:max)
|
55
|
+
def max
|
56
|
+
self.inject(self.first) { |m, n| if n > m then n else m end }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
unless method_defined?(:min)
|
61
|
+
def min
|
62
|
+
self.inject(self.first) { |m, n| if n < m then n else m end }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class Object
|
68
|
+
def returning(value)
|
69
|
+
yield(value)
|
70
|
+
value
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
27
74
|
class String #:nodoc:
|
28
75
|
unless method_defined?(:constantize)
|
29
76
|
# Based on constantize from ActiveSupport::Inflector
|
@@ -62,4 +109,4 @@ class String #:nodoc:
|
|
62
109
|
word
|
63
110
|
end
|
64
111
|
end
|
65
|
-
end
|
112
|
+
end
|
@@ -0,0 +1,199 @@
|
|
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.rb
|
24
|
+
#
|
25
|
+
# This file adds a few additional pieces of functionality (e.g., inspect,
|
26
|
+
# pretty_print).
|
27
|
+
|
28
|
+
############
|
29
|
+
# Requires #
|
30
|
+
############
|
31
|
+
|
32
|
+
#######################
|
33
|
+
# Classes and Modules #
|
34
|
+
#######################
|
35
|
+
|
36
|
+
class NMatrix
|
37
|
+
# Read and write extensions for NMatrix. These are only loaded when needed.
|
38
|
+
module IO
|
39
|
+
module Matlab
|
40
|
+
class << self
|
41
|
+
def load_mat file_path
|
42
|
+
NMatrix::IO::Matlab::Mat5Reader.new(File.open(file_path, "rb+")).to_ruby
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# FIXME: Remove autoloads
|
47
|
+
autoload :MatReader, 'nmatrix/io/mat_reader'
|
48
|
+
autoload :Mat5Reader, 'nmatrix/io/mat5_reader'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# TODO: Make this actually pretty.
|
53
|
+
def pretty_print(q = nil)
|
54
|
+
if dim != 2 || (dim == 2 && shape[1] > 10) # FIXME: Come up with a better way of restricting the display
|
55
|
+
inspect
|
56
|
+
else
|
57
|
+
|
58
|
+
arr = (0...shape[0]).map do |i|
|
59
|
+
ary = []
|
60
|
+
(0...shape[1]).each do |j|
|
61
|
+
o = begin
|
62
|
+
self[i, j]
|
63
|
+
rescue ArgumentError
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
ary << (o.nil? ? 'nil' : o)
|
67
|
+
end
|
68
|
+
ary.inspect
|
69
|
+
end
|
70
|
+
|
71
|
+
if q.nil?
|
72
|
+
puts arr.join("\n")
|
73
|
+
else
|
74
|
+
q.group(1, "", "\n") do
|
75
|
+
q.seplist(arr, lambda { q.text " " }, :each) { |v| q.text v.to_s }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
alias :pp :pretty_print
|
82
|
+
|
83
|
+
|
84
|
+
# Get the complex conjugate of this matrix. See also complex_conjugate! for
|
85
|
+
# an in-place operation (provided the dtype is already :complex64 or
|
86
|
+
# :complex128).
|
87
|
+
#
|
88
|
+
# Does not work on list matrices, but you can optionally pass in the type you
|
89
|
+
# want to cast to if you're dealing with a list matrix.
|
90
|
+
def complex_conjugate(new_stype = self.stype)
|
91
|
+
self.cast(new_stype, NMatrix::upcast(dtype, :complex64)).complex_conjugate!
|
92
|
+
end
|
93
|
+
|
94
|
+
# Calculate the conjugate transpose of a matrix. If your dtype is already
|
95
|
+
# complex, this should only require one copy (for the transpose).
|
96
|
+
def conjugate_transpose
|
97
|
+
self.transpose.complex_conjugate!
|
98
|
+
end
|
99
|
+
|
100
|
+
def hermitian?
|
101
|
+
return false if self.dim != 2 or self.shape[0] != self.shape[1]
|
102
|
+
|
103
|
+
if [:complex64, :complex128].include?(self.dtype)
|
104
|
+
# TODO: Write much faster Hermitian test in C
|
105
|
+
self.eql?(self.conjugate_transpose)
|
106
|
+
else
|
107
|
+
symmetric?
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def inspect
|
112
|
+
original_inspect = super()
|
113
|
+
original_inspect = original_inspect[0...original_inspect.size-1]
|
114
|
+
original_inspect + inspect_helper.join(" ") + ">"
|
115
|
+
end
|
116
|
+
|
117
|
+
def __yale_ary__to_s(sym)
|
118
|
+
ary = self.send("__yale_#{sym.to_s}__".to_sym)
|
119
|
+
|
120
|
+
'[' + ary.collect { |a| a ? a : 'nil'}.join(',') + ']'
|
121
|
+
end
|
122
|
+
|
123
|
+
class << self
|
124
|
+
def load_file(file_path)
|
125
|
+
NMatrix::IO::Mat5Reader.new(File.open(file_path, 'rb')).to_ruby
|
126
|
+
end
|
127
|
+
|
128
|
+
# Helper function for loading a file in the first sparse format given here:
|
129
|
+
# http://math.nist.gov/MatrixMarket/formats.html
|
130
|
+
#
|
131
|
+
# Override type specifier (e.g., 'real') using :read_with => :to_f (or any other string-to-numeric conversion
|
132
|
+
# function), and with :dtype => :float32 or :dtype => :int8 to force storage in a lesser type.
|
133
|
+
def load_matrix_matrix_coordinate_file(filename, options = {})
|
134
|
+
f = File.new(filename, "r")
|
135
|
+
|
136
|
+
func = options[:read_with]
|
137
|
+
dtype = options[:dtype]
|
138
|
+
|
139
|
+
line = f.gets
|
140
|
+
raise IOError, 'Incorrect file type specifier.' unless line =~ /^%%MatrixMarket\ matrix\ coordinate/
|
141
|
+
spec = line.split
|
142
|
+
|
143
|
+
case spec[3]
|
144
|
+
when 'real'
|
145
|
+
func ||= :to_f
|
146
|
+
dtype ||= :float64
|
147
|
+
when 'integer'
|
148
|
+
func ||= :to_i
|
149
|
+
dtype ||= :int64
|
150
|
+
when 'complex'
|
151
|
+
func ||= :to_complex
|
152
|
+
dtype ||= :complex128
|
153
|
+
when 'rational'
|
154
|
+
func ||= :to_rational
|
155
|
+
dtype ||= :rational128
|
156
|
+
else
|
157
|
+
raise ArgumentError, 'Unrecognized dtype.'
|
158
|
+
end unless func and dtype
|
159
|
+
|
160
|
+
begin
|
161
|
+
line = f.gets
|
162
|
+
end while line =~ /^%/
|
163
|
+
|
164
|
+
# Close the file.
|
165
|
+
f.close
|
166
|
+
|
167
|
+
rows, cols, entries = line.split.collect { |x| x.to_i }
|
168
|
+
|
169
|
+
matrix = NMatrix.new(:yale, [rows, cols], entries, dtype)
|
170
|
+
|
171
|
+
entries.times do
|
172
|
+
i, j, v = line.split
|
173
|
+
matrix[i.to_i - 1, j.to_i - 1] = v.send(func)
|
174
|
+
end
|
175
|
+
|
176
|
+
matrix
|
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}"
|
187
|
+
|
188
|
+
# These are enabled by the DEBUG_YALE compiler flag in extconf.rb.
|
189
|
+
if respond_to?(:__yale_a__)
|
190
|
+
ary << "ija:#{__yale_ary__to_s(:ija)}" << "ia:#{__yale_ary__to_s(:ia)}" <<
|
191
|
+
"ja:#{__yale_ary__to_s(:ja)}" << "a:#{__yale_ary__to_s(:a)}" << "d:#{__yale_ary__to_s(:d)}" <<
|
192
|
+
"lu:#{__yale_ary__to_s(:lu)}" << "yale_size:#{__yale_size__}"
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
ary
|
198
|
+
end
|
199
|
+
end
|
@@ -0,0 +1,103 @@
|
|
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.rb
|
24
|
+
#
|
25
|
+
# This file defines the NVector class.
|
26
|
+
|
27
|
+
############
|
28
|
+
# Requires #
|
29
|
+
############
|
30
|
+
|
31
|
+
|
32
|
+
#######################
|
33
|
+
# Classes and Modules #
|
34
|
+
#######################
|
35
|
+
|
36
|
+
# This is a specific type of NMatrix in which only one dimension is not 1.
|
37
|
+
# Although it is stored as a dim-2, n x 1, matrix, it acts as a dim-1 vector
|
38
|
+
# of size n. If the @orientation flag is set to :row, it is stored as 1 x n
|
39
|
+
# instead of n x 1.
|
40
|
+
class NVector < NMatrix
|
41
|
+
def initialize(length, *args)
|
42
|
+
super(:dense, [length, 1], *args)
|
43
|
+
orientation
|
44
|
+
end
|
45
|
+
|
46
|
+
# Orientation defaults to column (e.g., [3,1] is a column of length 3). It
|
47
|
+
# may also be row, e.g., for [1,5].
|
48
|
+
def orientation
|
49
|
+
@orientation ||= :column
|
50
|
+
end
|
51
|
+
|
52
|
+
def transpose
|
53
|
+
t = super()
|
54
|
+
t.flip!
|
55
|
+
end
|
56
|
+
|
57
|
+
def transpose!
|
58
|
+
super()
|
59
|
+
self.flip!
|
60
|
+
end
|
61
|
+
|
62
|
+
def multiply(m)
|
63
|
+
t = super(m)
|
64
|
+
t.flip!
|
65
|
+
end
|
66
|
+
|
67
|
+
def multiply!(m)
|
68
|
+
super().flip!
|
69
|
+
end
|
70
|
+
|
71
|
+
def [](i)
|
72
|
+
case @orientation
|
73
|
+
when :column; super(i, 0)
|
74
|
+
when :row; super(0, i)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def []=(i, val)
|
79
|
+
case @orientation
|
80
|
+
when :column; super(i, 0, val)
|
81
|
+
when :row; super(0, i, val)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def dim; 1; end
|
86
|
+
|
87
|
+
# TODO: Make this actually pretty.
|
88
|
+
def pretty_print
|
89
|
+
dim = @orientation == :row ? 1 : 0
|
90
|
+
|
91
|
+
puts (0...shape[dim]).inject(Array.new) { |a, i| a << self[i] }.join(' ')
|
92
|
+
end
|
93
|
+
|
94
|
+
protected
|
95
|
+
def inspect_helper
|
96
|
+
super() << "orientation:#{self.orientation}"
|
97
|
+
end
|
98
|
+
|
99
|
+
def flip_orientation!
|
100
|
+
returning(self) { @orientation = @orientation == :row ? :column : :row }
|
101
|
+
end
|
102
|
+
alias :flip! :flip_orientation!
|
103
|
+
end
|
@@ -0,0 +1,27 @@
|
|
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
|
+
|
24
|
+
class NMatrix
|
25
|
+
VERSION = '0.0.2'
|
26
|
+
end
|
27
|
+
|