This repository has been archived by the owner on May 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.c
82 lines (73 loc) · 2.03 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
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
void transpose(double mat[], size_t lines, size_t cols, double res[])
{
//double res[lines*cols] = {0};
for (size_t i = 0; i < lines; i++) {
for (size_t j = 0; j < cols; j++) {
res[i+j*lines] = mat[j+i*cols];
}
}
}
void scalar(double mat[], double scal, size_t lines, size_t cols, double res[])
{
for (size_t i = 0; i < lines; i++) {
for (size_t j = 0; j < cols; j++) {
res[j+i*lines] = mat[j+i*cols] * scal;
}
}
}
void dot(double m1[], double m2[], size_t lines, size_t cols, double res[])
{
//double res[lines*cols] = {0};
for (size_t i = 0; i < lines; i++) {
for (size_t j = 0; j < cols; j++) {
res[j+i*lines] = m1[j+i*cols] * m2[j+i*cols];
}
}
}
void add(double mat1[], double mat2[], size_t lines, size_t cols, double res[])
{
for (size_t i = 0; i < lines; i++) {
for (size_t j = 0; j < cols; j++) {
res[j+i*cols] = mat1[j+i*cols] + mat2[j+i*cols];
}
}
}
void substract(double mat1[], double mat2[], size_t lines, size_t cols, double res[])
{
for (size_t i = 0; i < lines; i++) {
for (size_t j = 0; j < cols; j++) {
res[j+i*cols] = mat1[j+i*cols] - mat2[j+i*cols];
}
}
}
void mul(double m1[], double m2[], size_t n, size_t m, size_t p, double res[])
{
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < p; j++) {
res[j+i*p] = 0;
for (size_t k = 0; k < m; k++) {
res[j+i*p] += m1[k+i*m] * m2[j+k*p];
}
}
}
}
void print_matrix(double mat[], size_t lines, size_t cols)
{
for (size_t i = 0; i < lines; i++) {
for (size_t j = 0; j < cols; j++) {
if (mat[j+i*cols] > 0.5) {
mat[j+i*cols] = 0;
//printf("1");
}
else {
mat[j+i*cols] = 1;
//printf("0");
//printf("%4g ", mat[j+i*cols]);
}
}
//printf("\n");
}
}