home *** CD-ROM | disk | FTP | other *** search
- OrderedCollection variableSubclass: #Matrix2D
- instanceVariableNames: 'numrows numcols view window '
- classVariableNames: ''
- poolDictionaries: ''
- category: 'Examples-Matrix2D'!
-
-
- !Matrix2D methodsFor: 'accessing'!
-
- at: anArray put: aNumber
- "Place an element (aNumber) in the row and column
- specified by anArray in the receiver."
-
- (self at: (anArray at: 1)) at:(anArray at:2) put:aNumber.!
-
- atRow: aNumber1
- "Return an ordered collection from row aNumber1of the receiver."
-
- ^(self at: aNumber1).!
-
- atRow: aNumber1 atCol: aNumber2
- "Return an element from row aNumber1, column aNumber2
- in the receiver."
-
- ^(self at: aNumber1) at: aNumber2.!
-
- atRow: aNumber1 atCol: aNumber2 put: aNumber3
- "Place an element (aNumber3) in row aNumber1, column aNumber2
- in the receiver."
-
- (self at: aNumber1) at: aNumber2 put: aNumber3.!
-
- atRow: aNumber1 put: anOrderedCollection
- "Puts an OrderedCollection into the matrix as a row."
-
- super at: aNumber1 put: anOrderedCollection.!
-
- cols
- "Returns the number of cols in the matrix."
-
- ^numcols.!
-
- readAt: anArray
- "Returns an element from the row and column
- specified by anArray in the receiver."
-
- ^(self at: (anArray at: 1)) at:(anArray at:2).!
-
- rows
- "Returns the number of rows in the matrix."
-
- ^numrows.!
-
- setrows: aNumber1 cols: aNumber2
- "Sets the size of the matrix."
-
- numrows := aNumber1.
- numcols := aNumber2.! !
-
- !Matrix2D methodsFor: 'representation'!
-
- writeToFile: aName
- "Writes the matrix to the file aName. The format is such that fromFile:
- can be called to read it back into a matrix"
-
- | aStream aFilename|
- aFilename := Filename named: aName.
- aStream := aFilename writeStream.
-
- aStream nextPut: $[.
- 1 to: (self rows) do: [ :row |
- 1 to: (self cols) do: [ :col |
- ((self atRow: row atCol: col) printString) do: [ :char |
- aStream nextPut: char
- ].
- aStream nextPut:$ . "space"
- ].
- row = (self rows)
- ifFalse: [ aStream nextPut: 13 asCharacter. "cr"].
- ].
- aStream nextPut: $].
-
- aStream close.!
-
- writeToTranscript
- "Writes the matrix to the Transcript."
-
- Transcript show: ' ';cr.
- 1 to: (self rows) do: [ :i |
- Transcript show: ' '; tab.
- 1 to: (self cols) do: [ :j |
- Transcript show: (self atRow: i atCol: j) printString; tab.
- ].
- Transcript show: ' ';cr.
- ].! !
-
- !Matrix2D methodsFor: 'operations'!
-
- matrixAdd: aMatrix
- "Adds the receiver and aMatrix, that is, Receiver + aMatrix."
-
- | matrix |
- ( (numrows == ( aMatrix rows)) & (numcols == ( aMatrix cols)) )
- ifFalse: [ Transcript nextPutAll: 'matrixAdd - bad matrix size' ;endEntry.
- ^nil. ].
- matrix := Matrix2D rows: numrows cols: numcols.
- 1 to: numrows do: [ :row |
- 1 to: numcols do: [ :col |
- matrix atRow: row atCol: col put:
- ((self atRow: row atCol: col) + (aMatrix atRow: row atCol: col)).
- ].
- ].
- "Returns a new matrix"
- ^matrix!
-
- matrixMult: aMatrix
- "Multiplies the receiver and aMatrix, that is, Receiver * aMatrix."
-
- | nrows ncols matrix sum |
- nrows := self rows.
- ncols := self cols.
- (ncols == ( aMatrix rows))
- ifFalse: [ Transcript nextPutAll: 'matrixMult - bad matrix size' ;endEntry.
- ^nil. ].
- matrix := Matrix2D rows: nrows cols:(aMatrix cols).
-
- 1 to: (aMatrix cols) do: [ :k |
- 1 to: nrows do: [ :i |
- sum := 0.
- 1 to: ncols do: [ :j |
- sum := sum + ((self atRow: i atCol: j) * (aMatrix atRow: j atCol: k)).
- ].
- matrix atRow: i atCol:k put: sum.
- ]
- ].
- ^matrix!
-
- scalarAdd: aNumber
- "Adds aNumber to each element of the receiver."
-
- | nrows ncols matrix |
- nrows := self rows.
- ncols := self cols.
- matrix := Matrix2D rows: nrows cols: ncols.
- 1 to: nrows do: [ :row |
- 1 to: ncols do: [ :col |
- matrix atRow: row atCol: col put: ( self atRow: row atCol: col ) + aNumber.
- ].
- ].
- ^matrix!
-
- scalarMult: aNumber
- "Multiplies each element of the receiver by aNumber."
-
- | nrows ncols matrix |
- nrows := self rows.
- ncols := self cols.
- matrix := Matrix2D rows: nrows cols: ncols.
- 1 to: nrows do: [ :row |
- 1 to: ncols do: [ :col |
- matrix atRow: row atCol: col put: ( self atRow: row atCol: col ) * aNumber.
- ].
- ].
- ^matrix!
-
- transpose
- "Returns the transpose of the receiver."
-
- | nrows ncols matrix |
- nrows := self rows.
- ncols := self cols.
- matrix := Matrix2D rows: nrows cols: ncols.
- 1 to: nrows do: [ :row |
- 1 to: ncols do: [ :col |
- matrix atRow: row atCol: col put: ( self atRow: col atCol: row ).
- ].
- ].
- ^matrix!
-
- vectorColMult: aVector
- "Multiplies each column of the receiver by aVector and returns a Vector."
-
- | ncols vector tmatrix v1 |
- ncols := self cols.
- (ncols == ( aVector size))
- ifFalse: [ Transcript nextPutAll: 'vectorColMult - bad matrix size' ;endEntry.
- ^nil. ].
- vector := Vector new.
- tmatrix := self transpose.
- 1 to: ncols do: [ :col |
- v1 := Vector newwith: (tmatrix atRow: col).
- vector addLast: ( aVector dotProd: v1 ).
- ].
- ^vector!
-
- vectorRowMult: aVector
- "Multiplies each row of the receiver by aVector and returns a Vector."
-
- | nrows vector v1 |
- nrows := self rows.
- (nrows == ( aVector size))
- ifFalse: [ Transcript nextPutAll: 'vectorRowMult - bad matrix size' ;endEntry.
- ^nil. ].
- vector := Vector new.
- 1 to: nrows do: [ :row |
- v1 := Vector newwith: (self atRow: row).
- vector addLast: ( v1 dotProd: aVector ).
- ].
- ^vector! !
- "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
-
- Matrix2D class
- instanceVariableNames: ''!
-
-
- !Matrix2D class methodsFor: 'instance creation'!
-
- fromFile: aName
- "Creates a 2D matrix from a file of the name aName"
-
- | aStream aFilename aString aChar aCollection aMatrix|
- aFilename := Filename named: aName.
- aStream := aFilename readStream.
- aChar := aStream next.
-
- "Create the Matrix"
- aMatrix := Matrix2D rows:0 cols:0.
-
- "eat up everything until the open bracket"
- [ aChar = $[ ]
- whileFalse: [ aChar := aStream next].
- "matrix has started"
- aCollection := OrderedCollection new.
- aString := String new.
- [ aChar = $] ]
- whileFalse: [
- aChar := aStream next.
- aChar asInteger = 13 "cr"
- ifTrue: [
- aString size > 0
- ifTrue: [
- aCollection add: (aString asNumber).
- aString := String new.
- ].
- aMatrix addLast: aCollection.
- aMatrix setrows: (aMatrix rows + 1) cols: (aCollection size).
- aCollection := OrderedCollection new.
- ].
- aChar isSeparator "any white space"
- ifTrue: [
- aString size > 0
- ifTrue: [
- aCollection add: (aString asNumber).
- aString := String new.
- ]
- ].
- (aChar isDigit)
- ifTrue: [aString := aString,
- (aChar digitValue printString)].
- ].
- aStream close.
-
- "Add the last one, since the ']' was on the last line"
- aMatrix addLast: aCollection.
- aMatrix setrows: (aMatrix rows + 1) cols: (aCollection size).
- aCollection := OrderedCollection new.
-
-
- ^aMatrix!
-
- rows: aNumber1 cols: aNumber2
- "Creates a 2D matrix of size aNumber1 X aNumber2 and
- initializes all elements to 0."
-
- | matrix |
- matrix := self new.
- 1 to: aNumber1 do: [ :i | | temp |
- temp := OrderedCollection new.
- 1 to: aNumber2 do: [ :j | temp addLast: 0.0 ].
- matrix addLast: temp ].
- matrix setrows: aNumber1 cols: aNumber2.
- ^matrix! !
-
- OrderedCollection variableSubclass: #Vector
- instanceVariableNames: ''
- classVariableNames: ''
- poolDictionaries: ''
- category: 'Examples-Matrix2D'!
-
-
- !Vector methodsFor: 'converting'!
-
- asPoint
- "Convert a 3D vector to a 2D point by rounding the x and y values."
-
- | aPoint |
- aPoint := ((self at: 1) rounded) @ ((self at: 2) rounded).
- ^aPoint! !
-
- !Vector methodsFor: 'operations'!
-
- dotProd: aVector
- "Takes the dot product of the reciever and aVector
- and returns the numerical result."
-
- | temp |
- temp := 0.
- 1 to: self size do:
- [ :index | temp := temp + ( (self at: index) * (aVector at: index) ) ].
- ^temp!
-
- print
- "Prints the recieveing vector on the system transcript."
-
- Transcript flush.
- Transcript show: self printString.
- Transcript cr.
- Transcript flush.!
-
- scalarAdd: aNumber
- "Adds aNumber to each element of the vector."
-
- ^self collect: [ :element | element + aNumber ].!
-
- scalarMult: aNumber
- "Multiplies each element of the vector by aNumber."
-
- ^self collect: [ :element | element * aNumber ].!
-
- vectorAdd: aVector
- "Adds aVector to the reciever."
-
- | temp |
- temp := Vector new.
- 1 to: self size do:
- [ :index | temp addLast: ( (self at: index) + (aVector at: index) ) ].
- ^temp!
-
- vectorMult: aVector
- "Takes the dot product of the reciever and aVector
- and returns the numerical result."
-
- ^self dotProd: aVector! !
- "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
-
- Vector class
- instanceVariableNames: ''!
-
-
- !Vector class methodsFor: 'converting to'!
-
- newwith: anOrderedCollection
- "Returns a vector which is the equivalent of the ordered collection."
-
- | vector |
- vector := Vector new.
- vector addAllLast: anOrderedCollection.
- ^vector! !
-
-