Thread: 2 mathematical / class object questions

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    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 !
    Last edited by Lurker; 12-22-2003 at 10:57 PM.
    Do not make direct eye contact with me.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    No.
    Round.

    gg

  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Would you recommend down or up ?

    Also, what DON'T you like about my method of representing a matrix, and how do you?
    Last edited by Lurker; 12-22-2003 at 11:09 PM.
    Do not make direct eye contact with me.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    2-D array or vector of vectors.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yes, explain #1.

    gg

  6. #6
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    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 .
    Do not make direct eye contact with me.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yes, if you are dealing with sparse matracies then multi-dimensional arrays aren't the way to go.

    gg

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    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. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    OK, thanks all .
    Do not make direct eye contact with me.

  15. #15
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. pointer to struct object in class object
    By Stevo in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2004, 07:58 PM
  4. Base class initialization
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2004, 04:52 AM
  5. Replies: 4
    Last Post: 03-20-2002, 02:31 PM