1

I currently have a matrix class which is a std::vector<std::vector<double>>. I also have a vector class (not to get confused with stl's vector its more of math's vector of 1,2,3 .. dimension) which is a std::vector<double>.

After I made these two class I came into problem since now I wanted to do tmp_vector*tmp_matrix but since they are of different type I was not able to do it.

So my question is:

What would be a appropriate design choice?

  • Should I inherit both matrix and vector from same class (lets say for example matvec class which is just a empty class) or
  • should I inherit vector from matrix class (Here I am inheriting vector from a heavy matrix class)
4
  • 5
    Why not add overloads to each type to be multiplied by the other? I do not see why the types need a common base or why one needs to inherit from the other. Commented Oct 25, 2016 at 18:25
  • take a look at here Commented Oct 25, 2016 at 18:25
  • a vector is a 1xN or Nx1 matrix, so the most natural way to implement it would be to make a vector a special case of a matrix, possibly with simpler/more efficient implementations Commented Oct 25, 2016 at 18:27
  • @tobi303 so you mean inherit vector from matrix ? Commented Oct 25, 2016 at 18:29

2 Answers 2

3

Should I inherit both matrix and vector from same class?

I suppose that such design won't help you to get multiplication work.

Should I inherit vector from matrix class?

May be. But if I were you, I wouldn't do so if a single reason is an implementation of multiplication operation.

I advice you to simply overload operator* for both (vector, matrix) and (matrix, vector) combinations. I believe that it is the most natural way to solve the problem.

Sign up to request clarification or add additional context in comments.

Comments

2

In general implementing classes that represent (mathematical) vectors and matrices as derived classes from stl vector or vector of vectors isn't very efficient. What's mostly done in numerical libraries is have a Matrix class that doesn't derive from anything, but contains a pointer to a very simple and straight one-dimensional array of e.g. doubles, not using stl at all.

Behaviour as multi-dimensional matrix is achieved by suitably overloading the () operator, to do the proper index computations.

Vectors could very well be Matrices with either one column or one row, since you can have multiple overloads of () as long as the parameter lists are different. This would allow you to address elements in your Vector's with only one index.

You can overload +, - and * for these classes, and give them conversion constructors. Don't forget to add a destructor to deallocate the memory and a copy constructor to prevent two such matrices from sharing the same memory.

You also need to overload the assignment operator, for the same reason. It should deallocate the old contents of the lefthand side of your assignments and reallocate the right amount of memory.

11 Comments

Simple example of what Jacques is talking about in the first half of his answer: isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op
Look at the source code of ANY major numerical processing library if you want to see it... And it is not difficult at all, made things like that for as long as I can remember.
@pokche vector is great. It's awesome actually. Makes life so much easier. But... There is no such thing as a 2D vector. There is only vector containing vector which mean you actually have N+1 vectors scattered through RAM with nothing but a logical connection between them. This leads to low spatial locality and poor caching behaviour. The logic off the program is no slower, but the actual handling of the data by the CPU is hindered because it's ability to look ahead and preload data is diminished. Waiting for the CPU to get data from RAM can slow things down by orders of magnitude.
The Take-away: Use vector for the automatic memory management unless you have a really good reason not to. But use a 1D vector and do the 2D to 1D indexing math by hand. And since doing the math by hand means a lot of repetition, sooner or later you'll slip up and introduce a bug, so you want to wrap that 1D vector in a class and provide an access function to do the indexing math only in that one function.
1 vector is contiguous. But a vector is basically a set of pointers to a dynamically allocated block of memory. The outer vector contains a pointer to N contiguous vectors, but in each of those inner vectors is a pointer to their own dynamically allocated block. That ruins the contiguity. You can have a vector of fixed-size arrays to maintain contiguity, for example an Nx3 matrix, but if both dimensions are configurable, you're stuck.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.