对于我的矩阵类,我想在range-v3视图上进行某种运算符重载(可能使用表达式模板)来实现+ - / * %
。例如,如果我想获得两个列之和的视图,我想写成
col_1 + col_2
取而代之
rv::zip_with([](auto c1, auto c2) {return c1 + c2;}, col_1, col_2);
可能可以从这篇论文中得到一些想法,以避免构造过多的临时变量。这是我想编写的代码:
//simple example
//what I want to write
auto rangeview = col_1 + col_2;
//what I can write
auto rangeview = rv::zip_with([](auto c1, auto c2) {
return c1 + c2;
}, col_1, col_2);
//itermediate
//what I want to write
auto rangeview = col_1 + col_2 + col_3;
//what I can write
auto rangeview = rv::zip_with([](auto c1, auto c2, auto c3) {
return c1 + c2 + c3;
}, col_1, col_2, col_3);
//advanced
//what I want to write
auto rangeview = 10*col_1 + 20*col_2 - 30*col_3;
//what I can write
auto rangeview = rv::zip_with([](auto c1, auto c2, auto c3) {
return 10.0*c1 + 20.0*c2 - 30.0*c3;
}, col_1, col_2, col_3);
//more advanced with elementwise multiplication
//what I want to write
auto rangeview = 10*col_1 + 20*col_2 - col_2 % col_3;
//what I can write
auto rangeview = rv::zip_with([](auto c1, auto c2, auto c3) {
return 10.0*c1 + 20.0*c2 - c2*c3;
}, col_1, col_2, col_3);