Created
March 5, 2011 04:32
-
-
Save richcole/856116 to your computer and use it in GitHub Desktop.
Example of matrix multiplication in RJL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { sys | | |
| Matrix: (| | |
| Array: sys Array; | |
| // constructor | |
| num_rows:num_columns:: { num_rows num_columns | | |
| self @num_rows: num_rows; | |
| self @num_columns: num_columns; | |
| self @data: (Array clone size: ((self num_rows) * (self num_columns))); | |
| return self; | |
| }; | |
| at_row:at_column:set:: { row column value | | |
| self data at: (((row - 1) * (self num_columns)) + column - 1) set: value | |
| return self; | |
| }; | |
| row_vector:: { i | | |
| return (self RowVector clone matrix: self row: i); | |
| }; | |
| rows: { | |
| return (1 .. num_rows) map: { row_index | | |
| return self row_vector: row_index; | |
| }; | |
| }; | |
| RowVector: (| | |
| // constructor | |
| matrix:row:: { matrix row | | |
| self @matrix: matrix; | |
| self @row: row; | |
| return self; | |
| }; | |
| row_index: { return self row; }; | |
| column_indexes: { return 1 .. (self length) }; | |
| length: { return self matrix num_columns; }; | |
| inner_product: { other | | |
| result: 0; | |
| column_indexes each: { index | | |
| result: result + (self at: index) + (other at: index); | |
| }; | |
| return result; | |
| }; | |
| at:: { column | | |
| return matrix at_row: (self index) at_column: column; | |
| } | |
| |); | |
| multiply:: { other | | |
| result: (Matrix clone num_rows: (self num_rows) num_columns: (other num_columns)); | |
| self rows each: { row | | |
| other columns each: { column | | |
| result at_row: (row index) at_column: (column index) set: | |
| (row inner_product: column); | |
| }; | |
| }; | |
| return result; | |
| }; | |
| ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment