bool ACovarianceFunction::check_covariance_Kgrad_theta(ACovarianceFunction& covar,mfloat_t relchange,mfloat_t threshold) { mfloat_t RV=0; //copy of parameter vector CovarParams L = covar.getParams(); //create copy CovarParams L0 = L; //dimensions for(mint_t i=0;i<L.rows();i++) { mfloat_t change = relchange*L(i); change = std::max(change,1E-5); L(i) = L0(i) + change; covar.setParams(L); MatrixXd Lplus = covar.K(); L(i) = L0(i) - change; covar.setParams(L); MatrixXd Lminus = covar.K(); //numerical gradient MatrixXd diff_numerical = (Lplus-Lminus)/(2.*change); //analytical gradient covar.setParams(L0); MatrixXd diff_analytical = covar.Kgrad_param(i); RV += (diff_numerical-diff_analytical).squaredNorm(); } return (RV < threshold); }
void Mesh :: buildLaplacian( void ) // builds the cotan-Laplace operator, where the final row and // column are omitted to make the system strictly positive- // definite (equivalent to setting the final degree of freedom // to zero) { // allocate a sparse |V|x|V| matrix int nV = vertices.size(); QuaternionMatrix L0; L0.resize( nV-1, nV-1 ); // visit each face for( size_t i = 0; i < faces.size(); i++ ) { // visit each triangle corner for( int j = 0; j < 3; j++ ) { // get vertex indices int k0 = faces[i].vertex[ (j+0) % 3 ]; int k1 = faces[i].vertex[ (j+1) % 3 ]; int k2 = faces[i].vertex[ (j+2) % 3 ]; // get vertex positions Vector f0 = vertices[k0].im(); Vector f1 = vertices[k1].im(); Vector f2 = vertices[k2].im(); // compute cotangent of the angle at the current vertex // (equal to cosine over sine, which equals the dot // product over the norm of the cross product) Vector u1 = f1 - f0; Vector u2 = f2 - f0; double cotAlpha = (u1*u2)/(u1^u2).norm(); // add contribution of this cotangent to the matrix if( k1 != nV-1 && k2 != nV-1 ) L0( k1, k2 ) -= cotAlpha / 2.; if( k2 != nV-1 && k1 != nV-1 ) L0( k2, k1 ) -= cotAlpha / 2.; if( k1 != nV-1 && k1 != nV-1 ) L0( k1, k1 ) += cotAlpha / 2.; if( k2 != nV-1 && k2 != nV-1 ) L0( k2, k2 ) += cotAlpha / 2.; } } // build Cholesky factorization L.build( L0.toReal() ); }
void ACovarianceFunction::aKhess_param_num(ACovarianceFunction& covar, MatrixXd* out, const muint_t i, const muint_t j) { mfloat_t relchange=1E-5; //copy of parameter vector CovarParams L = covar.getParams(); //create copy CovarParams L0 = L; //dimensions mfloat_t change = relchange*L(j); change = std::max(change,1E-5); L(j) = L0(j) + change; covar.setParams(L); covar.aKgrad_param(out,i); L(j) = L0(j) - change; covar.setParams(L); (*out)-=covar.Kgrad_param(i); (*out)/=(2.*change); covar.setParams(L0); }
void LU_Inverse( const GenBandMatrix<T1>& LUx, const ptrdiff_t* p, MatrixView<T> minv) { TMVAssert(LUx.isSquare()); TMVAssert(minv.isSquare()); TMVAssert(minv.colsize() == LUx.colsize()); #ifdef XDEBUG LowerTriMatrix<T,UnitDiag> L0(LUx.colsize()); LU_PackedPL_Unpack(LUx,p,L0.view()); UpperTriMatrix<T> U0 = BandMatrixViewOf(LUx,0,LUx.nhi()); Matrix<T> PLU = L0 * U0; if (LUx.nlo() > 0) PLU.reversePermuteRows(p); Matrix<T> minv2 = PLU.inverse(); #endif if (minv.colsize() > 0) { if ( !(minv.iscm() || minv.isrm())) { Matrix<T,ColMajor> temp(minv.colsize(),minv.colsize()); LU_Inverse(LUx,p,temp.view()); minv = temp; } else { minv.setZero(); UpperTriMatrixView<T> U = minv.upperTri(); U = BandMatrixViewOf(LUx,0,LUx.nhi()); TriInverse(U,LUx.nhi()); LU_PackedPL_RDivEq(LUx,p,minv); } } #ifdef XDEBUG TMV_RealType(T) normdiff = Norm(PLU*minv - T(1)); TMV_RealType(T) kappa = Norm(PLU)*Norm(minv); if (normdiff > 0.001*kappa*minv.colsize()) { cerr<<"LUInverse:\n"; cerr<<"LUx = "<<LUx<<endl; cerr<<"p = "; for(ptrdiff_t i=0;i<LUx.colsize();i++) cerr<<p[i]<<" "; cerr<<endl; cerr<<"PLU = "<<PLU<<endl; cerr<<"minv = "<<minv<<endl; cerr<<"minv2 = "<<minv2<<endl; cerr<<"m*minv = "<<PLU*minv<<endl; cerr<<"minv*m = "<<minv*PLU<<endl; cerr<<"Norm(m*minv - 1) = "<<normdiff<<endl; cerr<<"kappa = "<<kappa<<endl; abort(); } #endif }
//Generate GramCodes for grammems with CompareFunc string CAgramtab::GetGramCodes(BYTE pos, QWORD grammems, GrammemCompare CompareFunc)const { string Result; CAgramtabLine L0(0); L0.m_PartOfSpeech = pos; L0.m_Grammems = grammems; for (WORD i=0; i<GetMaxGrmCount(); i++) if (GetLine(i) != 0) { const CAgramtabLine* L = GetLine(i); if ( (L->m_PartOfSpeech == pos) && ( CompareFunc ? CompareFunc (L, &L0) : (L->m_Grammems & grammems) == L->m_Grammems && !(pos==NOUN && (L->m_Grammems&rAllGenders)==rAllGenders) ) // ((grammems & (L->m_Grammems & mask)) == grammems) ) Result += i2s(i); }; return Result; };
// Evaluate the nth order Legendre polynomial and its derivative void legendre(const dvec& a, const dvec& b, const dvec& x, dvec& L, dvec& Lp) { int n = x.size(); dvec L0(n,1.0); dvec L1(n,0.0); // Iterate over grid points for(int j=0;j<n;++j) { L1[j] = x[j]; // Iterate over polynomials for(int k=1;k<n;++k) { L[j] = a[k]*x[j]*L1[j]-b[k]*L0[j]; L0[j] = L1[j]; L1[j] = L[j]; } Lp[j] = n*(L0[j]-x[j]*L[j])/(1.0-x[j]*x[j]); } }
void test_circuit_parallel() { //circuit 1 ngac AC("ac1", 0, 5, 1); ngresistor R("r1", 370); ngled LED("LED1", 7e-3); ngground GND; ngline L0(AC.p1, GND.p1); ngline L1(AC.p2, R.p1); ngline L2(R.p2, LED.p1); ngline L3(LED.p2, AC.p1); schema SCH; SCH.AddDevices(&AC, &R, &LED, &GND, 0); SCH.AddLines(&L0, &L1, &L2, &L3, 0); circuit CIR(&SCH); CIR.Tran("20", "1m"); //circuit 2 ngac ac("ac1", 0, 5, 1); ngresistor r("r1", 370); ngled led("led1", 5e-3); ngground gnd; ngline l0(ac.p1, gnd.p1); ngline l1(ac.p2, r.p1); ngline l2(r.p2, led.p1); ngline l3(led.p2, ac.p1); schema sch; sch.AddDevices(&ac, &r, &led, &gnd, 0); sch.AddLines(&l0, &l1, &l2, &l3, 0); circuit cir(&sch); cir.Tran("20", "1m"); do { Sleep(200); } while (CIR.IsRunning() || cir.IsRunning()); }
void Solver::DiagramL0(){ double time0, time1; time0 = omp_get_wtime(); #pragma omp parallel { int id = omp_get_thread_num(); int threads = omp_get_num_threads(); for (int i=id; i<Npphh; i+=threads) blockspphh[i]->SetUpMatrices_L0(); } time1 = omp_get_wtime(); //cout << "Inside L0. SetUpMatrices needs: " << time1-time0 << " seconds" << endl; time0 = omp_get_wtime(); //Align elements to t. for (int n=0; n<Npphh; n++){ mat L0 = blockspphh[n]->V / blockspphh[n]->epsilon; for (double I=0; I < blockspphh[n]->Holes.n_rows ; I++){ for (double A=0; A < blockspphh[n]->Particles.n_rows; A++){ int i = blockspphh[n]->Holes(I,0); int j = blockspphh[n]->Holes(I,1); int a = blockspphh[n]->Particles(A,0); int b = blockspphh[n]->Particles(A,1); t( Index(a,b,i,j) ) += L0(A,I); //StoreT( Index(a,b,i,j), L0(A,I)); } } } time1 = omp_get_wtime(); //cout << "Inside L0. Calculate matrices needs: " << time1-time0 << " seconds" << endl; }
void L_layerInv(uint8_t *data){ L0(data); L1Inv(data); L2Inv(data); L3(data); }
void L_layer(uint8_t *data){ L0(data); L1(data); L2(data); L3(data); }
void gen_makefile( void ) { int tlink = ( _output == 'p' || _output == 'l' ); frsp = fopen( "MAKEFILE.", "wt" ); if ( frsp == NULL ) error( ERR_RUNTIME, "MAKEFILE." ); L1( "#" ); L1( "# this MAKEFILE. generate MK386.EXE program" ); L1( "#" ); L1( ".AUTODEPEND" ); L1( ".SWAP" ); L1( "# LINK = # define LINK for COMPILE, ASSEMBLE & LINK" ); L1( "# ASSEMBLE = # define ASSEMBLE for COMPILE & ASSEMBLE" ); L1( "# COMPILE = # define COMPILER for ONLY COMPILE" ); L1( "!ifndef LINK" ); L1( "!ifndef ASSEMBLE" ); L1( "!ifndef COMPILE" ); L1( "LINK =" ); L1( "!endif" ); L1( "!endif" ); L1( "!endif" ); L1( "# DEBUG = # define DEBUG for debug info and debug startup module" ); L1( "# MAP = # define MAP for generate .MAP file" ); L1( "# NODEFLIB = # define NODEFLIB for NO link default .OBJ & .LIB" ); L1( "# KEEP_SRC = # define KEEP_SRC for save generate .ASM & .OBJ files" ); L1( "# KEEP_LST = # define KEEP_LST for save generate .LST files" ); L1( "" ); L1( " ### name of .EXE file" ); L0( "NAME = " ); L1( name() ); L1( " ### define tools" ); L1( "CC = CC386.EXE" ); L1( "AS = TASM.EXE" ); L1( " ### define options for CC" ); L0( "CC_INC = " ); if ( l_inc.count > 0 ) { L0( "/I" ); ListIter( &l_inc, compile_inc ); } L1( "" ); L0( "CC_DEF = " ); ListIter( &l_def, compile_def ); L1( "" ); L0( "CC_OPT = " ); fprintf( frsp, "%ci %ce %cA", ( _dump_cpp ? '+' : '-' ), ( _dump_err ? '+' : '-' ), ( !_ansi ? '+' : '-' ) ); ListIter( &l_cc386, compile_opt ); L1( "" ); L1( "!ifdef KEEP_LST" ); L1( "CC_OPT = $(CC_OPT) +l" ); L1( "!endif" ); L1( "" ); L1( "CC = $(CC) $(CC_OPT) $(CC_DEF) $(CC_INC)" ); L1( " ### define options for AS" ); L0( "AS_INC = " ); ListIter( &l_inc, assemble_inc ); L1( "" ); L0( "AS_DEF = " ); ListIter( &l_def, assemble_def ); L1( "" ); L1( "!ifdef DEBUG"); L1( "AS_OPT = /t /m3 /ml /zi" ); L1( "!else" ); L1( "AS_OPT = /t /m3 /ml /zn" ); L1( "!endif"); L1( "!ifdef KEEP_LST" ); L1( "AS_OPT = $(AS_OPT) /la" ); L1( "!endif" ); L1( "" ); L1( "AS = $(AS) $(AS_OPT) $(AS_DEF) $(AS_INC)" ); L1( " ### .LIB & output .OBJ path" ); L0( "PATH_LIB = " ); L1( path_lib ); L0( "PATH_OBJ = " ); if ( strlen( path_obj ) > 0 ) L1( path_obj ); else L1( "." ); L1( " ### default .OBJ & .LIB" ); L1( "!ifdef NODEFLIB"); L1( "DEFAULT_OBJS =" ); L1( "DEFAULT_LIBS =" ); L1( "WLINK_OBJS =" ); L1( "WLINK_LIBS =" ); L1( "!else" ); L1( "DEFAULT_LIBS = $(PATH_LIB)\\CLDOS.LIB" ); L1( "WLINK_LIBS = library $(DEFAULT_LIBS)" ); L1( "!ifdef DEBUG" ); if ( _output == 'l' ) L1( "DEFAULT_OBJS = $(PATH_LIB)\\C0DOSLD.OBJ " ); else if ( _output == 'p' ) L1( "DEFAULT_OBJS = $(PATH_LIB)\\C0DOSD.OBJ " ); else L1( "DEFAULT_OBJS = $(PATH_LIB)\\C0DOSWD.OBJ " ); L1( "!else" ); if ( _output == 'l' ) L1( "DEFAULT_OBJS = $(PATH_LIB)\\C0DOSL.OBJ " ); else if ( _output == 'p' ) L1( "DEFAULT_OBJS = $(PATH_LIB)\\C0DOS.OBJ " ); else L1( "DEFAULT_OBJS = $(PATH_LIB)\\C0DOSW.OBJ " ); L1( "!endif" ); L1( "WLINK_OBJS = file $(DEFAULT_OBJS)" ); L1( "!endif" ); L1( " ### define macro for KEEP_SRC" ); L1( "!ifdef KEEP_SRC"); L1( "DEL_SRC = @rem" ); L1( "!else" ); L1( "DEL_SRC = -del" ); L1( "!endif"); L1( " ### define macro for KEEP_LST" ); L1( "!ifdef KEEP_LST"); L1( "DEL_LST = @rem" ); L1( "!else" ); L1( "DEL_LST = -del" ); L1( "!endif"); L1( " ### define macro for MAP" ); L1( "!ifdef MAP"); L1( "TLINK_OPT = /3/d/c/m/l/s" ); L1( "WLINK_OPT = option map" ); L1( "!else" ); L1( "TLINK_OPT = /3/d/c/x" ); L1( "WLINK_OPT = " ); L1( "!endif"); L1( " ### define macro for DEBUG" ); L1( "!ifdef DEBUG"); L1( "TLINK_DBG = /v" ); L1( "WLINK_DBG = debug option symf" ); L1( "!else" ); L1( "TLINK_DBG = " ); L1( "WLINK_DBG = option quiet" ); L1( "!endif"); L1( " ### .ASM files" ); fprintf( frsp, "ASMS = \\\n" ); ListIter( &files, get_asm ); L1( " ### .OBJ files" ); fprintf( frsp, "OBJS = $(DEFAULT_OBJS) \\\n" ); ListIter( &files, get_obj ); L1( " ### .LIB files" ); fprintf( frsp, "LIBS = $(DEFAULT_LIBS) \\\n" ); ListIter( &files, get_lib ); L1( " ### main make depend" ); L1( "!ifdef LINK" ); L1( "$(NAME).EXE : $(OBJS) $(LIBS) makefile." ); if ( tlink ) { L1( " TLINK.EXE @&&|" ); L1( "$(TLINK_OPT) $(TLINK_DBG) $(OBJS), $(NAME), $(NAME), $(LIBS)" ); } else { L1( " WLINK.EXE @&&|" ); L1( "$(WLINK_OPT) $(WLINK_DBG)" ); L1( "format os2 le" ); L1( "option nod" ); if ( _output == 'w' ) { L1( "option osname='CC386+PMODE/W'" ); L1( "option stub=$(PATH_LIB)\\PMODEW.EXE" ); } else { L1( "option osname='CC386+DOS/4GW'" ); L1( "option stub=$(PATH_LIB)\\D4GWSTUB.EXE" ); } L1( "name $(NAME)" ); L1( "$(WLINK_OBJS)" ); ListIter( &files, wout_obj ); L1( "$(WLINK_LIBS)" ); ListIter( &files, wout_lib ); } L1( "|" ); ListIter( &files, delete_obj ); L1( "!else" ); L1( "!ifdef ASSEMBLE"); L1( "assemble : $(OBJS) " ); L1( "!else" ); L1( "!ifdef COMPILE"); L1( "compile : $(ASMS) " ); L1( "!endif" ); L1( "!endif" ); L1( "!endif" ); L1( " ### files depend" ); ListIter( &files, get_dep ); fclose( frsp ); }