The Squirrel interpreter. See http://www.squirrel-lang.org/
samples/matrix.nut@0:97a4f8cc534c, 2014-12-16 (annotated)
- Committer:
- jhnwkmn
- Date:
- Tue Dec 16 10:20:34 2014 +0000
- Revision:
- 0:97a4f8cc534c
Initial import of Squirrel.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhnwkmn | 0:97a4f8cc534c | 1 | /* |
jhnwkmn | 0:97a4f8cc534c | 2 | * |
jhnwkmn | 0:97a4f8cc534c | 3 | * Original Javascript version by David Hedbor(http://www.bagley.org/~doug/shootout/) |
jhnwkmn | 0:97a4f8cc534c | 4 | * |
jhnwkmn | 0:97a4f8cc534c | 5 | */ |
jhnwkmn | 0:97a4f8cc534c | 6 | local SIZE=30; |
jhnwkmn | 0:97a4f8cc534c | 7 | |
jhnwkmn | 0:97a4f8cc534c | 8 | function mkmatrix(rows, cols) { |
jhnwkmn | 0:97a4f8cc534c | 9 | local i, j, count = 1; |
jhnwkmn | 0:97a4f8cc534c | 10 | local m = []; m.resize(rows); |
jhnwkmn | 0:97a4f8cc534c | 11 | for (i = 0; i < rows; i+=1) { |
jhnwkmn | 0:97a4f8cc534c | 12 | m[i] = [];m[i].resize(cols) |
jhnwkmn | 0:97a4f8cc534c | 13 | for (j = 0; j < cols; j+=1) { |
jhnwkmn | 0:97a4f8cc534c | 14 | m[i][j] = count+=1; |
jhnwkmn | 0:97a4f8cc534c | 15 | } |
jhnwkmn | 0:97a4f8cc534c | 16 | } |
jhnwkmn | 0:97a4f8cc534c | 17 | return m; |
jhnwkmn | 0:97a4f8cc534c | 18 | } |
jhnwkmn | 0:97a4f8cc534c | 19 | |
jhnwkmn | 0:97a4f8cc534c | 20 | function mmult(rows, cols, m1, m2, m3) { |
jhnwkmn | 0:97a4f8cc534c | 21 | local i, j, k, val; |
jhnwkmn | 0:97a4f8cc534c | 22 | for (i = 0; i < rows; i+=1) { |
jhnwkmn | 0:97a4f8cc534c | 23 | for (j = 0; j < cols; j+=1) { |
jhnwkmn | 0:97a4f8cc534c | 24 | val = 0; |
jhnwkmn | 0:97a4f8cc534c | 25 | for (k = 0; k < cols; k+=1) { |
jhnwkmn | 0:97a4f8cc534c | 26 | val += m1[i][k] * m2[k][j]; |
jhnwkmn | 0:97a4f8cc534c | 27 | } |
jhnwkmn | 0:97a4f8cc534c | 28 | m3[i][j] = val; |
jhnwkmn | 0:97a4f8cc534c | 29 | } |
jhnwkmn | 0:97a4f8cc534c | 30 | } |
jhnwkmn | 0:97a4f8cc534c | 31 | return m3; |
jhnwkmn | 0:97a4f8cc534c | 32 | } |
jhnwkmn | 0:97a4f8cc534c | 33 | |
jhnwkmn | 0:97a4f8cc534c | 34 | local n = vargv.len()!=0?vargv[0].tointeger():1 |
jhnwkmn | 0:97a4f8cc534c | 35 | |
jhnwkmn | 0:97a4f8cc534c | 36 | local m1 = mkmatrix(SIZE, SIZE); |
jhnwkmn | 0:97a4f8cc534c | 37 | local m2 = mkmatrix(SIZE, SIZE); |
jhnwkmn | 0:97a4f8cc534c | 38 | local mm = mkmatrix(SIZE, SIZE); |
jhnwkmn | 0:97a4f8cc534c | 39 | |
jhnwkmn | 0:97a4f8cc534c | 40 | for (local i = 0; i < n; i+=1) { |
jhnwkmn | 0:97a4f8cc534c | 41 | mmult(SIZE, SIZE, m1, m2, mm); |
jhnwkmn | 0:97a4f8cc534c | 42 | } |
jhnwkmn | 0:97a4f8cc534c | 43 | |
jhnwkmn | 0:97a4f8cc534c | 44 | print(mm[0][0]+" "+mm[2][3]+" "+mm[3][2]+" "+mm[4][4]); |