aboutsummaryrefslogtreecommitdiff
path: root/matrix-operations.c
blob: 18c29c16ed66fb71b302ba3a4291df24c018feb4 (plain) (blame)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "matrix-operations.h"


matrix_t matrixError(error_t error){
	switch(error){
		case NOT_EQUAL:
			perror("Matrices are not of the same dimensions\n");
			break;
		case NOT_MULTIPLIABLE:
			perror("Matrix A's number of columns is different to matrix B's number of rows\n");
			break;
		case NOT_SQUARE:
			perror("Matrix is not square");
			break;
		case ZERO_DET:
			perror("Determinant of the matrix should be 0\n");
			break;
		case UNKNOWN:
		default:
			perror("Unknown error\n");
	}
	return createMatrix(0, 0);
}

// Creates a rows x columns matrix;
matrix_t createMatrix(size_t rows, size_t columns){
	matrix_t newmatrix;
	newmatrix.rows = rows;
	newmatrix.columns = columns;
	newmatrix.matrix = malloc(rows*sizeof(float*));
	for(size_t i = 0; i < rows; ++i)
		newmatrix.matrix[i] = calloc(columns,sizeof(float));
	return newmatrix;
}

// Fills the matrix with values provided by the user
void initializeMatrix(matrix_t *m){
	printf("Initializing %dx%d matrix\n", m->rows, m->columns);
	for(size_t r = 0; r < m->rows; ++r){
		printf("Please insert %d numbers for each of the columns of the %d%s row\n",
			m->columns, r+1, (r+1==1)?("st"):((r+1==2)?("nd"):((r+1==3)?("rd"):("th"))));
		for(size_t c = 0; c < m->columns; ++c){
			scanf("%f", &m->matrix[r][c]);
		}
	}
}

// Returns true if the matrix is square, false otherwise
int isSquare(matrix_t m){
	return m.rows == m.columns;
}

// Displays the given matrix
void displayMatrix(matrix_t m){
	for(size_t r = 0; r < m.rows; ++r){
		for(size_t c = 0; c < m.columns; ++c)
			printf("%3.3g ", m.matrix[r][c]);
		printf("\n");
	}
}

// Returns the identity matrix n x n
matrix_t identityMatrix(size_t n){
	matrix_t identityMatrix = createMatrix(n, n);
	for(size_t r = 0; r < n; ++r)
		for(size_t c = 0; c < n; ++c)
			if(r == c)
				identityMatrix.matrix[r][c] = 1;
	return identityMatrix; 
}

// Returns a matrix of the same size as "m" filled with the number "n"
matrix_t fillN(matrix_t m, float n){
	matrix_t filledmatrix = createMatrix(m.rows, m.columns);
	for(size_t r = 0; r < m.rows; ++r)
		for(size_t c = 0; c < m.columns; ++c)
			filledmatrix.matrix[r][c] = n;
	return filledmatrix;
}

// Adds a number to all the positions of the matrix and returns the result
matrix_t addN(matrix_t m, float n){
	matrix_t addedmatrix = createMatrix(m.rows, m.columns);
	for(size_t r = 0; r < m.rows; ++r)
		for(size_t c = 0; c < m.columns; ++c)
			addedmatrix.matrix[r][c] = m.matrix[r][c]+n;
	return addedmatrix;
}

// Substracts a number from all the positions of the matrix and returns the result
matrix_t substractN(matrix_t m, float n){
	matrix_t substractedmatrix = createMatrix(m.rows, m.columns);
	for(size_t r = 0; r < m.rows; ++r)
		for(size_t c = 0; c < m.columns; ++c)
			substractedmatrix.matrix[r][c] = m.matrix[r][c]-n;
	return substractedmatrix;
}

// Adds two matrices together and returns the result
matrix_t addMatrices(matrix_t a, matrix_t b){
	if(a.rows != b.rows || a.columns != b.columns)
		return matrixError(NOT_EQUAL);
	matrix_t addedmatrices = createMatrix(a.rows, b.columns);
	for(size_t r = 0; r < a.rows; ++r)
		for(size_t c = 0; c < a.columns; ++c)
			addedmatrices.matrix[r][c] = a.matrix[r][c] + b.matrix[r][c];
	return addedmatrices;
}

// Negates all of the positions of the matrix and returns the result
matrix_t negateMatrix(matrix_t m){
	matrix_t negatedmatrix = createMatrix(m.rows, m.columns);
	for(size_t r = 0; r < m.rows; ++r)
		for(size_t c = 0; c < m.columns; ++c)
			negatedmatrix.matrix[r][c] = -m.matrix[r][c];
	return negatedmatrix;
}

