Skip to content

Instantly share code, notes, and snippets.

@hjroh0315
Created January 23, 2022 11:17
Show Gist options
  • Select an option

  • Save hjroh0315/ef4066b4e8f75825fa25aa11d5e0d588 to your computer and use it in GitHub Desktop.

Select an option

Save hjroh0315/ef4066b4e8f75825fa25aa11d5e0d588 to your computer and use it in GitHub Desktop.
엉엉 우리 Matrix 구조체가 일하고 있어요 너무 자랑스럽네요
#include<iostream>
#include<valarray>
using namespace std;
template<class T>
struct Matrix
{
valarray<T> data;
int dim,s;
Matrix(int R, int C):data(R*C),dim(C),s(R){}
Matrix(int R, int C, initializer_list<T> il):data(R*C),dim(C),s(R){data=il;}
T&operator()(int R, int C){return data[R*dim+C];}
slice_array<T> row(int r){return data[slice(r*dim,dim,1)];}
slice_array<T> col(int c){return data[slice(c,dim,dim)];}
Matrix<T> operator*(Matrix<T>& m)
{
valarray<T> r(dim),c(m.s);
Matrix A(s,m.dim);
for(int i=0;i<s;i++)
for(int j=0;j<m.dim;j++)
{
r=col(i);
c=m.row(j);
A(j,i)=(r*c).sum();
}
return A;
}
Matrix<T>& operator*=(Matrix<T>& m)
{
(*this)=(*this)*m;
return *this;
}
};
int main()
{
Matrix a(2,2,{1,1,1,0});
Matrix b=a;
for(int i=0;i<10;i++)
{
cout<<b(0,0) << " " << b(0,1) <<endl;
cout<<b(1,0) << " " << b(1,1) <<endl;
b*=a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment