-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmath.cpp
62 lines (57 loc) · 1.65 KB
/
vmath.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
52
53
54
55
56
57
58
59
60
61
62
#include<vector>
#include "vmath.h"
std::vector<std::vector<long double>> Vmath::dot(std::vector<std::vector<long double>> &a, std::vector<std::vector<long double>> &b){
std::vector<std::vector<long double>> pr;
if(a[0].size() == b.size()){
int r = a.size(), c = b[0].size();
for(int i = 0; i < r; i++){
std::vector<long double> row;
for(int j = 0; j < c; j++){
long double sum = 0;
for(int k = 0; k < a[0].size(); k++)
sum += a[i][k] * b[k][j];
row.push_back(sum);
}
pr.push_back(row);
}
return pr;
}
return pr;
}
void Vmath::transpose(std::vector<std::vector<long double>> &A,std::vector<std::vector<long double>> &T){
int r = A.size(),c = A[0].size();
for(int j = 0; j < c; j++){
std::vector<long double> t;
for(int i = 0; i < r; i++)
t.push_back(A[i][j]);
T.push_back(t);
}
}
void Vmath::multiply(std::vector<std::vector<long double>> &a,std::vector<std::vector<long double>> &b,std::vector<std::vector<long double>> &re){
int r = a.size(), c = a[0].size();
for(int i = 0; i < r; i++){
std::vector<long double> t;
for(int j = 0; j < c; j++)
t.push_back(a[i][j]*b[i][j]);
re.push_back(t);
}
}
void Vmath::power(std::vector<std::vector<long double>> &A, int n){
int r = A.size();
int c = A[0].size();
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++)
A[i][j] = A[i][j]*A[i][j];
}
}
void Vmath::rowSum(std::vector<std::vector<long double>> &A,std::vector<std::vector<long double>> &b,int m){
int r = A.size(), c = A[0].size();
std::vector<long double> t;
for(int i = 0; i < r; i++){
long double sum = 0;
for(int j = 0; j < c; j++)
sum += A[i][j];
t.push_back(sum/m);
}
b.push_back(t);
}