multiarray 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/tc_int.rb CHANGED
@@ -31,12 +31,16 @@ class TC_Int < Test::Unit::TestCase
31
31
  U32 = Hornetseye::UINT
32
32
  S32 = Hornetseye::INT
33
33
 
34
+ def I( *args )
35
+ Hornetseye::INT *args
36
+ end
37
+
34
38
  def UI( bits )
35
- Hornetseye::INT Hornetseye::UNSIGNED, bits
39
+ I Hornetseye::UNSIGNED, bits
36
40
  end
37
41
 
38
42
  def SI( bits )
39
- Hornetseye::INT Hornetseye::SIGNED, bits
43
+ I Hornetseye::SIGNED, bits
40
44
  end
41
45
 
42
46
  def sum( *args, &action )
@@ -71,6 +75,12 @@ class TC_Int < Test::Unit::TestCase
71
75
  assert_equal 0, I.new[]
72
76
  end
73
77
 
78
+ def test_int_indgen
79
+ assert_equal 0, I.indgen
80
+ assert_equal 1, I.indgen( 1 )
81
+ assert_equal 1, I.indgen( 1, 2 )
82
+ end
83
+
74
84
  def test_int_typecode
75
85
  assert_equal I, I.typecode
76
86
  end
@@ -78,17 +88,21 @@ class TC_Int < Test::Unit::TestCase
78
88
  def test_int_dimension
79
89
  assert_equal 0, I.dimension
80
90
  end
81
-
91
+ !
82
92
  def test_int_shape
83
93
  assert_equal [], I.shape
84
94
  end
85
95
 
96
+ def test_int_size
97
+ assert_equal 1, I.size
98
+ end
99
+
86
100
  def test_inspect
87
- assert_equal 'INT(42)', I.new( 42 ).inspect
101
+ assert_equal 'INT(42)', I( 42 ).inspect
88
102
  end
89
103
 
90
104
  def test_marshal
91
- assert_equal I.new( 42 ), Marshal.load( Marshal.dump( I.new( 42 ) ) )
105
+ assert_equal I( 42 ), Marshal.load( Marshal.dump( I( 42 ) ) )
92
106
  end
93
107
 
94
108
  def test_typecode
@@ -103,26 +117,30 @@ class TC_Int < Test::Unit::TestCase
103
117
  assert_equal [], I.new.shape
104
118
  end
105
119
 
120
+ def test_size
121
+ assert_equal 1, I.new.size
122
+ end
123
+
106
124
  def test_at_assign
107
- i = I.new 42
125
+ i = I 42
108
126
  assert_equal 42, i[]
109
127
  assert_equal 3, i[] = 3
110
128
  assert_equal 3, i[]
111
129
  end
112
130
 
113
131
  def test_equal
114
- assert_not_equal I.new( 3 ), I.new( 4 )
115
- assert_equal I.new( 3 ), I.new( 3 )
132
+ assert_not_equal I( 3 ), I( 4 )
133
+ assert_equal I( 3 ), I( 3 )
116
134
  end
117
135
 
118
136
  def test_inject
119
- assert_equal 2, I.new( 2 ).inject { |a,b| a + b }[]
120
- assert_equal 3, I.new( 2 ).inject( 1 ) { |a,b| a + b }[]
137
+ assert_equal 2, I( 2 ).inject { |a,b| a + b }[]
138
+ assert_equal 3, I( 2 ).inject( 1 ) { |a,b| a + b }[]
121
139
  end
122
140
 
123
141
  def test_not
124
- assert !I.new( 0 ).not[]
125
- assert !I.new( 3 ).not[]
142
+ assert !I( 0 ).not[]
143
+ assert !I( 3 ).not[]
126
144
  end
127
145
 
128
146
  def test_sum
@@ -130,45 +148,55 @@ class TC_Int < Test::Unit::TestCase
130
148
  end
131
149
 
132
150
  def test_zero
