PDA

View Full Version : Comparing Matrices

 Eric-Harvey01-31-2008, 06:50 PMthe following code returns true: matrix1 = (matrix3 [1,0,0] [0,0,1] [0,-1,0] [1,1,1]) matrix2 = matrix1 matrix1 == matrix2 Yet this code returns false: matrix1 = (matrix3 [1,0,0] [0,0,1] [0,-1,0] [1,1,1]) matrix2 = (matrix3 [1,0,0] [0,0,1] [0,-1,0] [1,1,1]) matrix1 == matrix2 I have found a way around this already, so I dont need a solution, but I would like to know why this evaulates the way it does. If anyone has any ideas please let me know. Thanks
PEN
01-31-2008, 08:04 PM
I'm guessing it is one of Max's rounding issues, Bobo?

Bobo
01-31-2008, 09:17 PM
the following code returns true:

matrix1 = (matrix3 [1,0,0] [0,0,1] [0,-1,0] [1,1,1])
matrix2 = matrix1
matrix1 == matrix2

Yet this code returns false:

matrix1 = (matrix3 [1,0,0] [0,0,1] [0,-1,0] [1,1,1])
matrix2 = (matrix3 [1,0,0] [0,0,1] [0,-1,0] [1,1,1])
matrix1 == matrix2

I have found a way around this already, so I dont need a solution, but I would like to know why this evaulates the way it does.

If anyone has any ideas please let me know.

Thanks

Matrices, like Arrays, are compound values where the comparison only looks to see if the two variables point at the same memory (in other words, if they are instances). As with arrays, you cannot compare the CONTENT of the value as it does not do a component by component comparison, only a top-level test.

So
a = matrix3 1
-->(matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
b = a
-->(matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
a == b
-->true
a.row1 = [0.5,1,1]
-->[0.5,1,1]
b
-->(matrix3 [0.5,1,1] [0,1,0] [0,0,1] [0,0,0])

As you can see changing A changed B because B is a variable whose pointer points at the same memory as A. That's why the comparison gives true.

But once you make B an independent value with its own memory, they become different:

b = copy a
-->(matrix3 [0.5,1,1] [0,1,0] [0,0,1] [0,0,0])
a == b
-->false

Possible workarounds:

a = matrix3 1
-->(matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
b = matrix3 1
-->(matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])
a as string == b as string
-->true --converting to string is what I usually employ
a[1] == b[1] and a[2] == b[2] and a[3] == b[3] and a[4] == b[4]
-->true --comparing explicitly each component works too
(for i = 1 to 4 where a[i] != b[i] collect i).count == 0
-->true --this is a fancy way of comparing the components

Eric-Harvey
01-31-2008, 09:23 PM
Thanks for the explanation Bobo.

I hadnt been thinking of them like arrays, I had just assumed they were like values.

Also your "as string" solution is much better than the fix I had for it.

Thanks,

CGTalk Moderation
01-31-2008, 09:23 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.