|
| 1 | +Class { |
| 2 | + #name : #PMNDArray, |
| 3 | + #superclass : #Object, |
| 4 | + #instVars : [ |
| 5 | + 'array', |
| 6 | + 'shape', |
| 7 | + 'first', |
| 8 | + 'strides' |
| 9 | + ], |
| 10 | + #category : #'Math-Matrix' |
| 11 | +} |
| 12 | + |
| 13 | +{ #category : #'instance creation' } |
| 14 | +PMNDArray class >> fromNestedArray: anArray [ |
| 15 | + |
| 16 | + ^ self new array: anArray flattened withShape: (self shape:anArray) |
| 17 | +] |
| 18 | + |
| 19 | +{ #category : #'instance creation' } |
| 20 | +PMNDArray class >> newWith: anInteger [ |
| 21 | + |
| 22 | + ^ self new array: {anInteger} withShape: #( ) |
| 23 | +] |
| 24 | + |
| 25 | +{ #category : #accessing } |
| 26 | +PMNDArray class >> shape: anArray [ |
| 27 | + |
| 28 | +anArray isArray ifFalse:[^#()] |
| 29 | +ifTrue:[ |
| 30 | + ^ {anArray size}, (self shape: anArray first) |
| 31 | + ] |
| 32 | + |
| 33 | +] |
| 34 | + |
| 35 | +{ #category : #comparing } |
| 36 | +PMNDArray >> = anArray [ |
| 37 | + |
| 38 | + ^ array = anArray array & (first = anArray first) |
| 39 | + & (strides = anArray strides) & (shape = anArray shape) |
| 40 | +] |
| 41 | + |
| 42 | +{ #category : #accessing } |
| 43 | +PMNDArray >> array [ |
| 44 | +^array |
| 45 | +] |
| 46 | + |
| 47 | +{ #category : #'as yet unclassified' } |
| 48 | +PMNDArray >> array: aFlatArray withShape: aShape [ |
| 49 | + |
| 50 | + array := aFlatArray. |
| 51 | + shape := aShape copy. |
| 52 | + self updateFirst. |
| 53 | + shape ifNotEmpty: [ self updateStrides] |
| 54 | +] |
| 55 | + |
| 56 | +{ #category : #accessing } |
| 57 | +PMNDArray >> first [ |
| 58 | +^first |
| 59 | +] |
| 60 | + |
| 61 | +{ #category : #'primitives - file' } |
| 62 | +PMNDArray >> 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 | + |
| 71 | +{ #category : #'as yet unclassified' } |
| 72 | +PMNDArray >> fromNestedArray: aFlatArray withShape: aShape [ |
| 73 | + |
| 74 | + array := aFlatArray. |
| 75 | + shape := aShape copy. |
| 76 | + self updateFirst. |
| 77 | + shape ifNotEmpty: [ self updateStrides] |
| 78 | +] |
| 79 | + |
| 80 | +{ #category : #public } |
| 81 | +PMNDArray >> get: coords [ |
| 82 | + |
| 83 | + | position | |
| 84 | + position := self flattenedIndexOf: coords. |
| 85 | + ^ array at: position |
| 86 | +] |
| 87 | + |
| 88 | +{ #category : #accessing } |
| 89 | +PMNDArray >> rank [ |
| 90 | + ^ shape size |
| 91 | +] |
| 92 | + |
| 93 | +{ #category : #'as yet unclassified' } |
| 94 | +PMNDArray >> reshape: aNewShape [ |
| 95 | + |
| 96 | + ^ self viewWithShape: aNewShape. |
| 97 | + |
| 98 | +] |
| 99 | + |
| 100 | +{ #category : #initialization } |
| 101 | +PMNDArray >> set: coords value: aValue [ |
| 102 | + |
| 103 | +array at: (self flattenedIndexOf: coords) put: aValue |
| 104 | +] |
| 105 | + |
| 106 | +{ #category : #accessing } |
| 107 | +PMNDArray >> shape [ |
| 108 | + |
| 109 | + ^ shape |
| 110 | +] |
| 111 | + |
| 112 | +{ #category : #accessing } |
| 113 | +PMNDArray >> size [ |
| 114 | + |
| 115 | +^ shape inject: 1 into: [ :each :product | each * product]. |
| 116 | + |
| 117 | +] |
| 118 | + |
| 119 | +{ #category : #accessing } |
| 120 | +PMNDArray >> strides [ |
| 121 | +^strides |
| 122 | +] |
| 123 | + |
| 124 | +{ #category : #'as yet unclassified' } |
| 125 | +PMNDArray >> updateFirst [ |
| 126 | + |
| 127 | + first := Array new: shape size withAll: 1 |
| 128 | +] |
| 129 | + |
| 130 | +{ #category : #'as yet unclassified' } |
| 131 | +PMNDArray >> updateStrides [ |
| 132 | + |
| 133 | + strides := Array new: shape size. |
| 134 | + strides at: shape size put: 1. |
| 135 | + ((shape size -1) to: 1 by: -1) do: [ :i | |
| 136 | + strides at: i put: ((strides at: i + 1) * (shape at: i+1))] |
| 137 | +] |
| 138 | + |
| 139 | +{ #category : #'as yet unclassified' } |
| 140 | +PMNDArray >> view [ |
| 141 | + |
| 142 | + "Share only the array" |
| 143 | + |
| 144 | + ^ self viewWithShape: shape |
| 145 | +] |
| 146 | + |
| 147 | +{ #category : #'as yet unclassified' } |
| 148 | +PMNDArray >> viewWithShape: aNewShape [ |
| 149 | + |
| 150 | + ^ PMNDArray new array: self array withShape: aNewShape |
| 151 | + |
| 152 | +] |
0 commit comments