What are the key considerations for building a Matrix class in C++?

In summary: That being said, I think the important thing is not so much the details of how you store the data, but that you are using a matrix to solve a linear system.In summary, the author is considering using a vector as the building block for a Matrix class that will be used for solving systems of linear equations. He is also looking for advice on what elementary member functions/properties should be included in the class. He has outlined a plan for creating the class and has listed some properties and functions that should be included. He is currently working on the implementation of the class.
  • #1
Saladsamurai
3,020
7
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:
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:
 
Technology news on Phys.org
  • #2
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?
 
  • #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.
 
  • #4
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).
 
  • #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.
 
  • #6
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.
 
  • #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.

http://www.stroustrup.com/Programming/

Thanks
 
  • #8
CFDFEAGURU said:
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.

http://www.stroustrup.com/Programming/

Thanks

Excellent! I will have to look into this. Thanks for the link.
 
  • #9
CFDFEAGURU said:
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.

http://www.stroustrup.com/Programming/

Thanks

Holy crap! That matrix class is far beyond my programming abilities! I have a lot of work to do!
 

What is a matrix class in C++?

A matrix class in C++ is a user-defined data type that represents a mathematical matrix, which is a rectangular array of numbers. It allows for efficient storage and manipulation of matrix data using various operations, such as addition, subtraction, multiplication, and transposition.

Why do we need a matrix class in C++?

A matrix class in C++ is useful for performing complex mathematical computations, particularly in fields such as machine learning, computer graphics, and scientific computing. It provides a convenient and organized way to store and manipulate large amounts of data in the form of matrices.

How do you create a matrix class in C++?

To create a matrix class in C++, you need to define a class with appropriate attributes and member functions that represent the properties and operations of a matrix. This includes defining the size of the matrix, its elements, and various methods for performing operations on the matrix.

What are the advantages of using a matrix class in C++?

Using a matrix class in C++ allows for efficient storage and manipulation of matrix data, as well as providing a clear and organized structure for performing mathematical operations. It also allows for easy integration with other C++ programs and libraries.

Are there any limitations to using a matrix class in C++?

One limitation of using a matrix class in C++ is that it can be memory-intensive for large matrices. Additionally, the implementation of certain operations, such as matrix inversion or decomposition, may require more complex algorithms and may not be as efficient as specialized libraries.

Similar threads

  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
6
Views
885
  • Programming and Computer Science
Replies
3
Views
255
  • Precalculus Mathematics Homework Help
Replies
25
Views
968
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
1
Views
751
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top