133
- assert I.new( 0 ).zero?[]
134
- assert !I.new( 3 ).zero?[]
151
+ assert I( 0 ).zero?[]
152
+ assert !I( 3 ).zero?[]
135
153
  end
136
154
 
137
155
  def test_nonzero
138
- assert !I.new( 0 ).nonzero?[]
139
- assert I.new( 3 ).nonzero?[]
156
+ assert !I( 0 ).nonzero?[]
157
+ assert I( 3 ).nonzero?[]
140
158
  end
141
159
 
142
160
  def test_bitwise_not
143
- assert_equal I.new( -3 ), ~I.new( 2 )
161
+ assert_equal I( -3 ), ~I( 2 )
144
162
  end
145
163
 
146
164
  def test_bitwise_and
147
- assert_equal I.new( 2 ), I.new( 3 ) & I.new( 6 )
165
+ assert_equal I( 2 ), I( 3 ) & I( 6 )
148
166
  end
149
167
 
150
168
  def test_bitwise_or
151
- assert_equal I.new( 7 ), I.new( 3 ) | I.new( 6 )
169
+ assert_equal I( 7 ), I( 3 ) | I( 6 )
152
170
  end
153
171
 
154
172
  def test_bitwise_xor
155
- assert_equal I.new( 1 ), I.new( 3 ) ^ I.new( 2 )
173
+ assert_equal I( 1 ), I( 3 ) ^ I( 2 )
156
174
  end
157
175
 
158
176
  def test_shl
159
- assert_equal I.new( 4 ), I.new( 2 ) << I.new( 1 )
177
+ assert_equal I( 4 ), I( 2 ) << I( 1 )
160
178
  end
161
179
 
162
180
  def test_shr
163
- assert_equal I.new( 2 ), I.new( 4 ) >> I.new( 1 )
181
+ assert_equal I( 2 ), I( 4 ) >> I( 1 )
164
182
  end
165
183
 
166
184
  def test_negate
167
- assert_equal I.new( -5 ), -I.new( 5 )
185
+ assert_equal I( -5 ), -I( 5 )
168
186
  end
169
187
 
170
188
  def test_plus
171
- assert_equal I.new( 3 + 5 ), I.new( 3 ) + I.new( 5 )
189
+ assert_equal I( 3 + 5 ), I( 3 ) + I( 5 )
190
+ end
191
+
192
+ def test_major
193
+ assert_equal I( 4 ), I( 3 ).major( I( 4 ) )
194
+ assert_equal I( 5 ), I( 5 ).major( I( 3 ) )
195
+ end
196
+
197
+ def test_minor
198
+ assert_equal I( 3 ), I( 3 ).minor( I( 4 ) )
199
+ assert_equal I( 4 ), I( 5 ).minor( I( 4 ) )
172
200
  end
173
201
 
174
202
  end
