PetscErrorCode SolveFinal(FEMInf fem, int L1, PetscScalar energy, Vec x0, Vec *x1, PetscScalar *alpha) { PetscErrorCode ierr; Mat S, L, D; CalcMat(fem, L1, &L, &S); MatAXPY(L, -energy, S, DIFFERENT_NONZERO_PATTERN); MatDestroy(&S); PetscScalar mat_ele_cos; // <Y_10 | P_q(cos theta) | Y_00> mat_ele_cos = Y1ElePq(1, 1, 0, 0, 0); if(getenv("SHOW_DEBUG")) printf("mat_ele_cos = %f\n", PetscRealPart(mat_ele_cos)); PF dp_length; PotCreate(PETSC_COMM_SELF, &dp_length); ierr = PotSetPower(dp_length, mat_ele_cos, 1); CHKERRQ(ierr); ierr =FEMInfCreateMat(fem, 1, &D); ierr = FEMInfPotR1Mat(fem, dp_length, D); CHKERRQ(ierr); Vec driv; MatCreateVecs(L, &driv, NULL); ierr = MatMult(D, x0, driv); CHKERRQ(ierr); ierr = MatDestroy(&D); CHKERRQ(ierr); KSP ksp; ierr = KSPCreate(fem->comm, &ksp); CHKERRQ(ierr); ierr = KSPSetOperators(ksp, L, L); CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr); ierr = MatCreateVecs(L, x1, NULL); ierr = KSPSolve(ksp, driv, *x1); CHKERRQ(ierr); ierr = VecDot(*x1, driv, alpha); CHKERRQ(ierr); // KSPView(ksp, PETSC_VIEWER_STDOUT_SELF); KSPDestroy(&ksp); MatDestroy(&L); VecDestroy(&driv); return 0; }
void MixMod::Compute(int * NUMK, double * LL, double * P, double * T, double * COMP_VAR) { int numk,i; // MessageBox (0, "0", "START", MB_ICONINFORMATION); Grid(); // construct grid of potential subpopulation means CalcMat(); // calculation of mxing kernel density vem(); // VEM algorithm Update(); // Find subpoplations with positve weight, i.e. p > 0.01 EM(numstep); // refiend soultion with EM algorithm numk=Combine(); // final solution: combine close components if (dens==0) COMP_VAR[0] = compvar; NUMK[0] = numk; LL[0] = ll; for(i=0;i<numk;i++){ P[i] = p[i]; T[i] = t[i]; } }
int MixMod::Combine() { int i,j,jj,tempk,tmpnumstep=1; std::vector<int>count(k); std::vector<double>tempp(k); std::vector<double>tempt(k); double diff; for (i=0;i<k;i++) { tempt[i] =1.E+8; tempp[i] =0.; count[i]=-1; } jj=-1; // find identical parameters and store their index for (i=0;i<k-1;i++) { diff=t[i+1]-t[i]; if(fabs(diff)<limit) { jj++; count[jj]=i+1; } } jj=jj+1; tempk=k-jj; j=0; jj=-1; /*cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc c c Find remaining different parameters and update mixing weights c cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc*/ for (i=0;i<k;i++) { if ( i==count[j] ) { j++; tempp[jj]=tempp[jj]+p[i]; } // end if else { jj++; tempt[jj]=t[i]; tempp[jj]=p[i]; } // end else } // end for loop /*cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc c done! cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc*/ k=tempk; for (i=0;i<k;i++) { p[i]=tempp[i]; t[i]=tempt[i]; } CalcMat(); Gradient(); EM(tmpnumstep); ll=likelihood(); return tempk; }
void MixMod::EM(int nstep) { int i,j,icount; double s11,s12,s13=0.,ymax; int l; l=k-1; if(k==1) { s11=0.; s12=0.; for (i=0;i<n;i++) { s11=s11+w[i]*x[i][0]/x[i][3]; s12=s12+w[i]*x[i][2]/x[i][3]; } // end for t[l]=s11/s12; p[l]=1.; CalcMat (); Gradient(); icount=1; maxderiv(ymax); return; } // end if for(icount=0;icount<nstep;icount++) { CalcMat(); Gradient(); double su=0; for(i=0;i<l;i++) { p[i]=p[i]*grad[i]; su=su+p[i]; } p[l]=1.0-su; s13=0.; for (j=0;j<k;j++) { s11=0.; s12=0.; for (i=0;i<n;i++) { if(s1[i] > 1.E-10) { s11=s11+w[i]*x[i][0]/x[i][3]*xf[i][j]/s1[i]; s12=s12+w[i]*x[i][2]*(xf[i][j]/x[i][3])/s1[i]; if (dens==0) s13=s13+w[i]*pow( (x[i][0]-t[j]),2)*p[j]*xf[i][j]/s1[i]; } } // end i if(s12 > 1.E-12) t[j]=s11/s12; } maxderiv (ymax); em_details[0] = fabs(ymax-1.0); em_details[1] = (em_details[1] + 1.) ; if ((dens==0) && (!ismeta)){ for (i=0;i<n;i++) { x[i][3] = s13; } } if ( (em_details[0] < acc) && (icount > 10 )) break; } compvar = s13; }