Determinant of a matrix I'm visiting some code that I wrote for one of my finals projects and wanted to know whether there were a more optimal, more elegant way to do this so it does not look so "hard-coded".

The problem was that I needed to calculate the Determinant of a matrix, and, at the time (some of it was rushed) I did not know of a better way, using loops or re-usable code that could make the code a little better. Here is the code:

namespace Determinant {


    template&lt;int X&gt;
    float determinant(std::vector&lt;Vector&gt; &amp;data)
    {   

        float deter = 0.0;
        if(X == 2)
        {
            float a = data[0][0]; 
            float b = data[1][0]; 
            float c = data[1][1]; 

            deter = (a + c) * (a + c) -4 * (a*c-b*b);
        }else if(X == 3)
        {   

            float determinant1 = (data[1][1] * data[2][2]) - (data[2][1] * data[1][2]); 
            float determinant2 = (data[1][0] * data[2][2]) - (data[2][0] * data[1][2]); 
            float determinant3 = (data[1][0] * data[2][1]) - (data[2][0] * data[1][1]);

            deter = (data[0][0] * determinant1) - (data[0][1] * determinant2) + (data[0][2] * determinant3);
        }
        return deter; 
    }
}


As you can see the Determinant is very hard-coded which is probably not the right way -- But are there any other alternatives that isn't hard-coded? I want to start to use a design pattern, as I feel one would be useful here but can't seem to figure out which one.
You don't really need a design pattern for determinants, just a better algorithm. Generally one of the easiest (and fastest) ways of calculating a matrix determinant is by using what is known as LU-Decomposition. This factors a matrix into two matrices, a lower triangular and an upper triangular matrix. From these, the determinant can simply be calculated as the product of diagonal elements.

Note that you have to be careful when calculating determinants of large matrices; for a 100x100 matrix, it can easily overflow the maximum size of a float (or double). For this reason it's often better to calculate a log-determinant.

On to the actual code you've presented:

data should be passed by const&amp; since it isn't (and shouldn't be) modified:

float determinant(const std::vector&lt;Vector&gt;&amp; data)


Using a template int parameter to choose between determinant sizes is really odd, and is potentially easily misused. What if I use Determinant::determinant&lt;2&gt;(...) on a 3x3 matrix? It'll give me the wrong answer. You should generally try to make your code easy to use and hard to misuse. In this case, that means calculating the determinant size based on the row/column size of the passed parameter. Better yet would be creating a matrix class to encapsulate all of this information.

These days, it generally doesn't make a lot of sense to use float over double unless you really need the speed (and even then, it is often no faster, and can sometimes even be slower on modern hardware). Stick to using double by default.