data/test/tc_lazy.rb ADDED
@@ -0,0 +1,109 @@
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_Lazy < Test::Unit::TestCase
25
+
26
+ O = Hornetseye::OBJECT
27
+ B = Hornetseye::BOOL
28
+ I = Hornetseye::INT
29
+ S = Hornetseye::Sequence
30
+ M = Hornetseye::MultiArray
31
+
32
+ def S( *args )
33
+ Hornetseye::Sequence *args
34
+ end
35
+
36
+ def M( *args )
37
+ Hornetseye::MultiArray *args
38
+ end
39
+
40
+ def lazy( *args, &action )
41
+ Hornetseye::lazy *args, &action
42
+ end
43
+
44
+ def sum( *args, &action )
45
+ Hornetseye::sum *args, &action
46
+ end
47
+
48
+ def setup
49
+ @s = S[ -1, 2, 3, 5, 7 ]
50
+ @m = M[ [ -1, 2, 3 ], [ 4, 5, 6 ] ]
51
+ @w = 2 * 10 ** 6
52
+ @h = 10 ** 6
53
+ end
54
+
55
+ def teardown
56
+ end
57
+
58
+ def test_const
59
+ assert_equal 0, lazy { 0 }
60
+ assert_equal [ 0, 0, 0 ], lazy( 3 ) { 0 }.to_a
61
+ assert_equal [ 0, 0, 0 ], lazy( 3 ) { |i| 0 }.to_a
62
+ end
63
+
64
+ def test_index
65
+ assert_equal [ 0, 1, 2 ], lazy( 3 ) { |i| i }.to_a
66
+ assert_equal [ [ 0, 1, 2 ], [ 0, 1, 2 ] ], lazy( 3, 2 ) { |i,j| i }.to_a
67
+ assert_equal [ [ 0, 0, 0 ], [ 1, 1, 1 ] ], lazy( 3, 2 ) { |i,j| j }.to_a
68
+ end
69
+
70
+ def test_minus_at
71
+ assert_equal -2, lazy { ( -@s )[ 1 ] }
72
+ end
73
+
74
+ def test_minus_slice
75
+ assert_equal [ -2, -3, -5 ], lazy { ( -@s )[ 1 .. 3 ] }.to_a
76
+ end
77
+
78
+ def test_add_at
79
+ assert_equal 4, lazy { ( @s + @s )[ 1 ] }
80
+ end
81
+
82
+ def test_add_slice
83
+ assert_equal [ 6, 10, 14 ], lazy { ( @s + @s )[ 2 .. 4 ] }.to_a
84
+ end
85
+
86
+ def test_index_at
87
+ assert_equal 3, lazy { lazy( @h ) { |i| i }[ 3 ] }
88
+ assert_equal 5, lazy { lazy( @w, @h ) { |i,j| 2 * i + j }[ 2, 1 ] }
89
+ end
90
+
91
+ def test_index_slice
92
+ assert_equal [ 3, 4, 5 ], lazy { lazy( @h ) { |i| i }[ 3 .. 5 ] }.to_a
93
+ assert_equal [ [ 5, 7, 9 ], [ 6, 8, 10 ] ],
94
+ lazy { lazy( @w, @h ) { |i,j| 2 * i + j }[ 2 .. 4, 1 .. 2 ] }.to_a
95
+ end
96
+
97
+ def test_index_inject
98
+ assert_equal 10, lazy( 5 ) { |i| i }.inject { |a,b| a + b }
99
+ assert_equal [ 0, 3, 6, 9 ], sum { |k| lazy( 4, 3 ) { |i,j| i }[ k ] }.to_a
100
+ assert_equal [ 3, 3, 3, 3 ], sum { |k| lazy( 4, 3 ) { |i,j| j }[ k ] }.to_a
101
+ end
102
+
103
+ def test_indgen_diagonal
104
+ assert_equal [ 15, 18, 17 ],
105
+ M( I, 4, 3 ).indgen.diagonal { |a,b| a + b }.to_a
106
+ end
107
+
108
+ end
109
+
@@ -26,9 +26,14 @@ class TC_MultiArray < Test::Unit::TestCase
26
26
  O = Hornetseye::OBJECT
27
27
  B = Hornetseye::BOOL
28
28
  I = Hornetseye::INT
29
+ C = Hornetseye::INTRGB
29
30
  S = Hornetseye::Sequence
30
31
  M = Hornetseye::MultiArray
31
32
 
33
+ def C( *args )
34
+ Hornetseye::RGB *args
35
+ end
36
+
32
37
  def S( *args )
33
38
  Hornetseye::Sequence *args
34
39
  end
@@ -60,19 +65,39 @@ class TC_MultiArray < Test::Unit::TestCase
60
65
  end
61
66
 
62
67
  def test_multiarray_default
63
- assert_equal [ [ O.default ] * 3 ] * 2, M( O, 3, 2 ).default.to_a
68
+ assert_equal [ [ nil ] * 3 ] * 2, M( O, 3, 2 ).default.to_a
69
+ assert_equal [ [ 0 ] * 3 ] * 2, M( I, 3, 2 ).default.to_a
70
+ assert_equal [ [ C( 0, 0, 0 ) ] * 3 ] * 2, M( C, 3, 2 ).default.to_a
64
71
  end
