20 #include <gsl/gsl_math.h> 25 #define M(matrix, rows, col) static gsl_matrix*matrix=0; if(!matrix) matrix = gsl_matrix_alloc(rows,col); 30 const double*x0,
const double *cov_x0,
33 M(bigM, 4,4);
M(g, 4,1);
M(bigM_k,2,4);
34 M(bigM_k_t,4,2);
M(C_k,2,2);
M(q_k, 2,1);
35 M(temp41, 4,1);
M(temp22,2,2);
M(temp22b,2,2);
36 M(temp42, 4,2);
M(temp44,4,4);
M(temp21, 2,1);
37 M(temp22c, 2,2);
M(temp12,1,2);
39 gsl_matrix_set_zero(bigM);
40 gsl_matrix_set_zero(g);
41 gsl_matrix_set_zero(temp42);
43 double d_bigM[4][4] = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}};
44 double d_g[4] = {0, 0, 0, 0};
47 if(!c[k].valid)
continue;
49 double C00 = c[k].
C[0][0];
50 double C01 = c[k].
C[0][1];
51 double C10 = c[k].
C[1][0];
52 double C11 = c[k].
C[1][1];
55 fprintf(stderr,
"k=%d; I expect C to be a symmetric matrix.\n", k);
59 double det = C00*C11 - C01*C10;
60 double trace = C00 + C11;
61 int is_semidef_pos = (det >= 0) && (trace>0);
63 fprintf(stderr,
"k=%d; I expect the matrix to be semidef positive (det>=0 and trace>0), det = %.20f trace= %.10f C = [%.15f,%.15f;%.15f %.15f]\n",
64 k, det, trace, C00,C01,C10,C11);
69 double qx = c[k].
q[0];
70 double qy = c[k].
q[1];
71 double px = c[k].
p[0];
72 double py = c[k].
p[1];
77 d_bigM[0][2] += +px * C00 + py * C01;
78 d_bigM[0][3] += -py * C00 + px * C01;
83 d_bigM[1][2] += +px * C10 + py * C11;
84 d_bigM[1][3] += +px * C11 - py * C10;
91 d_bigM[2][0] += px * C00 + py * C10;
92 d_bigM[2][1] += px * C01 + py * C11;
93 d_bigM[2][2] += (px*px)*(+C00) + (px*py)*(+C10+C01) + (py*py)*(+C11);
94 d_bigM[2][3] += (px*px)*(+C01) + (px*py)*(-C00+C11) + (py*py)*(-C10);
100 d_bigM[3][0] += -py * C00 + px * C10;
101 d_bigM[3][1] += -py * C01 + px * C11;
102 d_bigM[3][2] += (px*px)*(+C10) + (px*py)*(-C00+C11) + (py*py)*(-C01);
103 d_bigM[3][3] += (px*px)*(+C11) + (px*py)*(-C10-C01) + (py*py)*(+C00);
105 d_g[0] += C00*qx+C10*qy;
106 d_g[1] += C01*qx+C11*qy;
107 d_g[2] += qx * (C00*px+C01*py) + qy * (C10*px+C11*py);
108 d_g[3] += qx * (C00*(-py)+C01*px) + qy * (C10*(-py)+C11*px);
115 *gsl_matrix_ptr(g, a, 0) = -2 * d_g[a];
118 gsl_matrix_set(bigM, a, b, 2 * d_bigM[a][b]);
122 M(mA,2,2);
gms(mA,0,0,
gmg(bigM,0,0));
gms(mA,0,1,
gmg(bigM,0,1));
123 gms(mA,1,0,
gmg(bigM,1,0));
gms(mA,1,1,
gmg(bigM,1,1));
124 M(mB,2,2);
gms(mB,0,0,
gmg(bigM,0,2));
gms(mB,0,1,
gmg(bigM,0,3));
125 gms(mB,1,0,
gmg(bigM,1,2));
gms(mB,1,1,
gmg(bigM,1,3));
126 M(mD,2,2);
gms(mD,0,0,
gmg(bigM,2,2));
gms(mD,0,1,
gmg(bigM,2,3));
127 gms(mD,1,0,
gmg(bigM,3,2));
gms(mD,1,1,
gmg(bigM,3,3));
129 M(mS,2,2);
M(mSa,2,2);
136 m_mult(temp22b, mB, temp22c);
139 m_mult(temp22, temp22c, temp22b);
141 m_add(mD,temp22b,mS);
155 M(g1,2,1);
M(g2,2,1);
156 M(g1t,1,2);
M(g2t,1,2);
157 M(mAi,2,2);
M(mBt,2,2);
168 M(m1t,1,2);
M(m1,2,1);
169 M(m2t,1,2);
M(m2,2,1);
170 M(m3t,1,2);
M(
m3,2,1);
189 double l[3] = {
m_det(mS), 2*
gmg(mS,0,0)+2*
gmg(mS,1,1), 4};
192 double q[5] = {p[0]-(l[0]*l[0]), p[1]-(2*l[1]*l[0]),
193 p[2]-(l[1]*l[1]+2*l[0]*l[2]), -(2*l[2]*l[1]), -(l[2]*l[2])};
196 fprintf(stderr,
"p = %f %f %f \n", p[2], p[1], p[0]);
197 fprintf(stderr,
"l = %f %f %f \n", l[2], l[1], l[0]);
198 fprintf(stderr,
"q = %f %f %f %f %f \n", q[4], q[3], q[2], q[1], q[0]);
253 M(W,4,4); gsl_matrix_set_zero(W);
gms(W,2,2,1.0);
gms(W,3,3,1.0);
257 gsl_matrix_add(bigM,W);
262 x_out[0] =
gmg(x,0,0);
263 x_out[1] =
gmg(x,1,0);
264 x_out[2] = atan2(
gmg(x,3,0),
gmg(x,2,0));
267 fprintf(stderr,
"x = %f %f %f deg\n", x_out[0], x_out[1],x_out[2]*180/
M_PI);
273 MF(bigM);
MF(g);
MF(bigM_k);
274 MF(bigM_k_t);
MF(C_k);
MF(q_k);
275 MF(temp42);
MF(temp44);
MF(temp21);
276 MF(temp41);
MF(temp22);
MF(temp22b);
277 MF(temp22c);
MF(temp12);
286 double c = cos(x[2]);
287 double s = sin(x[2]);
289 e[0] = c*(co->
p[0]) -s*(co->
p[1]) + x[0] - co->
q[0];
290 e[1] = s*(co->
p[0]) +c*(co->
p[1]) + x[1] - co->
q[1];
291 double this_error = e[0]*e[0]*co->
C[0][0]+2*e[0]*e[1]*co->
C[0][1]+e[1]*e[1]*co->
C[1][1];
295 fprintf(stderr,
"Something fishy: error = %lf e = [%lf %lf] C = [%lf,%lf;%lf,%lf]\n", this_error,
296 e[0],e[1],co->
C[0][0],co->
C[0][1],co->
C[1][0],co->
C[1][1]);
303 for(
int i=0;i<n;i++) {
304 if(!co[i].valid)
continue;
309 fprintf(stderr,
"Something fishy!\n");
void m_trans(const gsl_matrix *A, gsl_matrix *A_t)
void m_scale(double m, gsl_matrix *A)
void m_display(const char *str, gsl_matrix *m)
int poly_greatest_real_root(unsigned int n, const double *a, double *root)
void m_add(const gsl_matrix *A, const gsl_matrix *B, gsl_matrix *ApB)
void m_inv(const gsl_matrix *A, gsl_matrix *invA)
#define M(matrix, rows, col)
double m_det(const gsl_matrix *A)
void m_mult(const gsl_matrix *A, const gsl_matrix *B, gsl_matrix *AB)
int gpc_solve(int K, const struct gpc_corr *c, const double *x0, const double *cov_x0, double *x_out)
double gpc_total_error(const struct gpc_corr *co, int n, const double *x)
double gpc_error(const struct gpc_corr *co, const double *x)
double m_dot(const gsl_matrix *A, const gsl_matrix *B)