|
| 1 | +Class { |
| 2 | + #name : #PMTensor, |
| 3 | + #superclass : #Object, |
| 4 | + #instVars : [ |
| 5 | + 'array', |
| 6 | + 'shape', |
| 7 | + 'first', |
| 8 | + 'strides' |
| 9 | + ], |
| 10 | + #category : #'Math-Matrix' |
| 11 | +} |
| 12 | + |
| 13 | +{ #category : #comparing } |
| 14 | +PMTensor >> = aTensor [ |
| 15 | + |
| 16 | + ^ array = aTensor array & (first = aTensor first) |
| 17 | + & (strides = aTensor strides) & (shape = aTensor shape) |
| 18 | +] |
| 19 | + |
| 20 | +{ #category : #accessing } |
| 21 | +PMTensor >> array [ |
| 22 | +^array |
| 23 | +] |
| 24 | + |
| 25 | +{ #category : #'as yet unclassified' } |
| 26 | +PMTensor >> array: aFlatArray withShape: aShape [ |
| 27 | + |
| 28 | + array := aFlatArray. |
| 29 | + shape := aShape copy. |
| 30 | + self updateFirst. |
| 31 | + shape ifNotEmpty: [ self updateStrides] |
| 32 | +] |
| 33 | + |
| 34 | +{ #category : #accessing } |
| 35 | +PMTensor >> first [ |
| 36 | +^first |
| 37 | +] |
| 38 | + |
| 39 | +{ #category : #public } |
| 40 | +PMTensor >> get: coords [ |
| 41 | + |
| 42 | + | position | |
| 43 | + position := self getPosition: coords. |
| 44 | + ^ array at: position |
| 45 | +] |
| 46 | + |
| 47 | +{ #category : #'primitives - file' } |
| 48 | +PMTensor >> getPosition: coords [ |
| 49 | + |
| 50 | + | position | |
| 51 | + position := 1. |
| 52 | + coords withIndexDo: [ :elt :i | |
| 53 | + position := elt - 1 * (strides at: i) + position ]. |
| 54 | + ^ position |
| 55 | +] |
| 56 | + |
| 57 | +{ #category : #accessing } |
| 58 | +PMTensor >> rank [ |
| 59 | + ^ shape size |
| 60 | +] |
| 61 | + |
| 62 | +{ #category : #'as yet unclassified' } |
| 63 | +PMTensor >> reshape: aNewShape [ |
| 64 | + |
| 65 | + ^ self viewWithShape: aNewShape. |
| 66 | + |
| 67 | +] |
| 68 | + |
| 69 | +{ #category : #initialization } |
| 70 | +PMTensor >> set: coords value: aValue [ |
| 71 | + |
| 72 | +array at: (self getPosition: coords) put: aValue |
| 73 | +] |
| 74 | + |
| 75 | +{ #category : #accessing } |
| 76 | +PMTensor >> shape [ |
| 77 | + |
| 78 | + ^ shape |
| 79 | +] |
| 80 | + |
| 81 | +{ #category : #accessing } |
| 82 | +PMTensor >> size [ |
| 83 | + |
| 84 | + | product | |
| 85 | + product := 1. |
| 86 | + shape do: [ :each | product := each * product ]. |
| 87 | + ^ product |
| 88 | +] |
| 89 | + |
| 90 | +{ #category : #accessing } |
| 91 | +PMTensor >> strides [ |
| 92 | +^strides |
| 93 | +] |
| 94 | + |
| 95 | +{ #category : #'as yet unclassified' } |
| 96 | +PMTensor >> updateFirst [ |
| 97 | + |
| 98 | + first := Array new: shape size withAll: 1 |
| 99 | +] |
| 100 | + |
| 101 | +{ #category : #'as yet unclassified' } |
| 102 | +PMTensor >> updateStrides [ |
| 103 | + |
| 104 | + strides := Array new: shape size. |
| 105 | + strides at: shape size put: 1. |
| 106 | + ((shape size -1) to: 1 by: -1) do: [ :i | |
| 107 | + strides at: i put: ((strides at: i + 1) * (shape at: i+1))] |
| 108 | +] |
| 109 | + |
| 110 | +{ #category : #'as yet unclassified' } |
| 111 | +PMTensor >> view [ |
| 112 | + |
| 113 | + "Share only the array" |
| 114 | + |
| 115 | + ^ self viewWithShape: shape |
| 116 | +] |
| 117 | + |
| 118 | +{ #category : #'as yet unclassified' } |
| 119 | +PMTensor >> viewWithShape: aNewShape [ |
| 120 | + |
| 121 | + ^ PMTensor new array: self array withShape: aNewShape |
| 122 | + |
| 123 | +] |
0 commit comments