65
72
 
66
73
  def test_multiarray_at
67
74
  assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
68
75
  M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].to_a
76
+ assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
77
+ M( O, 3, 2 )[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].to_a
69
78
  assert_equal O, M[ [ :a ] ].typecode
70
79
  assert_equal B, M[ [ false ], [ true ] ].typecode
71
80
  assert_equal I, M[ [ -2 ** 31, 2 ** 31 - 1 ] ].typecode
72
81
  end
73
82
 
83
+ def test_multiarray_indgen
84
+ assert_equal [ [ 0, 1, 2 ], [ 3, 4, 5 ] ],
85
+ M( I, 3, 2 ).indgen.to_a
86
+ assert_equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
87
+ M( I, 3, 2 ).indgen( 1 ).to_a
88
+ assert_equal [ [ 0, 2, 4 ], [ 6, 8, 10 ] ],
89
+ M( I, 3, 2 ).indgen( 0, 2 ).to_a
90
+ assert_equal [ [ 1, 3, 5 ], [ 7, 9, 11 ] ],
91
+ M( I, 3, 2 ).indgen( 1, 2 ).to_a
92
+ assert_equal [ [ C( 1, 2, 3 ), C( 2, 2, 2 ) ],
93
+ [ C( 3, 2, 1 ), C( 4, 2, 0 ) ] ],
94
+ M( C, 2, 2 ).indgen( C( 1, 2, 3 ), C( 1, 0, -1 ) ).to_a
95
+ end
96
+
74
97
  def test_multiarray_typecode
75
98
  assert_equal O, M( O, 3, 2 ).typecode
99
+ assert_equal I, M( I, 3, 2 ).typecode
100
+ assert_equal C, M( C, 3, 2 ).typecode
76
101
  end
77
102
 
78
103
  def test_multiarray_dimension
@@ -83,23 +108,50 @@ class TC_MultiArray < Test::Unit::TestCase
83
108
  assert_equal [ 3, 2 ], M( O, 3, 2 ).shape
84
109
  end
85
110
 
111
+ def test_multiarray_size
112
+ assert_equal 6, M( O, 3, 2 ).size
113
+ assert_equal 6, M( I, 3, 2 ).size
114
+ assert_equal 6, M( C, 3, 2 ).size
115
+ end
116
+
86
117
  def test_inspect
87
118
  assert_equal "MultiArray(OBJECT,3,2):\n[ [ :a, 2, 3 ],\n [ 4, 5, 6 ] ]",
88
119
  M[ [ :a, 2, 3 ], [ 4, 5, 6 ] ].inspect
120
+ assert_equal "MultiArray(UBYTE,4,3,2):\n" +
121
+ "[ [ [ 0, 1, 2, 3 ],\n" +
122
+ " [ 4, 5, 6, 7 ],\n" +
123
+ " [ 8, 9, 10, 11 ] ],\n" +
124
+ " [ [ 12, 13, 14, 15 ],\n" +
125
+ " [ 16, 17, 18, 19 ],\n" +
126
+ " [ 20, 21, 22, 23 ] ] ]",
127
+ M[ [ [ 0, 1, 2, 3 ],
128
+ [ 4, 5, 6, 7 ],
129
+ [ 8, 9, 10, 11 ] ],
130
+ [ [ 12, 13, 14, 15 ],
131
+ [ 16, 17, 18, 19 ],
132
+ [ 20, 21, 22, 23 ] ] ].inspect
89
133
  end
90
134
 
91
135
  def test_typecode
92
136
  assert_equal O, M( O, 3, 2 ).new.typecode
137
+ assert_equal I, M( I, 3, 2 ).new.typecode
138
+ assert_equal C, M( C, 3, 2 ).new.typecode
93
139
  end
