int ICovarianceModel::UpdateComponents(const ImmutableTriangularMatrix<double> & minus_hessian, const ImmutableVector<double> & jacobian, const Control & control) { // Decomponse precision this->chol.Decompose(minus_hessian); // Calculate update Vector<double> h = chol.Solve(jacobian); // Constrain update ConstrainUpdate(h, this->theta, control.max_values.vcomp); // Check if update is significant const int update = control.comparer.IsZero(h, this->theta) ? 0 : 1; // Scale update ScaleUpdate(h, control.max_updates.vcomp); // Update this->theta += h; // Constrain value ConstrainValue(this->theta, control.max_values.vcomp); // Print if (control.verbose) Print("Max update variance components: %g\n", MaxAbs(h)); return update; }
int Block::UpdateCoefficients(const ImmutableVector<double> & weights, const ImmutableVector<double> & values, const Control & control) { // Evaluate design Jacobian and design precision const int nvars = this->variables.Size(); Vector<double> design_jacobian(nvars); TriangularMatrix<double> design_precision(nvars); for (int j = 0; j < nvars; ++j) { design_jacobian(j) = Variables::ScalarProduct(this->variables(j), values); for (int k = 0; k <= j; ++k) design_precision(j, k) = Variables::ScalarProduct(this->variables(j), weights, this->variables(k)); } // Decompose full precision this->covariance_model->Decompose(design_precision); // Evaluate coefficients update Vector<double> h = covariance_model->CoefficientsUpdate(design_jacobian, this->beta); // Check if update is significant const int update = control.comparer.IsZero(h, this->beta) ? 0 : 1; // Scale update ScaleUpdate(h, control.max_updates.ranef); // Update this->beta += h; // Print if (control.verbose) Print("Max update random effects: %g\n", MaxAbs(h)); return update; }
void PhaseEnumerationLeafInner ( PhaseEnumerationCache<Field>& cache, const PhaseEnumerationCtrl& ctrl, vector<pair<Int,Field>>& y, const Int beg, const Int baseInf, const Int baseOne ) { EL_DEBUG_CSE const Int n = ctrl.phaseOffsets.back(); const Int minInf = ctrl.minInfNorms.back(); const Int maxInf = ctrl.maxInfNorms.back(); const Int minOne = ctrl.minOneNorms.back(); const Int maxOne = ctrl.maxOneNorms.back(); const bool constrained = (y.size() == 0); SpiralState<Field> spiral; spiral.Initialize( constrained ); while( true ) { const Field beta = spiral.Step(); const Int betaInf = Int(MaxAbs(beta)); const Int betaOne = Int(OneAbs(beta)); const Int newInf = Max(betaInf,baseInf); const Int newOne = betaOne + baseOne; if( newInf > maxInf || newOne > maxOne ) break; if( newOne >= minOne && newInf >= minInf ) { for( Int i=beg; i<n; ++i ) { if( ctrl.enqueueProb >= 1. || SampleUniform<double>(0,1) <= ctrl.enqueueProb ) { y.emplace_back( i, beta ); cache.Enqueue( y ); y.pop_back(); } } } if( newOne < maxOne ) { // We can insert beta into any position and still have room // left for the one norm bound, so do so and then recurse for( Int i=beg; i<n-1; ++i ) { y.emplace_back( i, beta ); PhaseEnumerationLeafInner ( cache, ctrl, y, i+1, newInf, newOne ); y.pop_back(); } } } }
void DiffractionDataSingleCrystal::SetWeightToInvSigma2(const REAL minRelatSigma, const REAL minIobsSigmaRatio) { //:KLUDGE: If less than 1e-6*max, set to 0.... Do not give weight to unobserved points const REAL min=MaxAbs(mObsSigma)*minRelatSigma; for(long i=0;i<mObsSigma.numElements();i++) { if(mObsSigma(i)<min) mWeight(i)=0 ; else mWeight(i) =1./mObsSigma(i)/mObsSigma(i); if(mObsIntensity(i)<(minIobsSigmaRatio*mObsSigma(i))) mWeight(i)=0; } if(1==mGroupOption.GetChoice()) mClockPrepareTwinningCorr.Reset(); // This is not needed for mGroupOption==2 }
int integratorNextStep(integrator_ *r, double x0, double *h) { double y0, ey = 0.0, eyp, e, dd; ReturnErrIf(r == NULL); ReturnErrIf(h == NULL); ReturnErrIf(isnan(x0)); y0 = r->dydx0 * x0 - r->y0; /* Calculate y error */ ey = r->control->reltol * MaxAbs(r->y[r->n%N], y0) + r->abstol; /* Calculate y' error */ eyp = r->control->reltol * MaxAbs(MaxAbs(x0, r->x[r->n%N]) * (*r->ydtdx / r->h[r->n%N]), r->control->chgtol); e = MaxAbs(eyp, ey); /* Calculate Estimated Error of Numerical Intergration */ if(r->control->integratorOrder < 2) { /* Backward-Euler */ dd = DD2(((*r->ydtdx) * x0), (r->f[r->n%N] * r->x[r->n%N]), (r->f[(r->n-1)%N] * r->x[(r->n-1)%N]), r->h[r->n%N], r->h[(r->n-1)%N]); dd *= 1.0/2.0; *h = r->control->trtol * e / MaxAbs(dd , r->abstol); } else { /* Trapazoidal */ dd = DD3(((*r->ydtdx) * x0), (r->f[r->n%N] * r->x[(r->n)%N]), (r->f[(r->n-1)%N] * r->x[(r->n-1)%N]), (r->f[(r->n-2)%N] * r->x[(r->n-2)%N]), r->h[r->n%N], r->h[(r->n-1)%N], r->h[(r->n-2)%N]); /* See page 309 of "The Spice Book" */ dd *= 1.0/12.0; *h = sqrtf(r->control->trtol * e / MaxAbs(dd , r->abstol)); } Debug("h = %e, e = %e, dd = %e", *h, e, dd); ReturnErrIf(isnan(*h)); return 0; }
void cylsph_c ( SpiceDouble r, SpiceDouble lonc, SpiceDouble z, SpiceDouble * radius, SpiceDouble * colat, SpiceDouble * lon ) /* -Brief_I/O VARIABLE I/O DESCRIPTION -------- --- ------------------------------------------------- r I Distance of point from z axis. lonc I Angle (radians) of point from XZ plane. z I Height of point above XY plane. radius O Distance of point from origin. colat O Polar angle (co-latitude in radians) of point. lon O Azimuthal angle (longitude) of point (radians). -Detailed_Input r Distance of the point of interest from z axis. lonc Cylindrical angle (radians) of the point from the XZ plane. z Height of the point above XY plane. -Detailed_Output radius Distance of the point from origin. colat Polar angle (co-latitude in radians) of the point. lon Azimuthal angle (longitude) of the point (radians). -Parameters None. -Particulars This returns the spherical coordinates of a point whose position is input through cylindrical coordinates. -Examples Below are two tables: The first is a set of input values the second is the result of the following sequence of calls to Spicelib routines. Note all input and output angular quantities are in degrees. convrt_c ( lonc, "DEGREES", "RADIANS", lonc ); cylsph_c ( r, lonc, z, &radius, &colat, &lon ); convrt_c ( lon, "RADIANS", "DEGREES", lon ); convrt_c ( lat, "RADIANS", "DEGREES", lat ); Inputs: Results: r lonc z radius lon colat ------ ------ ------ ------ ------ ------ 1.0000 0 0 1.0000 0 90.00 1.0000 90.00 0 1.0000 90.00 90.00 1.0000 180.00 1.000 1.4142 180.00 45.00 1.0000 180.00 -1.000 1.4142 180.00 135.00 0.0000 180.00 1.000 1.0000 180.00 0.00 0.0000 33.00 0 0.0000 33.00 0.00 -Restrictions None. -Exceptions Error free. -Files None. -Author_and_Institution E.D. Wright (JPL) W.L. Taber (JPL) -Literature_References None. -Version -CSPICE Version 1.0.1, 08-FEB-1998 (EDW) Corrected and clarified header entries. -CSPICE Version 1.0.0, 25-OCT-1997 (EDW) -Index_Entries cylindrical to spherical -& */ { /* Begin cylsph_c */ /* Local variables */ SpiceDouble big; SpiceDouble th; SpiceDouble rh; SpiceDouble x; SpiceDouble y; /* Computing biggest absolute value */ big = MaxAbs( r, z ); if (big == 0.) { th = 0.; rh = 0.; } else { x = r / big; y = z / big; rh = big * sqrt( x * x + y * y); th = atan2( r, z ); } /* Move the results to output variables */ *lon = lonc; *radius = rh; *colat = th; } /* End cylsph_c */
const Int n = A.Width(); Ones( dRowA, mA, 1 ); Ones( dRowB, mB, 1 ); Ones( dCol, n, 1 ); // TODO: Expose these as control parameters const Int minIter = 3; const Int maxIter = 6; const Real relTol = Real(9)/Real(10); // TODO: Incorporate damping //const Real damp = Real(1)/Real(1000); //const Real sqrtDamp = Sqrt(damp); // Compute the original ratio of the maximum to minimum nonzero auto maxAbsA = MaxAbs( A ); auto maxAbsB = MaxAbs( B ); const Real maxAbsVal = Max(maxAbsA.value,maxAbsB.value); if( maxAbsVal == Real(0) ) return; const Real minAbsValA = MinAbsNonzero( A, maxAbsVal ); const Real minAbsValB = MinAbsNonzero( B, maxAbsVal ); const Real minAbsVal = Min(minAbsValA,minAbsValB); Real ratio = maxAbsVal / minAbsVal; if( progress && A.Grid().Rank() == 0 ) Output("Original ratio is ",maxAbsVal,"/",minAbsVal,"=",ratio); DistMatrix<Real,MC,STAR> rowScaleA(A.Grid()), rowScaleB(A.Grid()); DistMatrix<Real,MR,STAR> colScale(A.Grid()), colScaleB(B.Grid()); const Int indent = PushIndent();
//----------------------------------------------------------------------------- // TestMatrixMaxAbs //----------------------------------------------------------------------------- bool TestMatrixMaxAbs() { Matrix A("-1,2,-3;4,-5,6;-7,8,-9"); return ApproxEqual( MaxAbs(A), 9.0, TOLERANCE); }
void reclat_c ( ConstSpiceDouble rectan[3], SpiceDouble * radius, SpiceDouble * longitude, SpiceDouble * latitude ) /* -Brief_I/O VARIABLE I/O DESCRIPTION -------- --- -------------------------------------------------- rectan I Rectangular coordinates of a point. radius O Distance of the point from the origin. longitude O Longitude of the point in radians. latitude O Latitude of the point in radians. -Detailed_Input rectan The rectangular coordinates of the input point. `rectan' is a 3-vector. -Detailed_Output radius Distance of the point from the origin. The units associated with `radius' are those associated with the input `rectan'. longitude Longitude of the input point. This is angle between the prime meridian and the meridian containing `rectan'. The direction of increasing longitude is from the +X axis towards the +Y axis. Longitude is output in radians. The range of `longitude' is [-pi, pi]. latitude Latitude of the input point. This is the angle from the XY plane of the ray from the origin through the point. Latitude is output in radians. The range of `latitude' is [-pi/2, pi/2]. -Files None. -Exceptions Error free. 1) If the X and Y components of `rectan' are both zero, the longitude is set to zero. 2) If `rectan' is the zero vector, longitude and latitude are both set to zero. -Particulars None. -Parameters None. -Examples Below are two tables. Listed in the first table (under rectan[0], rectan[1], and rectan[2]) are a number of points whose rectangular coordinates are taken from the set {-1, 0, 1}. The results of the code fragment reclat_c ( rectan, &r, &longitude, &latitude ); latitude *= dpr_c(); longitude *= dpr_c(); are listed to four decimal places in the second parallel table under r (radius), longitude, and latitude. rectan[0] rectan[1] rectan[2] r longitude latitude ------------------------------- ---------------------------- 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 1.0000 0.0000 0.0000 1.0000 0.0000 0.0000 0.0000 1.0000 0.0000 1.0000 90.0000 0.0000 0.0000 0.0000 1.0000 1.0000 0.0000 90.0000 -1.0000 0.0000 0.0000 1.0000 180.0000 0.0000 0.0000 -1.0000 0.0000 1.0000 -90.0000 0.0000 0.0000 0.0000 -1.0000 1.0000 0.0000 -90.0000 1.0000 1.0000 0.0000 1.4142 45.0000 0.0000 1.0000 0.0000 1.0000 1.4142 0.0000 45.0000 0.0000 1.0000 1.0000 1.4142 90.0000 45.0000 1.0000 1.0000 1.0000 1.7320 45.0000 35.2643 -Restrictions None. -Literature_References None. -Author_and_Institution N.J. Bachman (JPL) W.L. Taber (JPL) E.D. Wright (JPL) -Version -CSPICE Version 1.2.1, 30-JUL-2003 (NJB) Various header changes were made to improve clarity. Some minor header corrections were made. -CSPICE Version 1.2.0, 28-AUG-2001 (NJB) Removed tab characters from source file. Now includes interface macro header SpiceZim.h. -CSPICE Version 1.1.0, 21-OCT-1998 (NJB) Made input vector const. -CSPICE Version 1.0.0, 08-FEB-1998 (EDW) -Index_Entries rectangular to latitudinal coordinates -& */ { /* Begin reclat_c */ /* Local variables and definitions. */ SpiceDouble vmax; SpiceDouble x1; SpiceDouble y1; SpiceDouble z1; /* Function Body */ vmax = MaxAbs( rectan[0], MaxAbs( rectan[1], rectan[2] ) ); if ( vmax > 0.) { x1 = rectan[0] / vmax; y1 = rectan[1] / vmax; z1 = rectan[2] / vmax; *radius = vmax * sqrt( x1*x1 + y1*y1 + z1*z1 ); *latitude = atan2(z1, sqrt( x1*x1 + y1*y1 ) ); if ( x1 == 0. && y1 == 0.) { *longitude = 0.; } else { *longitude = atan2(y1, x1); } } else { /* The vector is the zero vector. */ *radius = 0.; *longitude = 0.; *latitude = 0.; } } /* End reclat_c */
SpiceDouble vnormg_c ( ConstSpiceDouble * v1, SpiceInt ndim ) /* -Brief_I/O VARIABLE I/O DESCRIPTION -------- --- -------------------------------------------------- v1 I Vector whose magnitude is to be found. ndim I Dimension of v1. -Detailed_Input v1 This may be any double precision vector or arbitrary size. -Detailed_Output vnormg_c is the magnitude of v1 calculated in a numerically stable way. -Parameters None. -Exceptions 1) If ndim is not physically realistic, greater than zero, a BADDIMENSION error is signaled. The value 0. is returned. -Files None. -Particulars vnormg_c finds the component of v1 whose magnitude is the largest. If the absolute magnitude of that component indicates that a numeric overflow would occur when it is squared, or if it indicates that an underflow would occur when squared (falsely giving a magnitude of zero) then the following expression is used: vnormg_c = v1max * MAGNITUDE OF [ (1/v1max)*v1 ] therwise a simpler expression is used: vnormg_c = MAGNITUDE OF [ v1 ] Beyond the logic described above, no further checking of the validity of the input is performed. -Examples The following table show the correlation between various input vectors v1 and vnormg_c: ndim v1 ndim vnormg_c ----------------------------------------------------------------- 1 (-7.0D20) 1 7.D20 3 (1., 2., 2.) 3 3. 4 (3., 3., 3., 3.) 4 6. 5 (5., 12., 0., 0., 0.) 5 13. 3 (-5.D-17, 0.0, 12.D-17) 3 13.D-17 -Restrictions None. -Author_and_Institution W.M. Owen (JPL) -Literature_References None. -Version -CSPICE Version 1.1.0, 22-OCT-1998 (NJB) Made input vector const. -CSPICE Version 1.0.0, 1-APR-1998 (EDW) -Index_Entries norm of n-dimensional vector -& */ { /* Begin vnormg_c */ /* Local variables */ SpiceInt i; SpiceDouble norm; SpiceDouble scale; /* Use discovery check-in. */ /* Initialize norm and scale to zero. */ norm = 0.; scale = 0.; /* Check ndim is cool. Dimension is positive definite. */ if ( ndim <= 0 ) { chkin_c ( "vnormg_c" ); SpiceError ( "Vector dimension less than or equal to zero", "BADDIMENSION" ); chkout_c ( "vnormg_c" ); return ( 0. ); } /* Determine an appropriate scale factor to prevent numerical overflow. Overflow would be bad! */ for ( i = 0; i < ndim; i++ ) { scale = MaxAbs( scale, v1[i] ); } /* If the vector is zero, return zero. */ if ( scale == 0. ) { return 0.; } /* Do the calculation. Not very involved. */ for ( i = 0; i < ndim; i++ ) { norm += pow( v1[i] / scale, 2 ); } /* Return the value. */ return ( scale * sqrt( norm ) ); } /* End vnormg_c */
SpiceDouble vnorm_c ( ConstSpiceDouble v1[3] ) /* -Brief_I/O VARIABLE I/O DESCRIPTION -------- --- -------------------------------------------------- v1 I Vector whose magnitude is to be found. The function returns the norm of v1. -Detailed_Input v1 may be any 3-dimensional, double precision vector. -Detailed_Output The function returns the magnitude of v1 calculated in a numerically stable way. -Parameters None. -Exceptions Error free. -Files None. -Particulars vnorm_c takes care to avoid overflow while computing the norm of the input vector v1. vnorm_c finds the component of v1 whose magnitude is the largest. Calling this magnitude v1max, the norm is computed using the formula vnorm_c = v1max * || (1/v1max) * v1 || where the notation ||x|| indicates the norm of the vector x. -Examples The following table show the correlation between various input vectors v1 and vnorm_c: v1 vnorm_c ----------------------------------------------------------------- ( 1.e0, 2.e0, 2.e0 ) 3.e0 ( 5.e0, 12.e0, 0.e0 ) 13.e0 ( -5.e-17, 0.0e0, 12.e-17 ) 13.e-17 -Restrictions None. -Literature_References None. -Author_and_Institution N.J. Bachman (JPL) W.L. Taber (JPL) W.M. Owen (JPL) -Version -CSPICE Version 1.0.2, 16-JAN-2008 (EDW) Corrected typos in header titles: Detailed Input to Detailed_Input Detailed Output to Detailed_Output -CSPICE Version 1.0.1, 12-NOV-2006 (EDW) Added Parameters section header. -CSPICE Version 1.0.0, 16-APR-1999 (NJB) -Index_Entries norm of 3-dimensional vector -& */ { /* Begin vnorm_c */ /* Local variables */ SpiceDouble normSqr; SpiceDouble tmp0; SpiceDouble tmp1; SpiceDouble tmp2; SpiceDouble v1max; /* Determine the maximum component of the vector. */ v1max = MaxAbs( v1[0], MaxAbs( v1[1], v1[2] ) ); /* If the vector is zero, return zero; otherwise normalize first. Normalizing helps in the cases where squaring would cause overflow or underflow. In the cases where such is not a problem it not worth it to optimize further. */ if ( v1max == 0.0 ) { return ( 0.0 ); } else { tmp0 = v1[0]/v1max; tmp1 = v1[1]/v1max; tmp2 = v1[2]/v1max; normSqr = tmp0*tmp0 + tmp1*tmp1 + tmp2*tmp2; return ( v1max * sqrt( normSqr ) ); } } /* End vnorm_c */