static void createscores(int score) { FILE *handle; int i,j; score_t scores[NUMSCORES]; char header[strlen (SCORE_HEADER)+1]; if (score == 0) return; /* No need saving this */ for (i = 1; i < NUMSCORES; i++) { strcpy (scores[i].name,"None"); scores[i].score = -1; scores[i].timestamp = 0; } getname (scores[0].name); scores[0].score = score; scores[0].timestamp = time (NULL); if ((handle = fopen (scorefile,"w")) == NULL) err1 (); strcpy (header,SCORE_HEADER); i = fwrite (header,strlen (SCORE_HEADER),1,handle); if (i != 1) err2(); for (i = 0; i < NUMSCORES; i++) { j = fwrite (scores[i].name,strlen (scores[i].name) + 1,1,handle); if (j != 1) err2(); j = fwrite (&(scores[i].score),sizeof (int),1,handle); if (j != 1) err2(); j = fwrite (&(scores[i].timestamp),sizeof (time_t),1,handle); if (j != 1) err2(); } fclose (handle); fprintf (stderr,"%s",scoretitle); fprintf (stderr,"\t 1* %7d %s\n\n",score,scores[0].name); }
static void read_bidi_mirroring () { char s[500]; unsigned int i, j; FILE *f; for (i = 0; i < 0x110000; i++) table[i] = 0; mirroring_count = 0; printf ("Reading `BidiMirroring.txt'\n"); if (!(f = fopen (bidi_mirroring_file, "rt"))) err2 ("cannot open `%s' for reading", bidi_mirroring_file); /* fgets (s, sizeof s, f); sscanf (s, "# BidiMirroring-%s.txt", bidi_mirroring_version = malloc (20));*/ while (fgets (s, sizeof s, f)) { if (s[0] == '#' || s[0] == '\0' || s[0] == '\n') continue; sscanf (s, "%x; %x", &i, &j); table[i] = j; table[j] = i; mirroring_count++; } fclose (f); }
static void write_char_type (char *file, int max_depth) { int i; FILE *f; char *FILENAME = headermacro (file); printf ("Writing `%s', it may take a few minutes\n", file); if (!(f = fopen (file, "wt"))) err2 ("error: cannot open `%s' for writing", file); fprintf (f, "/*\n" " This file was automatically created from UnicodeData.txt version %s\n" " by fribidi_create_char_types\n*/\n\n", FRIBIDI_UNICODE_VERSION); fprintf (f, "#ifndef %s\n#define %s\n\n#include \"fribidi.h\"\n\n", FILENAME, FILENAME); for (i = 0; i < type_names_count; i++) if (names[i]) fprintf (f, "#define %s FRIBIDI_PROP_TYPE_%s\n", names[i], names[i]); fprintf (f, "\n"); fprintf (f, "#define PACKTAB_UINT8 fribidi_uint8\n"); fprintf (f, "#define PACKTAB_UINT16 fribidi_uint16\n"); fprintf (f, "#define PACKTAB_UINT32 fribidi_uint32\n"); if (!pack_table (table, FRIBIDI_UNICODE_CHARS, SIZEOF_CHAR, max_depth, 3, names, key_type_name, table_name, macro_name, f)) err ("error: insufficient memory, decrease max_depth"); for (i = type_names_count - 1; i >= 0; i--) if (names[i]) fprintf (f, "#undef %s\n", names[i]); fprintf (f, "/*======================================================================\n" " * %s() returns the bidi type of a character.\n" " *----------------------------------------------------------------------*/\n" "%s %s\n" "%s (%s uch)\n" "{\n" " if (uch < 0x%x)\n" " return %s[(unsigned char)%s (uch)];\n" " else\n" " return FRIBIDI_TYPE_%s;\n" " /* Non-Unicode chars */\n" "}\n" "\n", function_name, export_api, char_type_name, function_name, char_name, FRIBIDI_UNICODE_CHARS, prop_to_type_name, macro_name, default_type); fprintf (f, "\n#endif /* %s */\n", FILENAME); fclose (f); }
static char get_type (char *s) { int i; for (i = 0; i < type_names_count; i++) if (!strcmp (s, type_names[i].name)) return type_names[i].key; err2 ("type name `%s' not found", s); return 0; }
void doit(const char *name, const char *d0, const char *d) { char *s; s = env_get(d0); if (!s) { if (d) err(name, "env_get find existing variable"); return; } if (!s) err(name, "env_get didn't find existing variable"); if (strcmp(s, d)) err2(name, s, d); return; }
void repel_use(){ switch(*var_access(0x800E)){ case ITEM_SCHUTZ: *var_access(0x4020) = 100; break; case ITEM_SUPERSCHUTZ: *var_access(0x4020) = 200; break; case ITEM_TOP_SCHUTZ: *var_access(0x4020) = 250; break; default: err2(ERR_NO_REPEL_ITEM, *var_access(0x800E)); break; } }
static void read_unicode_data () { char s[500], tp[10]; unsigned int i; FILE *f; printf ("Reading `UnicodeData.txt'\n"); if (!(f = fopen (unidata_file, "rt"))) err2 ("error: cannot open `%s' for reading", unidata_file); while (fgets (s, sizeof s, f)) { sscanf (s, "%x;%*[^;];%*[^;];%*[^;];%[^;]", &i, tp); table[i] = get_type (tp); } fclose (f); }
static void write_mirror (char *file) { int i; FILE *f; char *FILENAME = headermacro (file); printf ("Writing `%s'\n", file); if (!(f = fopen (file, "wt"))) err2 ("cannot open `%s' for writing", file); fprintf (f, "/*\n" " This file was automatically created from BidiMirroring.txt, version %s\n" " by fribidi_create_mirroring\n*/\n\n", FRIBIDI_UNICODE_VERSION); fprintf (f, "#ifndef %s\n#define %s\n\n#include \"fribidi.h\"\n\n", FILENAME, FILENAME); fprintf (f, "/*\n" " Mirrored characters include all the characters in the Unicode list\n" " that have been declared as being mirrored and that have a mirrored\n" " equivalent.\n" "\n" " There are lots of characters that are designed as being mirrored\n" " but do not have any mirrored glyph, e.g. the sign for there exist.\n"); fprintf (f, " Are these used in Arabic? That is are all the mathematical signs\n" " that are assigned to be mirrorable actually mirrored in Arabic?\n" " If that is the case, we'll change the below code to include also\n" " characters that mirror to themself. It will then be the responsibility\n" " of the display engine to actually mirror these.\n" "*/\n\n"); fprintf (f, "/* *INDENT-OFF" "* */\n\n"); fprintf (f, "static const struct\n" "{\n" " FriBidiChar ch, mirrored_ch;\n" "}\n" "FriBidiMirroredChars[] =\n" "{\n"); for (i = 0; i < 0x110000; i++) if (table[i]) fprintf (f, " {0x%04X, 0x%04X},\n", i, table[i]); fprintf (f, "} ;\n\n"); fprintf (f, "/* *INDE" "NT-ON* */\n\n"); fprintf (f, "static const int nFriBidiMirroredChars = %d;\n\n", mirroring_count); fprintf (f, "\n#endif /* %s */\n", FILENAME); fclose (f); }
int main (int argc, char **argv) { int max_depth; char file[50], *p; if (argc < 2) err ("usage: fribidi_create_char_types max_depth [UnicodeData.txt path]"); p = (argc >= 3) ? argv[2] : "unidata"; unidata_file = malloc (50 + strlen (p)); sprintf (unidata_file, "%s/UnicodeData.txt", p); max_depth = atoi (argv[1]); if (!max_depth) err ("invalid depth"); if (max_depth < 2 || max_depth > 9) err2 ("invalid max_depth `%s', max_depth should be between 2 and 9", argv[1]); sprintf (file, "fribidi_tab_char_type_%d.i", max_depth); init_table (); read_unicode_data (); write_char_type (file, max_depth); return 0; }
Stokhos::ProductLanczosPCEBasis<ordinal_type, value_type>:: ProductLanczosPCEBasis( ordinal_type p, const Teuchos::Array< Stokhos::OrthogPolyApprox<ordinal_type, value_type> >& pce, const Teuchos::RCP<const Stokhos::Quadrature<ordinal_type, value_type> >& quad, const Teuchos::RCP< const Stokhos::Sparse3Tensor<ordinal_type, value_type> >& Cijk, const Teuchos::ParameterList& params_) : name("Product Lanczos PCE Basis"), params(params_) { Teuchos::RCP<const Stokhos::OrthogPolyBasis<ordinal_type,value_type> > pce_basis = pce[0].basis(); ordinal_type pce_sz = pce_basis->size(); // Check if basis is a product basis Teuchos::RCP<const Stokhos::ProductBasis<ordinal_type,value_type> > prod_basis = Teuchos::rcp_dynamic_cast<const Stokhos::ProductBasis<ordinal_type,value_type> >(pce_basis); Teuchos::Array< Teuchos::RCP<const OneDOrthogPolyBasis<ordinal_type,value_type> > > coord_bases; if (prod_basis != Teuchos::null) coord_bases = prod_basis->getCoordinateBases(); // Build Lanczos basis for each pce bool project = params.get("Project", true); bool normalize = params.get("Normalize", true); bool limit_integration_order = params.get("Limit Integration Order", false); bool use_stieltjes = params.get("Use Old Stieltjes Method", false); Teuchos::Array< Teuchos::RCP<const Stokhos::OneDOrthogPolyBasis<int,double > > > coordinate_bases; Teuchos::Array<int> is_invariant(pce.size(),-2); for (ordinal_type i=0; i<pce.size(); i++) { // Check for pce's lying in invariant subspaces, which are pce's that // depend on only a single dimension. In this case use the corresponding // original coordinate basis. Convention is: -2 -- not invariant, -1 -- // constant, i >= 0 pce depends only on dimension i if (prod_basis != Teuchos::null) is_invariant[i] = isInvariant(pce[i]); if (is_invariant[i] >= 0) { coordinate_bases.push_back(coord_bases[is_invariant[i]]); } // Exclude constant pce's from the basis since they don't represent // stochastic dimensions else if (is_invariant[i] != -1) { if (use_stieltjes) { coordinate_bases.push_back( Teuchos::rcp( new Stokhos::StieltjesPCEBasis<ordinal_type,value_type>( p, Teuchos::rcp(&(pce[i]),false), quad, false, normalize, project, Cijk))); } else { if (project) coordinate_bases.push_back( Teuchos::rcp( new Stokhos::LanczosProjPCEBasis<ordinal_type,value_type>( p, Teuchos::rcp(&(pce[i]),false), Cijk, normalize, limit_integration_order))); else coordinate_bases.push_back( Teuchos::rcp( new Stokhos::LanczosPCEBasis<ordinal_type,value_type>( p, Teuchos::rcp(&(pce[i]),false), quad, normalize, limit_integration_order))); } } } ordinal_type d = coordinate_bases.size(); // Build tensor product basis tensor_lanczos_basis = Teuchos::rcp( new Stokhos::CompletePolynomialBasis<ordinal_type,value_type>( coordinate_bases, params.get("Cijk Drop Tolerance", 1.0e-15), params.get("Use Old Cijk Algorithm", false))); // Build reduced quadrature Teuchos::ParameterList sg_params; sg_params.sublist("Basis").set< Teuchos::RCP< const Stokhos::OrthogPolyBasis<ordinal_type,value_type> > >("Stochastic Galerkin Basis", tensor_lanczos_basis); sg_params.sublist("Quadrature") = params.sublist("Reduced Quadrature"); reduced_quad = Stokhos::QuadratureFactory<ordinal_type,value_type>::create(sg_params); // Build Psi matrix -- Psi_ij = Psi_i(x^j)*w_j/<Psi_i^2> const Teuchos::Array<value_type>& weights = quad->getQuadWeights(); const Teuchos::Array< Teuchos::Array<value_type> >& points = quad->getQuadPoints(); const Teuchos::Array< Teuchos::Array<value_type> >& basis_vals = quad->getBasisAtQuadPoints(); ordinal_type nqp = weights.size(); SDM Psi(pce_sz, nqp); for (ordinal_type i=0; i<pce_sz; i++) for (ordinal_type k=0; k<nqp; k++) Psi(i,k) = basis_vals[k][i]*weights[k]/pce_basis->norm_squared(i); // Build Phi matrix -- Phi_ij = Phi_i(y(x^j)) ordinal_type sz = tensor_lanczos_basis->size(); Teuchos::Array<value_type> red_basis_vals(sz); Teuchos::Array<value_type> pce_vals(d); Phi.shape(sz, nqp); for (int k=0; k<nqp; k++) { ordinal_type jdx = 0; for (int j=0; j<pce.size(); j++) { // Exclude constant pce's if (is_invariant[j] != -1) { // Use the identity mapping for invariant subspaces if (is_invariant[j] >= 0) pce_vals[jdx] = points[k][is_invariant[j]]; else pce_vals[jdx] = pce[j].evaluate(points[k], basis_vals[k]); jdx++; } } tensor_lanczos_basis->evaluateBases(pce_vals, red_basis_vals); for (int i=0; i<sz; i++) Phi(i,k) = red_basis_vals[i]; } bool verbose = params.get("Verbose", false); // Compute matrix A mapping reduced space to original A.shape(pce_sz, sz); ordinal_type ret = A.multiply(Teuchos::NO_TRANS, Teuchos::TRANS, 1.0, Psi, Phi, 0.0); TEUCHOS_ASSERT(ret == 0); //print_matlab(std::cout << "A = " << std::endl, A); // Compute pseudo-inverse of A mapping original space to reduced // A = U*S*V^T -> A^+ = V*S^+*U^T = (S^+*V^T)^T*U^T where // S^+ is a diagonal matrix comprised of the inverse of the diagonal of S // for each nonzero, and zero otherwise Teuchos::Array<value_type> sigma; SDM U, Vt; value_type rank_threshold = params.get("Rank Threshold", 1.0e-12); ordinal_type rank = svd_threshold(rank_threshold, A, sigma, U, Vt); Ainv.shape(sz, pce_sz); TEUCHOS_ASSERT(rank == Vt.numRows()); for (ordinal_type i=0; i<Vt.numRows(); i++) for (ordinal_type j=0; j<Vt.numCols(); j++) Vt(i,j) = Vt(i,j) / sigma[i]; ret = Ainv.multiply(Teuchos::TRANS, Teuchos::TRANS, 1.0, Vt, U, 0.0); TEUCHOS_ASSERT(ret == 0); //print_matlab(std::cout << "Ainv = " << std::endl, Ainv); if (verbose) { std::cout << "rank = " << rank << std::endl; std::cout << "diag(S) = ["; for (ordinal_type i=0; i<rank; i++) std::cout << sigma[i] << " "; std::cout << "]" << std::endl; // Check A = U*S*V^T SDM SVt(rank, Vt.numCols()); for (ordinal_type i=0; i<Vt.numRows(); i++) for (ordinal_type j=0; j<Vt.numCols(); j++) SVt(i,j) = Vt(i,j) * sigma[i] * sigma[i]; // since we divide by sigma // above SDM err_A(pce_sz,sz); err_A.assign(A); ret = err_A.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, -1.0, U, SVt, 1.0); TEUCHOS_ASSERT(ret == 0); std::cout << "||A - U*S*V^T||_infty = " << err_A.normInf() << std::endl; //print_matlab(std::cout << "A - U*S*V^T = " << std::endl, err_A); // Check Ainv*A == I SDM err(sz,sz); err.putScalar(0.0); for (ordinal_type i=0; i<sz; i++) err(i,i) = 1.0; ret = err.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, Ainv, A, -1.0); TEUCHOS_ASSERT(ret == 0); std::cout << "||Ainv*A - I||_infty = " << err.normInf() << std::endl; //print_matlab(std::cout << "Ainv*A-I = " << std::endl, err); // Check A*Ainv == I SDM err2(pce_sz,pce_sz); err2.putScalar(0.0); for (ordinal_type i=0; i<pce_sz; i++) err2(i,i) = 1.0; ret = err2.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, A, Ainv, -1.0); TEUCHOS_ASSERT(ret == 0); std::cout << "||A*Ainv - I||_infty = " << err2.normInf() << std::endl; //print_matlab(std::cout << "A*Ainv-I = " << std::endl, err2); } }
static void savescores(int score) { FILE *handle; int i,j,ch; score_t scores[NUMSCORES]; char header[strlen(SCORE_HEADER)+1]; time_t tmp = 0; if ((handle = fopen(scorefile,"r")) == NULL) { createscores(score); return; } i = fread(header,strlen (SCORE_HEADER),1,handle); if ((i != 1) || (strncmp (SCORE_HEADER,header,strlen (SCORE_HEADER)) != 0)) { createscores (score); return; } for (i = 0; i < NUMSCORES; i++) { j = 0; while ((ch = fgetc(handle)) != '\0') { if ((ch == EOF) || (j >= NAMELEN - 2)) { createscores(score); return; } scores[i].name[j++] = (char) ch; } scores[i].name[j] = '\0'; j = fread(&(scores[i].score),sizeof (int),1,handle); if (j != 1) { createscores (score); return; } j = fread(&(scores[i].timestamp),sizeof (time_t),1,handle); if (j != 1) { createscores (score); return; } } fclose(handle); if (score > scores[NUMSCORES - 1].score) { getname(scores[NUMSCORES - 1].name); scores[NUMSCORES - 1].score = score; scores[NUMSCORES - 1].timestamp = tmp = time (NULL); } qsort(scores,NUMSCORES,sizeof (score_t),cmpscores); if ((handle = fopen(scorefile, "w")) == NULL) { err2(); } strcpy(header, SCORE_HEADER); i = fwrite(header, strlen(SCORE_HEADER), 1, handle); if (i != 1) { err2(); } for (i = 0; i < NUMSCORES; i++) { j = fwrite(scores[i].name,strlen (scores[i].name) + 1,1,handle); if (j != 1) { err2(); } j = fwrite(&(scores[i].score),sizeof (int),1,handle); if (j != 1) { err2(); } j = fwrite(&(scores[i].timestamp),sizeof (time_t),1,handle); if (j != 1) { err2(); } } fclose(handle); fprintf(stderr,"%s",scoretitle); i = 0; while ((i < NUMSCORES) && (scores[i].score != -1)) { j = scores[i].timestamp == tmp ? '*' : ' '; fprintf (stderr,"\t %2d%c %7d %s\n",i + 1,j,scores[i].score,scores[i].name); i++; } fprintf(stderr,"\n"); }