root/trunk/samples/matrix.md

Revision 292, 0.7 kB (checked in by JarrettBillingsley, 7 months ago)

Closes #65. Also fixed a bug in the regexp.email predefined regexp, and a small bug in dumpVal.

Line 
1 module matrix
2
3 local SIZE = 30
4
5 function mkmatrix(rows, cols)
6 {
7     local count = 1
8     local m = array.new(rows)
9
10     for(i: 0 .. rows)
11     {
12         m[i] = array.new(cols)
13
14         for(j: 0 .. cols)
15         {
16             ++count
17             m[i][j] = count
18         }
19     }
20
21     return m
22 }
23
24 function mmult(rows, cols, m1, m2, m3)
25 {
26     for(i: 0 .. rows)
27     {
28         for(j: 0 .. cols)
29         {
30             local val = 0
31
32             for(k: 0 .. cols)
33                 val += m1[i][k] * m2[k][j]
34
35             m3[i][j] = val
36         }
37     }
38
39     return m3
40 }
41
42 function main(N)
43 {
44     local n = 1
45
46     if(isString(N))
47         n = toInt(N)
48
49     local m1 = mkmatrix(SIZE, SIZE)
50     local m2 = mkmatrix(SIZE, SIZE)
51     local mm = mkmatrix(SIZE, SIZE)
52
53     for(i: 0 .. n)
54         mmult(SIZE, SIZE, m1, m2, mm)
55
56     writefln(mm[0][0], " ", mm[2][3], " ", mm[3][2], " ", mm[4][4])
57 }
Note: See TracBrowser for help on using the browser.