Skip to content

Instantly share code, notes, and snippets.

@adler3d
Last active June 27, 2024 16:43
Show Gist options
  • Save adler3d/a664818673075b62a37ff803f2682e43 to your computer and use it in GitHub Desktop.
Save adler3d/a664818673075b62a37ff803f2682e43 to your computer and use it in GitHub Desktop.
умножение матрицы на вектор в с++ используя std::array и шаблоны
#include <iostream>
#include <array>
using namespace std;
template<class real,size_t h,size_t w>
void mul(const array<array<real,w>,h>&m,const array<real,w>&v,array<real,h>&out){
for(size_t y=0;y<h;y++){
auto&s=out[y];s={};
for(size_t x=0;x<w;x++){
s+=v[x]*m[y][x];
}
}
}
int main(){
typedef array<double,3> t_vec;
array<t_vec,2> m={
t_vec{0,1,3},
t_vec{4,5,6}
};
array<double,3> v={10,11,12};
array<double,2> result{};
mul(m,v,result);
for(auto&x:result)cout<<" "<<x;
cout<<endl; // 47 167
return 0;
}
@adler3d
Copy link
Author

adler3d commented Jun 27, 2024

array<t_vec,3> m={
  t_vec{+3,+2,-1},
  t_vec{+2,-2,+4},
  t_vec{-1,0.5,-1}
};
t_vec point={1,-2,0};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment