-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputfunctions.c
133 lines (119 loc) · 3.66 KB
/
outputfunctions.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
//transpose scalar
double transpose_s(float s){
return s;
};
// choose function
double choose(double expr1, double expr2, double expr3, double expr4){
if(expr1 == 0){
return expr2;
}
else if (expr1>0){
return expr3;
}
else if (expr1<0){
return expr4;
}
}
//matrix multiplier
double **matrix_mult(double **m1, double **m2, int m1_r, int m1_c, int m2_c){
//initializes the output array
double **output_matrix = malloc(sizeof(double*)*m1_r);
for(int i = 0; i< m2_c; i++){
output_matrix[i] = malloc(sizeof(double)*m2_c);
}
//sum of products for output marix's entries
double sop = 0 ;
for (int i = 0; i<m1_r; i++){
for (int j = 0; j<m2_c ; j++){
for(int k = 0; k< m1_c; k++){
sop += m1[i][k] * m2[k][j];
}
output_matrix[i][j] = sop;
sop = 0;
}
}
return output_matrix;
}
//matrix adder
double **matrix_add(double **m1, double **m2, int m1_r, int m1_c){
//initializes the output array
double **output_matrix = malloc(sizeof(double*)*m1_r);
for(int i = 0; i< m1_c; i++){
output_matrix[i] = malloc(sizeof(double)*m1_c);
}
// adds the entries with one to one correspondence
for (int i = 0; i<m1_r ; i++){
for (int j = 0 ; j< m1_c ; j++){
output_matrix[i][j] = m1[i][j] + m2[i][j];
}
}
return output_matrix;
}
//matrix subtructor
double **matrix_sub(double **m1, double **m2, int m1_r, int m1_c){
//initializes the output array
double **output_matrix = malloc(sizeof(double*)*m1_r);
for(int i = 0; i< m1_c; i++){
output_matrix[i] = malloc(sizeof(double)*m1_c);
}
// subtracts the entries with one to one correspondence
for (int i = 0; i<m1_r ; i++){
for (int j = 0 ; j< m1_c ; j++){
output_matrix[i][j] = m1[i][j] - m2[i][j];
}
}
return output_matrix;
}
// matrix transposer
double **matrix_transpose(double **matrix, int matrix_r, int matrix_c){
//initializes the output array
double **output_matrix = malloc(sizeof(double*)*matrix_r);
for(int i = 0; i< matrix_c; i++){
output_matrix[i] = malloc(sizeof(double)*matrix_c);
}
// defines the output matrix's [j][i] as input matrix's [i][j]
for(int i = 0; i< matrix_r; i++){
for (int j = 0; j<matrix_c; j++){
output_matrix[j][i] = matrix[i][j];
}
}
return output_matrix;
}
// Print statements
// printsep
void printsep(){
printf("----------");
}
// matrix printer / i think there is no need for implementing a printer for scalars
void matrix_printer(double **print_matrix, int row_num, int col_num){
//defines an epsilon for comparising if number is integer or float
double epsilon = 1/(1048576);
for (int i=0 ; i<row_num; i++){
for (int j=0 ; j<col_num ; j++){
double value = print_matrix[i][j];
int floor_val = (int)(value);
if (value-floor_val>0){
printf("%f\n", value);
}else{
printf("%d\n", floor_val);
}
}
printf("\n" );
}
}
// scalar matrix multiplyer
double ** matrix_scalar(double **m1,int num, int m1_r, int m1_c){
double **output_matrix = malloc(sizeof(double*)*m1_r);
for(int i = 0; i< m1_r; i++){
output_matrix[i] = malloc(sizeof(double)*m1_c);
}
double sop = 0 ;
for (int i = 0; i<m1_r; i++){
for (int j = 0; j<m1_c ; j++){
sop += m1[i][j] * num;
output_matrix[i][j] = sop;
sop = 0;
}
}
return output_matrix;
}