// Constructor for non-blocked operators Impl(const BoundaryOperator<BasisFunctionType, ResultType>& op_, ConvergenceTestMode::Mode mode_) : op(op_), mode(mode_) { typedef BoundaryOperator<BasisFunctionType, ResultType> BoundaryOp; typedef Solver<BasisFunctionType, ResultType> Solver_; const BoundaryOp& boundaryOp = boost::get<BoundaryOp>(op); if (!boundaryOp.isInitialized()) throw std::invalid_argument("DefaultIterativeSolver::Impl::Impl(): " "boundary operator must be initialized"); if (mode == ConvergenceTestMode::TEST_CONVERGENCE_IN_DUAL_TO_RANGE) { if (boundaryOp.domain()->globalDofCount() != boundaryOp.dualToRange()->globalDofCount()) throw std::invalid_argument("DefaultIterativeSolver::Impl::Impl(): " "non-square system provided"); solverWrapper.reset( new BelosSolverWrapper<ResultType>( Teuchos::rcp<const Thyra::LinearOpBase<ResultType> >( boundaryOp.weakForm()))); } else if (mode == ConvergenceTestMode::TEST_CONVERGENCE_IN_RANGE) { if (boundaryOp.domain()->globalDofCount() != boundaryOp.range()->globalDofCount()) throw std::invalid_argument("DefaultIterativeSolver::Impl::Impl(): " "non-square system provided"); BoundaryOp id = identityOperator( boundaryOp.context(), boundaryOp.range(), boundaryOp.range(), boundaryOp.dualToRange()); pinvId = pseudoinverse(id); shared_ptr<DiscreteBoundaryOperator<ResultType> > totalBoundaryOp = boost::make_shared<DiscreteBoundaryOperatorComposition<ResultType> >( boost::get<BoundaryOp>(pinvId).weakForm(), boundaryOp.weakForm()); solverWrapper.reset( new BelosSolverWrapper<ResultType>( Teuchos::rcp<const Thyra::LinearOpBase<ResultType> >( totalBoundaryOp))); } else throw std::invalid_argument( "DefaultIterativeSolver::DefaultIterativeSolver(): " "invalid convergence test mode"); }
/* alpha = k*dx points should be independent of dx */ gsl_matrix *interp_matrix(double alpha, point *points_in, int npoints_in, point *points_out, int npoints_out, int M, double r_typical) { gsl_matrix *A = bessel_matrix(alpha, points_in, npoints_in, M, r_typical); dump_matrix(A, "A.dat"); gsl_matrix *B = bessel_matrix(alpha, points_out, npoints_out, M, r_typical); dump_matrix(B, "B.dat"); gsl_matrix *A_plus = gsl_matrix_alloc(2*M+1, npoints_in); gsl_matrix *interp = gsl_matrix_alloc(npoints_out, npoints_in); pseudoinverse(A, A_plus); dump_matrix(A_plus, "A_plus.dat"); gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1.0, B, A_plus, 0.0, interp); gsl_matrix_free(A); gsl_matrix_free(A_plus); gsl_matrix_free(B); return interp; }
void convert_to_rgb_fast() { unsigned i,j,c; int row, col, k; ushort *img; float out_cam[3][4]; double num, inverse[3][3]; static const double xyzd50_srgb[3][3] = { { 0.436083, 0.385083, 0.143055 }, { 0.222507, 0.716888, 0.060608 }, { 0.013930, 0.097097, 0.714022 } }; static const double rgb_rgb[3][3] = { { 1,0,0 }, { 0,1,0 }, { 0,0,1 } }; static const double adobe_rgb[3][3] = { { 0.715146, 0.284856, 0.000000 }, { 0.000000, 1.000000, 0.000000 }, { 0.000000, 0.041166, 0.958839 } }; static const double wide_rgb[3][3] = { { 0.593087, 0.404710, 0.002206 }, { 0.095413, 0.843149, 0.061439 }, { 0.011621, 0.069091, 0.919288 } }; static const double prophoto_rgb[3][3] = { { 0.529317, 0.330092, 0.140588 }, { 0.098368, 0.873465, 0.028169 }, { 0.016879, 0.117663, 0.865457 } }; static const double (*out_rgb[])[3] = { rgb_rgb, adobe_rgb, wide_rgb, prophoto_rgb, xyz_rgb }; static const char *name[] = { "sRGB", "Adobe RGB (1998)", "WideGamut D65", "ProPhoto D65", "XYZ" }; static const unsigned phead[] = { 1024, 0, 0x2100000, 0x6d6e7472, 0x52474220, 0x58595a20, 0, 0, 0, 0x61637370, 0, 0, 0x6e6f6e65, 0, 0, 0, 0, 0xf6d6, 0x10000, 0xd32d }; unsigned pbody[] = { 10, 0x63707274, 0, 36, /* cprt */ 0x64657363, 0, 40, /* desc */ 0x77747074, 0, 20, /* wtpt */ 0x626b7074, 0, 20, /* bkpt */ 0x72545243, 0, 14, /* rTRC */ 0x67545243, 0, 14, /* gTRC */ 0x62545243, 0, 14, /* bTRC */ 0x7258595a, 0, 20, /* rXYZ */ 0x6758595a, 0, 20, /* gXYZ */ 0x6258595a, 0, 20 }; /* bXYZ */ static const unsigned pwhite[] = { 0xf351, 0x10000, 0x116cc }; unsigned pcurve[] = { 0x63757276, 0, 1, 0x1000000 }; gamma_curve (gamm[0], gamm[1], 0, 0); memcpy (out_cam, rgb_cam, sizeof out_cam); raw_color |= colors == 1 || document_mode || output_color < 1 || output_color > 5; if (!raw_color) { oprof = (unsigned *) calloc (phead[0], 1); merror (oprof, "convert_to_rgb()"); memcpy (oprof, phead, sizeof phead); if (output_color == 5) oprof[4] = oprof[5]; oprof[0] = 132 + 12*pbody[0]; for (i=0; i < pbody[0]; i++) { oprof[oprof[0]/4] = i ? (i > 1 ? 0x58595a20 : 0x64657363) : 0x74657874; pbody[i*3+2] = oprof[0]; oprof[0] += (pbody[i*3+3] + 3) & -4; } memcpy (oprof+32, pbody, sizeof pbody); oprof[pbody[5]/4+2] = strlen(name[output_color-1]) + 1; memcpy ((char *)oprof+pbody[8]+8, pwhite, sizeof pwhite); pcurve[3] = (short)(256/gamm[5]+0.5) << 16; for (i=4; i < 7; i++) memcpy ((char *)oprof+pbody[i*3+2], pcurve, sizeof pcurve); pseudoinverse ((double (*)[3])out_rgb[output_color-1], inverse, 3); for (i=0; i < 3; i++) for (j=0; j < 3; j++) { for (num = k=0; k < 3; k++) num += xyzd50_srgb[i][k] * inverse[j][k]; oprof[pbody[j*3+23]/4+i+2] = num * 0x10000 + 0.5; } for (i=0; i < phead[0]/4; i++) oprof[i] = htonl(oprof[i]); strcpy ((char *)oprof+pbody[2]+8, "auto-generated by dcraw"); strcpy ((char *)oprof+pbody[5]+12, name[output_color-1]); for (i=0; i < 3; i++) for (j=0; j < colors; j++) for (out_cam[i][j] = k=0; k < 3; k++) out_cam[i][j] += out_rgb[output_color-1][i][k] * rgb_cam[k][j]; } if (verbose) fprintf (stderr, raw_color ? _("Building histograms...\n") : _("Converting to %s colorspace...\n"), name[output_color-1]); memset (histogram, 0, sizeof histogram); if(!raw_color) { __m128 outcam0= {out_cam[0][0],out_cam[1][0],out_cam[2][0],0}, outcam1= {out_cam[0][1],out_cam[1][1],out_cam[2][1],0}, outcam2= {out_cam[0][2],out_cam[1][2],out_cam[2][2],0}, outcam3= {out_cam[0][3],out_cam[1][3],out_cam[2][3],0}; for (img=image[0]; img < image[width*height]; img+=4) { __m128 out0; __m128 vimg0 = {img[0],img[0],img[0],0}, vimg1 = {img[1],img[1],img[1],0}, vimg2 = {img[2],img[2],img[2],0}, vimg3 = {img[3],img[3],img[3],0}; // out[0] = out_cam[0][0] * img[0] // +out_cam[0][1] * img[1] // +out_cam[0][2] * img[2] // +out_cam[0][3] * img[3]; // out[1] = out_cam[1][0] * img[0] // +out_cam[1][1] * img[1] // +out_cam[1][2] * img[2] // +out_cam[1][3] * img[3]; // out[2] = out_cam[2][0] * img[0] // +out_cam[2][1] * img[1] // +out_cam[2][2] * img[2] // +out_cam[2][3] * img[3]; out0 = _mm_add_ps(_mm_add_ps( _mm_mul_ps(vimg0, outcam0), _mm_mul_ps(vimg1, outcam1) ), _mm_add_ps( _mm_mul_ps(vimg2, outcam2), _mm_mul_ps(vimg3, outcam3) )); //clip out0 = _mm_max_ps(_mm_set1_ps(0), _mm_min_ps(_mm_set1_ps(0xffff), _mm_round_ps(out0, _MM_FROUND_TO_ZERO))); __m128i o = _mm_cvtps_epi32(out0); o = _mm_packus_epi32(o,_mm_setzero_si128()); memcpy(img, &o, sizeof(short)*3); FORCC histogram[c][img[c] >> 3]++; } } else if (document_mode) {
/* This routine computes standard and nonstandard earthquake location error estimates. standard error estimate returned is the covariance matrix of the predicted errors that can be used to derive standard error ellipses. The covariance that is returned is unscaled. It is the true covariance only when the weights are ideal 1/sigma weighting. (Note residual (robust) weighting does not really alter this as the goal of residual weighting is to downweight outliers and reshape the residual distribution to be closer to a normal disltribution. The nonstandard error that is returned is the "model error" defined in Pavlis (1986) BSSA, 76, 1699-1717. It would be preferable in some ways to split these into two modules, but it is more efficient to compute them together. Arguments: -input- h - hypocenter location of point to appraise errors from attbl- Associate array of Arrival structures utbl - associate array of slowness structures o- Location options structure -output- C - 4x4 covariance matrix. Assumed allocated externally and passed here. alloc with modified numerical recipes "matrix" routine. emodel - 4 vector of x,y,z model error estimates Both error estimates have 0 in positions where fix is set. Returns 0 if no problems are encountered. Nonzero return indicates a problem. +1 - Inverse matrix was singular to machine precision, error results are unreliable as svd truncation was need to stabilize calculations. Author: Gary L. Pavlis Written: June 25, 1998 */ void predicted_errors(Hypocenter h, Tbl *attbl, Tbl *utbl, Location_options o, double **C, float *emodel) { Arrival *atimes; Slowness_vector *slow; int natimes; int nslow; int npar, nused, ndata_feq; float **U, **V, s[4]; /* SVD of A = USVT */ float **Agi; /* holds computed generalized inverse */ float *b; /* weighted residual vector (rhs of Ax=b)*/ float *r,*w; /* r=raw residual and w= vector of total weights */ float *reswt; /* we store residual weights seperately to calculate effective degrees of freedom */ int m; /* total number of data points = natimes+2*nslow */ Robust_statistics statistics; int mode; int i,ii,j; /* because o is a dynamic variable, we force the plain pseudoinverse soltuion */ o.generalized_inverse = PSEUDOINVERSE; natimes = maxtbl(attbl); nslow = maxtbl(utbl); m = 2*nslow + natimes; for(i=0,npar=0;i<4;++i) if(!o.fix[i]) ++npar; Agi = matrix(0,3,0,m-1); U = matrix(0,m-1,0,3); V = matrix(0,3,0,3); b=(float *)calloc(m,sizeof(float)); r=(float *)calloc(m,sizeof(float)); w=(float *)calloc(m,sizeof(float)); reswt=(float *)calloc(m,sizeof(float)); if ( (b==NULL) || (r==NULL) || (w==NULL) || (reswt==NULL) ) elog_die(1,"Alloc errors in location error function\n"); statistics = form_equations(ALL, h, attbl,utbl, o, U, b, r, w, reswt,&ndata_feq); svdcmp(U,m,npar,s,V); /* This computes the generalized inverse as the pseudoinverse. This version forces this to be the true pseudoinverse no matter what the user selected as a location option. This is my (glp) prejudice that reporting anything else is a lie.*/ nused = pseudoinverse(U,s,V,m,npar,Agi); if(nused != npar ) { elog_log(0,"predicted_errors function found system of equations was singular, Errors estimates are unreliable\n"); } compute_covariance(Agi,m,npar,4,C,o.fix); /* Now we compute the emodel vector defined in Pavlis (1986) BSSA, 76, 1699-1717. A complication is that we need to do a complex rescan of the arrival and slowness data tables. This is necessary to allow different bounding terms for different phases and to obtain most easily the computed travel time to each stations. We just recompute the travel times and use an associated scale factor. For slowness we just enter a fixed value under and assumption slowness errors do not increase with distance as travel times tend to do. */ mode = RESIDUALS_ONLY; for(i=0;i<natimes;++i) { Travel_Time_Function_Output tto; atimes = (Arrival *) gettbl(attbl,i); tto = calculate_travel_time(*atimes, h,mode); if(tto.time == TIME_INVALID) b[i] = 0.0; else { b[i] = tto.time; b[i] *= w[i]*reswt[i]*atimes->phase->deltat_bound; } } for(i=0,j=natimes;i<nslow;++i,j+=2) { slow = (Slowness_vector *) gettbl(utbl,i); b[j] = slow->phase->deltau_bound; b[j+1]=b[j]; } /* we recycle w here blindly assuming m>n */ compute_emodel(Agi, m, npar, b, w); for(i=0,ii=0;i<4;++i) if(o.fix[i]) emodel[i] = 0.0; else { emodel[i] = b[ii]; ++ii; } free_matrix((char **)Agi,0,3,0); free_matrix((char **)U,0,m-1,0); free_matrix((char **)V,0,3,0); free(b); free(r); free(w); free(reswt); }
// Constructor for blocked operators Impl(const BlockedBoundaryOperator<BasisFunctionType, ResultType>& op_, ConvergenceTestMode::Mode mode_) : op(op_), mode(mode_) { typedef BlockedBoundaryOperator<BasisFunctionType, ResultType> BoundaryOp; typedef Solver<BasisFunctionType, ResultType> Solver_; const BoundaryOp& boundaryOp = boost::get<BoundaryOp>(op); if (mode == ConvergenceTestMode::TEST_CONVERGENCE_IN_DUAL_TO_RANGE) { if (boundaryOp.totalGlobalDofCountInDomains() != boundaryOp.totalGlobalDofCountInDualsToRanges()) throw std::invalid_argument("DefaultIterativeSolver::Impl::Impl(): " "non-square system provided"); solverWrapper.reset( new BelosSolverWrapper<ResultType>( Teuchos::rcp<const Thyra::LinearOpBase<ResultType> >( boundaryOp.weakForm()))); } else if (mode == ConvergenceTestMode::TEST_CONVERGENCE_IN_RANGE) { if (boundaryOp.totalGlobalDofCountInDomains() != boundaryOp.totalGlobalDofCountInRanges()) throw std::invalid_argument("DefaultIterativeSolver::Impl::Impl(): " "non-square system provided"); // Construct a block-diagonal operator composed of pseudoinverses // for appropriate spaces BlockedOperatorStructure<BasisFunctionType, ResultType> pinvIdStructure; size_t rowCount = boundaryOp.rowCount(); size_t columnCount = boundaryOp.columnCount(); for (size_t row = 0; row < rowCount; ++row) { // Find the first non-zero block in row #row and retrieve its context shared_ptr<const Context<BasisFunctionType, ResultType> > context; for (int col = 0; col < columnCount; ++col) if (boundaryOp.block(row, col).context()) { context = boundaryOp.block(row, col).context(); break; } assert(context); BoundaryOperator<BasisFunctionType, ResultType> id = identityOperator( context, boundaryOp.range(row), boundaryOp.range(row), boundaryOp.dualToRange(row)); pinvIdStructure.setBlock(row, row, pseudoinverse(id)); } pinvId = BlockedBoundaryOperator<BasisFunctionType, ResultType>( pinvIdStructure); shared_ptr<DiscreteBoundaryOperator<ResultType> > totalBoundaryOp = boost::make_shared<DiscreteBoundaryOperatorComposition<ResultType> >( boost::get<BoundaryOp>(pinvId).weakForm(), boundaryOp.weakForm()); solverWrapper.reset( new BelosSolverWrapper<ResultType>( Teuchos::rcp<const Thyra::LinearOpBase<ResultType> >( totalBoundaryOp))); } else throw std::invalid_argument( "DefaultIterativeSolver::DefaultIterativeSolver(): " "invalid convergence test mode"); }