-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
275 lines (176 loc) · 6.67 KB
/
matrix.h
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#ifndef MATRIX_H
#define MATRIX_H
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <complex.h>
#include <stdbool.h>
typedef struct {
int rows;
int cols;
double * data;
} matrix;
#define ELEM(mtx, row, col) \
mtx->data[(col-1) * mtx->rows + (row-1)]
extern bool __DELETE_norma__;
extern bool __DELETE_matrixColToVector__;
extern bool __DELETE_dotProduct2__;
extern bool __DELETE_product2__;
extern bool __DELETE_subtraction2__;
extern bool __DELETE_productByScalar2__;
extern bool __DELETE_matrixAbs2__;
extern bool __DELETE_getMinElemVec__;
extern bool __DELETE_qr__;
extern bool __DELETE_sum2__;
//bool __MATRIX_DELETE__ = false;
/* Creates a ``rows by cols'' matrix with all values 0.
* Returns NULL if rows <= 0 or cols <= 0 and otherwise a
* pointer to the new matrix.
*/
matrix * newMatrix(int rows, int cols);
/* Deletes a matrix. Returns 0 if successful and -1 if mtx
* is NULL.
*/
int deleteMatrix(matrix * mtx);
/* Copies a matrix. Returns NULL if mtx is NULL.
*/
matrix * copyMatrix(matrix * mtx);
/* Sets the (row, col) element of mtx to val. Returns 0 if
* successful, -1 if mtx is NULL, and -2 if row or col are
* outside of the dimensions of mtx.
*/
int setElement(matrix * mtx, int row, int col, double val);
/* Sets the reference val to the value of the (row, col)
* element of mtx. Returns 0 if successful, -1 if either
* mtx or val is NULL, and -2 if row or col are outside of
* the dimensions of mtx.
*/
int getElement(matrix * mtx, int row, int col,
double * val);
/* Sets the reference n to the number of rows of mtx.
* Returns 0 if successful and -1 if mtx or n is NULL.
*/
int nRows(matrix * mtx, int * n);
/* Sets the reference n to the number of columns of mtx.
* Returns 0 if successful and -1 if mtx is NULL.
*/
int nCols(matrix * mtx, int * n);
/* Prints the matrix to stdout. Returns 0 if successful
* and -1 if mtx is NULL.
*/
int printMatrix(matrix * mtx);
/* Writes the transpose of matrix in into matrix out.
* Returns 0 if successful, -1 if either in or out is NULL,
* and -2 if the dimensions of in and out are incompatible.
*/
int transpose(matrix * in, matrix * out);
/* Writes the sum of matrices mtx1 and mtx2 into matrix
* sum. Returns 0 if successful, -1 if any of the matrices
* are NULL, and -2 if the dimensions of the matrices are
* incompatible.
*/
int sum(matrix * mtx1, matrix * mtx2, matrix * sum);
/* Writes the product of matrices mtx1 and mtx2 into matrix
* prod. Returns 0 if successful, -1 if any of the
* matrices are NULL, and -2 if the dimensions of the
* matrices are incompatible.
*/
int product(matrix * mtx1, matrix * mtx2, matrix * prod);
int areEqual(matrix * mtx1, matrix * mtx2);
/* Writes the dot product of vectors v1 and v2 into
* reference prod. Returns 0 if successful, -1 if any of
* v1, v2, or prod are NULL, -2 if either matrix is not a
* vector, and -3 if the vectors are of incompatible
* dimensions.
*/
int dotProduct(matrix * v1, matrix * v2, double * prod);
void subMatrix(matrix *A, int startRow, int endRow, int startCol, int endCol, matrix *B);
int subtractionByScalar(matrix *A, double x, matrix *sub);
int identity(matrix * m);
int isSquare(matrix * mtx);
int isDiagonal(matrix * mtx);
int isUpperTriangular(matrix * mtx);
int vectorToDiagonal(matrix * v, matrix * mtx);
int isSymmetric(matrix * mtx);
int printLowerTriangular(matrix * mtx);
int printLowerTriangularCol(matrix * mtx);
matrix * addElementMatrix(int rows, int cols, matrix * mtx);
matrix * vec(matrix * mtx);
double vecGetLowerValue(matrix * vec, int i, int j, int n);
double vecGetUpperValue(matrix * vec, int i, int j, int n);
int vecGetLowerIndex(int i, int j, int n);
int vecGetUpperIndex(int i, int j, int n);
double laplace(matrix *A);
double norma(matrix *A, int p);
double normaInf(matrix *A);
double normaMatricial(matrix *A, int p);
double normaMatricialInf(matrix *A);
double normaMatricial1(matrix *A);
int productByScalar(matrix *A, double x, matrix *prod);
int divisionByScalar(matrix *A, double x, matrix *div);
int subtraction(matrix *A, matrix *B, matrix *sub);
int division(matrix *A, matrix *B, matrix *div);
void metodoDasPotencias(matrix *A, matrix *z0, double tol, int maxit, matrix **_z, double *_lambda);
matrix* matrixRowToVector(matrix *A, int i);
matrix* matrixColToVector(matrix *A, int j);
int jacobi(matrix *A, matrix *e, matrix *V);
typedef struct {
matrix *Q;
matrix *R;
} _qr;
_qr qr(matrix *A);
void qrold(matrix *M, matrix *Q, matrix *R);
//void qr(matrix *M, matrix *Q, matrix *R);
void copyColumn(matrix *A, matrix *c, int j);
void copyNColumn(matrix *A, matrix *c, int n, int j);
int tolerance(matrix *A, double tol);
void householder(matrix *A, matrix *HH);
void diagonalToVector(matrix *A, matrix *v);
typedef struct {
double complex x1;
double complex x2;
} _eqSegGrau;
_eqSegGrau eqSegGrau(double a, double b, double c);
typedef struct {
double min;
int pos;
} _minElemVec;
_minElemVec getMinElemVec(matrix *A, int col);
void matrixAbs(matrix *A, matrix *B);
void loadMatrix(matrix **A, char *input);
int getLowerTriangular(matrix *A, matrix *B, int k);
int getUpperTriangular(matrix *A, matrix *B, int k);
matrix *transpose2(matrix * in);
matrix *sum2(matrix * mtx1, matrix * mtx2);
matrix *product2(matrix *A, matrix *B);
double dotProduct2(matrix * v1, matrix * v2);
matrix *identity2(int order);
double getElement2(matrix * mtx, int row, int col);
matrix *subtraction2(matrix *A, matrix *B);
matrix *division2(matrix *A, matrix *B);
matrix *sumByScalar2(matrix *A, double x);
matrix *subtractionByScalar2(matrix *A, double x);
matrix *productByScalar2(matrix *A, double x);
matrix *divisionByScalar2(matrix *A, double x);
matrix *getUpperTriangular2(matrix *A, int k);
matrix *getLowerTriangular2(matrix *A, int k);
int matrixSwapLines(matrix *matrix, int n, int x, int y);
int matrixSwapLines2(matrix *A, int x, int y);
int menorPrincipal(matrix *A, matrix *B, int n);
matrix *menorPrincipal2(matrix *A, int n);
matrix *matrixAbs2(matrix *A);
matrix *subMatrix2(matrix *A, int startRow, int endRow, int startCol, int endCol);
matrix *diagonalToVector2(matrix *A);
matrix *householder2(matrix *A);
int printMatrix2(matrix * mtx, char *name);
matrix *qr2(matrix *A, double epsilon);
void memoryUsage(void);
void gauss(matrix *A, matrix *x, matrix *b, int n);
void gaussP(matrix *A, matrix *x, matrix *b, int n);
void gaussJordan(matrix *A, matrix *x, matrix *b, int n);
int diagonalContainsZero(matrix * mtx);
int retroSubstituicao(matrix *A, matrix *x, matrix *b, int n);
void decomposicaoLU(matrix *A, int n, matrix *L, matrix *U);
int decomposicaoCholesky(matrix *A, matrix *S, matrix *St, int n);
#endif