-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMatrixProduct.cpp
More file actions
49 lines (43 loc) · 1.33 KB
/
Copy pathMatrixProduct.cpp
File metadata and controls
49 lines (43 loc) · 1.33 KB
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
/*************************************************************************
> File Name: MatrixProduct.cpp
> Author: Netcan
> Blog: https://netcan.github.io/
> Mail: netcan1996@gmail.com
> Created Time: 2021-08-16 20:24
************************************************************************/
#include <vector>
#include <numeric>
#include <iostream>
#include "Views.h"
using namespace std;
auto product(ranges::viewable_range auto&& lhs, ranges::viewable_range auto&& rhs) {
return lhs | views::transform([=](auto&& xrow) mutable {
return rhs | transpose | views::transform([=](auto&& wcol) {
return std::inner_product(xrow.begin(), xrow.end(),
wcol.begin(), 0);
});
});
};
int main(int argc, char** argv) {
{
auto X = views::iota(1, 2 * 3 + 1) | chunk(3);
auto W = views::iota(1, 2 * 3 + 1) | chunk(2);
// X * W
print_range(product(X, W));
cout << "\n";
}
{
vector X { // 3 x 4
vector{3, 1, 1, 4},
vector{5, -3, 2, 1},
vector{6, 2, -9, 5},
};
vector W { // 4 x 2
vector{4, 9}, vector{6, -8},
vector{9, 7}, vector{7, 6},
};
print_range(product(X, W), true);
cout << "\n";
}
return 0;
}