-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.hpp
64 lines (56 loc) · 1.58 KB
/
matrix.hpp
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
#include <iostream>
/**
* @brief initialize a matrix of size n * m.
* The matrix memory layout is a 1D array of size n * m in row-wise order.
*
* @param n
* @param m
* @return int**
*/
int** new_matrix(int n, int m) {
int *arr = new int[n * m];
std::fill(arr, arr + n * m, 0);
int** matrix = new int*[n];
for (int i = 0; i < n; ++i) {
*(matrix + i) = arr;
arr += m;
}
return matrix;
}
void matrix_print(int** a, int n, int m) {
std::cout << "Matrix:" << n << ' ' << m << '\n';
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
std:: cout << a[i][j] << " \n"[j == m - 1];
}
}
}
void delete_matrix(int** matrix) {
delete[] *matrix;
delete[] matrix;
}
void matrix_add(int** a, int** b, int** c, int n, int m) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) c[i][j] = a[i][j] + b[i][j];
}
}
void matrix_sub(int** a, int** b, int** c, int n, int m) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) c[i][j] = a[i][j] - b[i][j];
}
}
void matrix_multiply(int** a, int** b, int** c, int n, int m, int p) {
for (int i = 0; i < n; ++i) {
for (int k = 0; k < m; ++k) {
for (int j = 0; j < p; ++j) c[i][j] += a[i][k] * b[k][j];
}
}
}
void init_sub_matrix(int** a, int** a1, int n, int m, int sr, int sc) {
int n1 = (n + 1) / 2, m1 = (m + 1) / 2;
for (int i = sr; i < std::min(sr + n1, n); ++i) {
for (int j = sc; j < std::min(sc + m1, m); ++j) {
a1[i - sr][j - sc] = a[i][j];
}
}
}