-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatops.c
186 lines (158 loc) · 4.21 KB
/
matops.c
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* matops.c :
* Matrix operations - Multiplication, addition, subtraction, transpose
* and minimum element along the diagonal
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int dim = 3; /* Dimensions dim x dim of matrix */
void accept_user_matrix (double ** mat)
{
int i, j;
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
scanf ("%lf", &mat[i][j]);
}
}
}
/* Swap two elements of the matrix,
* used by the transpose function */
void swap (double * a, double * b)
{
double temp = *a;
*a = *b;
*b = temp;
}
/* Set all elements to zero */
void clear_matrix (double ** matrix)
{
int i, j;
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
matrix[i][j] = 0.0;
}
}
}
void display (double ** matrix)
{
int i,j;
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
printf ("%.2lf\t", matrix[i][j]);
if(j == dim - 1)
printf("\n");
}
}
}
/* No parameters are needed since we only construct dim x dim matrices */
double ** allocate_array (void)
{
int i;
/* allocate space for the array */
double * data = malloc (dim * dim * sizeof(double));
/* create pointers for the rows starting points */
double ** rows = malloc (dim * sizeof(double *));
for (i = 0; i < dim; i++) {
rows[i] = data + i * dim;
}
return rows;
}
/* Deallocates the memory for a 2D matrix */
void free_array (double ** mat)
{
free (mat[0]); /* Free the pointers to the rows */
free (mat); /* Free the memory for the data */
}
/* Multiplies two square matrices.
* Allocates memory for the result matrix.
* The caller is responsible for freeing the memory.
*/
double ** multiply (double ** m1, double ** m2)
{
int i, j, k;
double ** result = allocate_array();
clear_matrix(result);
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
for (k = 0; k < dim; k++) {
result [i][j] += (m1[i][k] * m2[k][j]);
}
}
}
return result;
}
double ** transpose (double ** mat)
{
int i, j;
for (i = 0; i < dim; i++) {
for (j = 0; j < i; j++) {
swap (&mat[i][j], &mat[j][i]);
}
}
return mat;
}
/* Returns the sum of the elements along the diagonal */
double sum_diag (double ** matrix)
{
double sum = 0;
int i;
for (i = 0 ; i < dim; i++) {
sum += matrix[i][i];
}
return sum;
}
double ** add (double ** m1, double ** m2)
{
int i,j;
double ** result = allocate_array(); /* Caller must free this */
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
result [i][j] = (m1[i][j] + m2[i][j]);
}
}
return result;
}
double ** sub (double ** m1, double ** m2)
{
int i, j;
double ** result = allocate_array(); /* Caller must free this */
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
result [i][j] = (m1[i][j] - m2[i][j]);
}
}
return result;
}
int main (void)
{
double ** m1 = NULL, **m2 = NULL, **result = NULL;
printf ("Enter square matrix dimension : ");
scanf ("%d", &dim);
m1=allocate_array();
m2=allocate_array();
printf ("\nEnter first array : ");
accept_user_matrix (m1);
printf ("\nEnter second array : ");
accept_user_matrix (m2);
result = multiply (m1,m2);
printf ("The product of the two matrices is : \n");
display (result);
printf ("Transpose of the first matrix is : \n");
display (transpose (m1));
printf ("Transpose of the second matrix is : \n");
display (transpose (m2));
printf ("The sum of the diagonal elements of the first matrix is %.2lf\n", sum_diag (m1));
printf ("The sum of the diagonal elements of the second matrix is %.2lf\n", sum_diag (m2));
double ** add_result = add (m1,m2);
printf ("The result of addition is : \n");
display (add_result);
double ** sub_result = sub (m1,m2);
printf ("The result of subtraction is : \n");
display (sub_result);
free_array (m1);
free_array (m2);
free_array (result);
free_array (add_result);
free_array (sub_result);
return 0;
}