matrix calculator

i hope i'm in the right place to ask this problem.
i want to find inverse, upper triangular and determinant for a matrix. but i'm not sure why it doesn't work like what i want. for now, i just want try to find determinant.
here's what i've done, if someone can point out my mistake, it'll be great help.


this is in cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "A2.h"
BEGIN_MESSAGE_MAP(CA5,CFrameWnd)
    ON_BN_CLICKED (IDC_BUTTON, OnInverse)
    ON_BN_CLICKED (IDC_BUTTON,OnUpperTri)
    ON_BN_CLICKED (IDC_BUTTON, OnDeterminant)
END_MESSAGE_MAP()
CA2::CA2()
{ 
    int i, j;
    CString bTitle[4]={L"",L"Inverse",L"UpperTriangular",L"Determinant"};
    Create(NULL,L"A2: A Matrix Calculator",
        WS_OVERLAPPEDWINDOW,CRect(0,0,1150,700));
    //input for matrix A in edit boxes
    for (i=1; i<=N; i++)
        for (j=1; j<=N; j++)
        {
            ea[i][j].Create(WS_CHILD | WS_VISIBLE | WS_BORDER, 
                CRect(CPoint(30+100*(j-1),80+40*(i-1)),CSize(70,25)),this,IDC_a);
        }
    //button for a1,a2&a3
    for (i=1; i<=3; i++)
        bZ[i].Create(bTitle[i],WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 
        CRect(CPoint(150,350+40*(i-1)),CSize(180,25)),this,IDC_BUTTON);
    //output for inverse, uppertriangular, determinant of matrix A
    for (i=1; i<=N; i++)
        for (j=1; j<=N; j++)
        {
            sa1[i][j].Create(L"",WS_CHILD | WS_VISIBLE | WS_BORDER, 
                CRect(CPoint(600+100*(j-1),80+40*(i-1)),CSize(70,25)),this,IDC_a1);
        }
    for (i=1; i<=N; i++)
        for (j=1; j<=N; j++)
        {
            sa2[i][j].Create(L"",WS_CHILD | WS_VISIBLE | WS_BORDER, 
                CRect(CPoint(600+100*(j-1),350+40*(i-1)),CSize(70,25)),this,IDC_a2);
        }
        sa3.Create(L" ",WS_CHILD | WS_VISIBLE | WS_BORDER | SS_CENTER, 
        CRect(CPoint(750,600),CSize(140,25)),this,IDC_a3);
    ea[1][1].SetFocus();
}
void CA2::OnInverse()
{
}
void CA2::OnUpperTri()
{
}
void CA2::OnDeterminant()
{
    int i,j,k;
    CString s;
    for (i=1; i<=N; i++)
        for (j=1; j<=N; j++)
        {
            ea[i][j].GetWindowText(s); a[i][j]=_tstof(s);
        }
    //ea[6][6].GetWindowText(s); a[6][6]=_tstof(s);
    //row operations
    //a3=product
    double p;
    for (k=1; k<=N-1; k++)
        for (i=k+1; i<=N; i++)
        {
            p=a[i][k]/a[k][k];
            for (j=1; j<=2; j++)
                a[i][j]-=p*a[k][j];
        }
        a3=1;
        for (i=1; i<=N; i++)
            a3 *=a[i][i];
        s.Format(L"=%lf", a3); sa3.SetWindowText(s);
}
Last edited on
Do you want to calculate determinants for matrices of an arbitrary size or of a specific size lyn18?
I see #define N 2 which looks like you're going for 2x2 matrices but it's not clear to me if that's your intent or not.
Last edited on
N 2 is just for example that i did before. i want to find 5X5 matrices.
To compute a determinant I'd use a recursive function. That is because, if you want to use determinant expansion by minors ( http://mathworld.wolfram.com/DeterminantExpansionbyMinors.html ) for a 5x5 matrix, you would essentially have to compute 5 4x4 matrix determinants (each of these being converted through minors to 4 3x3 matrix determinant calculations, etc.) .

(There are other ways to compute determinants, I'm just personally acquainted with the "minors" method as it's useful for small matrices). If you know you will be calculating only 5x5 determinants, you could even use an explicit formula with no recursion (although it would no doubt be a very large formula for 5x5).


For a 5x5 matrix M:

| a11 a12 a13 a14 a15 |
| a21 a22 a23 a24 a25 |
| a31 a32 a33 a34 a35 |
| a41 a42 a43 a44 a45 |
| a51 a52 a53 a54 a55 |


detM = a11*(detM11)-a12*(detM12)+a13*(detM13)-a14*(detM14)+a15*(detM15)

where M11 is a 4x4 matrix derived from M:
| a22 a23 a24 a25 |
| a32 a33 a34 a35 |
| a42 a43 a44 a45 |
| a52 a53 a54 a55 |

detM11 = a22*(a33*(a44*a55-a45*a54)-a34*(a43*a55-a45*a53)+a35*(a43*a54-a44*a53))-a23*(a32*(a44*a55-a45*a54)-a34*(a42*a55-a45*a52)+a35*(a42*a54-a44*a52))+a24*(a32*(a43*a55-a45*a53)-a33*(a42*a55-a45*a52)+a35*(a42*a53-a43*a52))-a25*(a32*(a43*a54-a44*a53)-a33*(a42*a54-a44*a52)+a34*(a42*a53-a43*a52))


M12 is a 4x4 matrix derived from M:

| a21 a23 a24 a25 |
| a31 a33 a34 a35 |
| a41 a43 a44 a45 |
| a51 a53 a54 a55 |

detM12 = . . .

. . . etc. . . .

detM13 = . . .

detM14 = . . .

detM15 = . . .

etc.

which, once calculated, allow detM to be computed (through the formula given above for detM).
Last edited on
A recursive function for calculating determinants for square matrices of an arbitrary size could be something like the following (I haven't tested the code, I'm writing it here as an example):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <math.h>
#include <vector>
using namespace std;

double calculateDeterminant(vector<double> matrix)
{
        if(matrix.size()==0) // empty std::vector
        {
		return 0;
        }

	int numOfRows = sqrt(matrix.size());
	
	if(numOfRows==1) //1x1 matrix
	{
		return matrix[0];
	}
	
	else if(numOfRows==2) //2x2 matrix
	{
		return ((matrix[0]*matrix[3])-(matrix[1]*matrix[2]));
	}
	
	else // nxn matrix with n>2
	{
		double determinant=0;
		vector<double> minorMatrix;
		int l=0;
		
		for(int i=0; i<numOfRows; i++)
		{
			for(j=0; j<numOfRows; j++)
			{
				for(int k=0; k<matrix.size(); k++)
				{
					if(k>numOfRows-1)
					{
						if(k==(numOfRows*l+j))
						{
							if(l<numOfRows)
							{l++;}
							continue;
						}
						
						minorMatrix.push_back(matrix[k]);
					}
				}
			}
			
			determinant += (matrix[i]*pow(-1,i+2))*calculateDeterminant(minorMatrix);
			
			minorMatrix.clear(); //reset for reusing on next iteration
			l=0;
		}
		
		return determinant;
	}
	
}


This version uses 1-dimensional arrays in the form of std::vectors to represent the values of a matrix such as this:

| a0 a1 a2 a3 a4 |
| a5 a6 a7 a8 a9 |
| a10 a11 a12 a13 a14 |
| a15 a16 a17 a18 a19 |
| a20 a21 a22 a23 a24 |
Last edited on
thank you. it's quite different from my codes but i'll try this codes.
Topic archived. No new replies allowed.