Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP design question on inheritance and operator overloading

For a math package, I am trying to have classes for different types of matrices, like typical rectangular matrix, triangular matrix, diagonal matrix etc. The reason is naturally to save on efficient storage and efficient algorithm implementation for special matrices. But I would still like to have the flexibility of overloaded operators where C = A + B would take A and B as any type of matrix and return corresponding result (the result can be downgraded to typical rectangular matrix if one of the operands is rectangular).

I have thought of 2 possible ideas, both of which are messy:

(1) An IMatrix interface, which would list all the methods that need to be implemented for each type of matrix, e.g., transpose, inverse etc. the efficient implementation of which which is different for each type of matrix. Two problems here: (a) Operator overloads are static methods, so can't be listed in the interface, or even a base class implementing the interface. Operator overloads would have to be written in each class separately, and I can't possibly achieve C=A+B type operation (as I mentioned above), without messy type checking and casting in the client code, that I really want to avoid. (b) I cannot have both operands as interface when I define operator overloads: i.e. I cannot do the following in, say, DiagonalMatrix class:

public override IMatrix operator +(IMAtrix lhsMatrix, IMatrix rhsMatrix)
{ ... }

(2) Can have one Matrix class with a matrix type variable stored in the class (may be an Enum). Depending on the type, we can implement the data structure and algorithms. The operator overloading would then work seamlessly. One problems here: (a) The class would be huge with possible switch-case syntax for checking matrix type before launching specific algorithm. For each binary operators, I would have to have n^2 cases, n being the number of matrix type I want to implement. Might also be a maintenance nightmare.

Looks like, without the operator overloading detail, I could have used Factory pattern or Visitor pattern, but not so with op overloads. What would be the best way to go for this problem?

Resources I have found so far:

  1. One related thread here.
  2. Explanation of a similar problem faced by the dev of another OS C# Numerics package.

Edits:

4/25/2011: Added more resources I have found about this problem so far.

like image 824
Samik R Avatar asked Feb 16 '11 17:02

Samik R


1 Answers

If this were my project, I would go with a variant of #1: define an abstract Matrix class, which is inherited by more specific types like TriangularMatrix. This will allow you to create the operators (even if said operators just throw a NotImplementedException) that you can then override in derived classes. It will also allow you to deal with any matrix as a Matrix, with that common set of functionality.

The only thing you'll lose is the compiler checking that you have actually overridden methods and operators; since the operators are static, they cannot be made abstract. You can work around this if you wish by making the operators in the base class simply call an equivalent named method (for instance, + would call an Add method) that CAN be abstracted in the base class, thus forcing child classes to implement it.

Math question: can a triangular matrix be added to a rectangular one, or do both addends have to match in type and/or dimensions? If it's the former, consider implementing the operator in the base Matrix class, and have that operator implement a strategy pattern, calling internal classes that can perform the actual manipulations for each combination of types. If it's the latter, simply override the base class implementation of the valid operators for that type of matrix.

like image 79
KeithS Avatar answered Sep 28 '22 00:09

KeithS