Not a fan of matrix access

I’m new to Odin, and am liking it quite a bit. Recently, however I spent over an hour debugging a piece of code in Odin.

The mistake was I had a matrix type and I called

mat[0][1], instead of mat[0,1].

It’s annoying to me that these both represent different things. I would prefer if mat[0][1] just errored out since it is inbuilt into the language. It would be nice to at least add this to the overview docs, I didn’t see it in the overview docs. I had to print every single value to figure this out.

Yes I made an account just to complain about this.

Hey,

welcome this forum!

The matrix access notation is well documented:

https://odin-lang.org/docs/overview/#matrix-type :wink:

elem := m[1, 2] // row 1, column 2

Cheers!

3 Likes

You can get your expected behavior only if you declare your matrix as #row_major, like so:

	A := #row_major matrix[2, 3]f32 {}
	assert(&A[0][1] == &A[0,1]) // true

	B := matrix[2,3]f32 {}
	assert(&B[0][1] == &B[1,0]) // true

In any language/library, one should always check whether matrices are stored in row-major or column-major form. Odin defaults to column-major storage as it’s more compatible with SIMD instructions.

5 Likes

I appreciate that snippet, but I have to ask anyway, is the access order really the only difference between these notations?

Yes. Also, it’s just less durable to use array-indexing.

For example, if you write a bunch of code that says M[j][k] = f that is now broken if you choose to convert M to #row_major.

2 Likes