@@ -58,6 +58,16 @@ PMTensor >> first [
5858^ first
5959]
6060
61+ { #category : #' primitives - file' }
62+ PMTensor >> flattenedIndexOf: coords [
63+
64+ | position |
65+ position := 1 .
66+ coords withIndexDo: [ :elt :i |
67+ position := elt - 1 * (strides at: i) + position ].
68+ ^ position
69+ ]
70+
6171{ #category : #' as yet unclassified' }
6272PMTensor >> flattenedIndexOf: aFlatArray withShape: aShape [
6373
@@ -67,22 +77,21 @@ PMTensor >> flattenedIndexOf: aFlatArray withShape: aShape [
6777 shape ifNotEmpty: [ self updateStrides]
6878]
6979
70- { #category : #public }
71- PMTensor >> get: coords [
80+ { #category : #' as yet unclassified ' }
81+ PMTensor >> fromNestedArray: aFlatArray withShape: aShape [
7282
73- | position |
74- position := self getPosition: coords.
75- ^ array at: position
83+ array := aFlatArray.
84+ shape := aShape copy.
85+ self updateFirst.
86+ shape ifNotEmpty: [ self updateStrides]
7687]
7788
78- { #category : #' primitives - file ' }
79- PMTensor >> getPosition : coords [
89+ { #category : #public }
90+ PMTensor >> get : coords [
8091
8192 | position |
82- position := 1 .
83- coords withIndexDo: [ :elt :i |
84- position := elt - 1 * (strides at: i) + position ].
85- ^ position
93+ position := self flattenedIndexOf: coords.
94+ ^ array at: position
8695]
8796
8897{ #category : #accessing }
@@ -100,7 +109,7 @@ PMTensor >> reshape: aNewShape [
100109{ #category : #initialization }
101110PMTensor >> set: coords value: aValue [
102111
103- array at: (self getPosition : coords) put: aValue
112+ array at: (self flattenedIndexOf : coords) put: aValue
104113]
105114
106115{ #category : #accessing }
@@ -112,10 +121,8 @@ PMTensor >> shape [
112121{ #category : #accessing }
113122PMTensor >> size [
114123
115- | product |
116- product := 1 .
117- shape do: [ :each | product := each * product ].
118- ^ product
124+ ^ shape inject: 1 into: [ :each :product | each * product].
125+
119126]
120127
121128{ #category : #accessing }
0 commit comments