//Hermite void Hermite() { float passot=0.0001,tg,tc,cx,cy; int i=1; Derivata_x_rapp_incr();//calcola la derivata in x dei punti Derivata_y_rapp_incr();//calcola la derivata in y dei punti if(mod_der==1)//checkbox spuntato -> modifico il valore della derivata in un punto { dx[ip]=val_der_x; dy[ip]=val_der_y; } for (tg=0;tg<=1;tg=tg+passot) //nell'intervallo [0,1] disegno punti variando 0.0001 ogni volta { //localizziamo l'intervallo iesimo [t(i),t(i+1)] a cui il valore del parametro tg appartiene //all'inizio i=1,parto quindi dal primo intervallo...ogni volta che supero l'intervallo incremento la i,spostandomi quindi nell'intervallo successivo if (tg>t[i+1]) i++; // mappare il punto tg appartenente all'intervallo [t(i),t(i+1)] in un punto tcappello in [0,1] tc=(tg-t[i])/(t[i+1]-t[i]); // valutiamo le coordinate del punto sulla curva con le formule di interpolazione di hermite cx=Punti[i].x*phi0(tc)+ dx[i]*phi1(tc)*(t[i+1]-t[i])+ Punti[i+1].x*psi0(tc)+ dx[i+1]*psi1(tc)*(t[i+1]-t[i]); cy=Punti[i].y*phi0(tc)+ dy[i]*phi1(tc)*(t[i+1]-t[i])+ Punti[i+1].y*psi0(tc)+ dy[i+1]*psi1(tc)*(t[i+1]-t[i]); //disegno il punto ottenuto sullo schermo glBegin(GL_POINTS); glVertex2f(cx,cy); glEnd(); } glFlush(); }
void Disegna_Funzioni_Base() { float fbasephi0[1000],fbasephi1[1000],fbasepsi0[1000],fbasepsi1[1000],tf,vt[1000]; int n_pti_val=900,i=1; float passo=1.0/(n_pti_val-1); for(tf=0;tf<=1;tf+=passo) { vt[i]=tf; fbasephi0[i]=phi0(tf); fbasephi1[i]=phi1(tf); fbasepsi0[i]=psi0(tf); fbasepsi1[i]=psi1(tf); i++; } //definiamo le coordinate della finestra reale(sul mondo) glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 1.0, -0.8, 1.0); //il contenuto della finestra reale prima definita verrà mappato nella viewport sullo schermo che ha origine in (0,500),h 150 e largh 150 glViewport(0,500,150,150); glColor3f(0.0,0.0,1.0); glBegin(GL_POINTS); for(i=1;i<=n_pti_val;i++) glVertex2f(vt[i],fbasephi0[i]); glEnd(); glColor3f(0.0,1.0,1.0); glBegin(GL_POINTS); for(i=1;i<=n_pti_val;i++) glVertex2f(vt[i],fbasephi1[i]); glEnd(); glColor3f(0.0,1.0,0.0); glBegin(GL_POINTS); for(i=1;i<=n_pti_val;i++) glVertex2f(vt[i],fbasepsi0[i]); glEnd(); glColor3f(1.0,0.0,0.0); glBegin(GL_POINTS); for(i=1;i<=n_pti_val;i++) glVertex2f(vt[i],fbasepsi1[i]); glEnd(); glFlush(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, WINH, 0.0, WINH); glViewport(0,0,WINH,WINH); }
// вектор b[i] double psi_vector(int i,double x_p, double h_x) { switch (i) { case 1: return psi1(x_p, h_x); break; case 2: return psi2(x_p, h_x); break; case 3: return psi3(x_p, h_x); break; case 4: return psi4(x_p, h_x); break; } }
//матрица M[i][j] double psi_matrix(int i, int j,double x_p, double h_x) { switch (i) { case 1: switch (j) { case 1:return psi1(x_p, h_x)*psi1(x_p, h_x);break; case 2:return psi1(x_p, h_x)*psi2(x_p, h_x);break; case 3:return psi1(x_p, h_x)*psi3(x_p, h_x);break; case 4:return psi1(x_p, h_x)*psi4(x_p, h_x);break; } case 2: switch (j) { case 1:return psi2(x_p, h_x)*psi1(x_p, h_x);break; case 2:return psi2(x_p, h_x)*psi2(x_p, h_x);break; case 3:return psi2(x_p, h_x)*psi3(x_p, h_x);break; case 4:return psi2(x_p, h_x)*psi4(x_p, h_x);break; } case 3: switch (j) { case 1:return psi3(x_p, h_x)*psi1(x_p, h_x);break; case 2:return psi3(x_p, h_x)*psi2(x_p, h_x);break; case 3:return psi3(x_p, h_x)*psi3(x_p, h_x);break; case 4:return psi3(x_p, h_x)*psi4(x_p, h_x);break; } case 4: switch (j) { case 1:return psi4(x_p, h_x)*psi1(x_p, h_x);break; case 2:return psi4(x_p, h_x)*psi2(x_p, h_x);break; case 3:return psi4(x_p, h_x)*psi3(x_p, h_x);break; case 4:return psi4(x_p, h_x)*psi4(x_p, h_x);break; } } }
vector<vector<double> > qFinderDMM::getHessian(){ try { vector<double> alpha(numOTUs, 0.0000); double alphaSum = 0.0000; vector<double> pi = zMatrix[currentPartition]; vector<double> psi_ajk(numOTUs, 0.0000); vector<double> psi_cjk(numOTUs, 0.0000); vector<double> psi1_ajk(numOTUs, 0.0000); vector<double> psi1_cjk(numOTUs, 0.0000); for(int j=0;j<numOTUs;j++){ if (m->control_pressed) { break; } alpha[j] = exp(lambdaMatrix[currentPartition][j]); alphaSum += alpha[j]; for(int i=0;i<numSamples;i++){ double X = (double) countMatrix[i][j]; psi_ajk[j] += pi[i] * psi(alpha[j]); psi1_ajk[j] += pi[i] * psi1(alpha[j]); psi_cjk[j] += pi[i] * psi(alpha[j] + X); psi1_cjk[j] += pi[i] * psi1(alpha[j] + X); } } double psi_Ck = 0.0000; double psi1_Ck = 0.0000; double weight = 0.0000; for(int i=0;i<numSamples;i++){ if (m->control_pressed) { break; } weight += pi[i]; double sum = 0.0000; for(int j=0;j<numOTUs;j++){ sum += alpha[j] + countMatrix[i][j]; } psi_Ck += pi[i] * psi(sum); psi1_Ck += pi[i] * psi1(sum); } double psi_Ak = weight * psi(alphaSum); double psi1_Ak = weight * psi1(alphaSum); vector<vector<double> > hessian(numOTUs); for(int i=0;i<numOTUs;i++){ hessian[i].assign(numOTUs, 0.0000); } for(int i=0;i<numOTUs;i++){ if (m->control_pressed) { break; } double term1 = -alpha[i] * (- psi_ajk[i] + psi_Ak + psi_cjk[i] - psi_Ck); double term2 = -alpha[i] * alpha[i] * (-psi1_ajk[i] + psi1_Ak + psi1_cjk[i] - psi1_Ck); double term3 = 0.1 * alpha[i]; hessian[i][i] = term1 + term2 + term3; for(int j=0;j<i;j++){ hessian[i][j] = - alpha[i] * alpha[j] * (psi1_Ak - psi1_Ck); hessian[j][i] = hessian[i][j]; } } return hessian; } catch(exception& e){ m->errorOut(e, "qFinderDMM", "getHessian"); exit(1); } }
inline void internal::PanelLU ( DistMatrix<F, STAR,STAR>& A, DistMatrix<F, MC, STAR>& B, DistMatrix<int,STAR,STAR>& p, int pivotOffset ) { #ifndef RELEASE PushCallStack("internal::PanelLU"); if( A.Grid() != p.Grid() || p.Grid() != B.Grid() ) throw std::logic_error ("Matrices must be distributed over the same grid"); if( A.Width() != B.Width() ) throw std::logic_error("A and B must be the same width"); if( A.Height() != p.Height() || p.Width() != 1 ) throw std::logic_error("p must be a vector that conforms with A"); #endif const Grid& g = A.Grid(); const int r = g.Height(); const int colShift = B.ColShift(); const int colAlignment = B.ColAlignment(); // Matrix views DistMatrix<F,STAR,STAR> ATL(g), ATR(g), A00(g), a01(g), A02(g), ABL(g), ABR(g), a10(g), alpha11(g), a12(g), A20(g), a21(g), A22(g); DistMatrix<F,MC,STAR> BL(g), BR(g), B0(g), b1(g), B2(g); DistMatrix<int,STAR,STAR> pT(g), p0(g), pB(g), psi1(g), p2(g); const int width = A.Width(); const int numBytes = (width+1)*sizeof(F)+sizeof(int); std::vector<byte> sendData(numBytes); std::vector<byte> recvData(numBytes); // Extract pointers to send and recv data F* sendBufFloat = (F*) &sendData[0]; F* recvBufFloat = (F*) &recvData[0]; int* sendBufInt = (int*) &sendData[(width+1)*sizeof(F)]; int* recvBufInt = (int*) &recvData[(width+1)*sizeof(F)]; // Start the algorithm PushBlocksizeStack( 1 ); PartitionDownDiagonal ( A, ATL, ATR, ABL, ABR, 0 ); PartitionRight( B, BL, BR, 0 ); PartitionDown ( p, pT, pB, 0 ); while( ATL.Height() < A.Height() ) { RepartitionDownDiagonal ( ATL, /**/ ATR, A00, /**/ a01, A02, /*************/ /**********************/ /**/ a10, /**/ alpha11, a12, ABL, /**/ ABR, A20, /**/ a21, A22 ); RepartitionRight ( BL, /**/ BR, B0, /**/ b1, B2 ); RepartitionDown ( pT, p0, /**/ /****/ psi1, pB, p2 ); //--------------------------------------------------------------------// // Store the index/value of the pivot candidate in A F pivotValue = alpha11.GetLocalEntry(0,0); int pivotIndex = a01.Height(); for( int i=0; i<a21.Height(); ++i ) { F value = a21.GetLocalEntry(i,0); if( FastAbs(value) > FastAbs(pivotValue) ) { pivotValue = value; pivotIndex = a01.Height() + i + 1; } } // Update the pivot candidate to include local data from B for( int i=0; i<B.LocalHeight(); ++i ) { F value = b1.GetLocalEntry(i,0); if( FastAbs(value) > FastAbs(pivotValue) ) { pivotValue = value; pivotIndex = A.Height() + colShift + i*r; } } // Fill the send buffer with: // [ pivotValue | pivotRow | pivotIndex ] if( pivotIndex < A.Height() ) { sendBufFloat[0] = A.GetLocalEntry(pivotIndex,a10.Width()); const int ALDim = A.LocalLDim(); const F* ABuffer = A.LocalBuffer(pivotIndex,0); for( int j=0; j<width; ++j ) sendBufFloat[j+1] = ABuffer[j*ALDim]; } else { const int localIndex = ((pivotIndex-A.Height())-colShift)/r; sendBufFloat[0] = b1.GetLocalEntry(localIndex,0); const int BLDim = B.LocalLDim(); const F* BBuffer = B.LocalBuffer(localIndex,0); for( int j=0; j<width; ++j ) sendBufFloat[j+1] = BBuffer[j*BLDim]; } *sendBufInt = pivotIndex; // Communicate to establish the pivot information mpi::AllReduce ( &sendData[0], &recvData[0], numBytes, PivotOp<F>(), g.ColComm() ); // Update the pivot vector const int maxIndex = *recvBufInt; p.SetLocalEntry(a01.Height(),0,maxIndex+pivotOffset); // Copy the current row into the pivot row if( maxIndex < A.Height() ) { const int ALDim = A.LocalLDim(); F* ASetBuffer = A.LocalBuffer(maxIndex,0); const F* AGetBuffer = A.LocalBuffer(A00.Height(),0); for( int j=0; j<width; ++j ) ASetBuffer[j*ALDim] = AGetBuffer[j*ALDim]; } else { const int ownerRank = (colAlignment+(maxIndex-A.Height())) % r; if( g.Row() == ownerRank ) { const int localIndex = ((maxIndex-A.Height())-colShift) / r; const int ALDim = A.LocalLDim(); const int BLDim = B.LocalLDim(); F* BBuffer = B.LocalBuffer(localIndex,0); const F* ABuffer = A.LocalBuffer(A00.Height(),0); for( int j=0; j<width; ++j ) BBuffer[j*BLDim] = ABuffer[j*ALDim]; } } // Copy the pivot row into the current row { F* ABuffer = A.LocalBuffer(A00.Height(),0); const int ALDim = A.LocalLDim(); for( int j=0; j<width; ++j ) ABuffer[j*ALDim] = recvBufFloat[j+1]; } // Now we can perform the update of the current panel F alpha = alpha11.GetLocalEntry(0,0); if( alpha == (F)0 ) throw SingularMatrixException(); F alpha11Inv = ((F)1) / alpha; Scal( alpha11Inv, a21.LocalMatrix() ); Scal( alpha11Inv, b1.LocalMatrix() ); Geru( (F)-1, a21.LocalMatrix(), a12.LocalMatrix(), A22.LocalMatrix() ); Geru( (F)-1, b1.LocalMatrix(), a12.LocalMatrix(), B2.LocalMatrix() ); //--------------------------------------------------------------------// SlidePartitionDownDiagonal ( ATL, /**/ ATR, A00, a01, /**/ A02, /**/ a10, alpha11, /**/ a12, /*************/ /**********************/ ABL, /**/ ABR, A20, a21, /**/ A22 ); SlidePartitionRight ( BL, /**/ BR, B0, b1, /**/ B2 ); SlidePartitionDown ( pT, p0, psi1, /**/ /****/ pB, p2 ); } PopBlocksizeStack(); #ifndef RELEASE PopCallStack(); #endif }