Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++: Building a Matrix Class

  1. Aug 7, 2012 #1
    Hello all!

    I know they are out there, but I think that I can learn a lot by creating my own. I do not want to get too convoluted with optimizing this thing, but I was hoping some folks could offer some input.

    I am narrowing the scope of this project so that the goal is to end up with a Matrix class that could be used for Gaussian elimination and matrix addition and multiplication for now.

    My outline is as follows:

    Code (Text):

    1. Class name: Matrix

    2. Uses <vector> as a building block.

    3. Constructors should consider:
      a. Data Type
      b. Size of Matrix to be created

    4. Member functions:
      a. row swapping
      b. row multiply by non zero, real, constant
      c. add multiple of one row to another row
      d. get/set Element
      e. augment (to append column of constants for solving systems)
      f. gauss solve

    5. Properties:
      a. is singular
      b. is zero
      c. is square
    I am sure there is much to consider, but I am just looking for a starting point.

    Do you see any problems with using vector as my building block? Any other elementary member functions/properties you would suggest?

    Thanks! :smile:
  2. jcsd
  3. Aug 7, 2012 #2


    User Avatar
    Science Advisor

    Hey Saladsamurai.

    The real question I have is more so how you want to use this and the context of this with regard to performance requirements.

    One reason of using normal arrays is that the compiler has a better chance of optimizing the code than if you used other classes or techniques that otherwise prohibited this.

    Some compilers are able to look at code with loops for example on arrays and convert this directly to SIMD instructions (i.e. single instructions on vector data).

    The other thing has to do with whether you want the matrix to have a constant size or not: some applications only require a few different kinds of matrices and others require the size to be determined at run-time.

    So the question ultimately before we continue boils down to this: what do you want to use it for?
  4. Aug 11, 2012 #3
    Hi chiro.

    At the moment, I am not too concerned about optimization, I just want to start coding something useful (as opposed to the boring examples found in texts). If we a need a solid starting point, I would have to say that solving systems of linear equations using Guass elimination would be a great start. It would seem that in that case, the matrices would remain fixed in size, though I am not sure that we would know the size at compile time (I think that matters).

    For example, we might use the solver to solve a finite-difference problem for n nodes. But if we want to refine the mesh size, the number of nodes will change.

    But again, that might be getting ahead. I just want to get started simple. Then either build up or start over fresh for more advanced applications. I don't mind writing a bunch of code and then discovering that this particular route will only get me so far before I have to start over with a different approach. It's all part of my learning process.

    That being said, what do you think of my initial layout? Any other critical functions or properties that make sense to have in a Matrix class in the context of solving systems of linear equations that I am missing?

    Thanks again.
  5. Aug 11, 2012 #4


    User Avatar
    Science Advisor

    Well the first question has to be: do you only want square matrices or non-square matrices?

    Since you are doing something like Gaussian elimination (and most likely solutions of a linear system), then you will need to decide whether you have a general matrix class where you can have any dimension, or a specific class for n*n.

    It's not hard to change if you make the internal structure a 2D array of doubles, and if you can use a high precision format like a 128-bit double, then use this.

    The thing though you will have to do, is you will have to implement checks like for example making sure the condition number is big or that the determinant is big enough (when you try and solve a system in MATLAB, take note of these kind of errors when the matrices are ill-conditioned).

    If you have very specific needs (i.e. you are dealing with lots of large integers or lots of really small fractional numbers), then you may have to use your own number format: this is what they do in so called fixed-point numbers. In some applications they use a number which allocates n bits for the integer and n bits for the remainder and not something like the IEEE format that is used (which is based on an exponent and a mantissa).

    If you have specific needs like the above, you will need to use a 2D array of these special structures rather than of simple IEEE doubles (even the higher ones).

    So the above is critical for having a design for the actual number and its important to consider in the context of accuracy for the final calculations and it will affect every single routine in your Matrix class.

    Also it's important to have general routines that detect when you get the potential for numerical screw-ups: the condition number is one standard way of doing this for solving linear systems, but you should consider each operation you do and how this compound the error for an entire series of steps.

    You could if you want to get serious, give routines error profiles to the class to make sure that the computation is even allowed to fit inside the bounds of some error interval which is a nice thing for engineering applications.

    Also with regard to the data structure, you could use different structures (i.e. fixed-point, IEEE double, other formats) which you pass at construction time or use some kind of template structure where you supply a data-type and the template class just uses these to do the calculations (i.e. you can overload things like +,-,* etc for a custom data type).
  6. Aug 12, 2012 #5
    Good info chiro. Thanks for your insight. After reading your response, I started to realize just how much I need to learn about linear algebra (LA) still. It was a little discouraging, but I think that's OK. I am going to just start programming a class that solves square matrices and that cares little for the 'trustworthiness' of the solution. I know it does not seem like a very useful class, but I need to start somewhere. Then I can teach myself the necessary LA to refine and improve the class. I know that if I wait until I have all of the requisite LA under my belt before I start, I will never start.

    I will likely start with the above template, plus a condition number function and I will use array[] instead of vector for now. I will consider precision and error as we go. I will continue to post to this thread if you care to check in every now and again.

    Thanks again.
  7. Aug 12, 2012 #6


    User Avatar
    Science Advisor

    No worries, good luck with your project and if you ask a question, then I'll do my best to answer it.

    Your strategy is good: start simple and add features and functionality bit by bit. I wouldn't worry too much though about the LA issues: there is still a decent amount of stuff you can implement, but one thing that I recommend you do is think about handling the same arithmetic operations in different data types.

    In C++, you can overload all the usual operators, so this means if you want to use say a fixed point number instead of an IEEE double, you don't have to change your code too much, but again it's something to aware of.
  8. Aug 27, 2012 #7
    Bjarne Stroutstrup has a pretty easy to use matrix class that you can download from his website. The code is fully open so you can explore it to understand how it works.


  9. Sep 3, 2012 #8
    Excellent! I will have to look into this. Thanks for the link.
  10. Sep 4, 2012 #9
    Holy crap! That matrix class is far beyond my programming abilities! I have a lot of work to do!!!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook