|
25 | 25 | # Enumerator tests for NMatrix. These should load early, as they |
26 | 26 | # test functionality essential to matrix printing. |
27 | 27 | # |
28 | | -require 'spec_helper' |
| 28 | +require "spec_helper" |
29 | 29 |
|
30 | 30 | describe "NMatrix enumeration for" do |
31 | 31 | [:dense, :yale, :list].each do |stype| |
32 | 32 | context stype do |
33 | 33 | let(:n) { create_rectangular_matrix(stype) } |
34 | | - let(:m) { n[1..4,1..3] } |
| 34 | + let(:m) { n[1..4, 1..3] } |
35 | 35 |
|
36 | 36 | if stype == :yale |
37 | 37 | it "should iterate properly along each row of a slice" do |
|
41 | 41 | jj = [] |
42 | 42 | m.extend NMatrix::YaleFunctions |
43 | 43 | m.each_row do |row| |
44 | | - row.each_with_indices do |v,i,j| |
| 44 | + row.each_with_indices do |v, i, j| |
45 | 45 | vv << v |
46 | 46 | ii << i |
47 | 47 | jj << j |
48 | 48 | end |
49 | 49 | end |
50 | 50 |
|
51 | | - expect(vv).to eq([7,8,9, 12,13,0, 0,0,0, 0,17,18]) |
52 | | - expect(ii).to eq([0]*12) |
53 | | - expect(jj).to eq([0,1,2]*4) |
| 51 | + expect(vv).to eq([7, 8, 9, 12, 13, 0, 0, 0, 0, 0, 17, 18]) |
| 52 | + expect(ii).to eq([0] * 12) |
| 53 | + expect(jj).to eq([0, 1, 2] * 4) |
54 | 54 | end |
55 | 55 |
|
56 | 56 | it "should iterate along diagonal portion of A array" do |
57 | 57 | pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby? |
58 | 58 | vv = [] |
59 | 59 | ii = [] |
60 | 60 | jj = [] |
61 | | - n.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j| |
| 61 | + n.send :__yale_stored_diagonal_each_with_indices__ do |v, i, j| |
62 | 62 | vv << v |
63 | 63 | ii << i |
64 | 64 | jj << j |
65 | 65 | end |
66 | | - expect(vv).to eq([1,7,13,0,19]) |
67 | | - expect(ii).to eq([0,1,2,3,4]) |
| 66 | + expect(vv).to eq([1, 7, 13, 0, 19]) |
| 67 | + expect(ii).to eq([0, 1, 2, 3, 4]) |
68 | 68 | expect(jj).to eq(ii) |
69 | 69 | end |
70 | 70 |
|
|
73 | 73 | vv = [] |
74 | 74 | ii = [] |
75 | 75 | jj = [] |
76 | | - n.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j| |
| 76 | + n.send :__yale_stored_nondiagonal_each_with_indices__ do |v, i, j| |
77 | 77 | vv << v |
78 | 78 | ii << i |
79 | 79 | jj << j |
80 | 80 | end |
81 | 81 |
|
82 | | - expect(vv).to eq([2,3,4,5, 6,8,9,10, 11,12,14,15, 16,17,18,20]) |
83 | | - expect(ii).to eq([[0]*4, [1]*4, [2]*4, [4]*4].flatten) |
84 | | - expect(jj).to eq([1,2,3,4, 0,2,3,5, 0,1,4,5, 0,2,3,5]) |
| 82 | + expect(vv).to eq([2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 20]) |
| 83 | + expect(ii).to eq([[0] * 4, [1] * 4, [2] * 4, [4] * 4].flatten) |
| 84 | + expect(jj).to eq([1, 2, 3, 4, 0, 2, 3, 5, 0, 1, 4, 5, 0, 2, 3, 5]) |
85 | 85 | end |
86 | 86 |
|
87 | 87 | it "should iterate along a sliced diagonal portion of an A array" do |
88 | 88 | pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby? |
89 | | - m = n[0..3,1..3] |
| 89 | + m = n[0..3, 1..3] |
90 | 90 | vv = [] |
91 | 91 | ii = [] |
92 | 92 | jj = [] |
93 | | - m.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j| |
| 93 | + m.send :__yale_stored_diagonal_each_with_indices__ do |v, i, j| |
94 | 94 | vv << v |
95 | 95 | ii << i |
96 | 96 | jj << j |
97 | 97 | end |
98 | | - expect(vv).to eq([7,13,0]) |
99 | | - expect(ii).to eq([1,2,3]) |
100 | | - expect(jj).to eq([0,1,2]) |
| 98 | + expect(vv).to eq([7, 13, 0]) |
| 99 | + expect(ii).to eq([1, 2, 3]) |
| 100 | + expect(jj).to eq([0, 1, 2]) |
101 | 101 | end |
102 | 102 |
|
103 | 103 | it "should iterate along a sliced non-diagonal portion of a sliced A array" do |
|
107 | 107 | jj = [] |
108 | 108 | n.extend NMatrix::YaleFunctions |
109 | 109 | m.extend NMatrix::YaleFunctions |
110 | | - m.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j| |
| 110 | + m.send :__yale_stored_nondiagonal_each_with_indices__ do |v, i, j| |
111 | 111 | vv << v |
112 | 112 | ii << i |
113 | 113 | jj << j |
114 | 114 | end |
115 | 115 |
|
116 | | - expect(ii).to eq([0,0, 1, 3,3 ]) |
117 | | - expect(jj).to eq([1,2, 0, 1,2 ]) |
118 | | - expect(vv).to eq([8,9, 12, 17,18]) |
| 116 | + expect(ii).to eq([0, 0, 1, 3, 3]) |
| 117 | + expect(jj).to eq([1, 2, 0, 1, 2]) |
| 118 | + expect(vv).to eq([8, 9, 12, 17, 18]) |
119 | 119 | end |
120 | 120 |
|
121 | 121 | it "should visit each stored element of the matrix in order by indices" do |
122 | 122 | pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby? |
123 | 123 | vv = [] |
124 | 124 | ii = [] |
125 | 125 | jj = [] |
126 | | - n.each_ordered_stored_with_indices do |v,i,j| |
| 126 | + n.each_ordered_stored_with_indices do |v, i, j| |
127 | 127 | vv << v |
128 | 128 | ii << i |
129 | 129 | jj << j |
130 | 130 | end |
131 | 131 |
|
132 | 132 | expect(vv).to eq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 16, 17, 18, 19, 20]) |
133 | | - expect(ii).to eq([[0]*5, [1]*5, [2]*5, [3]*1, [4]*5].flatten) |
134 | | - expect(jj).to eq([0,1,2,3,4, 0,1,2,3,5, 0,1,2,4,5, 3, 0,2,3,4,5]) |
| 133 | + expect(ii).to eq([[0] * 5, [1] * 5, [2] * 5, [3] * 1, [4] * 5].flatten) |
| 134 | + expect(jj).to eq([0, 1, 2, 3, 4, 0, 1, 2, 3, 5, 0, 1, 2, 4, 5, 3, 0, 2, 3, 4, 5]) |
135 | 135 | end |
136 | 136 |
|
137 | 137 | it "should visit each stored element of the slice in order by indices" do |
138 | 138 | pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby? |
139 | 139 | vv = [] |
140 | 140 | ii = [] |
141 | 141 | jj = [] |
142 | | - m.each_ordered_stored_with_indices do |v,i,j| |
| 142 | + m.each_ordered_stored_with_indices do |v, i, j| |
143 | 143 | vv << v |
144 | 144 | ii << i |
145 | 145 | jj << j |
146 | 146 | end |
147 | | - expect(ii).to eq([0,0,0, 1,1, 2, 3,3 ]) |
148 | | - expect(jj).to eq([0,1,2, 0,1, 2, 1,2 ]) |
149 | | - expect(vv).to eq([7,8,9, 12,13, 0, 17,18 ]) |
| 147 | + expect(ii).to eq([0, 0, 0, 1, 1, 2, 3, 3]) |
| 148 | + expect(jj).to eq([0, 1, 2, 0, 1, 2, 1, 2]) |
| 149 | + expect(vv).to eq([7, 8, 9, 12, 13, 0, 17, 18]) |
150 | 150 | end |
151 | 151 | end |
152 | 152 |
|
153 | 153 | it "should visit each cell in the matrix as if dense, making indices available" do |
154 | 154 | vv = [] |
155 | 155 | ii = [] |
156 | 156 | jj = [] |
157 | | - n.each_with_indices do |v,i,j| |
| 157 | + n.each_with_indices do |v, i, j| |
158 | 158 | vv << v |
159 | 159 | ii << i |
160 | 160 | jj << j |
161 | 161 | end |
162 | 162 |
|
163 | | - expect(vv).to eq([1,2,3,4,5,0,6,7,8,9,0,10,11,12,13,0,14,15,0,0,0,0,0,0,16,0,17,18,19,20]) |
164 | | - expect(ii).to eq([[0]*6, [1]*6, [2]*6, [3]*6, [4]*6].flatten) |
165 | | - expect(jj).to eq([0,1,2,3,4,5]*5) |
| 163 | + expect(vv).to eq([1, 2, 3, 4, 5, 0, 6, 7, 8, 9, 0, 10, 11, 12, 13, 0, 14, 15, 0, 0, 0, 0, 0, 0, 16, 0, 17, 18, 19, 20]) |
| 164 | + expect(ii).to eq([[0] * 6, [1] * 6, [2] * 6, [3] * 6, [4] * 6].flatten) |
| 165 | + expect(jj).to eq([0, 1, 2, 3, 4, 5] * 5) |
166 | 166 | end |
167 | 167 |
|
168 | 168 | it "should visit each cell in the slice as if dense, making indices available" do |
169 | 169 | vv = [] |
170 | 170 | ii = [] |
171 | 171 | jj = [] |
172 | | - m.each_with_indices do |v,i,j| |
| 172 | + m.each_with_indices do |v, i, j| |
173 | 173 | vv << v |
174 | 174 | ii << i |
175 | 175 | jj << j |
176 | 176 | end |
177 | | - expect(jj).to eq([0,1,2]*4) |
178 | | - expect(ii).to eq([[0]*3, [1]*3, [2]*3, [3]*3].flatten) |
179 | | - expect(vv).to eq([7,8,9,12,13,0,0,0,0,0,17,18]) |
180 | | - |
| 177 | + expect(jj).to eq([0, 1, 2] * 4) |
| 178 | + expect(ii).to eq([[0] * 3, [1] * 3, [2] * 3, [3] * 3].flatten) |
| 179 | + expect(vv).to eq([7, 8, 9, 12, 13, 0, 0, 0, 0, 0, 17, 18]) |
181 | 180 | end |
182 | 181 |
|
183 | | - if stype == :list or stype == :dense then |
| 182 | + if (stype == :list) || (stype == :dense) |
184 | 183 | it "should correctly map to a matrix with a single element" do |
185 | 184 | nm = N.new([1], [2.0], stype: stype) |
186 | 185 | expect(nm.map { |e| e**2 }).to eq N.new([1], [4.0], stype: stype) |
|
0 commit comments