Forums

n-dimensional Matrix class

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

n-dimensional Matrix class

Postby Charles » 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 == 21
for x in 3, for y in 7, m[x, y] = x*y
for x in 3, for y in 7, assert m[x, y] == x*y
expect 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, 3
m = Matrix<of int>(d1, d2, d3)
assert m.count == d1 * d2 * d3
for x in d1, for y in d2, for z in d3, m[x, y, z] = x*y*z
for x in d1, for y in d2, for z in d3, assert m[x, y, z] == x*y*z

dims = [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*z
for x in d1, for y in d2, for z in d3, assert m2[x, y, z] == x*y*z
assert m2 == m
m2[0, 0, 0] += 1
assert 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.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Return to Discussion

Who is online

Users browsing this forum: No registered users and 111 guests