1 /***************************************************************************
2 Matrix.c - description
3 -------------------
4 begin : Wed Apr 19 2000
5 copyright : (C) 2000 by Nayani Abhishek
6 email : nayaniabhishek@yahoo.com
7 patched by : Ondrej 'Nepto' Jombik <jombik9@kepler.fmph.uniba.sk>
8 compile options : gcc -o matrix matrix.c -lnewt -lncurses
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
20
21 #include <curses.h>
22 #include <newt.h>
23
24 struct array
25 {
26 double ray[10][10];
27 };
28
29 main()
30 {
31
32 newtComponent ls,fm;
33 int p = 1, q = 2, r = 3, s = 4, t = 5, *u;
34
35 newtInit();
36 initscr();
37 start_color();
38 cbreak();
39 idlok(stdscr,TRUE);
40 scrollok(stdscr,TRUE);
41 init_pair(1,COLOR_WHITE,COLOR_BLUE);
42 init_pair(2,COLOR_YELLOW,COLOR_BLUE);
43 init_pair(3,COLOR_RED,COLOR_BLUE);
44
45 do {
46 clear();
47 refresh();
48 newtCls();
49 newtRefresh();
50 newtDrawRootText(-15,0,"Nayani Abhishek");
51 newtCenteredWindow(50,10,"MENU");
52 ls = newtListbox(3,3,5,NEWT_FLAG_RETURNEXIT);
53 newtListboxAppendEntry(ls,"1. Solution Of Simultaneous Equations",&p);
54 newtListboxAppendEntry(ls,"2. Inverse Of A Matrix ",&q);
55 newtListboxAppendEntry(ls,"3. Determinant Of A Matrix ",&r);
56 newtListboxAppendEntry(ls,"4. Multiplication Of Two Matrices ",&s);
57 newtListboxAppendEntry(ls,"5. Exit ",&t);
58 newtPushHelpLine(" Move using the arrow keys and press ENTER to select");
59 fm = newtForm(NULL,NULL,0);
60 newtFormAddComponents(fm,ls,NULL);
61 newtRunForm(fm);
62 u = newtListboxGetCurrent(ls);
63 newtPopWindow();
64 newtFormDestroy(fm);
65
66
67 if(*u!=5) {
68 border(0,0,0,0,0,0,0,0);
69 bkgd(COLOR_PAIR(1));
70 refresh();
71 }
72
73 switch(*u) {
74
75 case 1:
76 linear();
77 break;
78 case 2:
79 inversef();
80 break;
81 case 3:
82 determinantf();
83 break;
84 case 4:
85 multiplyf();
86 break;
87 case 5:
88 endwin();
89 newtFinished();
90 system("clear");
91 exit(1);
92
93 }
94 } while(1);
95
96
97 }
98
99
100 /***********************************************************************
101 ** FUNCTION TO FIND THE DETERMINANT OF THE GIVEN MATRIX **
102 ************************************************************************/
103
104 double determinant(struct array matrix, int dimension)
105 {
106
107 int i, orig_mat_row, orig_mat_col, temp_mat_row, temp_mat_col;
108 double det;
109 struct array temp_matrix;
110
111 if(dimension == 2) {
112 det = ( (matrix.ray[0][0]) * (matrix.ray[1][1]) )
113 - ( (matrix.ray[0][1]) * (matrix.ray[1][0]) );
114
115 return det;
116 }
117
118 det = 0.0;
119 for( i = 0; i < dimension; i++ ) {
120 temp_mat_row = 0;
121 temp_mat_col = 0;
122 for (orig_mat_row = 1;
123 orig_mat_row < dimension;
124 orig_mat_row++) {
125 for (orig_mat_col = 0;
126 orig_mat_col < dimension;
127 orig_mat_col++) {
128 if (orig_mat_col != i) {
129 temp_matrix.ray[temp_mat_row][temp_mat_col] =
130 matrix.ray[orig_mat_row][orig_mat_col];
131 temp_mat_col++;
132 }
133 }
134 temp_mat_row++;
135 temp_mat_col = 0;
136 }
137
138 det += (matrix.ray[0][i] * determinant(temp_matrix, dimension - 1))
139 * pow(-1,i+1);
140 }
141
142 return(-1 * det);
143 }
144
145 /***************************************************************************
146 ** FUNCTION WHICH RETURNS THE ADJOINT OF A GIVE MATRIX **
147 ****************************************************************************/
148
149 struct array adjoint(struct array matrix, int dimension)
150 {
151 int row, col, orig_mat_row, orig_mat_col, temp_mat_row, temp_mat_col;
152 struct array temp_matrix, conjugate_matrix, transpose_matrix;
153
154 if(dimension == 2) {
155 conjugate_matrix.ray[0][0] = matrix.ray[1][1];
156 conjugate_matrix.ray[0][1] = -1 * matrix.ray[0][1];
157 conjugate_matrix.ray[1][0] = -1 * matrix.ray[1][0];
158 conjugate_matrix.ray[1][1] = matrix.ray[0][0];
159
160 return(conjugate_matrix);
161 }
162 else {
163 for(row = 0; row < dimension; row++)
164 for(col = 0; col < dimension; col++) {
165
166 temp_mat_row = 0;
167 temp_mat_col = 0;
168
169 for(orig_mat_row = 0; orig_mat_row < dimension; orig_mat_row++) {
170 for(orig_mat_col = 0; orig_mat_col < dimension ; orig_mat_col++) {
171 if(orig_mat_row != row && orig_mat_col != col) {
172 temp_matrix.ray[temp_mat_row][temp_mat_col] = matrix.ray[orig_mat_row][orig_mat_col];
173 temp_mat_col++;
174 }
175 }
176 if( temp_mat_col > (dimension - 2) ) {
177 temp_mat_row++;
178 temp_mat_col = 0;
179 }
180 }
181
182 conjugate_matrix.ray[row][col] = determinant(temp_matrix,dimension - 1) * pow(-1,row+col+2);
183
184 }
185
186 for(row = 0; row < dimension; row++)
187 for(col = 0; col < dimension; col++)
188 transpose_matrix.ray[col][row] = conjugate_matrix.ray[row][col];
189 return(transpose_matrix);
190 }
191 }
192
193 /***********************************************************************
194 ** FUNCTION TO FIND THE PRODUCT OF TWO MATRICES **
195 ************************************************************************/
196
197
198 struct array multiply(struct array matrix_one, struct array matrix_two, int dimension)
199 {
200 struct array result_matrix;
201 int i,j,k;
202
203 for(i=0;i<dimension;i++)
204 for(j=0;j<dimension;j++) {
205 result_matrix.ray[i][j] = 0;
206 for(k=0;k<dimension;k++)
207 result_matrix.ray[i][j] = result_matrix.ray[i][j] + ( matrix_one.ray[i][k] * matrix_two.ray[k][j] );
208 }
209
210 return(result_matrix);
211 }
212
213
214 /***************************************************************************
215 ** FUNCTION TO FIND THE INVERSE OF A GIVEN MATRIX **
216 ****************************************************************************/
217
218
219 struct array inverse(struct array adjoint_matrix,double det,int dimension)
220 {
221 int row, col;
222 struct array inverse_matrix;
223
224 for(row = 0; row < dimension; row++)
225 for(col = 0; col < dimension; col++)
226 inverse_matrix.ray[row][col] = (adjoint_matrix.ray[row][col]) / det ;
227
228 return(inverse_matrix);
229 }
230
231
232 /*****************************************************************************
233 ** FUNCTION TO SOLVE SIMULTANEOUS LINEAR EQUATIONS **
234 ******************************************************************************/
235
236
237 int linear()
238 {
239 struct array variable_matrix, adjoint_matrix, result_matrix, const_matrix, inverse_matrix;
240 int n,i,j,y,x;
241 double det;
242 WINDOW *win;
243
244 win = newwin((LINES-7),76,5,3);
245 scrollok(win,TRUE);
246 clear();
247 border(0,0,0,0,0,0,0,0);
248 wbkgd(win,COLOR_PAIR(1));
249
250 mvprintw(3,((COLS/2)-14),"SOLUTION OF LINEAR EQUATIONS");
251 refresh();
252 mvwprintw(win,2,0,"ENTER THE NO. OF VARIABLES := ");
253 wrefresh(win);
254 mvwscanw(win,2,32,"%d",&n);
255
256 for(i = 0; i < n; i++) {
257 wprintw(win,"\nEQUATION - %d\n",i+1);
258 for(j = 0; j < n; j++) {
259 wprintw(win,"ENTER THE COEFFICIENT OF VARIABLE (%d) := ",j+1);
260 wrefresh(win);
261 wscanw(win,"%lf",&variable_matrix.ray[i][j]);
262 }
263 wprintw(win,"ENTER THE VALUE OF CONSTANT := ");
264 wrefresh(win);
265 wscanw(win,"%lf",&const_matrix.ray[i][0]);
266 }
267
268 det = determinant(variable_matrix,n);
269 if(det == 0) {
270 getyx(win,y,x);
271 wscrl(win,3);
272 wattron(win,A_BOLD | COLOR_PAIR(3));
273 mvwprintw(win,y,(COLS/2)-23,"DETERMINANT = 0 ; SOLUTION NOT POSSIBLE");
274 wattroff(win,A_BOLD | COLOR_PAIR(3));
275 }
276 else {
277 adjoint_matrix = adjoint(variable_matrix,n);
278 inverse_matrix = inverse(adjoint_matrix,det,n);
279 result_matrix = multiply(inverse_matrix,const_matrix,n);
280 wattron(win,A_BOLD | COLOR_PAIR(2));
281 for(i = 0; i < n; i++)
282 wprintw(win,"\nTHE VALUE OF VAR(%d) := %6.2lf",i+1,result_matrix.ray[i][0]);
283 wattroff(win,A_BOLD | COLOR_PAIR(2));
284 }
285 wrefresh(win);
286 getch();
287 delwin(win);
288 return(0);
289 }
290
291
292 /**********************************************************************
293 ** FRONT-END TO THE INVERSE FUNCTION **
294 ***********************************************************************/
295
296
297 int inversef()
298 {
299 struct array adjoint_matrix, matrix, inverse_matrix;
300 int dimension,row,col,y,x;
301 double det;
302 WINDOW *win;
303
304 win = newwin((LINES-10),76,8,3);
305 scrollok(win,TRUE);
306 clear();
307 border(0,0,0,0,0,0,0,0);
308 wbkgd(win,COLOR_PAIR(1));
309
310 mvprintw(3,(COLS/2)-11,"INVERSE OF A MATRIX");
311 mvprintw(6,3,"ENTER THE DIMENSION OF THE SQUARE MATRIX := ");
312 refresh();
313 mvscanw(6,50,"%d",&dimension);
314
315 for(row = 0; row < dimension; row++) {
316 wprintw(win,"\nENTER (%d) ROW ELEMENTS\n",row+1);
317 for(col = 0; col < dimension; col++) {
318 wprintw(win,"ENTER (%d) COLUMN ELEMENTS := ",col+1);
319 wrefresh(win);
320 wscanw(win,"%lf",&matrix.ray[row][col]);
321 }
322 }
323
324 det = determinant(matrix,dimension);
325
326 if(det == 0) {
327 getyx(win,y,x);
328 wscrl(win,3);
329 wattron(win,A_BOLD | COLOR_PAIR(3));
330 mvwprintw(win,y,(COLS/2)-23,"INVALID ENTRY - DETERMINANT IS ZERO !!");
331 wattroff(win,A_BOLD | COLOR_PAIR(3));
332 wrefresh(win);
333 getch();
334 return(0);
335 }
336
337 adjoint_matrix = adjoint(matrix,dimension);
338
339 inverse_matrix = inverse(adjoint_matrix,det,dimension);
340
341 wprintw(win,"\n\nTHE INVERSE OF THE GIVEN MATRIX IS :=\n");
342 for(row = 0; row < dimension; row++) {
343 wprintw(win,"\n");
344 for(col = 0; col < dimension; col++) {
345 wprintw(win,"%5.2lf\t",inverse_matrix.ray[row][col]);
346 }
347 }
348 wrefresh(win);
349 getch();
350 delwin(win);
351 return(0);
352
353 }
354
355
356 /*************************************************************************
357 ** FRONT-END TO THE DETERMINANT FUNCTION **
358 **************************************************************************/
359
360 determinantf()
361 {
362
363 int row,col,dimension;
364 double det;
365 struct array matrix;
366 WINDOW *win;
367
368
369 win = newwin((LINES-10),76,8,3);
370 scrollok(win,TRUE);
371 clear();
372 border(0,0,0,0,0,0,0,0);
373 wbkgd(win,COLOR_PAIR(1));
374
375 mvprintw(3,(COLS/2)-12,"DETERMINANT OF A MATRIX");
376 mvprintw(6,3,"ENTER THE DIMENSION OF THE SQUARE MATRIX := ");
377 refresh();
378 mvscanw(6,48,"%d",&dimension);
379
380 for(row = 0; row < dimension; row++) {
381 wprintw(win,"\nENTER (%d) ROW ELEMENTS\n",row+1);
382 for(col = 0; col < dimension; col++) {
383 wprintw(win,"ENTER (%d) COLUMN ELEMENTS := ",col+1);
384 wrefresh(win);
385 wscanw(win,"%lf",&matrix.ray[row][col]);
386 }
387 }
388
389 det = determinant(matrix,dimension);
390 wattron(win,A_BOLD | COLOR_PAIR(2));
391 wprintw(win,"\n\nTHE DETERMINANT OF THE GIVEN MATRIX := %4.2lf",det);
392 wattroff(win,A_BOLD | COLOR_PAIR(2));
393 wrefresh(win);
394 getch();
395 delwin(win);
396
397 }
398
399
400 /*************************************************************************
401 ** FRONT-END TO MULTIPLICATION FUNCTION **
402 **************************************************************************/
403
404
405 int multiplyf()
406 {
407 struct array matrix_one, matrix_two, result_matrix;
408 int i,j,row1,row2,col1,col2;
409 WINDOW *win;
410
411 win = newwin((LINES-6),74,5,4);
412 scrollok(win,TRUE);
413 clear();
414 border(0,0,0,0,0,0,0,0);
415 wbkgd(win,COLOR_PAIR(1));
416
417 mvprintw(3,(COLS/2)-11,"MATRIX MULTIPLICATION");
418 refresh();
419 mvwprintw(win,2,0,"ENTER THE NO. OF ROWS OF FIRST MATRIX := ");
420 wrefresh(win);
421 mvwscanw(win,2,46,"%d",&row1);
422 mvwprintw(win,3,0,"ENTER THE NO. OF COLUMNS OF FIRST MATRIX := ");
423 wrefresh(win);
424 mvwscanw(win,3,46,"%d",&col1);
425
426 for(i = 0; i < row1; i++) {
427 wprintw(win,"\nENTER (%d) ROW ELEMENTS\n",i+1);
428 for(j = 0; j < col1; j++) {
429 wprintw(win,"ENTER (%d) COLUMN ELEMENTS := ",j+1);
430 wrefresh(win);
431 wscanw(win,"%lf",&matrix_one.ray[i][j]);
432 }
433 }
434
435 do {
436
437 wclear(win);
438 mvwprintw(win,2,0,"ENTER THE NO. OF ROWS OF SECOND MATRIX := ");
439 mvwscanw(win,2,48,"%d",&row2);
440 mvwprintw(win,3,0,"ENTER THE NO. OF COLUMNS OF SECOND MATRIX := ");
441 mvwscanw(win,3,48,"%d",&col2);
442 refresh();
443
444 if(col1 != row2) {
445 wattron(win,A_BOLD | COLOR_PAIR(3));
446 mvwprintw(win,LINES/2,COLS/2-29," COLUMNS OF FIRST MATRIX MUST BE EQUAL TO");
447 mvwprintw(win,LINES/2+1,COLS/2-29," ROWS OF SECOND MATRIX !!");
448 wattroff(win,A_BOLD | COLOR_PAIR(3));
449 wrefresh(win);
450 getch();
451 }
452 } while(col1 != row2);
453
454 for(i = 0; i < row2; i++) {
455 wprintw(win,"\nENTER (%d) ROW ELEMENTS\n",i+1);
456 for(j = 0; j < col2 ;j++) {
457 wprintw(win,"ENTER (%d) COLUMN ELEMENTS := ",j+1);
458 wscanw(win,"%lf",&matrix_two.ray[i][j]);
459 }
460 }
461 result_matrix = multiply(matrix_one,matrix_two,col1);
462 wprintw(win,"\n\nTHE RESULTANT ARRAY IS := ");
463
464 for(i = 0; i < row1; i++) {
465 wprintw(win,"\n\t\t");
466 for(j = 0; j < col2; j++) {
467 wprintw(win,"%-6.2lf",result_matrix.ray[i][j]);
468 }
469 }
470 wrefresh(win);
471 getch();
472 delwin(win);
473 return(0);
474 }
475
476
477 /************************************************************************************
478 ** FUNCTION TO FIND THE POWER OF GIVEN NUMBER **
479 *************************************************************************************/
480
481 int pow(int base,int power)
482 {
483 if(power == 1)
484 return(base);
485 else
486 return(base * pow(base,power-1));
487 }
488
489
490
491
492
493
494
495