Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
805149d
Addition of class and operations on rank 2 tensors
YvanGuifo Jul 17, 2021
8d6033e
Rank2 tensor class methods
YvanGuifo Jul 17, 2021
cc9056d
Rank2 tensor tests
YvanGuifo Jul 17, 2021
3df00c5
The general class for a tensor > 2
YvanGuifo Jul 17, 2021
b1b923d
Class methods for the tensor of rank greater than 2
YvanGuifo Jul 17, 2021
c3d803a
The test class and the operations defined for the tensor of rank grea…
YvanGuifo Jul 17, 2021
a0d567d
The error management mechanism for our tensors
YvanGuifo Jul 17, 2021
fb2ba9a
Delete PMTensorRank2.class.st
YvanGuifo Jul 21, 2021
4de26bd
Delete PMTensorRank2Test.class.st
YvanGuifo Jul 21, 2021
d4f52de
Renaming methods. To make the names more expressive
YvanGuifo Jul 26, 2021
2c9d247
Modification of the tests taking into account the modified methods
YvanGuifo Jul 26, 2021
88aab41
Merge 4de26bd6a6f6f7ec80c39f8c91336cc7f912da3d
YvanGuifo Jul 26, 2021
2ae081b
Renaming methods.
YvanGuifo Jul 26, 2021
8062a30
Modification of the tests taking into account the modified methods
YvanGuifo Jul 26, 2021
e8455f1
Delete PMTensor class and PMTensorTest
YvanGuifo Jul 29, 2021
b28871f
YvanGuifo Jul 29, 2021
605be14
YvanGuifo Jul 29, 2021
ebc0721
Delete PMArray and MPArrayTest
YvanGuifo Jul 30, 2021
8a713dc
Addition of the PMNDArray class replacing PMArray
YvanGuifo Jul 30, 2021
edf3d8b
Addition of the PMNDArrayTest class replacing PMArrayTest
YvanGuifo Jul 30, 2021
a1c3525
Refactoring of PMNDarray class
YvanGuifo Jul 30, 2021
0a77c47
Refactoring of PMNDArrayTest
YvanGuifo Jul 30, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions src/Math-Matrix/PMNDArray.class.st
Original file line number Diff line number Diff line change
@@ -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

]
191 changes: 191 additions & 0 deletions src/Math-Matrix/PMNDArrayTest.class.st
Original file line number Diff line number Diff line change
@@ -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.

]
19 changes: 19 additions & 0 deletions src/Math-Matrix/ShapeMismatch.class.st
Original file line number Diff line number Diff line change
@@ -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'
]