• Forum
  • Lounge
  • One of my worst math library design deci

 
One of my worst math library design decisions:

Before you see my rant, what are the worst design decisions you ever made?
***here is one of mine: rant ahead***

I don't think any of my math colleagues would understand my pain, so here it is. As studied in high school, a polynomial is a letter expression using the *, +, - operations, something like:

x^2+y^2+2x*y


After 4 years of programming, I realized that one of the worst design decisions I made was to include a number of variables member of class (multivariate) Polynomial:

1
2
3
4
5
6
template <class CoefficientType> 
class Polynomial : MonomialCollection<MonomialP, CoefficientType>
{
/*...*/
int NumVars;
};  


Besides array-out-of-bound error, non-coinciding number of variables was by far the most frequent cause of crash of my program. At the same time, the number of variables can be deduced: just take the maximum of the variable number of each monomial of a polynomial.

Today, I had the last drop: in the middle of doing actual work, I had a mysterious bug that would not manifest itself on any of my test cases. The bug was due to a polynomial containing a monomial with a number of variables not equal to NumVars. I've had enough. I will not do any work or touch my code before I remove the damned variable NumVars from my code!

I am in the process of doing this and will update this post with the number of lines I have to touch. Let us see how much will this be...

Cheers,
tition
Last edited on
closed account (3hM2Nwbp)
I don't think cplusplus.com has enough space for all of my bad designs. It was actually just in the past year that I seem to have cut back on my dumb ideas.

I'm fortunate enough that my bad designs don't come back to haunt me though. That's a big advantage of not programming for a living.
I cannot decide whether I am programming for a living or not. My c++ math project basically contains all of my work. Further I do all my teaching-related computations exclusively in my project. I have a self-imposed ban on using any other system; the ban partially extends to hand-held calculator use as well.

For example, last semester I had to teach students how to draw in polar coordinates. Instead of using a ready-made software, I cooked up a solution from within my system.

So in a way, my living is tied to my program.
Last edited on
the number of variables can be deduced: just take the maximum of the variable number of each monomial of a polynomial.
Uhh, but what if no monomial references all the variables? Like x+y, as opposed to x+y+x*y.
My polynomial implementation is actually quite more complicated than that.

A polynomial in my implementation is a monomial collection with additional multiplication law. So are sparse matrix, an element of a weyl algebra, and an element of a universal enveloping algebra.

Monomial collections that do not have a natural multiplication structure are (sparse) vector spaces, multisets (sets with multiplicities), etc.

Sparse matrices and polynomials share the same code for multiplication (matrices are non-commutative, but I always take extra special care not to mess the order of multiplication: always left first, right second). This is easy to do with templates: the product of two monomials is a monomial, so I call the tempate monomial classes' multiplication method.

Now, each monomial of a polynomial is just a std::vector with the exponents of each variable. Two monomials of a polynomial are multiplied by adding the exponents. If one of the vectors has less entries than the other, I stretch the shorter one. This is how I discern between different variables. However, this is not how my calculator front-end reads different variables (this I do via contexts and merging contexts and is quite more involved).

As for sparse matrices, each monomial is just two indices (row and a column). Sparse matrices, just like polynomials, have no natural notion of "dimension" - you can always add extra rows& columns.


... After a day and a half of work, here comes the size of the change:

1339 lines of code changed in 15 files

That is quite a LOC churn, definitely one of my largest ones.
Last edited on
Topic archived. No new replies allowed.