-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorm.cpp
51 lines (47 loc) · 1.22 KB
/
norm.cpp
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
#include "norm.hpp"
#include <math.h>
#include <iostream>
using namespace std;
double norm(double* mat, int n, int m) {
double max = 0, sum = 0;
for (int j = 0; j < m; ++j) {
sum = 0;
for (int i = 0; i < n; ++i) {
sum += fabs(mat[i * m + j]);
}
if (sum > max) max = sum;
}
return max;
}
double E_norm(double* mat, int n, int m) { // the Euclidean norm of mat - E
double ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (i == j)
{
ans += (mat[i*m + j]-1)*(mat[i*m + j]-1);
}
else
{
ans += mat[i*m + j]*mat[i*m + j];
}
}
}
return sqrt(ans);
}
void substract(double* mat1, double* mat2, double* res, int n, int m) {
for (int i = 0; i < n * m; ++i) res[i] = mat1[i] - mat2[i];
}
void mult(double* mat1, double* mat2, double* res, int n, int m, int k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
double c = 0;
for (int r = 0; r < m; r++) {
c += mat1[i * m + r] * mat2[r * k + j];
}
res[i * k + j] = c;
}
}
}