multiarray 0.22.0 → 0.23.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/multiarray.rb +53 -16
- data/lib/multiarray/bool.rb +1 -1
- data/lib/multiarray/complex.rb +76 -68
- data/lib/multiarray/components.rb +11 -10
- data/lib/multiarray/composite.rb +1 -1
- data/lib/multiarray/diagonal.rb +11 -12
- data/lib/multiarray/element.rb +3 -3
- data/lib/multiarray/elementwise.rb +14 -14
- data/lib/multiarray/field.rb +380 -0
- data/lib/multiarray/float.rb +10 -10
- data/lib/multiarray/gcccache.rb +1 -1
- data/lib/multiarray/gcccontext.rb +35 -54
- data/lib/multiarray/gccfunction.rb +12 -19
- data/lib/multiarray/gcctype.rb +1 -1
- data/lib/multiarray/gccvalue.rb +63 -43
- data/lib/multiarray/histogram.rb +17 -19
- data/lib/multiarray/index.rb +7 -8
- data/lib/multiarray/inject.rb +11 -12
- data/lib/multiarray/int.rb +12 -11
- data/lib/multiarray/integral.rb +11 -12
- data/lib/multiarray/lambda.rb +23 -18
- data/lib/multiarray/list.rb +1 -1
- data/lib/multiarray/lookup.rb +18 -13
- data/lib/multiarray/lut.rb +13 -16
- data/lib/multiarray/malloc.rb +1 -1
- data/lib/multiarray/mask.rb +11 -8
- data/lib/multiarray/methods.rb +10 -10
- data/lib/multiarray/multiarray.rb +15 -44
- data/lib/multiarray/node.rb +64 -138
- data/lib/multiarray/object.rb +2 -6
- data/lib/multiarray/operations.rb +116 -134
- data/lib/multiarray/pointer.rb +7 -19
- data/lib/multiarray/random.rb +11 -8
- data/lib/multiarray/rgb.rb +53 -53
- data/lib/multiarray/sequence.rb +11 -496
- data/lib/multiarray/shortcuts.rb +4 -4
- data/lib/multiarray/store.rb +14 -11
- data/lib/multiarray/unmask.rb +10 -7
- data/lib/multiarray/variable.rb +11 -3
- data/test/tc_bool.rb +0 -8
- data/test/tc_compile.rb +72 -0
- data/test/tc_float.rb +0 -8
- data/test/tc_int.rb +0 -8
- data/test/tc_lazy.rb +22 -3
- data/test/tc_multiarray.rb +100 -126
- data/test/tc_object.rb +0 -16
- data/test/tc_rgb.rb +0 -16
- data/test/tc_sequence.rb +151 -165
- data/test/ts_multiarray.rb +2 -0
- metadata +7 -4
data/lib/multiarray/shortcuts.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# multiarray - Lazy multi-dimensional arrays for Ruby
|
2
|
-
# Copyright (C) 2010 Jan Wedekind
|
2
|
+
# Copyright (C) 2010, 2011 Jan Wedekind
|
3
3
|
#
|
4
4
|
# This program is free software: you can redistribute it and/or modify
|
5
5
|
# it under the terms of the GNU General Public License as published by
|
@@ -28,7 +28,7 @@ module Hornetseye
|
|
28
28
|
#
|
29
29
|
# @private
|
30
30
|
def constructor_shortcut( target )
|
31
|
-
define_method
|
31
|
+
define_method target.to_s.downcase do |*args|
|
32
32
|
new target, *args
|
33
33
|
end
|
34
34
|
end
|
@@ -77,7 +77,7 @@ module Hornetseye
|
|
77
77
|
#
|
78
78
|
# @private
|
79
79
|
def to_type_shortcut( target )
|
80
|
-
define_method
|
80
|
+
define_method "to_#{target.to_s.downcase}" do
|
81
81
|
to_type target
|
82
82
|
end
|
83
83
|
end
|
@@ -124,7 +124,7 @@ module Hornetseye
|
|
124
124
|
#
|
125
125
|
# @private
|
126
126
|
def read_shortcut( target )
|
127
|
-
define_method
|
127
|
+
define_method "read_#{target.to_s.downcase}" do
|
128
128
|
read.to_type target
|
129
129
|
end
|
130
130
|
end
|
data/lib/multiarray/store.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# multiarray - Lazy multi-dimensional arrays for Ruby
|
2
|
-
# Copyright (C) 2010 Jan Wedekind
|
2
|
+
# Copyright (C) 2010, 2011 Jan Wedekind
|
3
3
|
#
|
4
4
|
# This program is free software: you can redistribute it and/or modify
|
5
5
|
# it under the terms of the GNU General Public License as published by
|
@@ -29,6 +29,10 @@ module Hornetseye
|
|
29
29
|
@dest, @source = dest, source
|
30
30
|
end
|
31
31
|
|
32
|
+
def sexp?
|
33
|
+
true
|
34
|
+
end
|
35
|
+
|
32
36
|
# Get unique descriptor of this object
|
33
37
|
#
|
34
38
|
# @param [Hash] hash Labels for any variables.
|
@@ -40,13 +44,12 @@ module Hornetseye
|
|
40
44
|
"Store(#{@dest.descriptor( hash )},#{@source.descriptor( hash )})"
|
41
45
|
end
|
42
46
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
@dest.array_type
|
47
|
+
def typecode
|
48
|
+
@dest.typecode
|
49
|
+
end
|
50
|
+
|
51
|
+
def shape
|
52
|
+
@dest.shape
|
50
53
|
end
|
51
54
|
|
52
55
|
# Reevaluate computation
|
@@ -60,10 +63,10 @@ module Hornetseye
|
|
60
63
|
if variables.empty?
|
61
64
|
if dimension > 0
|
62
65
|
shape.last.times do |i|
|
63
|
-
dest = @dest.element INT.new(
|
66
|
+
dest = @dest.element INT.new(i)
|
64
67
|
source = @source.dimension == 0 ? @source :
|
65
|
-
@source.element(
|
66
|
-
Store.new(
|
68
|
+
@source.element(INT.new(i))
|
69
|
+
Store.new(dest, source).demand
|
67
70
|
end
|
68
71
|
elsif @dest.class < Pointer_
|
69
72
|
@dest.store @source.demand
|
data/lib/multiarray/unmask.rb
CHANGED
@@ -45,6 +45,10 @@ module Hornetseye
|
|
45
45
|
@dest, @source, @m, @index, @default = dest, source, m, index, default
|
46
46
|
end
|
47
47
|
|
48
|
+
def sexp?
|
49
|
+
true
|
50
|
+
end
|
51
|
+
|
48
52
|
# Get unique descriptor of this object
|
49
53
|
#
|
50
54
|
# @param [Hash] hash Labels for any variables.
|
@@ -58,13 +62,12 @@ module Hornetseye
|
|
58
62
|
"#{@default.descriptor( hash )})"
|
59
63
|
end
|
60
64
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
@dest.array_type
|
65
|
+
def typecode
|
66
|
+
@dest.typecode
|
67
|
+
end
|
68
|
+
|
69
|
+
def shape
|
70
|
+
@dest.shape
|
68
71
|
end
|
69
72
|
|
70
73
|
# Perform masking operation
|
data/lib/multiarray/variable.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# multiarray - Lazy multi-dimensional arrays for Ruby
|
2
|
-
# Copyright (C) 2010 Jan Wedekind
|
2
|
+
# Copyright (C) 2010, 2011 Jan Wedekind
|
3
3
|
#
|
4
4
|
# This program is free software: you can redistribute it and/or modify
|
5
5
|
# it under the terms of the GNU General Public License as published by
|
@@ -38,6 +38,10 @@ module Hornetseye
|
|
38
38
|
@meta = meta
|
39
39
|
end
|
40
40
|
|
41
|
+
def sexp?
|
42
|
+
true
|
43
|
+
end
|
44
|
+
|
41
45
|
# Display string with information about this object
|
42
46
|
#
|
43
47
|
# @return [String] String with information about this object (e.g.
|
@@ -90,8 +94,12 @@ module Hornetseye
|
|
90
94
|
# @return [Class] Type of result.
|
91
95
|
#
|
92
96
|
# @private
|
93
|
-
def
|
94
|
-
@meta.
|
97
|
+
def typecode
|
98
|
+
@meta.typecode
|
99
|
+
end
|
100
|
+
|
101
|
+
def shape
|
102
|
+
@meta.shape
|
95
103
|
end
|
96
104
|
|
97
105
|
# Strip of all values
|
data/test/tc_bool.rb
CHANGED
@@ -55,14 +55,6 @@ class TC_Bool < Test::Unit::TestCase
|
|
55
55
|
assert_equal 0, B.dimension
|
56
56
|
end
|
57
57
|
|
58
|
-
def test_bool_shape
|
59
|
-
assert_equal [], B.shape
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_bool_size
|
63
|
-
assert_equal 1, B.size
|
64
|
-
end
|
65
|
-
|
66
58
|
def test_inspect
|
67
59
|
assert_equal 'BOOL(true)', B( true ).inspect
|
68
60
|
end
|
data/test/tc_compile.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# multiarray - Lazy multi-dimensional arrays for Ruby
|
2
|
+
# Copyright (C) 2010 Jan Wedekind
|
3
|
+
#
|
4
|
+
# This program is free software: you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation, either version 3 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
#
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16
|
+
|
17
|
+
require 'test/unit'
|
18
|
+
begin
|
19
|
+
require 'rubygems'
|
20
|
+
rescue LoadError
|
21
|
+
end
|
22
|
+
Kernel::require 'multiarray'
|
23
|
+
|
24
|
+
class TC_Compile < Test::Unit::TestCase
|
25
|
+
|
26
|
+
B = Hornetseye::BYTE
|
27
|
+
W = Hornetseye::SINT
|
28
|
+
S = Hornetseye::Sequence
|
29
|
+
M = Hornetseye::MultiArray
|
30
|
+
|
31
|
+
def S( *args )
|
32
|
+
Hornetseye::Sequence *args
|
33
|
+
end
|
34
|
+
|
35
|
+
def M( *args )
|
36
|
+
Hornetseye::MultiArray *args
|
37
|
+
end
|
38
|
+
|
39
|
+
def lazy( *args, &action )
|
40
|
+
Hornetseye::lazy *args, &action
|
41
|
+
end
|
42
|
+
|
43
|
+
def finalise( *args, &action )
|
44
|
+
Hornetseye::finalise *args, &action
|
45
|
+
end
|
46
|
+
|
47
|
+
def sum( *args, &action )
|
48
|
+
Hornetseye::sum *args, &action
|
49
|
+
end
|
50
|
+
|
51
|
+
def setup
|
52
|
+
end
|
53
|
+
|
54
|
+
def teardown
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_unary
|
58
|
+
assert_equal S(W)[-1, -2, -3], -S(W)[1, 2, 3]
|
59
|
+
assert_equal S(W)[-1, -2, -3], -S(W)[1, 2, 3]
|
60
|
+
assert !lazy { -S(W)[1, 2, 3] }.finalised?
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_binary
|
64
|
+
assert_equal S(W)[2, 3, 4], S(W)[1, 2, 3] + 1
|
65
|
+
assert_equal S(W)[2, 3, 4], S(W)[1, 2, 3] + 1
|
66
|
+
assert !lazy { S(W)[1, 2, 3] + 1 }.finalised?
|
67
|
+
assert_equal S(W)[257, 258, 259], S(W)[1, 2, 3] + 256
|
68
|
+
assert_equal S(W)[2, 4, 6], S(W)[1, 2, 3] + S(W)[1, 2, 3]
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
data/test/tc_float.rb
CHANGED
@@ -73,14 +73,6 @@ class TC_Float < Test::Unit::TestCase
|
|
73
73
|
def test_float_dimension
|
74
74
|
assert_equal 0, F.dimension
|
75
75
|
end
|
76
|
-
!
|
77
|
-
def test_float_shape
|
78
|
-
assert_equal [], F.shape
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_float_size
|
82
|
-
assert_equal 1, F.size
|
83
|
-
end
|
84
76
|
|
85
77
|
def test_inspect
|
86
78
|
assert_equal 'DFLOAT(42.0)', D( 42.0 ).inspect
|
data/test/tc_int.rb
CHANGED
@@ -88,14 +88,6 @@ class TC_Int < Test::Unit::TestCase
|
|
88
88
|
def test_int_dimension
|
89
89
|
assert_equal 0, I.dimension
|
90
90
|
end
|
91
|
-
!
|
92
|
-
def test_int_shape
|
93
|
-
assert_equal [], I.shape
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_int_size
|
97
|
-
assert_equal 1, I.size
|
98
|
-
end
|
99
91
|
|
100
92
|
def test_inspect
|
101
93
|
assert_equal 'INT(42)', I( 42 ).inspect
|
data/test/tc_lazy.rb
CHANGED
@@ -37,6 +37,14 @@ class TC_Lazy < Test::Unit::TestCase
|
|
37
37
|
Hornetseye::MultiArray *args
|
38
38
|
end
|
39
39
|
|
40
|
+
def C( *args )
|
41
|
+
Hornetseye::RGB *args
|
42
|
+
end
|
43
|
+
|
44
|
+
def X( *args )
|
45
|
+
Complex *args
|
46
|
+
end
|
47
|
+
|
40
48
|
def lazy( *args, &action )
|
41
49
|
Hornetseye::lazy *args, &action
|
42
50
|
end
|
@@ -115,12 +123,23 @@ class TC_Lazy < Test::Unit::TestCase
|
|
115
123
|
|
116
124
|
def test_indgen_diagonal
|
117
125
|
assert_equal [ 15, 18, 17 ],
|
118
|
-
M(
|
126
|
+
M(I, 2).indgen(4, 3).diagonal { |a,b| a + b }.to_a
|
119
127
|
end
|
120
128
|
|
121
129
|
def test_lut
|
122
|
-
assert_equal S( I
|
123
|
-
finalise { S( I
|
130
|
+
assert_equal S( I )[ 4, 2, 3, 2 ],
|
131
|
+
finalise { S( I )[ 0, 2, 1, 2 ].lut( S( I )[ 3, 2, 1 ] ) + 1 }
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_complex
|
135
|
+
assert_equal [1, 1], lazy(2) { X 1, 2 }.real.to_a
|
136
|
+
assert_equal [2, 2], lazy(2) { X 1, 2 }.imag.to_a
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_rgb
|
140
|
+
assert_equal [1, 1], lazy(2) { C 1, 2, 3 }.r.to_a
|
141
|
+
assert_equal [2, 2], lazy(2) { C 1, 2, 3 }.g.to_a
|
142
|
+
assert_equal [3, 3], lazy(2) { C 1, 2, 3 }.b.to_a
|
124
143
|
end
|
125
144
|
|
126
145
|
end
|
data/test/tc_multiarray.rb
CHANGED
@@ -63,69 +63,43 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def test_multiarray_inspect
|
66
|
-
assert_equal 'MultiArray(OBJECT,
|
67
|
-
assert_equal 'MultiArray(OBJECT,
|
66
|
+
assert_equal 'MultiArray(OBJECT,2)', M(O,2).inspect
|
67
|
+
assert_equal 'MultiArray(OBJECT,2)', S(S(O)).inspect
|
68
68
|
end
|
69
69
|
|
70
70
|
def test_multiarray_to_s
|
71
|
-
assert_equal 'MultiArray(OBJECT,
|
72
|
-
assert_equal 'MultiArray(OBJECT,
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_multiarray_default
|
76
|
-
assert_equal [ [ nil ] * 3 ] * 2, M( O, 3, 2 ).default.to_a
|
77
|
-
assert_equal [ [ 0 ] * 3 ] * 2, M( I, 3, 2 ).default.to_a
|
78
|
-
assert_equal [ [ C( 0, 0, 0 ) ] * 3 ] * 2, M( C, 3, 2 ).default.to_a
|
71
|
+
assert_equal 'MultiArray(OBJECT,2)', M(O,2).to_s
|
72
|
+
assert_equal 'MultiArray(OBJECT,2)', S(S(O)).to_s
|
79
73
|
end
|
80
74
|
|
81
75
|
def test_multiarray_at
|
82
76
|
assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
|
83
77
|
M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].to_a
|
84
78
|
assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
|
85
|
-
M(
|
79
|
+
M(O, 2)[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].to_a
|
86
80
|
assert_equal O, M[ [ :a ] ].typecode
|
87
81
|
assert_equal B, M[ [ false ], [ true ] ].typecode
|
88
82
|
assert_equal I, M[ [ -2 ** 31, 2 ** 31 - 1 ] ].typecode
|
89
83
|
end
|
90
84
|
|
91
85
|
def test_multiarray_indgen
|
92
|
-
assert_equal M(
|
93
|
-
M(
|
94
|
-
assert_equal M(
|
95
|
-
M(
|
96
|
-
assert_equal M(
|
97
|
-
M(
|
98
|
-
assert_equal M(
|
99
|
-
M(
|
100
|
-
assert_equal M(
|
101
|
-
|
102
|
-
M(
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_multiarray_typecode
|
106
|
-
assert_equal O, M( O, 3, 2 ).typecode
|
107
|
-
assert_equal I, M( I, 3, 2 ).typecode
|
108
|
-
assert_equal C, M( C, 3, 2 ).typecode
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_multiarray_dimension
|
112
|
-
assert_equal 2, M( O, 3, 2 ).dimension
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_multiarray_shape
|
116
|
-
assert_equal [ 3, 2 ], M( O, 3, 2 ).shape
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_multiarray_size
|
120
|
-
assert_equal 6, M( O, 3, 2 ).size
|
121
|
-
assert_equal 6, M( I, 3, 2 ).size
|
122
|
-
assert_equal 6, M( C, 3, 2 ).size
|
86
|
+
assert_equal M(I, 2)[ [ 0, 1, 2 ], [ 3, 4, 5 ] ],
|
87
|
+
M(I, 2).indgen(3, 2)
|
88
|
+
assert_equal M(I, 2)[ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
|
89
|
+
M(I, 2).indgen(3, 2, 1)
|
90
|
+
assert_equal M(I, 2)[ [ 0, 2, 4 ], [ 6, 8, 10 ] ],
|
91
|
+
M(I, 2).indgen(3, 2, 0, 2)
|
92
|
+
assert_equal M(I, 2)[ [ 1, 3, 5 ], [ 7, 9, 11 ] ],
|
93
|
+
M(I, 2).indgen(3, 2, 1, 2)
|
94
|
+
assert_equal M(C, 2)[ [ C( 1, 2, 3 ), C( 2, 2, 2 ) ],
|
95
|
+
[ C( 3, 2, 1 ), C( 4, 2, 0 ) ] ],
|
96
|
+
M(C, 2).indgen(2, 2, C( 1, 2, 3 ), C( 1, 0, -1 ))
|
123
97
|
end
|
124
98
|
|
125
99
|
def test_inspect
|
126
|
-
assert_equal "MultiArray(OBJECT,
|
100
|
+
assert_equal "MultiArray(OBJECT,2):\n[ [ :a, 2, 3 ],\n [ 4, 5, 6 ] ]",
|
127
101
|
M[ [ :a, 2, 3 ], [ 4, 5, 6 ] ].inspect
|
128
|
-
assert_equal "MultiArray(UBYTE,
|
102
|
+
assert_equal "MultiArray(UBYTE,3):\n" +
|
129
103
|
"[ [ [ 0, 1, 2, 3 ],\n" +
|
130
104
|
" [ 4, 5, 6, 7 ],\n" +
|
131
105
|
" [ 8, 9, 10, 11 ] ],\n" +
|
@@ -148,23 +122,23 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
148
122
|
end
|
149
123
|
|
150
124
|
def test_typecode
|
151
|
-
assert_equal O, M(
|
152
|
-
assert_equal I, M(
|
153
|
-
assert_equal C, M(
|
125
|
+
assert_equal O, M(O, 2).new(3, 2).typecode
|
126
|
+
assert_equal I, M(I, 2).new(3, 2).typecode
|
127
|
+
assert_equal C, M(C, 2).new(3, 2).typecode
|
154
128
|
end
|
155
129
|
|
156
130
|
def test_dimension
|
157
|
-
assert_equal 2, M(
|
158
|
-
assert_equal 2, M(
|
159
|
-
assert_equal 2, M(
|
131
|
+
assert_equal 2, M(O, 2).new(3, 2).dimension
|
132
|
+
assert_equal 2, M(I, 2).new(3, 2).dimension
|
133
|
+
assert_equal 2, M(C, 2).new(3, 2).dimension
|
160
134
|
end
|
161
135
|
|
162
136
|
def test_shape
|
163
|
-
assert_equal [ 3, 2 ], M(
|
137
|
+
assert_equal [ 3, 2 ], M(O, 2).new(3, 2).shape
|
164
138
|
end
|
165
139
|
|
166
140
|
def test_size
|
167
|
-
assert_equal 6, M(
|
141
|
+
assert_equal 6, M(O, 2).new(3, 2).size
|
168
142
|
end
|
169
143
|
|
170
144
|
def test_import
|
@@ -177,8 +151,8 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
177
151
|
end
|
178
152
|
|
179
153
|
def test_at_assign
|
180
|
-
[ M(
|
181
|
-
m = t.new
|
154
|
+
[ M(O, 2), M(I, 2) ].each do |t|
|
155
|
+
m = t.new 3, 2
|
182
156
|
for j in 0 ... 2
|
183
157
|
for i in 0 ... 3
|
184
158
|
assert_equal j * 3 + i + 1, m[ j ][ i ] = j * 3 + i + 1
|
@@ -216,8 +190,8 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
216
190
|
end
|
217
191
|
|
218
192
|
def test_slice
|
219
|
-
[ M(
|
220
|
-
m = t.indgen[]
|
193
|
+
[ M(O, 2), M(I, 2) ].each do |t|
|
194
|
+
m = t.indgen(5, 4)[]
|
221
195
|
assert_equal [ [ 5, 10 ], [ 6, 11 ], [ 7, 12 ], [ 8, 13 ], [ 9, 14 ] ],
|
222
196
|
m[ 1 .. 2 ].to_a
|
223
197
|
assert_equal [ [ 6, 7, 8 ], [ 11, 12, 13 ] ],
|
@@ -305,27 +279,27 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
305
279
|
end
|
306
280
|
end
|
307
281
|
|
308
|
-
|
309
|
-
[ M(
|
310
|
-
m = t[
|
311
|
-
v = m[
|
282
|
+
def test_view
|
283
|
+
[ M(O, 2), M(I, 2) ].each do |t|
|
284
|
+
m = t[[1, 2, 3], [4, 5, 6]]
|
285
|
+
v = m[1, 0 ... 2]
|
312
286
|
v[] = 0
|
313
|
-
assert_equal [
|
287
|
+
assert_equal [[1, 0, 3], [4, 0, 6]], m.to_a
|
314
288
|
end
|
315
289
|
end
|
316
290
|
|
317
291
|
def test_transpose
|
318
292
|
assert_equal [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ],
|
319
|
-
M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].transpose(
|
293
|
+
M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].transpose(1, 0).to_a
|
320
294
|
assert_equal [ [ [ 0, 3 ], [ 1, 4 ], [ 2, 5 ] ] ],
|
321
|
-
M(
|
322
|
-
|
295
|
+
M(I, 3).indgen(3, 2, 1).transpose(1, 0, 2).to_a
|
296
|
+
end
|
323
297
|
|
324
298
|
def test_roll_unroll
|
325
299
|
assert_equal [ [ [ 0 ], [ 1 ], [ 2 ] ], [ [ 3 ], [ 4 ], [ 5 ] ] ],
|
326
|
-
M(
|
300
|
+
M(I, 3).indgen(3, 2, 1).unroll.to_a
|
327
301
|
assert_equal [ [ [ 0, 3 ] ], [ [ 1, 4 ] ], [ [ 2, 5 ] ] ],
|
328
|
-
M(
|
302
|
+
M(I, 3).indgen(3, 2, 1).roll.to_a
|
329
303
|
end
|
330
304
|
|
331
305
|
def test_equal
|
@@ -361,8 +335,8 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
361
335
|
end
|
362
336
|
|
363
337
|
def test_inject
|
364
|
-
assert_equal 21, M[
|
365
|
-
assert_equal 28, M[
|
338
|
+
assert_equal 21, M[[1, 2, 3], [4, 5, 6]].inject { |a,b| a + b }
|
339
|
+
assert_equal 28, M[[1, 2, 3], [4, 5, 6]].inject(7) { |a,b| a + b }
|
366
340
|
end
|
367
341
|
|
368
342
|
def test_collect
|
@@ -415,12 +389,12 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
415
389
|
[ 'b1', 'b2', 'b3' ],
|
416
390
|
[ 'c1', 'c2', 'c3' ],
|
417
391
|
[ 'd1', 'd2', 'd3' ] ].diagonal( 'x' ) { |a,b| a + b }
|
418
|
-
assert_equal S(
|
419
|
-
M(
|
420
|
-
assert_equal S(
|
421
|
-
M(
|
422
|
-
assert_equal S(
|
423
|
-
M(
|
392
|
+
assert_equal S(I)[ 4, 12, 21, 18 ],
|
393
|
+
M(I, 2).indgen(3, 4).diagonal { |a,b| a + b }
|
394
|
+
assert_equal S(I)[ 4, 12, 12 ],
|
395
|
+
M(I, 2).indgen(3, 3).diagonal { |a,b| a + b }
|
396
|
+
assert_equal S(I)[ 4, 6 ],
|
397
|
+
M(I, 2).indgen(3, 2).diagonal { |a,b| a + b }
|
424
398
|
end
|
425
399
|
|
426
400
|
def test_convolve
|
@@ -506,41 +480,41 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
506
480
|
end
|
507
481
|
|
508
482
|
def test_warp
|
509
|
-
[
|
510
|
-
[
|
483
|
+
[O, I].each do |t1|
|
484
|
+
[O, I].each do |t2|
|
511
485
|
z = t1.default
|
512
|
-
assert_equal M(
|
513
|
-
M(
|
514
|
-
warp( M(
|
515
|
-
M(
|
486
|
+
assert_equal M(t1, 2)[ [ 1, 2, z ], [ 3, 4, z ], [ z, z, z ] ],
|
487
|
+
M(t1, 2)[ [ 1, 2 ], [ 3, 4 ] ].
|
488
|
+
warp( M(t2, 2)[ [ 0, 1, 2 ], [ 0, 1, 2 ], [ 0, 1, 2 ] ],
|
489
|
+
M(t2, 2)[ [ 0, 0, 0 ], [ 1, 1, 1 ], [ 2, 2, 2 ] ] )
|
516
490
|
end
|
517
491
|
end
|
518
492
|
end
|
519
493
|
|
520
494
|
def test_flip
|
521
495
|
[ O, I ].each do |t|
|
522
|
-
assert_equal M(
|
523
|
-
M(
|
524
|
-
assert_equal M(
|
525
|
-
M(
|
526
|
-
assert_equal M(
|
527
|
-
M(
|
496
|
+
assert_equal M(t, 2)[ [ 3, 2, 1 ], [ 6, 5, 4 ] ],
|
497
|
+
M(t, 2)[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].flip( 0 )
|
498
|
+
assert_equal M(t, 2)[ [ 4, 5, 6 ], [ 1, 2, 3 ] ],
|
499
|
+
M(t, 2)[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].flip( 1 )
|
500
|
+
assert_equal M(t, 2)[ [ 6, 5, 4 ], [ 3, 2, 1 ] ],
|
501
|
+
M(t, 2)[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].flip( 0, 1 )
|
528
502
|
end
|
529
503
|
end
|
530
504
|
|
531
505
|
def test_shift
|
532
506
|
[ O, I ].each do |t|
|
533
|
-
assert_equal M(
|
534
|
-
M(
|
507
|
+
assert_equal M(t, 2)[ [ 4, 3 ], [ 2, 1 ] ],
|
508
|
+
M(t, 2)[ [ 1, 2 ], [ 3, 4 ] ].shift( 1, 1 )
|
535
509
|
end
|
536
510
|
end
|
537
511
|
|
538
512
|
def test_downsample
|
539
513
|
[ O, I ].each do |t|
|
540
|
-
assert_equal M(
|
541
|
-
M(
|
542
|
-
assert_equal M(
|
543
|
-
M(
|
514
|
+
assert_equal M(t, 2)[ [ 2 ], [ 6 ] ],
|
515
|
+
M(t, 2)[ [ 1, 2, 3 ], [ 5, 6, 7 ] ].downsample( 2, 1 )
|
516
|
+
assert_equal M(t, 2)[ [ 1, 3 ], [ 5, 7 ] ],
|
517
|
+
M(t, 2)[ [ 1, 2, 3 ], [ 5, 6, 7 ] ].
|
544
518
|
downsample( 2, 1, :offset => [ 0, 0 ] )
|
545
519
|
end
|
546
520
|
end
|
@@ -642,9 +616,9 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
642
616
|
M[ [ 1, 2, 4 ], [ 2, 4, 6 ] ] + 1
|
643
617
|
assert_equal M[ [ 2, 3, 5 ], [ 3, 5, 7 ] ],
|
644
618
|
1 + M[ [ 1, 2, 4 ], [ 2, 4, 6 ] ]
|
645
|
-
assert_equal M[ [ -
|
646
|
-
M[ [
|
647
|
-
M[ [
|
619
|
+
assert_equal M[ [ -1, 2, 3 ], [ 4, 5, 6 ] ],
|
620
|
+
M[ [ -3, 2, 1 ], [ 8, 6, 4 ] ] +
|
621
|
+
M[ [ 2, 0, 2 ], [ -4, -1, 2 ] ]
|
648
622
|
assert_equal M[ [ 2, 3, 4 ], [ 6, 7, 8 ] ],
|
649
623
|
M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ] + S[ 1, 2 ]
|
650
624
|
assert_equal M[ [ 2, 3, 4 ], [ 6, 7, 8 ] ],
|
@@ -702,33 +676,33 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
702
676
|
end
|
703
677
|
|
704
678
|
def test_fill
|
705
|
-
m = M(
|
706
|
-
assert_equal M(
|
707
|
-
assert_equal M(
|
679
|
+
m = M(I, 2)[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
|
680
|
+
assert_equal M(I, 2)[ [ 1, 1, 1 ], [ 1, 1, 1 ] ], m.fill!( 1 )
|
681
|
+
assert_equal M(I, 2)[ [ 1, 1, 1 ], [ 1, 1, 1 ] ], m
|
708
682
|
end
|
709
683
|
|
710
684
|
def test_to_type
|
711
|
-
assert_equal M(
|
712
|
-
M(
|
685
|
+
assert_equal M(C, 2)[ [ 1, 2 ], [ 3, 4 ] ],
|
686
|
+
M(I, 2)[ [ 1, 2 ], [ 3, 4 ] ].to_intrgb
|
713
687
|
end
|
714
688
|
|
715
689
|
def test_reshape
|
716
|
-
[
|
717
|
-
assert_equal M(
|
718
|
-
S(
|
719
|
-
assert_equal S(
|
720
|
-
M(
|
721
|
-
assert_equal M(
|
722
|
-
M(
|
723
|
-
assert_raise( RuntimeError ) { M(
|
690
|
+
[O, I].each do |t|
|
691
|
+
assert_equal M(t, 2)[[1, 2, 3], [4, 5, 6]],
|
692
|
+
S(t)[1, 2, 3, 4, 5, 6].reshape(3, 2)
|
693
|
+
assert_equal S(t)[1, 2, 3, 4, 5, 6],
|
694
|
+
M(t, 2)[[1, 2, 3], [4, 5, 6]].reshape(6)
|
695
|
+
assert_equal M(t, 2)[[1, 2], [3, 4], [5, 6]],
|
696
|
+
M(t, 2)[[1, 2, 3], [4, 5, 6]].reshape(2, 3)
|
697
|
+
assert_raise( RuntimeError ) { M(t, 2)[[1, 2], [3, 4]].reshape 3 }
|
724
698
|
end
|
725
699
|
end
|
726
700
|
|
727
701
|
def test_integral
|
728
|
-
assert_equal M(
|
729
|
-
M(
|
730
|
-
assert_equal M(
|
731
|
-
M(
|
702
|
+
assert_equal M(O, 2)[[1, 3, 6], [5, 12, 21]],
|
703
|
+
M(O, 2)[[1, 2, 3], [4, 5, 6]].integral
|
704
|
+
assert_equal M(I, 2)[[1, 3, 6], [5, 12, 21]],
|
705
|
+
M(I, 2)[[1, 2, 3], [4, 5, 6]].integral
|
732
706
|
end
|
733
707
|
|
734
708
|
def test_components
|
@@ -737,28 +711,28 @@ class TC_MultiArray < Test::Unit::TestCase
|
|
737
711
|
end
|
738
712
|
|
739
713
|
def test_mask
|
740
|
-
[
|
741
|
-
assert_equal M(
|
742
|
-
M(
|
743
|
-
mask(
|
744
|
-
assert_equal S(
|
745
|
-
mask( M[
|
714
|
+
[O, I].each do |t|
|
715
|
+
assert_equal M(t, 2)[[1, 2], [5, 7]],
|
716
|
+
M(t, 2)[[1, 2], [3, 4], [5, 7]].
|
717
|
+
mask(S[true, false, true])
|
718
|
+
assert_equal S(t)[2, 5, 7], M(t, 2)[[1, 2, 3], [4, 5, 7]].
|
719
|
+
mask( M[[false, true, false], [false, true, true]])
|
746
720
|
assert_raise( RuntimeError ) do
|
747
|
-
M(
|
721
|
+
M(t, 2)[[1, 2], [3, 4], [5, 7]].mask S[false, true]
|
748
722
|
end
|
749
723
|
end
|
750
724
|
end
|
751
725
|
|
752
726
|
def test_unmask
|
753
727
|
[ O, I ].each do |t|
|
754
|
-
assert_equal M(
|
755
|
-
M(
|
756
|
-
unmask( S[
|
757
|
-
assert_equal M(
|
758
|
-
S(
|
759
|
-
unmask( M[
|
728
|
+
assert_equal M(t, 2)[[1, 2], [4, 4], [5, 7]],
|
729
|
+
M(t, 2)[[1, 2], [5, 7]].
|
730
|
+
unmask( S[true, false, true], :default => S[3, 4, 5] )
|
731
|
+
assert_equal M(t, 2)[[0, 2, 0], [0, 5, 7]],
|
732
|
+
S(t)[2, 5, 7].
|
733
|
+
unmask( M[[false, true, false], [false, true, true]],
|
760
734
|
:default => 0 )
|
761
|
-
assert_raise( RuntimeError ) { S(
|
735
|
+
assert_raise( RuntimeError ) { S(t)[1].unmask M[[true, true]] }
|
762
736
|
end
|
763
737
|
end
|
764
738
|
|