diff --git a/src/Math-Matrix/PMNDArray.class.st b/src/Math-Matrix/PMNDArray.class.st new file mode 100644 index 00000000..c136785f --- /dev/null +++ b/src/Math-Matrix/PMNDArray.class.st @@ -0,0 +1,158 @@ +Class { + #name : #PMNDArray, + #superclass : #Object, + #instVars : [ + 'array', + 'shape', + 'first', + 'strides' + ], + #category : #'Math-Matrix' +} + +{ #category : #'instance creation' } +PMNDArray class >> fromNestedArray: anArray [ + + ^ self new array: anArray flattened withShape: (self shape:anArray) +] + +{ #category : #'instance creation' } +PMNDArray class >> fromScalar: anInteger [ + + ^ self new array: {anInteger} withShape: #( ) +] + +{ #category : #'instance creation' } +PMNDArray class >> newWith: anInteger [ + + ^ self new array: {anInteger} withShape: #( ) +] + +{ #category : #accessing } +PMNDArray class >> shape: anArray [ + +anArray isArray ifFalse:[^#()] +ifTrue:[ + ^ {anArray size}, (self shape: anArray first) + ] + +] + +{ #category : #comparing } +PMNDArray >> = anArray [ + + ^ array = anArray array & (first = anArray first) + & (strides = anArray strides) & (shape = anArray shape) +] + +{ #category : #'as yet unclassified' } +PMNDArray >> array: aFlatArray withShape: aShape [ + + array := aFlatArray. + shape := aShape copy. + self updateFirst. + shape ifNotEmpty: [ self updateStrides] +] + +{ #category : #private } +PMNDArray >> asArray [ + ^array +] + +{ #category : #public } +PMNDArray >> at: coords [ + + | position | + position := self flattenedIndexOf: coords. + ^ array at: position +] + +{ #category : #initialization } +PMNDArray >> at: coords put: aValue [ + +array at: (self flattenedIndexOf: coords) put: aValue +] + +{ #category : #accessing } +PMNDArray >> first [ +^first +] + +{ #category : #'primitives - file' } +PMNDArray >> flattenedIndexOf: coords [ + + | position | + position := 1. + coords withIndexDo: [ :elt :i | + position := (elt - 1) * (strides at: i) + position ]. + ^ position +] + +{ #category : #'as yet unclassified' } +PMNDArray >> fromNestedArray: aFlatArray withShape: aShape [ + + array := aFlatArray. + shape := aShape copy. + self updateFirst. + shape ifNotEmpty: [ self updateStrides] +] + +{ #category : #accessing } +PMNDArray >> rank [ + ^ shape size +] + +{ #category : #'as yet unclassified' } +PMNDArray >> reshape: aNewShape [ + + ^ self viewWithShape: aNewShape. + +] + +{ #category : #accessing } +PMNDArray >> shape [ + + ^ shape +] + +{ #category : #accessing } +PMNDArray >> size [ + +^ shape inject: 1 into: [ :each :product | each * product]. + +] + +{ #category : #accessing } +PMNDArray >> strides [ +^strides +] + +{ #category : #'as yet unclassified' } +PMNDArray >> updateFirst [ + + first := Array new: shape size withAll: 1 +] + +{ #category : #'as yet unclassified' } +PMNDArray >> updateStrides [ + + strides := Array new: shape size. + strides at: shape size put: 1. + ((shape size -1) to: 1 by: -1) do: [ :i | + strides at: i put: ((strides at: i + 1) * (shape at: i+1))] +] + +{ #category : #'as yet unclassified' } +PMNDArray >> view [ + + "Share only the array" + + ^ self viewWithShape: shape +] + +{ #category : #'as yet unclassified' } +PMNDArray >> viewWithShape: aNewShape [ + + ^ PMNDArray new array: self asArray withShape: aNewShape + +] diff --git a/src/Math-Matrix/PMNDArrayTest.class.st b/src/Math-Matrix/PMNDArrayTest.class.st new file mode 100644 index 00000000..d6edeba7 --- /dev/null +++ b/src/Math-Matrix/PMNDArrayTest.class.st @@ -0,0 +1,191 @@ +Class { + #name : #PMNDArrayTest, + #superclass : #TestCase, + #category : #'Math-Matrix' +} + +{ #category : #tests } +PMNDArrayTest >> testArray [ + + | t1 t2 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). + self assert: t1 asArray equals: #( 1 2 3 4 5 6 7 8 ). + + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). + self + assert: t2 asArray + equals: #( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ) +] + +{ #category : #tests } +PMNDArrayTest >> testAt [ + + | t1 t2 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). + self assert: (t1 at: #( 2 2 )) equals: 6. + + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). + self assert: (t2 at: #( 3 2 1 )) equals: 11. + + self should:[t1 at: #( 4 4 )] raise:Error +] + +{ #category : #tests } +PMNDArrayTest >> testAtPut [ + + | t1 t2 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). + t1 at: #( 2 2 ) put: 3. + self assert: (t1 at: #( 2 2 ) ) equals: 3. + + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) + #( #( 5 6 ) #( 7 8 ) ) + #( #( 9 10 ) #( 11 12 ) ) + #( #( 13 14 ) #( 15 16 ) ) ). + t2 at: #( 2 2 1) put: 10. + self assert: (t2 at: #( 2 2 1 )) equals: 10 +] + +{ #category : #tests } +PMNDArrayTest >> testCreateScalarNDArray [ + + | s | + s := PMNDArray fromScalar: 2. + self assert: (s at: #( )) equals: 2. + self should: [ s at: #( 1 1 ) ] raise: Error. + self assert: s rank equals: 0. + s at: #( ) put: 1. + self assert: (s at: #( )) equals: 1. + self assert: s shape equals: #( ). + self assert: s size equals: 1 +] + +{ #category : #tests } +PMNDArrayTest >> testFirst [ + + | a b | + a := PMNDArray fromNestedArray: (1 to: 6) asArray. + self assert: a first equals: #( 1). + b := a reshape: #( 3 2 ). + self assert: b first equals: #( 1 1 ) +] + +{ #category : #tests } +PMNDArrayTest >> testFlattenedIndexOf [ + + | t1 t2 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8) #( 9 10 11 12)). + self assert: (t1 flattenedIndexOf: #( 3 2 )) equals: 10. + + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). + self assert: (t2 flattenedIndexOf: #( 1 2 2 )) equals: 4 +] + +{ #category : #tests } +PMNDArrayTest >> testFromNestedArray [ + + | t1 t2 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) + #( 5 6 7 8 ) ). + self assert: t1 class equals: PMNDArray. + + t2 := PMNDArray fromNestedArray: #( #( #( 1 1 ) #( 2 2 ) ) + #( #( 3 3 ) #( 4 4 ) ) + #( #( 4 4 ) #( 4 4 ) ) + #( #( 4 4 ) #( 4 4 ) ) ). + self assert: t2 class equals: PMNDArray. + +] + +{ #category : #tests } +PMNDArrayTest >> testRank [ + + | t1 t2 | + + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). + self assert: t1 rank equals: 2. + + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). + self assert: t2 rank equals: 3 +] + +{ #category : #tests } +PMNDArrayTest >> testReshape [ + + | t t1 | + t := PMNDArray fromNestedArray: #( #( 0 1 ) #( 2 3 ) #( 4 5 ) ). + t1 := t reshape: #( 2 3 ). + + self assert: t shape equals: #( 3 2 ). + self assert: t1 shape equals: #( 2 3 ). + self assert: t1 asArray == t asArray equals: true +] + +{ #category : #tests } +PMNDArrayTest >> testShape [ + + | t1 t2 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) + #( 5 6 7 8 ) ). + self assert: t1 shape equals: #( 2 4 ). + + t2 := PMNDArray fromNestedArray: #( #( #( 1 1 ) #( 2 2 ) ) + #( #( 3 3 ) #( 4 4 ) ) + #( #( 4 4 ) #( 4 4 ) ) + #( #( 4 4 ) #( 4 4 ) ) ). + self assert: t2 shape equals: #( 4 2 2 ) +] + +{ #category : #tests } +PMNDArrayTest >> testSize [ + + | t1 t2 | + t1 := PMNDArray fromNestedArray: #( #( 1 2 3 4 ) #( 5 6 7 8 ) ). + self assert: t1 size equals: 8. + + t2 := PMNDArray fromNestedArray: #( #( #( 1 2 ) #( 3 4 ) ) #( #( 5 6 ) #( 7 8 ) ) + #( #( 9 10 ) #( 11 12 ) ) #( #( 13 14 ) #( 15 16 ) ) ). + self assert: t2 size equals: 16 +] + +{ #category : #tests } +PMNDArrayTest >> testStrides [ + + | a b | + a := PMNDArray fromNestedArray: (1 to: 24) asArray. + self assert: a strides equals: #( 1 ). + b := a reshape: #( 4 6 ). + self assert: b strides equals: #( 6 1 ). + b := a reshape: #( 6 4 ). + self assert: b strides equals: #( 4 1 ). + self assert: (b flattenedIndexOf: #( 4 2 )) equals: 14. + b := a reshape: #( 3 4 2 ). + self assert: b strides equals: #( 8 2 1 ). + self assert: (b flattenedIndexOf: #( 3 2 1)) equals: 19. + b := a reshape: #( 2 3 4 ). + self assert: b strides equals: #( 12 4 1 ). + self assert: (b flattenedIndexOf: #( 2 2 3 )) equals: 19 +] + +{ #category : #tests } +PMNDArrayTest >> testView [ + + | t t1 | + t := PMNDArray fromNestedArray: + #( #( 10 11 12 ) #( 13 14 15 ) #( 16 17 18 ) #( #( 20 21 22 ) + #( 23 24 25 ) #( 26 27 28 ) ) + #( #( 30 31 32 ) #( 33 34 35 ) #( 36 37 38 ) ) ). + t1 := t view. + self assert: t asArray == t1 asArray equals: true. + self assert: t shape equals: t1 shape. + self assert: t shape == t1 shape equals: false. + self assert: t strides equals: t1 strides. + self assert: t strides == t1 strides equals: false. + self assert: t first equals: t1 first. + self assert: t first == t1 first equals: false. + +] diff --git a/src/Math-Matrix/ShapeMismatch.class.st b/src/Math-Matrix/ShapeMismatch.class.st new file mode 100644 index 00000000..a56c23fa --- /dev/null +++ b/src/Math-Matrix/ShapeMismatch.class.st @@ -0,0 +1,19 @@ +Class { + #name : #ShapeMismatch, + #superclass : #Error, + #category : #'Math-Matrix' +} + +{ #category : #accessing } +ShapeMismatch >> messageText [ + "Overwritten to initialiaze the message text to a standard text if it has not yet been set" + + ^ messageText ifNil: [ messageText := self standardMessageText ] +] + +{ #category : #printing } +ShapeMismatch >> standardMessageText [ + "Generate a standard textual description" + + ^ 'Tensor shapes do not match' +]