-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
116 lines (95 loc) · 3.15 KB
/
matrix.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
#include "matrix.h"
//Multiply two square matrices of the same size.
double** matrixMultiply(double **matrix1, double **matrix2, int matrixSize){
//Allocates memory for a matrix of doubles.
int i, j, k;
double **matrixOut = (double **)malloc(matrixSize * sizeof(double*));
for (i = 0; i < matrixSize; i++){
matrixOut[i] = (double *) malloc(matrixSize * sizeof(double));
}
double result = 0;
//Fill each cell of the matrix output.
for(i = 0 ; i < matrixSize ; i++){
for (j = 0; j < matrixSize ; j++){
//Multiply each row of matrix 1 with each column of matrix 2.
for(k = 0; k < matrixSize; k++){
result += matrix1[i][k] * matrix2[k][j];
}
matrixOut[i][j] = result;
result = 0; //Reset;
}
}
return matrixOut;
}
//Add two square matrices of the same size.
double** matrixAddition(double **matrix1, double **matrix2, int matrixSize){
//Allocates memory for a matrix of doubles.
int i, j;
double **matrixOut = (double **)malloc(matrixSize * sizeof(double*));
for (i = 0; i < matrixSize; i++){
matrixOut[i] = (double *) malloc(matrixSize * sizeof(double));
}
//Fill each cell of the matrix output.
for(i = 0 ; i < matrixSize ; i++){
for (j = 0; j < matrixSize ; j++){
matrixOut[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return matrixOut;
}
//Return the transpose of a square matrix.
double** transpose(double **matrix, int matrixSize){
//Allocates memory for a matrix of doubles.
int i, j;
double **matrixOut = (double **)malloc(matrixSize * sizeof(double*));
for (i = 0; i < matrixSize; i++){
matrixOut[i] = (double *) malloc(matrixSize * sizeof(double));
}
//Transpose the matrix.
for(i = 0 ; i < matrixSize ; i++){
for (j = 0; j < matrixSize ; j++){
matrixOut[i][j] = matrix[j][i];
}
}
return matrixOut;
}
//Create a real positive-definite matrix.
double** initializeMatrix(int minValue, int maxValue, int matrixSize){
//Allocates memory for a matrices of doubles.
int i, j;
double **matrix = (double **)malloc(matrixSize * sizeof(double*));
double **identity = (double **)malloc(matrixSize * sizeof(double*));
for (i = 0; i < matrixSize; i++){
matrix[i] = (double *) malloc(matrixSize * sizeof(double));
identity[i] = (double *) malloc(matrixSize * sizeof(double));
}
//Creates an upper-triangular matrix of random numbers between minValue and maxValue.
//Creates an identity matrix multiplied by maxValue.
double random;
for(i = 0 ; i < matrixSize ; i++){
identity[i][i] = maxValue * matrixSize;
for(j = 0 ; j < matrixSize ; j++){
random = (maxValue - minValue) *
((double)rand() / (double)RAND_MAX) + minValue;
if(random == 0.0){
random = 1.0; //Avoid division by 0.
}
matrix[i][j] = random;
}
}
//Transform to positive-definite.
double **transposed = transpose(matrix, matrixSize);
matrix = matrixAddition(matrix, transposed, matrixSize);
matrix = matrixAddition(matrix, identity, matrixSize);
return matrix;
}
void print(double **matrix, int matrixSize){
int i, j;
for (i = 0; i < matrixSize; i++) {
for (j = 0; j < matrixSize; j++) {
printf("%.2f\t", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}