Page 1 of 1

### n-dimensional Matrix class

Posted: Mon Aug 15, 2011 1:54 am
I've created one in CobraWorkspace/Supplements/

You can browse it online at cobra/trunk/Supplements/Matrix.cobra.

Here is some of the test code for it:
`m = Matrix<of int>(3, 7)assert m.count == 21for x in 3, for y in 7, m[x, y] = x*yfor x in 3, for y in 7, assert m[x, y] == x*yexpect IndexOutOfRangeException, print m[3, 7]expect IndexOutOfRangeException, print m[3, 0]expect IndexOutOfRangeException, print m[0, 7]expect IndexOutOfRangeException, print m[4, 8]d1, d2, d3 = 10, 5, 3m = Matrix<of int>(d1, d2, d3)assert m.count == d1 * d2 * d3for x in d1, for y in d2, for z in d3, m[x, y, z] = x*y*zfor x in d1, for y in d2, for z in d3, assert m[x, y, z] == x*y*zdims = [d1, d2, d3]m2 = Matrix<of int>(dims)for x in d1, for y in d2, for z in d3, m2[x, y, z] = x*y*zfor x in d1, for y in d2, for z in d3, assert m2[x, y, z] == x*y*zassert m2 == mm2[0, 0, 0] += 1assert m2 <> m`

You can also pass in a list of dimensions where the list size and its values are not determined until run-time.

You can subclass Matrix<of T> to add more methods relevant to your purposes, or even hack up the existing version as you see fit.