// Substracts matrix "b" from "a" and returns the result
matrix_t substractMatrices(matrix_t a, matrix_t b){
	return addMatrices(a, negateMatrix(b));
}

// Multiplies all of the positions of the matrix by "n" and returns the result
matrix_t multiplyByN(matrix_t m, float n){
	matrix_t multipliedmatrix = createMatrix(m.rows, m.columns);
	for(size_t r = 0; r < m.rows; ++r)
		for(size_t c = 0; c < m.columns; ++c)
			multipliedmatrix.matrix[r][c] = m.matrix[r][c]*n;
	return multipliedmatrix;
}

// Divides all of the positions of the matrix by "n" and returns the result
matrix_t divideByN(matrix_t m, float n){
	return multiplyByN(m, (float)1/n);
}

// Multiplies matrix "a" by "b" and returns the result
matrix_t multiplyMatrices(matrix_t a, matrix_t b){
	if(a.columns != b.rows)
		return matrixError(NOT_MULTIPLIABLE);
	matrix_t multipliedmatrices = createMatrix(a.rows, b.columns);
	for(size_t r = 0; r < multipliedmatrices.rows; ++r)
		for(size_t c = 0; c < multipliedmatrices.columns; ++c)
			for(size_t n = 0; n < a.columns; ++n)
				multipliedmatrices.matrix[r][c] += a.matrix[r][n]*b.matrix[n][c];
	return multipliedmatrices;
}

// Divides matrix "a" by "b" and returns the result
matrix_t divideMatrices(matrix_t a, matrix_t b){
	return multiplyMatrices(a, inverse(b));
}

// Raises the matrix to the nth power and returns the result
matrix_t raiseMatrixToN(matrix_t m, int n){
	if(!isSquare(m))
		return matrixError(NOT_SQUARE);
	matrix_t raisedmatrix = identityMatrix(m.rows);
	if(n < 0){
		m = inverse(m);
		n = -n;
	}
	for(int i = 0; i < n; ++i)
		raisedmatrix = multiplyMatrices(raisedmatrix, m);
	return raisedmatrix;
}

// Creates a submatrix from "matrix" without the column "column" and row "row" of "matrix"
matrix_t createSubmatrix(matrix_t m, size_t row, size_t column){
	// TODO: not remove any
	int mod = m.rows, nmod = mod-1, sr = 0, sc = 0;
	matrix_t submatrix = createMatrix(nmod, nmod);
	for(size_t r = 0; r < mod; ++r){
		sc = 0;
		if(r != row){
			for(size_t c = 0; c < mod; ++c){
				if(c != column){
					submatrix.matrix[sr][sc] = m.matrix[r][c];
					++sc;
				}	
			}
			++sr;
		}
	}
	return submatrix;
}

// Returns the determinant of the given (square) matrix
int determinant(matrix_t m){
	if(!isSquare(m)){
		matrixError(NOT_SQUARE);
		return (int)NULL;
	}
	int det = 0;
	if(m.rows == 1)
		return m.matrix[0][0];
	else{
		int count = 0, nmod = m.rows-1;
		for(size_t r = 0; r < m.rows; ++r){
			matrix_t submatrix = createSubmatrix(m, 0, r);
			int subdeterminant = determinant(submatrix);
			int step = m.matrix[0][r]*subdeterminant;
			if((count++)%2 == 0)
				det += step;
			else
				det -= step;
		}
		return det;
	}
}

// Returns the cofactor matrix of the given matrix
matrix_t cofactor(matrix_t m){
	if(!isSquare(m))
		return matrixError(NOT_SQUARE);
	matrix_t cofactormatrix = createMatrix(m.rows, m.columns);
	for(size_t r = 0; r < m.rows; ++r){
		for(size_t c = 0; c < m.columns; ++c){
			matrix_t submatrix = createSubmatrix(m, r, c);
			int d = determinant(submatrix);
			cofactormatrix.matrix[r][c] = (r+c)%2?-d:d;
		}
	}
	return cofactormatrix;
}

// Returns the transpose matrix of the given matrix
matrix_t transpose(matrix_t m){
	matrix_t transposematrix = createMatrix(m.columns, m.rows);
	for(size_t r = 0; r < m.columns; ++r)
		for(size_t c = 0; c < m.rows; ++c)
			transposematrix.matrix[r][c] = m.matrix[c][r];
	return transposematrix;
}

// Returns the adjugate matrix of the given matrix
matrix_t adjugate(matrix_t m){
	return transpose(cofactor(m));
}

// Returns the inverse matrix of the given matrix
matrix_t inverse(matrix_t m){
	int det = determinant(m);
	if(det == 0)
		return matrixError(ZERO_DET);
	return divideByN(adjugate(m), det);
}