94
140
 
95
141
  def test_dimension
96
142
  assert_equal 2, M( O, 3, 2 ).new.dimension
143
+ assert_equal 2, M( I, 3, 2 ).new.dimension
144
+ assert_equal 2, M( C, 3, 2 ).new.dimension
97
145
  end
98
146
 
99
147
  def test_shape
100
148
  assert_equal [ 3, 2 ], M( O, 3, 2 ).new.shape
101
149
  end
102
150
 
151
+ def test_size
152
+ assert_equal 6, M( O, 3, 2 ).new.size
153
+ end
154
+
103
155
  def test_at_assign
104
156
  m = M.new O, 3, 2
105
157
  for j in 0 ... 2
@@ -116,6 +168,48 @@ class TC_MultiArray < Test::Unit::TestCase
116
168
  end
117
169
  end
118
170
 
171
+ def test_slice
172
+ m = M( I, 5, 4 ).indgen[]
173
+ assert_equal [ [ 5, 10 ], [ 6, 11 ], [ 7, 12 ], [ 8, 13 ], [ 9, 14 ] ],
174
+ m[ 1 .. 2 ].to_a
175
+ assert_equal [ [ 6, 7, 8 ], [ 11, 12, 13 ] ],
176
+ m[ 1 .. 2 ][ 1 .. 3 ].to_a
177
+ assert_equal [ [ 6, 7, 8 ], [ 11, 12, 13 ] ],
178
+ m[ 1 .. 3, 1 .. 2 ].to_a
179
+ m[ 1 .. 2 ] = 0
180
+ assert_equal [ [ 0, 1, 2, 3, 4 ], [ 0, 0, 0, 0, 0 ],
181
+ [ 0, 0, 0, 0, 0 ], [ 15, 16, 17, 18, 19 ] ], m.to_a
182
+ m[ 1 ... 3 ] = 1
183
+ assert_equal [ [ 0, 1, 2, 3, 4 ], [ 1, 1, 1, 1, 1 ],
184
+ [ 1, 1, 1, 1, 1 ], [ 15, 16, 17, 18, 19 ] ], m.to_a
185
+ m[ 1 .. 2 ] = S[ 2, 3, 4, 5, 6 ]
186
+ assert_equal [ [ 0, 1, 2, 3, 4 ], [ 2, 3, 4, 5, 6 ],
187
+ [ 2, 3, 4, 5, 6 ], [ 15, 16, 17, 18, 19 ] ], m.to_a
188
+ m[ 1 ... 3 ] = S[ 3, 4, 5, 6, 7 ]
189
+ assert_equal [ [ 0, 1, 2, 3, 4 ], [ 3, 4, 5, 6, 7 ],
190
+ [ 3, 4, 5, 6, 7 ], [ 15, 16, 17, 18, 19 ] ], m.to_a
191
+ m[ 1 .. 3, 1 .. 2 ] = 0
192
+ assert_equal [ [ 0, 1, 2, 3, 4 ], [ 3, 0, 0, 0, 7 ],
193
+ [ 3, 0, 0, 0, 7 ], [ 15, 16, 17, 18, 19 ] ], m.to_a
194
+ m[ 1 ... 4, 1 ... 3 ] = 1
195
+ assert_equal [ [ 0, 1, 2, 3, 4 ], [ 3, 1, 1, 1, 7 ],
196
+ [ 3, 1, 1, 1, 7 ], [ 15, 16, 17, 18, 19 ] ], m.to_a
197
+ end
198
+
199
+ def test_transpose
200
+ assert_equal [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ],
201
+ M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ].transpose( 1, 0 ).to_a
202
+ assert_equal [ [ [ 0, 3 ], [ 1, 4 ], [ 2, 5 ] ] ],
203
+ M( I, 3, 2, 1 ).indgen.transpose( 1, 0, 2 ).to_a
204
+ end
205
+
206
+ def test_roll_unroll
207
+ assert_equal [ [ [ 0 ], [ 1 ], [ 2 ] ], [ [ 3 ], [ 4 ], [ 5 ] ] ],
208
+ M( I, 3, 2, 1 ).indgen.unroll.to_a
209
+ assert_equal [ [ [ 0, 3 ] ], [ [ 1, 4 ] ], [ [ 2, 5 ] ] ],
210
+ M( I, 3, 2, 1 ).indgen.roll.to_a
211
+ end
212
+
119
213
  def test_equal
