Exemplo n.º 1
0
void CompressHistogram(uchar *ch, ulong *h, ulong max, int size)
{
  int i;
  uchar v;
  
  for(i=0; i<size; i++){
    v = ComputeNorm((float) h[i] / (float) max);
    ch[i] = (uchar)(v);
  }
}
Exemplo n.º 2
0
void LinearNormalizeHistogram(uchar *ch, float *h, float max, int size)
{
  int i;
  uchar v;
  
  for(i=0; i<size; i++){
    v = ComputeNorm((float) h[i] / (float) max);
    ch[i] = (uchar)(v);
  }
}
void BackwardEulerIvpOdeSolver::CalculateNextYValue(AbstractOdeSystem* pAbstractOdeSystem,
                                                    double timeStep,
                                                    double time,
                                                    std::vector<double>& rCurrentYValues,
                                                    std::vector<double>& rNextYValues)
{
    // Check the size of the ODE system matches the solvers expected
    assert(mSizeOfOdeSystem == pAbstractOdeSystem->GetNumberOfStateVariables());

    unsigned counter = 0;
//        const double eps = 1e-6 * rCurrentGuess[0]; // Our tolerance (should use min(guess) perhaps?)
    const double eps = 1e-6; // JonW tolerance
    double norm = 2*eps;

    std::vector<double> current_guess(mSizeOfOdeSystem);
    current_guess.assign(rCurrentYValues.begin(), rCurrentYValues.end());

    while (norm > eps)
    {
        // Calculate Jacobian and mResidual for current guess
        ComputeResidual(pAbstractOdeSystem, timeStep, time, rCurrentYValues, current_guess);
        ComputeJacobian(pAbstractOdeSystem, timeStep, time, rCurrentYValues, current_guess);
//            // Update norm (our style)
//            norm = ComputeNorm(mResidual);

        // Solve Newton linear system
        SolveLinearSystem();

        // Update norm (JonW style)
        norm = ComputeNorm(mUpdate);

        // Update current guess
        for (unsigned i=0; i<mSizeOfOdeSystem; i++)
        {
            current_guess[i] -= mUpdate[i];
        }

        counter++;
        assert(counter < 20); // avoid infinite loops
    }
    rNextYValues.assign(current_guess.begin(), current_guess.end());
}
AztecOO_StatusType
AztecOO_StatusTestResNorm::CheckStatus(int CurrentIter, 
                                       Epetra_MultiVector * CurrentResVector, 
                                       double CurrentResNormEst,  
                                       bool SolutionUpdated)
{
  (void)CurrentIter;
  Epetra_Vector * crv = dynamic_cast<Epetra_Vector *>(CurrentResVector);

  // This section computes the norm of the residual vector
  if (restype_==Implicit && resnormtype_==TwoNorm && CurrentResNormEst!=-1.0) 
    resvalue_ = CurrentResNormEst;
  else if (crv==0) { // Cannot proceed because there is no norm est or res vector
    status_ = Failed;
    return(status_);
  }
  else if (restype_==Explicit && SolutionUpdated) {
    curresvecexplicit_ = true;
    if (localresvector_==0) localresvector_ = new Epetra_Vector(crv->Map());
    // Compute explicit residual
    operator_.Apply(lhs_, *localresvector_);
    localresvector_->Update(1.0, rhs_, -1.0); // localresvector_ = rhs_ - operator_* lhs_
    if (resweights_!=0) { // Check if we should scale the vector
      // localresvector_ = resweights_ * localresvector_
      localresvector_->Multiply(1.0, *resweights_, *localresvector_, 0.0);
    }
    resvalue_ = ComputeNorm(*localresvector_, resnormtype_);
  }
  else {
    curresvecexplicit_ = false;
    if (resweights_!=0) { // Check if we should scale the vector
      if (localresvector_==0) localresvector_ = new Epetra_Vector(crv->Map());
      // localresvector_ = resweights_ * localresvector_
      localresvector_->Multiply(1.0, *resweights_, *crv, 0.0);
      resvalue_ = ComputeNorm(*localresvector_, resnormtype_);
    }
    else
      resvalue_ = ComputeNorm(*crv, resnormtype_);
  }


  // Compute scaling term (done once)
  if (firstcallCheckStatus_) {
    if (scaletype_==NormOfRHS) {
      if (scaleweights_!=0) { // Check if we should scale the vector
	if (localresvector_==0) localresvector_ = new Epetra_Vector(rhs_.Map());
	// localresvector = scaleweights_ * rhs_
	localresvector_->Multiply(1.0, *scaleweights_, rhs_, 0.0);
	scalevalue_ = ComputeNorm(*localresvector_, resnormtype_);
      }
      else {
	scalevalue_ = ComputeNorm(rhs_, scalenormtype_);
      }
    }
    else if (scaletype_==NormOfInitRes) {
      if (restype_==Implicit && scalenormtype_==TwoNorm && CurrentResNormEst!=-1.0) 
	scalevalue_ = CurrentResNormEst;
      else {
	if (scaleweights_!=0) { // Check if we should scale the vector
	  if (localresvector_==0) localresvector_ = new Epetra_Vector(crv->Map());
	  // weightedrhs = scaleweights_ * initial residual
	  localresvector_->Multiply(1.0, *scaleweights_, *crv, 0.0);
	  scalevalue_ = ComputeNorm(*localresvector_, resnormtype_);
	}
	else {
	  scalevalue_ = ComputeNorm(rhs_, scalenormtype_);
	}
      }
    }
    if (scalevalue_==0.0) {
      status_ = Failed;
      return(status_);
    } 
  }

  testvalue_ = resvalue_/scalevalue_;
  if (testvalue_>tolerance_) 
    if (convergedOnce_) {
      if (numExtraIterations_<maxNumExtraIterations_) {
	numExtraIterations_++;
	status_ = Unconverged;
      }
      else {
	status_ = PartialFailed;
      }
    }
    else {
      status_ = Unconverged;
    }
  else if (testvalue_<=tolerance_) {
    convergedOnce_ = true;	  
    status_ = Converged;
  }
  else
    status_ = NaN;

  firstcallCheckStatus_ = false;
  return status_;
}