### 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:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````#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):

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````#include #include using namespace std; double calculateDeterminant(vector 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 minorMatrix; int l=0; for(int i=0; inumOfRows-1) { if(k==(numOfRows*l+j)) { if(l

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.