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