|
| 1 | +Class { |
| 2 | + #name : #PMNDArrayTest, |
| 3 | + #superclass : #TestCase, |
| 4 | + #category : #'Math-Matrix' |
| 5 | +} |
| 6 | + |
| 7 | +{ #category : #tests } |
| 8 | +PMNDArrayTest >> testArray [ |
| 9 | + |
| 10 | + | t1 t2 | |
| 11 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). |
| 12 | + self assert: t1 asArray equals: #( 1 2 3 4 5 6 7 8 ). |
| 13 | + |
| 14 | + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) |
| 15 | + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). |
| 16 | + self |
| 17 | + assert: t2 asArray |
| 18 | + equals: #( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ) |
| 19 | +] |
| 20 | + |
| 21 | +{ #category : #tests } |
| 22 | +PMNDArrayTest >> testAt [ |
| 23 | + |
| 24 | + | t1 t2 | |
| 25 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). |
| 26 | + self assert: (t1 at: #( 2 2 )) equals: 6. |
| 27 | + |
| 28 | + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) |
| 29 | + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). |
| 30 | + self assert: (t2 at: #( 3 2 1 )) equals: 11. |
| 31 | + |
| 32 | + self should:[t1 at: #( 4 4 )] raise:Error |
| 33 | +] |
| 34 | + |
| 35 | +{ #category : #tests } |
| 36 | +PMNDArrayTest >> testAtPut [ |
| 37 | + |
| 38 | + | t1 t2 | |
| 39 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). |
| 40 | + t1 at: #( 2 2 ) put: 3. |
| 41 | + self assert: (t1 at: #( 2 2 ) ) equals: 3. |
| 42 | + |
| 43 | + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) |
| 44 | + #( #( 5 6 ) #( 7 8 ) ) |
| 45 | + #( #( 9 10 ) #( 11 12 ) ) |
| 46 | + #( #( 13 14 ) #( 15 16 ) ) ). |
| 47 | + t2 at: #( 2 2 1) put: 10. |
| 48 | + self assert: (t2 at: #( 2 2 1 )) equals: 10 |
| 49 | +] |
| 50 | + |
| 51 | +{ #category : #tests } |
| 52 | +PMNDArrayTest >> testCreateScalarNDArray [ |
| 53 | + |
| 54 | + | s | |
| 55 | + s := PMNDArray fromScalar: 2. |
| 56 | + self assert: (s at: #( )) equals: 2. |
| 57 | + self should: [ s at: #( 1 1 ) ] raise: Error. |
| 58 | + self assert: s rank equals: 0. |
| 59 | + s at: #( ) put: 1. |
| 60 | + self assert: (s at: #( )) equals: 1. |
| 61 | + self assert: s shape equals: #( ). |
| 62 | + self assert: s size equals: 1 |
| 63 | +] |
| 64 | + |
| 65 | +{ #category : #tests } |
| 66 | +PMNDArrayTest >> testFirst [ |
| 67 | + |
| 68 | + | a b | |
| 69 | + a := PMNDArray fromNestedArray: (1 to: 6) asArray. |
| 70 | + self assert: a first equals: #( 1). |
| 71 | + b := a reshape: #( 3 2 ). |
| 72 | + self assert: b first equals: #( 1 1 ) |
| 73 | +] |
| 74 | + |
| 75 | +{ #category : #tests } |
| 76 | +PMNDArrayTest >> testFlattenedIndexOf [ |
| 77 | + |
| 78 | + | t1 t2 | |
| 79 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8) #( 9 10 11 12)). |
| 80 | + self assert: (t1 flattenedIndexOf: #( 3 2 )) equals: 10. |
| 81 | + |
| 82 | + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) |
| 83 | + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). |
| 84 | + self assert: (t2 flattenedIndexOf: #( 1 2 2 )) equals: 4 |
| 85 | +] |
| 86 | + |
| 87 | +{ #category : #tests } |
| 88 | +PMNDArrayTest >> testFromNestedArray [ |
| 89 | + |
| 90 | + | t1 t2 | |
| 91 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) |
| 92 | + #( 5 6 7 8 ) ). |
| 93 | + self assert: t1 class equals: PMNDArray. |
| 94 | + |
| 95 | + t2 := PMNDArray fromNestedArray: #( #( #( 1 1 ) #( 2 2 ) ) |
| 96 | + #( #( 3 3 ) #( 4 4 ) ) |
| 97 | + #( #( 4 4 ) #( 4 4 ) ) |
| 98 | + #( #( 4 4 ) #( 4 4 ) ) ). |
| 99 | + self assert: t2 class equals: PMNDArray. |
| 100 | + |
| 101 | +] |
| 102 | + |
| 103 | +{ #category : #tests } |
| 104 | +PMNDArrayTest >> testRank [ |
| 105 | + |
| 106 | + | t1 t2 | |
| 107 | + |
| 108 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). |
| 109 | + self assert: t1 rank equals: 2. |
| 110 | + |
| 111 | + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) |
| 112 | + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). |
| 113 | + self assert: t2 rank equals: 3 |
| 114 | +] |
| 115 | + |
| 116 | +{ #category : #tests } |
| 117 | +PMNDArrayTest >> testReshape [ |
| 118 | + |
| 119 | + | t t1 | |
| 120 | + t := PMNDArray fromNestedArray: #( #( 0 1 ) #( 2 3 ) #( 4 5 ) ). |
| 121 | + t1 := t reshape: #( 2 3 ). |
| 122 | + |
| 123 | + self assert: t shape equals: #( 3 2 ). |
| 124 | + self assert: t1 shape equals: #( 2 3 ). |
| 125 | + self assert: t1 asArray == t asArray equals: true |
| 126 | +] |
| 127 | + |
| 128 | +{ #category : #tests } |
| 129 | +PMNDArrayTest >> testShape [ |
| 130 | + |
| 131 | + | t1 t2 | |
| 132 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) |
| 133 | + #( 5 6 7 8 ) ). |
| 134 | + self assert: t1 shape equals: #( 2 4 ). |
| 135 | + |
| 136 | + t2 := PMNDArray fromNestedArray: #( #( #( 1 1 ) #( 2 2 ) ) |
| 137 | + #( #( 3 3 ) #( 4 4 ) ) |
| 138 | + #( #( 4 4 ) #( 4 4 ) ) |
| 139 | + #( #( 4 4 ) #( 4 4 ) ) ). |
| 140 | + self assert: t2 shape equals: #( 4 2 2 ) |
| 141 | +] |
| 142 | + |
| 143 | +{ #category : #tests } |
| 144 | +PMNDArrayTest >> testSize [ |
| 145 | + |
| 146 | + | t1 t2 | |
| 147 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). |
| 148 | + self assert: t1 size equals: 8. |
| 149 | + |
| 150 | + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) |
| 151 | + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). |
| 152 | + self assert: t2 size equals: 16 |
| 153 | +] |
| 154 | + |
| 155 | +{ #category : #tests } |
| 156 | +PMNDArrayTest >> testStrides [ |
| 157 | + |
| 158 | + | a b | |
| 159 | + a := PMNDArray fromNestedArray: (1 to: 24) asArray. |
| 160 | + self assert: a strides equals: #( 1 ). |
| 161 | + b := a reshape: #( 4 6 ). |
| 162 | + self assert: b strides equals: #( 6 1 ). |
| 163 | + b := a reshape: #( 6 4 ). |
| 164 | + self assert: b strides equals: #( 4 1 ). |
| 165 | + self assert: (b flattenedIndexOf: #( 4 2 )) equals: 14. |
| 166 | + b := a reshape: #( 3 4 2 ). |
| 167 | + self assert: b strides equals: #( 8 2 1 ). |
| 168 | + self assert: (b flattenedIndexOf: #( 3 2 1)) equals: 19. |
| 169 | + b := a reshape: #( 2 3 4 ). |
| 170 | + self assert: b strides equals: #( 12 4 1 ). |
| 171 | + self assert: (b flattenedIndexOf: #( 2 2 3 )) equals: 19 |
| 172 | +] |
| 173 | + |
| 174 | +{ #category : #tests } |
| 175 | +PMNDArrayTest >> testView [ |
| 176 | + |
| 177 | + | t t1 | |
| 178 | + t := PMNDArray fromNestedArray: |
| 179 | + #( #( 10 11 12 ) #( 13 14 15 ) #( 16 17 18 ) #( #( 20 21 22 ) |
| 180 | + #( 23 24 25 ) #( 26 27 28 ) ) |
| 181 | + #( #( 30 31 32 ) #( 33 34 35 ) #( 36 37 38 ) ) ). |
| 182 | + t1 := t view. |
| 183 | + self assert: t asArray == t1 asArray equals: true. |
| 184 | + self assert: t shape equals: t1 shape. |
| 185 | + self assert: t shape == t1 shape equals: false. |
| 186 | + self assert: t strides equals: t1 strides. |
| 187 | + self assert: t strides == t1 strides equals: false. |
| 188 | + self assert: t first equals: t1 first. |
| 189 | + self assert: t first == t1 first equals: false. |
| 190 | + |
| 191 | +] |
0 commit comments