-
Notifications
You must be signed in to change notification settings - Fork 0
/
primal.cpp
45 lines (36 loc) · 955 Bytes
/
primal.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
#include "primal.h"
#include <cstddef>
#include <cassert>
double square(double x) {
return x * x;
}
double add(const double& x, const double& y) {
return x + y;
}
double sub(const double* x, const double* y) {
return (*x) - (*y);
}
double mul(double x, double y) {
return x * y;
}
void div(const double& x, const double& y, double& result) {
result = x / y;
}
double dot(const std::vector<double>& c, const std::vector<double>& x) {
assert(c.size() == x.size());
double y = 0;
for(size_t i = 0; i < c.size(); ++i) {
y += c[i] * x[i];
}
return y;
}
void gemv(double alpha,
const std::vector<std::vector<double>>& A,
const std::vector<double>& x,
double beta,
std::vector<double>& y) {
assert(A.size() == y.size() && A.size() > 1 && A[0].size() == x.size());
for(size_t i = 0; i < A.size(); ++i) {
y[i] = alpha * dot(A[i], x) + beta * y[i];
}
}