Skip to content

Commit c3d803a

Browse files
committed
The test class and the operations defined for the tensor of rank greater than 2
1 parent b1b923d commit c3d803a

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
Class {
2+
#name : #PMTensorTest,
3+
#superclass : #TestCase,
4+
#category : #'Math-Matrix'
5+
}
6+
7+
{ #category : #tests }
8+
PMTensorTest >> testArray [
9+
10+
| t1 t2 |
11+
t1 := PMTensor rows: #( #( 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 := PMTensor rows: #( #( #( 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+
PMTensorTest >> testCreateScalarTensor [
23+
24+
| s |
25+
s := PMTensor 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+
PMTensorTest >> testFirst [
37+
38+
| a b |
39+
a := PMTensor rows: (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+
PMTensorTest >> testGet [
47+
48+
| t1 t2 |
49+
t1 := PMTensor rows: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
50+
self assert: (t1 get: #( 2 2 )) equals: 6.
51+
52+
t2 := PMTensor rows: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) )
53+
#( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ).
54+
self assert: (t2 get: #( 3 2 1 )) equals: 11.
55+
56+
self should:[t1 get: #( 4 4 )] raise:Error
57+
]
58+
59+
{ #category : #tests }
60+
PMTensorTest >> testGetPosition [
61+
62+
| t1 t2 |
63+
t1 := PMTensor rows: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
64+
self assert: (t1 getPosition: #( 1 2 )) equals: 2.
65+
66+
t2 := PMTensor rows: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) )
67+
#( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ).
68+
self assert: (t2 getPosition: #( 1 2 2 )) equals: 4
69+
]
70+
71+
{ #category : #tests }
72+
PMTensorTest >> testRank [
73+
74+
| t1 t2 |
75+
76+
t1 := PMTensor rows: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
77+
self assert: t1 rank equals: 2.
78+
79+
t2 := PMTensor rows: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) )
80+
#( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ).
81+
self assert: t2 rank equals: 3
82+
]
83+
84+
{ #category : #tests }
85+
PMTensorTest >> testReshape [
86+
87+
| t t1 |
88+
t := PMTensor rows: #( #( 0 1 ) #( 2 3 ) #( 4 5 ) ).
89+
t1 := t reshape: #( 2 3 ).
90+
91+
self assert: t shape equals: #( 3 2 ).
92+
self assert: t1 shape equals: #( 2 3 ).
93+
self assert: t1 array == t array equals: true
94+
]
95+
96+
{ #category : #tests }
97+
PMTensorTest >> testRows [
98+
99+
| t1 t2 |
100+
t1 := PMTensor rows: #( #( 1 2 3 4 )
101+
#( 5 6 7 8 ) ).
102+
self assert: t1 class equals: PMTensor.
103+
104+
t2 := PMTensor rows: #( #( #( 1 1 ) #( 2 2 ) )
105+
#( #( 3 3 ) #( 4 4 ) )
106+
#( #( 4 4 ) #( 4 4 ) )
107+
#( #( 4 4 ) #( 4 4 ) ) ).
108+
self assert: t2 class equals: PMTensor.
109+
110+
]
111+
112+
{ #category : #tests }
113+
PMTensorTest >> testSetValue [
114+
115+
| t1 t2 |
116+
t1 := PMTensor rows: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
117+
t1 set: #( 2 2 ) value: 3.
118+
self assert: (t1 get: #( 2 2 ) ) equals: 3.
119+
120+
t2 := PMTensor rows: #( #( #( 1 2 ) #( 3 4 ) )
121+
#( #( 5 6 ) #( 7 8 ) )
122+
#( #( 9 10 ) #( 11 12 ) )
123+
#( #( 13 14 ) #( 15 16 ) ) ).
124+
t2 set: #( 2 2 1) value: 10.
125+
self assert: (t2 get: #( 2 2 1 )) equals: 10
126+
]
127+
128+
{ #category : #tests }
129+
PMTensorTest >> testShape [
130+
131+
| t1 t2 |
132+
t1 := PMTensor rows: #( #( 1 2 3 4 )
133+
#( 5 6 7 8 ) ).
134+
self assert: t1 shape equals: #( 2 4 ).
135+
136+
t2 := PMTensor rows: #( #( #( 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+
PMTensorTest >> testSize [
145+
146+
| t1 t2 |
147+
t1 := PMTensor rows: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ).
148+
self assert: t1 size equals: 8.
149+
150+
t2 := PMTensor rows: #( #( #( 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+
PMTensorTest >> testStrides [
157+
158+
| a b |
159+
a := PMTensor rows: (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 getPosition: #( 4 2 )) equals: 14.
166+
b := a reshape: #( 3 4 2 ).
167+
self assert: b strides equals: #( 8 2 1 ).
168+
self assert: (b getPosition: #( 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 getPosition: #( 2 2 3 )) equals: 19
172+
]
173+
174+
{ #category : #tests }
175+
PMTensorTest >> testView [
176+
177+
| t t1 |
178+
t := PMTensor rows:
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 array == t1 array 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

Comments
 (0)