00001 #ifndef EVENTPRIMITIVES_SYMMETRICMATRIXHELPERS_H
00002 #define EVENTPRIMITIVES_SYMMETRICMATRIXHELPERS_H
00003
00004 #include <cmath>
00005
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 bool inverseSym5(Matrix<Scalar, 5,5>& out) const {
00021
00022 if(this == &out || this->cols() != 5 || this->rows() != 5){
00023 return false;
00024 }
00025 const double * a = this->data();
00026
00027
00028 double* b = out.data();
00029 if (a[ 0] == 0. ) {
00030 return false;
00031 }
00032 double x1 = 1./a[ 0],
00033 x2 =-a[ 1]*x1,
00034 x3 =-a[ 2]*x1,
00035 x4 =-a[ 3]*x1,
00036 x5 =-a[ 4]*x1;
00037 if ((b[ 0] = a[ 6]+a[ 1]*x2)==0.){
00038 return false;
00039 }
00040 b[ 1] = a[ 7]+a[ 2]*x2;
00041 b[ 6] = a[12]+a[ 2]*x3;
00042 b[ 2] = a[ 8]+a[ 3]*x2;
00043 b[ 7] = a[13]+a[ 3]*x3;
00044 b[12] = a[18]+a[ 3]*x4;
00045 b[ 3] = a[ 9]+a[ 4]*x2;
00046 b[ 8] = a[14]+a[ 4]*x3;
00047 b[13] = a[19]+a[ 4]*x4;
00048 b[18] = a[24]+a[ 4]*x5;
00049 double y1 = 1./b[ 0],
00050 y2 =-b[ 1]*y1,
00051 y3 =-b[ 2]*y1,
00052 y4 =-b[ 3]*y1,
00053 y5 = x2 *y1;
00054 if ((b[ 0] = b[ 6]+b[ 1]*y2)==0.) {
00055 return false;
00056 }
00057 b[ 1] = b[ 7]+b[ 2]*y2;
00058 b[ 6] = b[12]+b[ 2]*y3;
00059 b[ 2] = b[ 8]+b[ 3]*y2;
00060 b[ 7] = b[13]+b[ 3]*y3;
00061 b[12] = b[18]+b[ 3]*y4;
00062 b[ 3] = x3 +x2 *y2;
00063 b[ 8] = x4 +x2 *y3;
00064 b[13] = x5 +x2 *y4;
00065 b[18] = x1 +x2 *y5;
00066 x2 =-b[ 1]*(x1 = 1./b[ 0]);
00067 x3 =-b[ 2]* x1;
00068 x4 = b[ 3]* x1;
00069 x5 = y2 * x1;
00070 if ((b[ 0] = b[ 6]+b[ 1]*x2)==0.) {
00071 return false;
00072 }
00073 b[ 1] = b[ 7]+b[ 2]*x2;
00074 b[ 6] = b[12]+b[ 2]*x3;
00075 b[ 2] = b[ 8]+b[ 3]*x2;
00076 b[ 7] = b[13]+b[ 3]*x3;
00077 b[12] = b[18]+b[ 3]*x4;
00078 b[ 3] = y3 +y2 *x2;
00079 b[ 8] = y4 +y2 *x3;
00080 b[13] = y5 +y2 *x4;
00081 b[18] = y1 +y2 *x5;
00082 y2 =-b[ 1]*(y1 = 1./b[ 0]);
00083 y3 = b[ 2]* y1;
00084 y4 = b[ 3]* y1;
00085 y5 = x2 * y1;
00086 if ((b[ 0] = b[ 6]+b[ 1]*y2)==0.) {
00087 return false;
00088 }
00089 b[ 1] = b[ 7]+b[ 2]*y2;
00090 b[ 6] = b[12]+b[ 2]*y3;
00091 b[ 2] = b[ 8]+b[ 3]*y2;
00092 b[ 7] = b[13]+b[ 3]*y3;
00093 b[12] = b[18]+b[ 3]*y4;
00094 b[ 3] = x3 +x2 *y2;
00095 b[ 8] = x4 +x2 *y3;
00096 b[13] = x5 +x2 *y4;
00097 b[18] = x1 +x2 *y5;
00098 b[ 4] = b[ 1]*(b[24] = 1./b[ 0]);
00099 b[ 9] = b[ 2]* b[24];
00100 b[14] = b[ 3]* b[24];
00101 b[19] = y2 * b[24];
00102 b[ 0] = b[ 6]+b[ 1]*b[ 4];
00103 b[ 1] = b[ 7]+b[ 2]*b[ 4];
00104 b[ 6] = b[12]+b[ 2]*b[ 9];
00105 b[ 2] = b[ 8]+b[ 3]*b[ 4];
00106 b[ 7] = b[13]+b[ 3]*b[ 9];
00107 b[12] = b[18]+b[ 3]*b[14];
00108 b[ 3] = y3 +y2 *b[ 4];
00109 b[ 8] = y4 +y2 *b[ 9];
00110 b[13] = y5 +y2 *b[14];
00111 b[18] = y1 +y2 *b[19];
00112
00113 b[ 5] = b[ 1];
00114 b[10] = b[ 2];
00115 b[15] = b[ 3];
00116 b[20] = b[ 4];
00117
00118 b[11] = b[ 7];
00119 b[16] = b[ 8];
00120 b[21] = b[ 9];
00121
00122 b[17] = b[13];
00123 b[22] = b[14];
00124
00125 b[23] = b[19];
00126
00127 return true;
00128
00129 }
00130
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> similaritySym5(Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime>& JacMat) const {
00151
00152
00153 if(this->rows() != 5 || this->cols() != 5 || JacMat.rows() != 5 || JacMat.cols() !=5){
00154 return (*this).similarity(JacMat);
00155 }
00156
00157 const double * V = this->data();
00158 const double * Jac = JacMat.data();
00159 double * Vn;
00160 Matrix<Scalar, 5, 5> out;
00161 Vn = out.data();
00162
00163 double a11 = (Jac[ 0]*V[ 0]+Jac[ 5]*V[ 1]+Jac[10]*V[ 2])+(Jac[15]*V[ 3]+Jac[20]*V[ 4]);
00164 double a12 = (Jac[ 0]*V[ 1]+Jac[ 5]*V[ 6]+Jac[10]*V[ 7])+(Jac[15]*V[ 8]+Jac[20]*V[ 9]);
00165 double a13 = (Jac[ 0]*V[ 2]+Jac[ 5]*V[ 7]+Jac[10]*V[12])+(Jac[15]*V[13]+Jac[20]*V[14]);
00166 double a14 = (Jac[ 0]*V[ 3]+Jac[ 5]*V[ 8]+Jac[10]*V[13])+(Jac[15]*V[18]+Jac[20]*V[19]);
00167 double a15 = (Jac[ 0]*V[ 4]+Jac[ 5]*V[ 9]+Jac[10]*V[14])+(Jac[15]*V[19]+Jac[20]*V[24]);
00168
00169 Vn[ 0] = (a11*Jac[ 0]+a12*Jac[ 5]+a13*Jac[10])+(a14*Jac[15]+a15*Jac[20]);
00170
00171 double a21 = (Jac[ 1]*V[ 0]+Jac[ 6]*V[ 1]+Jac[11]*V[ 2])+(Jac[16]*V[ 3]+Jac[21]*V[ 4]);
00172 double a22 = (Jac[ 1]*V[ 1]+Jac[ 6]*V[ 6]+Jac[11]*V[ 7])+(Jac[16]*V[ 8]+Jac[21]*V[ 9]);
00173 double a23 = (Jac[ 1]*V[ 2]+Jac[ 6]*V[ 7]+Jac[11]*V[12])+(Jac[16]*V[13]+Jac[21]*V[14]);
00174 double a24 = (Jac[ 1]*V[ 3]+Jac[ 6]*V[ 8]+Jac[11]*V[13])+(Jac[16]*V[18]+Jac[21]*V[19]);
00175 double a25 = (Jac[ 1]*V[ 4]+Jac[ 6]*V[ 9]+Jac[11]*V[14])+(Jac[16]*V[19]+Jac[21]*V[24]);
00176
00177 Vn[ 1] = (a21*Jac[ 0]+a22*Jac[ 5]+a23*Jac[10])+(a24*Jac[15]+a25*Jac[20]);
00178 Vn[ 6] = (a21*Jac[ 1]+a22*Jac[ 6]+a23*Jac[11])+(a24*Jac[16]+a25*Jac[21]);
00179
00180 double a31 = (Jac[ 2]*V[ 0]+Jac[ 7]*V[ 1]+Jac[12]*V[ 2])+(Jac[17]*V[ 3]+Jac[22]*V[ 4]);
00181 double a32 = (Jac[ 2]*V[ 1]+Jac[ 7]*V[ 6]+Jac[12]*V[ 7])+(Jac[17]*V[ 8]+Jac[22]*V[ 9]);
00182 double a33 = (Jac[ 2]*V[ 2]+Jac[ 7]*V[ 7]+Jac[12]*V[12])+(Jac[17]*V[13]+Jac[22]*V[14]);
00183 double a34 = (Jac[ 2]*V[ 3]+Jac[ 7]*V[ 8]+Jac[12]*V[13])+(Jac[17]*V[18]+Jac[22]*V[19]);
00184 double a35 = (Jac[ 2]*V[ 4]+Jac[ 7]*V[ 9]+Jac[12]*V[14])+(Jac[17]*V[19]+Jac[22]*V[24]);
00185
00186 Vn[ 2] = (a31*Jac[ 0]+a32*Jac[ 5]+a33*Jac[10])+(a34*Jac[15]+a35*Jac[20]);
00187 Vn[ 7] = (a31*Jac[ 1]+a32*Jac[ 6]+a33*Jac[11])+(a34*Jac[16]+a35*Jac[21]);
00188 Vn[12] = (a31*Jac[ 2]+a32*Jac[ 7]+a33*Jac[12])+(a34*Jac[17]+a35*Jac[22]);
00189
00190 double a41 = (Jac[ 3]*V[ 0]+Jac[ 8]*V[ 1]+Jac[13]*V[ 2])+(Jac[18]*V[ 3]+Jac[23]*V[ 4]);
00191 double a42 = (Jac[ 3]*V[ 1]+Jac[ 8]*V[ 6]+Jac[13]*V[ 7])+(Jac[18]*V[ 8]+Jac[23]*V[ 9]);
00192 double a43 = (Jac[ 3]*V[ 2]+Jac[ 8]*V[ 7]+Jac[13]*V[12])+(Jac[18]*V[13]+Jac[23]*V[14]);
00193 double a44 = (Jac[ 3]*V[ 3]+Jac[ 8]*V[ 8]+Jac[13]*V[13])+(Jac[18]*V[18]+Jac[23]*V[19]);
00194 double a45 = (Jac[ 3]*V[ 4]+Jac[ 8]*V[ 9]+Jac[13]*V[14])+(Jac[18]*V[19]+Jac[23]*V[24]);
00195
00196 Vn[ 3] = (a41*Jac[ 0]+a42*Jac[ 5]+a43*Jac[10])+(a44*Jac[15]+a45*Jac[20]);
00197 Vn[ 8] = (a41*Jac[ 1]+a42*Jac[ 6]+a43*Jac[11])+(a44*Jac[16]+a45*Jac[21]);
00198 Vn[13] = (a41*Jac[ 2]+a42*Jac[ 7]+a43*Jac[12])+(a44*Jac[17]+a45*Jac[22]);
00199 Vn[18] = (a41*Jac[ 3]+a42*Jac[ 8]+a43*Jac[13])+(a44*Jac[18]+a45*Jac[23]);
00200
00201 double a51 = (Jac[ 4]*V[ 0]+Jac[ 9]*V[ 1]+Jac[14]*V[ 2])+(Jac[19]*V[ 3]+Jac[24]*V[ 4]);
00202 double a52 = (Jac[ 4]*V[ 1]+Jac[ 9]*V[ 6]+Jac[14]*V[ 7])+(Jac[19]*V[ 8]+Jac[24]*V[ 9]);
00203 double a53 = (Jac[ 4]*V[ 2]+Jac[ 9]*V[ 7]+Jac[14]*V[12])+(Jac[19]*V[13]+Jac[24]*V[14]);
00204 double a54 = (Jac[ 4]*V[ 3]+Jac[ 9]*V[ 8]+Jac[14]*V[13])+(Jac[19]*V[18]+Jac[24]*V[19]);
00205 double a55 = (Jac[ 4]*V[ 4]+Jac[ 9]*V[ 9]+Jac[14]*V[14])+(Jac[19]*V[19]+Jac[24]*V[24]);
00206
00207 Vn[ 4] = (a51*Jac[ 0]+a52*Jac[ 5]+a53*Jac[10])+(a54*Jac[15]+a55*Jac[20]);
00208 Vn[ 9] = (a51*Jac[ 1]+a52*Jac[ 6]+a53*Jac[11])+(a54*Jac[16]+a55*Jac[21]);
00209 Vn[14] = (a51*Jac[ 2]+a52*Jac[ 7]+a53*Jac[12])+(a54*Jac[17]+a55*Jac[22]);
00210 Vn[19] = (a51*Jac[ 3]+a52*Jac[ 8]+a53*Jac[13])+(a54*Jac[18]+a55*Jac[23]);
00211 Vn[24] = (a51*Jac[ 4]+a52*Jac[ 9]+a53*Jac[14])+(a54*Jac[19]+a55*Jac[24]);
00212
00213 Vn[ 5] = Vn[ 1];
00214 Vn[10] = Vn[ 2];
00215 Vn[15] = Vn[ 3];
00216 Vn[20] = Vn[ 4];
00217
00218 Vn[11] = Vn[ 7];
00219 Vn[16] = Vn[ 8];
00220 Vn[21] = Vn[ 9];
00221
00222 Vn[17] = Vn[13];
00223 Vn[22] = Vn[14];
00224
00225 Vn[23] = Vn[19];
00226
00227 return out;
00228 }
00229
00230 #endif