Skip to content

Commit 605be14

Browse files
committed
Addition of the PMArrayTest class replacing PMTensorTest
1 parent b28871f commit 605be14

File tree

1 file changed

+218
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)