forked from pmkravets/strassen-winograd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c
193 lines (175 loc) · 4.31 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
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
//
// matrix.c
// strassen-winograd
//
// Created by Pavel Kravets on 03.10.13.
// Copyright (c) 2013 Pavel Kravets. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
//#include <time.h>
#include "matrix.h"
void zero_matrix(unsigned int rows, unsigned int cols, matrix* m)
{
m->matrix = (double*) malloc(rows * cols * sizeof(double));
m->rowNum = rows;
m->colNum = cols;
m->offset = 0;
m->strand = 0;
m->skip = 0;
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
set_element(m, i, j, .0);
}
}
}
void random_matrix(unsigned int rows, unsigned int cols, matrix* m)
{
//srand((unsigned int) time(NULL));
srand(123456);
m->matrix = (double*) malloc(rows * cols * sizeof(double));
m->rowNum = rows;
m->colNum = cols;
m->offset = 0;
m->strand = 0;
m->skip = 0;
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
set_element(m, i, j, (double) rand() / RAND_MAX);
}
}
}
void eye(unsigned int size, matrix* m)
{
m->matrix = (double*) malloc(size * size * sizeof(double));
m->rowNum = size;
m->colNum = size;
m->offset = 0;
m->strand = 0;
m->skip = 0;
for (int i=0; i<size; i++)
{
for (int j=0; j<size; j++)
{
if (i==j)
{
set_element(m, i, j, 1.);
}
else
{
set_element(m, i, j, .0);
}
}
}
}
void print_matrix(matrix* m)
{
int rows = m->rowNum;
int cols = m->colNum;
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
printf("%.3f ", element(m, i, j));
}
printf("\n");
}
}
void add_matrices(matrix* m1, matrix* m2, matrix* res)
{
if (m1->rowNum != m2->rowNum || m1->colNum != m2->colNum)
{
printf("Matrices can not be added");
return;
}
if (!res->matrix) {
res->matrix = (double*) malloc(m1->rowNum * m1->colNum * sizeof(double));
res->rowNum = m1->rowNum;
res->colNum = m2->colNum;
res->offset = 0;
res->strand = 0;
res->skip = 0;
}
for (int i=0; i<m1->rowNum; i++)
{
for (int j=0; j<m1->colNum; j++)
{
set_element(res, i, j, element(m1, i, j) + element(m2, i, j));
}
}
}
void subtract_matrices(matrix* m1, matrix* m2, matrix* res)
{
if (m1->rowNum != m2->rowNum || m1->colNum != m2->colNum)
{
printf("Matrices can not be subtracted");
return;
}
if (!res->matrix) {
res->matrix = (double*) malloc(m1->rowNum * m1->colNum * sizeof(double));
res->rowNum = m1->rowNum;
res->colNum = m2->colNum;
res->offset = 0;
res->strand = 0;
res->skip = 0;
}
for (int i=0; i<m1->rowNum; i++)
{
for (int j=0; j<m1->colNum; j++)
{
set_element(res, i, j, element(m1, i, j) - element(m2, i, j));
}
}
}
double element(matrix* m, unsigned int i, unsigned int j)
{
return m->matrix[m->offset + i*(m->colNum + m->skip) + j];
}
void set_element(matrix* m, unsigned int i, unsigned int j, double element)
{
m->matrix[m->offset + i*(m->colNum + m->skip) + j] = element;
}
void add_to_element(matrix* m, unsigned int i, unsigned int j, double element)
{
m->matrix[m->offset + i*(m->colNum + m->skip) + j] += element;
}
void submatrix(matrix* m, unsigned int rowNum, unsigned int colNum, unsigned int offset, unsigned int strand, unsigned int skip, matrix* res)
{
res->matrix = m->matrix;
res->rowNum = rowNum;
res->colNum = colNum;
res->offset = offset + m->offset;
res->strand = strand;
res->skip = skip + m->skip;
}
void copy_matrix(matrix* src, matrix* dest)
{
unsigned int row, col;
row = src->rowNum < dest->rowNum ? src->rowNum : dest->rowNum;
col = src->colNum < dest->colNum ? src->colNum : dest->colNum;
for (int i = 0; i<row; i++)
{
for (int j = 0; j<col; j++)
{
set_element(dest, i, j, element(src, i, j));
}
}
}
matrix* alloc_matrix()
{
matrix* m = malloc(sizeof(matrix));
m->matrix = NULL;
m->offset = 0;
m->skip = 0;
m->strand = 0;
return m;
}
void free_matrix(matrix* m)
{
free(m->matrix);
free(m);
}