# Thread: 2 mathematical / class object questions

1. ## 2 mathematical / class object questions

1. I represent a matrix using a float for each element. I don't use arrays, as they are not very efficent for a matrix class. For example, row 2 and column 3 would be represented as m23. Is this smart, or what are some other ways to represent matricies besides arrays?

2. Due to the nature of floating point variables, the length of a unit vector may not necessarily be exactly one. This means the length check
length() == 1.0f
may be false. Would this:
(length > .9999f) && (length < 1.1f)
return true, such as if length was 1.0000034? Or how else should I check for a normalized vector?

Thanks !

2. No.
Round.

gg

3. Would you recommend down or up ?

Also, what DON'T you like about my method of representing a matrix, and how do you?

4. 2-D array or vector of vectors.

5. Yes, explain #1.

gg

6. I don't really like the idea of a array for a matrix. It does have some pros, yes, but it also has some cons. A vector of vectors doesn't sound too bad, I might look at it.

Don't worry about number one, it was j/k .

7. Yes, if you are dealing with sparse matracies then multi-dimensional arrays aren't the way to go.

gg

8. One dimensional linear array:

m00 m01 m02 m03
m10 m11 m12 m13
m20 m21 m22 m23
m30 m31 m32 m33

What's wrong with this implementation?

or this one?:

m00 m01 m02 m03
m04 m05 m06 m07
m08 m09 m10 m11
m12 m13 m14 m15

I see no need for an overly complex data structure to represent a 2D array. All you will do is confuse yourself. Granted it will be easier to do inserts and sorting if you do, but who sorts 2D arrays, and if so, by what criteria?

Vector of vectors is ok, but it seems overly complex for the task at hand IMHO.

9. A great strength of using a vector of vectors in this situation is that you will be able to access the matrix using the intuitive subscript operator like so:
Code:
`myMatrix[1][3] = 3;`
Ya see, the first subscript operator will return a vector, and the second one will return a single element from that vector. The apmatrix class is implemented using a vector of vectors. If it'll work for you, you could just use the apmatrix class or write something similar for yourself.

http://www.cs.duke.edu/~ola/ap/exams...nit/apmatrix.h
http://ditto.stxavier-cincy.org/~mho...s/apmatrix.cpp

10. But given that a matrix is supposed to be a fixed size, what's the point of using a dynamic array like vector for it?

11. But given that a matrix is supposed to be a fixed size, what's the point of using a dynamic array like vector for it?
My point exactly, there is no reason. Keep it simple stupid is my motto.

12. Originally posted by CornedBee
But given that a matrix is supposed to be a fixed size, what's the point of using a dynamic array like vector for it?
Although a vector can vary in size, you can just as easily use it as a fixed size container. You can also benefit from a vector's range checking for element access if you wish to. Exception handling is minimal, but what there is comes for free.

13. > or what are some other ways to represent matricies besides arrays?
I'd go with arrays as being the most natural way of representing a matrix.
You seem to have a bad case of APOS

Get the damn thing working first - you can't optimise an incomplete program.
Then use a profiler to work out the actual hot-spots (not where you think they'll be). There's no point saving 10% in your matrix code if it spends 90% of the time somewhere else (the benefit to you is just 1% net).
Then plan, measure and test any optimisations you do make to ensure the mess you make of the code is worth the performance gain you get.

14. OK, thanks all .

15. For the most part I agree with the idea of avoiding premature optimization, but when you are designing your code to start with, it makes sense to consider what the best option would be theoretically. I mean, you wouldn't use two linked lists just because that is what you know, when five minutes of effort will show you that vectors or arrays would be more efficient. So I don't see the harm in putting a little thought into the efficiency of the algorithms and data structures you use.

If you've already coded it, then fine, leave it alone and optimize at the end, especially if you've bundled the behavior into a class that can be modified separately. But if you are just starting the design and you haven't implemented the Matrix class, I'd say just go with a vector of vectors, which is the easiest way to do it (in my opinion). Then when you've got it working you can see if that was the best choice.