120
214
  assert_equal M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ], M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
121
215
  assert_not_equal M[ [ 1, 2, 3 ], [ 4, 5, 6 ] ],
@@ -138,7 +232,37 @@ class TC_MultiArray < Test::Unit::TestCase
138
232
  assert_equal [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ], sum { || m }.to_a
139
233
  end
140
234
 
141
- def dont_test_convolve
235
+ def test_min
236
+ assert_equal 1, M[ [ 5, 3, 7 ], [ 2, 1, 6 ] ].min
237
+ end
238
+
239
+ def test_max
240
+ assert_equal 7, M[ [ 5, 3, 7 ], [ 2, 1, 6 ] ].max
241
+ end
242
+
243
+ def test_diagonal
244
+ assert_equal S[ 'b1a2', 'c1b2a3', 'c2b3' ],
245
+ M[ [ 'a1', 'a2', 'a3' ],
246
+ [ 'b1', 'b2', 'b3' ],
247
+ [ 'c1', 'c2', 'c3' ] ].diagonal { |a,b| a + b }
248
+ assert_equal S[ 'c1b2a3', 'c2b3a4', 'c3b4' ],
249
+ M[ [ 'a1', 'a2', 'a3', 'a4' ],
250
+ [ 'b1', 'b2', 'b3', 'b4' ],
251
+ [ 'c1', 'c2', 'c3', 'c4' ] ].diagonal { |a,b| a + b }
252
+ assert_equal S[ 'xb1a2', 'xc1b2a3', 'xd1c2b3', 'xd2c3' ],
253
+ M[ [ 'a1', 'a2', 'a3' ],
254
+ [ 'b1', 'b2', 'b3' ],
255
+ [ 'c1', 'c2', 'c3' ],
256
+ [ 'd1', 'd2', 'd3' ] ].diagonal( 'x' ) { |a,b| a + b }
257
+ assert_equal S( I, 4 )[ 4, 12, 21, 18 ],
258
+ M( I, 3, 4 ).indgen.diagonal { |a,b| a + b }
259
+ assert_equal S( I, 3 )[ 4, 12, 12 ],
260
+ M( I, 3, 3 ).indgen.diagonal { |a,b| a + b }
261
+ assert_equal S( I, 2 )[ 4, 6 ],
262
+ M( I, 3, 2 ).indgen.diagonal { |a,b| a + b }
263
+ end
264
+
265
+ def test_convolve
142
266
  f = M[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
143
267
  assert_equal M[ [ 5, 6, 0 ], [ 8, 9, 0 ], [ 0, 0, 0 ] ],
144
268
  M[ [ 1, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ].convolve( f )
@@ -250,4 +374,14 @@ class TC_MultiArray < Test::Unit::TestCase
250
374
  M[ [ -1, 2, 3 ], [ 4, 5, 6 ] ]
251
375
  end
252
376
 
377
+ def test_major
378
+ assert_equal M[ [ 4, 2 ], [ 3, 4 ] ],
379
+ M[ [ 1, 2 ], [ 3, 4 ] ].major( M[ [ 4, 1 ], [ 3, 2 ] ] )
380
+ end
381
+
382
+ def test_minor
383
+ assert_equal M[ [ 1, 1 ], [ 3, 2 ] ],
384
+ M[ [ 1, 2 ], [ 3, 4 ] ].minor( M[ [ 4, 1 ], [ 3, 2 ] ] )
385
+ end
386
+
253
387
  end