home *** CD-ROM | disk | FTP | other *** search
/ European Smalltalk User Group 2004 September / esugcd.iso / Books-Tutorial-Lectures / Tutorials / st-course / source / Examples-Matrix2D.st < prev   
Encoding:
Text File  |  1999-02-02  |  8.9 KB  |  360 lines

  1. OrderedCollection variableSubclass: #Matrix2D
  2.     instanceVariableNames: 'numrows numcols view window '
  3.     classVariableNames: ''
  4.     poolDictionaries: ''
  5.     category: 'Examples-Matrix2D'!
  6.  
  7.  
  8. !Matrix2D methodsFor: 'accessing'!
  9.  
  10. at: anArray put: aNumber
  11.     "Place an element (aNumber) in the row and column
  12.      specified by anArray in the receiver."
  13.  
  14.     (self at: (anArray at: 1)) at:(anArray at:2) put:aNumber.!
  15.  
  16. atRow: aNumber1
  17.     "Return an ordered collection from row aNumber1of the receiver."
  18.  
  19.     ^(self at: aNumber1).!
  20.  
  21. atRow: aNumber1 atCol: aNumber2
  22.     "Return an element from row aNumber1, column aNumber2
  23.      in the receiver."
  24.  
  25.     ^(self at: aNumber1) at: aNumber2.!
  26.  
  27. atRow: aNumber1 atCol: aNumber2 put: aNumber3
  28.     "Place an element (aNumber3) in row aNumber1, column aNumber2
  29.      in the receiver."
  30.  
  31.     (self at: aNumber1) at: aNumber2 put: aNumber3.!
  32.  
  33. atRow: aNumber1 put: anOrderedCollection
  34.     "Puts an OrderedCollection into the matrix as a row."
  35.  
  36.     super at: aNumber1 put: anOrderedCollection.!
  37.  
  38. cols
  39.     "Returns the number of cols in the matrix."
  40.  
  41.     ^numcols.!
  42.  
  43. readAt: anArray
  44.     "Returns an element from the row and column
  45.      specified by anArray in the receiver."
  46.  
  47.     ^(self at: (anArray at: 1)) at:(anArray at:2).!
  48.  
  49. rows
  50.     "Returns the number of rows in the matrix."
  51.  
  52.     ^numrows.!
  53.  
  54. setrows: aNumber1 cols: aNumber2
  55.     "Sets the size of the matrix."
  56.  
  57.     numrows := aNumber1.
  58.     numcols := aNumber2.! !
  59.  
  60. !Matrix2D methodsFor: 'representation'!
  61.  
  62. writeToFile: aName
  63.     "Writes the matrix to the file aName. The format is such that fromFile:
  64.     can be called to read it back into a matrix"
  65.  
  66.     | aStream aFilename|
  67.     aFilename := Filename named: aName.
  68.     aStream := aFilename writeStream.
  69.     
  70.     aStream nextPut: $[.
  71.     1 to: (self rows) do: [ :row |
  72.         1 to: (self cols) do: [ :col |
  73.             ((self atRow: row atCol: col) printString) do: [ :char |
  74.                 aStream nextPut: char
  75.             ].
  76.             aStream nextPut:$ . "space"
  77.         ].
  78.         row = (self rows)
  79.             ifFalse: [ aStream nextPut: 13 asCharacter. "cr"].
  80.     ].
  81.     aStream nextPut: $].
  82.  
  83.     aStream close.!
  84.  
  85. writeToTranscript
  86.     "Writes the matrix to the Transcript."
  87.  
  88.     Transcript show: ' ';cr.
  89.     1 to: (self rows) do: [ :i |
  90.         Transcript show: ' '; tab.
  91.         1 to: (self cols) do: [ :j |
  92.             Transcript show: (self atRow: i atCol: j) printString; tab.
  93.         ].
  94.         Transcript show: ' ';cr.
  95.     ].! !
  96.  
  97. !Matrix2D methodsFor: 'operations'!
  98.  
  99. matrixAdd: aMatrix
  100.     "Adds the receiver and aMatrix, that is, Receiver + aMatrix."
  101.  
  102.     | matrix |
  103.     ( (numrows == ( aMatrix rows)) & (numcols == ( aMatrix cols)) )
  104.     ifFalse: [ Transcript nextPutAll: 'matrixAdd - bad matrix size' ;endEntry.
  105.              ^nil. ].
  106.     matrix := Matrix2D rows: numrows cols: numcols.
  107.     1 to: numrows do: [ :row |
  108.         1 to: numcols do: [ :col |
  109.             matrix atRow: row atCol: col put: 
  110.                 ((self atRow: row atCol: col) +  (aMatrix atRow: row atCol: col)).
  111.         ].
  112.     ].
  113.     "Returns a new matrix"
  114.     ^matrix!
  115.  
  116. matrixMult: aMatrix
  117.     "Multiplies the receiver and aMatrix, that is, Receiver * aMatrix."
  118.  
  119.     | nrows ncols matrix sum |
  120.     nrows := self rows.
  121.     ncols := self cols.
  122.     (ncols == ( aMatrix rows))
  123.     ifFalse: [ Transcript nextPutAll: 'matrixMult - bad matrix size' ;endEntry.
  124.              ^nil. ].
  125.     matrix := Matrix2D rows: nrows cols:(aMatrix cols).
  126.  
  127.     1 to: (aMatrix cols) do: [ :k |
  128.         1 to: nrows  do: [ :i |
  129.             sum := 0.
  130.             1 to: ncols do: [ :j | 
  131.                 sum :=  sum + ((self atRow: i atCol: j) * (aMatrix atRow: j atCol: k)).
  132.             ].
  133.             matrix atRow: i atCol:k put: sum.
  134.         ]
  135.     ].
  136.     ^matrix!
  137.  
  138. scalarAdd: aNumber
  139.     "Adds aNumber to each element of the receiver."
  140.  
  141.     | nrows ncols matrix |
  142.     nrows := self rows.
  143.     ncols := self cols.
  144.     matrix := Matrix2D rows: nrows cols: ncols.
  145.     1 to: nrows do: [ :row |
  146.         1 to: ncols do: [ :col |
  147.             matrix atRow: row atCol: col put: ( self atRow: row atCol: col ) + aNumber.
  148.         ].
  149.     ].
  150.     ^matrix!
  151.  
  152. scalarMult: aNumber
  153.     "Multiplies each element of the receiver by aNumber."
  154.  
  155.     | nrows ncols matrix |
  156.     nrows := self rows.
  157.     ncols := self cols.
  158.     matrix := Matrix2D rows: nrows cols: ncols.
  159.     1 to: nrows do: [ :row |
  160.         1 to: ncols do: [ :col |
  161.             matrix atRow: row atCol: col put: ( self atRow: row atCol: col ) * aNumber.
  162.         ].
  163.     ].
  164.     ^matrix!
  165.  
  166. transpose
  167.     "Returns the transpose of the receiver."
  168.  
  169.     | nrows ncols matrix |
  170.     nrows := self rows.
  171.     ncols := self cols.
  172.     matrix := Matrix2D rows: nrows cols: ncols.
  173.     1 to: nrows do: [ :row |
  174.         1 to: ncols do: [ :col |
  175.             matrix atRow: row atCol: col put: ( self atRow: col atCol: row ).
  176.         ].
  177.     ].
  178.     ^matrix!
  179.  
  180. vectorColMult: aVector
  181.     "Multiplies each column of the receiver by aVector and returns a Vector."
  182.  
  183.     | ncols vector tmatrix v1 |
  184.     ncols := self cols.
  185.     (ncols == ( aVector size))
  186.     ifFalse: [ Transcript nextPutAll: 'vectorColMult - bad matrix size' ;endEntry.
  187.              ^nil. ].
  188.     vector := Vector new.
  189.     tmatrix := self transpose.
  190.     1 to: ncols do: [ :col |
  191.         v1 := Vector newwith: (tmatrix atRow: col).
  192.         vector addLast: ( aVector dotProd: v1 ).
  193.     ].
  194.     ^vector!
  195.  
  196. vectorRowMult: aVector
  197.     "Multiplies each row of the receiver by aVector and returns a Vector."
  198.  
  199.     | nrows vector  v1 |
  200.     nrows := self rows.
  201.     (nrows == ( aVector size))
  202.     ifFalse: [ Transcript nextPutAll: 'vectorRowMult - bad matrix size' ;endEntry.
  203.              ^nil. ].
  204.     vector := Vector new.
  205.     1 to: nrows do: [ :row |
  206.         v1 := Vector newwith: (self atRow: row).
  207.         vector addLast: ( v1 dotProd: aVector ).
  208.     ].
  209.     ^vector! !
  210. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  211.  
  212. Matrix2D class
  213.     instanceVariableNames: ''!
  214.  
  215.  
  216. !Matrix2D class methodsFor: 'instance creation'!
  217.  
  218. fromFile: aName
  219.     "Creates a 2D matrix from a file of the name aName"
  220.  
  221.     | aStream aFilename aString aChar aCollection aMatrix|
  222.     aFilename := Filename named: aName.
  223.     aStream := aFilename readStream.
  224.     aChar := aStream next.
  225.  
  226.     "Create the Matrix"
  227.     aMatrix := Matrix2D rows:0 cols:0.
  228.  
  229.     "eat up everything until the open bracket"
  230.     [ aChar = $[ ]
  231.         whileFalse: [ aChar := aStream next]. 
  232.     "matrix has started"
  233.     aCollection := OrderedCollection new.
  234.     aString := String new.
  235.     [ aChar = $] ]
  236.         whileFalse: [
  237.             aChar := aStream next.
  238.             aChar asInteger = 13 "cr"
  239.                 ifTrue: [
  240.                     aString size > 0
  241.                         ifTrue: [
  242.                             aCollection add: (aString asNumber). 
  243.                             aString := String new.
  244.                         ].
  245.                     aMatrix addLast: aCollection.
  246.                     aMatrix setrows: (aMatrix rows + 1) cols: (aCollection size).
  247.                     aCollection := OrderedCollection new.
  248.                 ].
  249.             aChar isSeparator "any white space"
  250.                 ifTrue: [
  251.                 aString size > 0
  252.                     ifTrue: [
  253.                         aCollection add: (aString asNumber). 
  254.                         aString := String new.
  255.                     ]
  256.                 ].
  257.             (aChar isDigit)
  258.                 ifTrue: [aString := aString, 
  259.                     (aChar digitValue printString)].
  260.         ].    
  261.     aStream close.
  262.  
  263.     "Add the last one, since the ']' was on the last line"
  264.     aMatrix addLast: aCollection.
  265.     aMatrix setrows: (aMatrix rows + 1) cols: (aCollection size).
  266.     aCollection := OrderedCollection new.
  267.  
  268.     
  269.     ^aMatrix!
  270.  
  271. rows: aNumber1 cols: aNumber2
  272.     "Creates a 2D matrix of size aNumber1 X aNumber2 and
  273.      initializes all elements to 0."
  274.  
  275.     | matrix |
  276.     matrix := self new.
  277.     1 to: aNumber1 do: [ :i | | temp |
  278.                             temp := OrderedCollection new.
  279.                             1 to: aNumber2 do: [ :j | temp addLast: 0.0 ].
  280.                              matrix addLast: temp ].
  281.     matrix setrows: aNumber1 cols: aNumber2.
  282.     ^matrix! !
  283.  
  284. OrderedCollection variableSubclass: #Vector
  285.     instanceVariableNames: ''
  286.     classVariableNames: ''
  287.     poolDictionaries: ''
  288.     category: 'Examples-Matrix2D'!
  289.  
  290.  
  291. !Vector methodsFor: 'converting'!
  292.  
  293. asPoint
  294.     "Convert a 3D vector to a 2D point by rounding the x and y values."
  295.  
  296.     | aPoint |
  297.     aPoint := ((self at: 1) rounded) @ ((self at: 2) rounded).
  298.     ^aPoint! !
  299.  
  300. !Vector methodsFor: 'operations'!
  301.  
  302. dotProd: aVector
  303.     "Takes the dot product of the reciever and aVector
  304.      and returns the numerical result."
  305.  
  306.     | temp |
  307.     temp := 0.
  308.     1 to: self size do:
  309.         [ :index | temp := temp + ( (self at: index) * (aVector at: index) ) ].
  310.     ^temp!
  311.  
  312. print
  313.     "Prints the recieveing vector on the system transcript."
  314.      
  315.     Transcript flush.
  316.     Transcript show: self printString.
  317.     Transcript cr.
  318.     Transcript flush.!
  319.  
  320. scalarAdd: aNumber
  321.     "Adds aNumber to each element of the vector."
  322.  
  323.     ^self collect: [ :element | element + aNumber ].!
  324.  
  325. scalarMult: aNumber
  326.     "Multiplies each element of the vector by aNumber."
  327.  
  328.     ^self collect: [ :element | element * aNumber ].!
  329.  
  330. vectorAdd: aVector
  331.     "Adds aVector to the reciever."
  332.  
  333.     | temp |
  334.     temp := Vector new.
  335.     1 to: self size do:
  336.         [ :index | temp addLast: ( (self at: index) + (aVector at: index) ) ].
  337.     ^temp!
  338.  
  339. vectorMult: aVector
  340.     "Takes the dot product of the reciever and aVector
  341.      and returns the numerical result."
  342.  
  343.     ^self dotProd: aVector! !
  344. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  345.  
  346. Vector class
  347.     instanceVariableNames: ''!
  348.  
  349.  
  350. !Vector class methodsFor: 'converting to'!
  351.  
  352. newwith: anOrderedCollection
  353.     "Returns a vector which is the equivalent of the ordered collection."
  354.  
  355.     | vector |
  356.     vector := Vector new.
  357.     vector addAllLast: anOrderedCollection.
  358.     ^vector! !
  359.  
  360.