94 Eigen::MatrixXd matPnt,
95 Eigen::MatrixXd matOri)
99 Eigen::MatrixXd r, r2, r5, x, y, z, mx, my, mz, Tx, Ty, Tz, lf;
101 iNchan = matPnt.rows();
104 matPnt.array().col(0) -=matPos(0);
105 matPnt.array().col(1) -=matPos(1);
106 matPnt.array().col(2) -=matPos(2);
108 r = matPnt.array().square().rowwise().sum().sqrt();
110 r2 = r5 = x = y = z = mx = my = mz = Tx = Ty = Tz = lf = Eigen::MatrixXd::Zero(iNchan,3);
112 for(
int i = 0;i < iNchan;i++) {
113 r2.row(i).array().fill(pow(r(i),2));
114 r5.row(i).array().fill(pow(r(i),5));
117 for(
int i = 0;i < iNchan;i++) {
118 x.row(i).array().fill(matPnt(i,0));
119 y.row(i).array().fill(matPnt(i,1));
120 z.row(i).array().fill(matPnt(i,2));
123 mx.col(0).array().fill(1);
124 my.col(1).array().fill(1);
125 mz.col(2).array().fill(1);
127 Tx = 3 * x.cwiseProduct(matPnt) - mx.cwiseProduct(r2);
128 Ty = 3 * y.cwiseProduct(matPnt) - my.cwiseProduct(r2);
129 Tz = 3 * z.cwiseProduct(matPnt) - mz.cwiseProduct(r2);
131 for(
int i = 0;i < iNchan;i++) {
132 lf(i,0) = Tx.row(i).dot(matOri.row(i));
133 lf(i,1) = Ty.row(i).dot(matOri.row(i));
134 lf(i,2) = Tz.row(i).dot(matOri.row(i));
137 for(
int i = 0;i < iNchan;i++) {
138 for(
int j = 0;j < 3;j++) {
139 lf(i,j) = u0 * lf(i,j)/(4 *
M_PI * r5(i,j));
208 const Eigen::MatrixXd& matData,
209 const Eigen::MatrixXd& matProjectors,
213 double tolx, tolf, rho, chi, psi, sigma, func_evals, usual_delta, zero_term_delta, temp1, temp2;
214 std::string header, how;
215 int n, itercount, prnt;
216 Eigen::MatrixXd onesn, two2np1, one2n, v, y, v1, tempX1, tempX2, xbar, xr, x, xe, xc, xcc, xin,posCopy;
217 std::vector <double> fv, fv1;
218 std::vector <int> idx;
232 header =
" Iteration Func-count min f(x) Procedure";
239 rho = 1; chi = 2; psi = 0.5; sigma = 0.5;
240 onesn = Eigen::MatrixXd::Ones(1,n);
241 two2np1 = one2n = Eigen::MatrixXd::Zero(1,n);
243 for(
int i = 0;i < n;i++) {
248 v = v1 = Eigen::MatrixXd::Zero(n, n+1);
253 for(
int i = 0;i < n; i++) {
257 tempdip =
dipfitError(posCopy, matData, sensors, matProjectors);
258 fv[0] = tempdip.
error;
267 zero_term_delta = 0.00025;
268 xin = posCopy.transpose();
270 for(
int j = 0;j < n;j++) {
274 y(j) = (1 + usual_delta) * y(j);
276 y(j) = zero_term_delta;
279 v.col(j+1).array() = y;
280 posCopy = y.transpose();
281 tempdip =
dipfitError(posCopy, matData, sensors, matProjectors);
282 fv[j+1] = tempdip.
error;
286 std::vector<HPISortStruct> vecSortStruct;
288 for (
int i = 0; i < fv.size(); i++) {
292 vecSortStruct.push_back(structTemp);
295 std::sort(vecSortStruct.begin(), vecSortStruct.end(),
compare);
297 for (
int i = 0; i < vecSortStruct.size(); i++) {
298 idx[i] = vecSortStruct[i].idx;
301 for (
int i = 0;i < n+1;i++) {
302 v1.col(i) = v.col(idx[i]);
308 how =
"initial simplex";
309 itercount = itercount + 1;
312 tempX1 = Eigen::MatrixXd::Zero(1,n);
314 while ((func_evals < iMaxfun) && (itercount < iMaxiter)) {
316 for (
int i = 0;i < n;i++) {
317 tempX1(i) = std::fabs(fv[0] - fv[i+1]);
320 temp1 = tempX1.maxCoeff();
322 tempX2 = Eigen::MatrixXd::Zero(n,n);
324 for(
int i = 0;i < n;i++) {
325 tempX2.col(i) = v.col(i+1) - v.col(0);
328 tempX2 = tempX2.array().abs();
330 temp2 = tempX2.maxCoeff();
332 if((temp1 <= tolf) && (temp2 <= tolx)) {
336 xbar = v.block(0,0,n,n).rowwise().sum();
339 xr = (1+rho) * xbar - rho * v.block(0,n,v.rows(),1);
344 fxr =
dipfitError(x, matData, sensors, matProjectors);
346 func_evals = func_evals+1;
348 if (fxr.
error < fv[0]) {
350 xe = (1 + rho * chi) * xbar - rho * chi * v.col(v.cols()-1);
352 fxe =
dipfitError(x, matData, sensors, matProjectors);
353 func_evals = func_evals+1;
356 v.col(v.cols()-1) = xe;
360 v.col(v.cols()-1) = xr;
366 if(fxr.
error < fv[n-1]) {
367 v.col(v.cols()-1) = xr;
372 if(fxr.
error < fv[n]) {
374 xc = (1 + psi * rho) * xbar - psi * rho * v.col(v.cols()-1);
376 fxc =
dipfitError(x, matData, sensors, matProjectors);
377 func_evals = func_evals + 1;
380 v.col(v.cols()-1) = xc;
382 how =
"contract outside";
388 xcc = (1 - psi) * xbar + psi * v.col(v.cols()-1);
390 fxcc =
dipfitError(x, matData, sensors, matProjectors);
391 func_evals = func_evals+1;
392 if(fxcc.
error < fv[n]) {
393 v.col(v.cols()-1) = xcc;
395 how =
"contract inside";
402 if(how.compare(
"shrink") == 0) {
403 for(
int j = 1;j < n+1;j++) {
404 v.col(j).array() = v.col(0).array() + sigma * (v.col(j).array() - v.col(0).array());
405 x = v.col(j).array().transpose();
406 tempdip =
dipfitError(x,matData, sensors, matProjectors);
407 fv[j] = tempdip.
error;
414 vecSortStruct.clear();
416 for (
int i = 0; i < fv.size(); i++) {
420 vecSortStruct.push_back(structTemp);
423 std::sort(vecSortStruct.begin(), vecSortStruct.end(),
compare);
424 for (
int i = 0; i < vecSortStruct.size(); i++) {
425 idx[i] = vecSortStruct[i].idx;
428 for (
int i = 0;i < n+1;i++) {
429 v1.col(i) = v.col(idx[i]);
435 itercount = itercount + 1;
438 x = v.col(0).transpose();
441 iSimplexNumitr = itercount;