|
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 |
template < int dim> class matrix { protected : enum {halfdim=dim/2}; matrix<halfdim> a,b,c,d; public : matrix<dim>() { } matrix<dim>(matrix<halfdim> _a, matrix<halfdim> _b, matrix<halfdim> _c, matrix<halfdim> _d) { a=_a; b=_b; c=_c; d=_d; } }; template <> class matrix<2> { protected : double a,b,c,d; public : matrix<2>() { } matrix<2>( double _a, double _b, double _c, double _d) { a=_a; b=_b; c=_c; d=_d; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
matrix<dim> operator+(matrix<dim> &t) { return matrix<dim>(a+t.a, b+t.b, c+t.c, d+t.d); } matrix<dim> operator-(matrix<dim> &t) { return matrix<dim>(a-t.a, b-t.b, c-t.c, d-t.d); } matrix<dim> operator-() { return matrix<dim>(-a, -b, -c, -d); } matrix<dim> operator*(matrix<dim> &t) { return matrix<dim>(a*t.a + b*t.c, a*t.b + b*t.d, c*t.a + d*t.c, c*t.b + d*t.d); } |
1 2 3 4 |
static matrix<dim> zero; //이 놈은 template<int dim> class matrix안에 static matrix<2> zero; //이 놈은 class matrix<2>안에 들어가야한다. |
1 2 3 4 5 6 7 |
template < int dim> matrix<dim> matrix<dim>::zero=matrix<dim> (matrix<halfdim>::zero, matrix<halfdim>::zero, matrix<halfdim>::zero, matrix<halfdim>::zero); //이 정의는 템플릿과 같은 헤더에 들어가있어야하지만, matrix<2> matrix<2>::zero=matrix<2>(0.0, 0.0, 0.0, 0.0); //이 정의는 소스파일에 들어가있어야 중복정의되었다는 에러가 안 뜬다. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
bool has_inverse() { return a*d!=b*c; } double det() { return a*d-b*c; } matrix<2> inverse() { double dt=det(); if (dt!=0) { return matrix<2>(d/dt, -b/dt, -c/dt, a/dt); } return zero; } |
if(ptr)ptr=NULL,return; 은 왜 안될까 (쉼표 연산자 이야기) (1) | 2009.09.13 |
---|---|
템플릿과 역행렬 3. 2^n차가 아닌 행렬 (3) | 2009.06.28 |
템플릿과 역행렬 2. 본격 역행렬 구하기 (0) | 2009.06.28 |
이진-최대공약수 알고리즘. (0) | 2008.12.20 |
역삼각함수를 구현해보자. (0) | 2008.12.08 |
삼각함수를 구현해보자. (0) | 2008.12.07 |
댓글 영역