00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00023
00024 #ifndef header_matrix
00025 #define header_matrix
00026
00027 template <int hoejde, int bredde>
00028 class CL_Matrix
00029 {
00030 public:
00031 int hoej() {return hoejde;}
00032 int bred() {return bredde;}
00033 float matrix[hoejde][bredde];
00034
00035 void set_nul()
00036 {
00037 for (int i=0; i<hoejde; i++)
00038 for (int j=0; j<bredde; j++)
00039 matrix[i][j]=(float) 0;
00040 }
00041
00042 void set_enhed()
00043 {
00044 set_nul();
00045 for (int i=0; i < (hoejde<bredde ? hoejde : bredde); i++)
00046 matrix[i][i]=(float) 1;
00047 }
00048
00049 void copy_from(CL_Matrix<hoejde, bredde> &matrix2)
00050 {
00051 for (int i=0; i<hoejde; i++)
00052 for (int j=0; j<bredde; j++)
00053 matrix[i][j] = matrix2.matrix[i][j];
00054 }
00055
00056 void copy_to(CL_Matrix<hoejde, bredde> &matrix2)
00057 {
00058 for (int i=0; i<hoejde; i++)
00059 for (int j=0; j<bredde; j++)
00060 matrix2.matrix[i][j] = matrix[i][j];
00061 }
00062
00063 void mul(CL_Matrix<hoejde, bredde> &matrix2)
00064 {
00065 CL_Matrix<hoejde, bredde> temp_matrix;
00066 temp_matrix.copy_from(*this);
00067 set_nul();
00068
00069 for (int linie=0; linie<hoejde; linie++)
00070 for (int kolone=0; kolone<bredde; kolone++)
00071 for (int element=0; element<bredde; element++)
00072 matrix[linie][kolone] +=
00073 temp_matrix.matrix[linie][element] *
00074 matrix2.matrix[element][kolone];
00075 }
00076 };
00077
00078 template <class A_Matrix, class B_Matrix, class C_Matrix>
00079 void mul(A_Matrix &A, B_Matrix &B, C_Matrix &C)
00080 {
00081 int A_hoejde=A.hoej();
00082 int A_bredde=A.bred();
00083 int B_hoejde=B.hoej();
00084 int B_bredde=B.bred();
00085 int C_hoejde=C.hoej();
00086 int C_bredde=C.bred();
00087 if (A_bredde!=B_hoejde || A_hoejde!=C_hoejde || B_bredde!=C_bredde)
00088 return;
00089
00090 C.set_nul();
00091
00092 for (int A_linie=0; A_linie<A_hoejde; A_linie++)
00093 for (int B_kolone=0; B_kolone<B_bredde; B_kolone++)
00094 for (int element=0; element<A_bredde; element++)
00095 C.matrix[A_linie][B_kolone] +=
00096 A.matrix[A_linie][element] * B.matrix[element][B_kolone];
00097 }
00098
00099 typedef CL_Matrix<4, 4> CL_Matrix4x4;
00100 typedef CL_Matrix<4, 1> CL_Matrix4x1;
00101
00102 #endif