Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3953722
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:00:34+00:00 2026-05-20T02:00:34+00:00

For a math package, I am trying to have classes for different types of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:00:35+00:00Added an answer on May 20, 2026 at 2:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small math problem I am trying to solve Given a number
I am trying to use GLSMultipleLinearRegression (from apache commons-math package) for multiple linear regression.
My math classes are far behind, and I'm currently struggling to find a decent
Using assorted matrix math, I've solved a system of equations resulting in coefficients for
I'm trying to create an android program. The program works like this, a user
How can I access math coprocessor from C# code? I would like to make
Math skills are becoming more and more essential, and I wonder where is a
I suck at math. I need to figure out how to calculate a video
I need to calculate Math.exp() from java very frequently, is it possible to get
I'm not a math guy in the least but I'm interested in learning about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.