void RedbackContinuationTangentAux::compute() { int nb_nodes = _subproblem.mesh().getMesh().n_nodes(); std::vector<dof_id_type> all_node_ids(nb_nodes); // const libMesh::MeshBase::node_iterator begin = _subproblem.mesh().getMesh().nodes_begin(); // const libMesh::MeshBase::node_iterator end = _subproblem.mesh().getMesh().nodes_end(); // for (libMesh::MeshBase::node_iterator k = begin; k != end; ++k) for (int k = 0; k < nb_nodes; ++k) { all_node_ids[ k ] = k; } _subproblem.reinitNodes(all_node_ids, _tid); // compute variables at nodes for (_i = 0; _i < _var.order(); ++_i) // _var.order()=1 for a scalar { Real value = computeValue(); _communicator.sum(value); _var.setValue(_i, value); // update variable data, which is referenced by other kernels, so the value is up-to-date } }
int main(void){ int length; char string[MAXNUM]; scanf("%d", &length); scanf("%d", &mod); scanf("%*[ \n\t\r]%[LP]", string); int i; state last = {0,0,0}; long long int answer = 1; for (i = 0; i < length; i++){ state curr = last; int value = string[i] == 'L' ? 1 : -1; curr.diff += value; updateState(&curr); if (string[i] == 'P'){ state otherBranch = last; int other = -value; otherBranch.diff += other; updateState(&otherBranch); answer += computeValue(otherBranch, length-i-1); answer %= mod; } last = curr; } printf("%lld\n", answer); return 0; }
shared_ptr<StateElement> JamomaAutomation::state() { if (!parent->getRunning()) throw runtime_error("parent time constraint is not running"); // if date hasn't been processed already TimeValue date = parent->getDate(); if (date != mLastDate) { mLastDate = date; // clear the former Value if (mValueToSend) delete mValueToSend; // compute a new value from the Curves mValueToSend = computeValue(parent->getPosition(), mDrive); // edit a Message handling the new Value mMessageToSend = Message::create(mDrivenAddress, mValueToSend); } return mMessageToSend; }
const MemoryLocation &DataflowAnalyzer::computeMemoryLocation(const Term *term, const ReachingDefinitions &definitions) { return dataflow().setMemoryLocation(term, [&]() -> MemoryLocation { switch (term->kind()) { case Term::MEMORY_LOCATION_ACCESS: { return term->asMemoryLocationAccess()->memoryLocation(); } case Term::DEREFERENCE: { auto dereference = term->asDereference(); auto addressValue = computeValue(dereference->address(), definitions); if (addressValue->abstractValue().isConcrete()) { if (dereference->domain() == MemoryDomain::MEMORY) { return MemoryLocation( dereference->domain(), addressValue->abstractValue().asConcrete().value() * CHAR_BIT, dereference->size()); } else { return MemoryLocation( dereference->domain(), addressValue->abstractValue().asConcrete().value(), dereference->size()); } } else if (addressValue->isStackOffset()) { return MemoryLocation(MemoryDomain::STACK, addressValue->stackOffset() * CHAR_BIT, dereference->size()); } else { return MemoryLocation(); } break; } default: { log_.warning(tr("%1: Term kind %2 cannot have a memory location.").arg(Q_FUNC_INFO).arg(term->kind())); return MemoryLocation(); } } }()); }
bool NOX::LineSearch::Polynomial::compute(Abstract::Group& newGrp, double& step, const Abstract::Vector& dir, const Solver::Generic& s) { printOpeningRemarks(); int nNonlinearIters = s.getNumIterations(); if (useCounter) counter.incrementNumLineSearches(); // Get the linear solve tolerance if doing ared/pred for conv criteria std::string direction = const_cast<Teuchos::ParameterList&>(s.getList()). sublist("Direction").get("Method", "Newton"); double eta = (suffDecrCond == AredPred) ? const_cast<Teuchos::ParameterList&>(s.getList()). sublist("Direction").sublist(direction).sublist("Linear Solver"). get("Tolerance", -1.0) : 0.0; // Computations with old group const Abstract::Group& oldGrp = s.getPreviousSolutionGroup(); double oldPhi = meritFuncPtr->computef(oldGrp); // \phi(0) double oldValue = computeValue(oldGrp, oldPhi); double oldSlope = meritFuncPtr->computeSlope(dir, oldGrp); // Computations with new group step = defaultStep; updateGrp(newGrp, oldGrp, dir, step); double newPhi = meritFuncPtr->computef(newGrp); double newValue = computeValue(newGrp, newPhi); bool isConverged = false; bool isFailed = false; int nIters = 1; if (oldSlope >= 0.0) { printBadSlopeWarning(oldSlope); isFailed = true; } else isConverged = checkConvergence(newValue, oldValue, oldSlope, step, eta, nIters, nNonlinearIters); // Increment the number of newton steps requiring a line search if ((useCounter) && (!isConverged)) counter.incrementNumNonTrivialLineSearches(); double prevPhi = 0.0; // \phi(\lambda_{k-1}) double prevPrevPhi = 0.0; // \phi(\lambda_{k-2}) double prevStep = 0.0; // \lambda_{k-1} double prevPrevStep = 0.0; // \lambda_{k-2} while ((!isConverged) && (!isFailed)) { print.printStep(nIters, step, oldValue, newValue, "", (suffDecrCond != AredPred)); if (nIters > maxIters) { isFailed = true; break; } if (interpolationType == Quadratic3) { /* 3-Point Quadratic Interpolation */ prevPrevPhi = prevPhi; prevPhi = newPhi; prevPrevStep = prevStep; prevStep = step; if (nIters == 1) { step = 0.5 * step; } else { double c1 = prevStep * prevStep * (prevPrevPhi - oldPhi) - prevPrevStep * prevPrevStep * (prevPhi - oldPhi); double c2 = prevPrevStep * (prevPhi - oldPhi) - prevStep * (prevPrevPhi - oldPhi); if (c1 < 0) step = -0.5 * c1 / c2; } } else if ((nIters == 1) || (interpolationType == Quadratic)) { /* Quadratic Interpolation */ prevPhi = newPhi; prevStep = step; step = - (oldSlope * prevStep * prevStep) / (2.0 * (prevPhi - oldPhi - prevStep * oldSlope)) ; } else { /* Cubic Interpolation */ prevPrevPhi = prevPhi; prevPhi = newPhi; prevPrevStep = prevStep; prevStep = step; double term1 = prevPhi - oldPhi - prevStep * oldSlope ; double term2 = prevPrevPhi - oldPhi - prevPrevStep * oldSlope ; double a = 1.0 / (prevStep - prevPrevStep) * (term1 / (prevStep * prevStep) - term2 / (prevPrevStep * prevPrevStep)) ; double b = 1.0 / (prevStep - prevPrevStep) * (-1.0 * term1 * prevPrevStep / (prevStep * prevStep) + term2 * prevStep / (prevPrevStep * prevPrevStep)) ; double disc = b * b - 3.0 * a * oldSlope; if (disc < 0) { isFailed = true; break; } if (b > 0.0) // Check to prevent round off error (H. Walker) { step = -oldSlope / (b + sqrt(disc)); } else { if (fabs(a) < 1.e-12) // check for when a is small { step = -oldSlope / (2.0 * b); } else { step = (-b + sqrt(disc))/ (3.0 * a); } } } // Apply bounds if (step < minBoundFactor * prevStep) step = minBoundFactor * prevStep; else if (step > maxBoundFactor * prevStep) step = maxBoundFactor * prevStep; // Check that step isn't too small if (step < minStep) { isFailed = true; break; } // Update the new group and compute new measures updateGrp(newGrp, oldGrp, dir, step); newPhi = meritFuncPtr->computef(newGrp); newValue = computeValue(newGrp, newPhi); nIters ++; if (useCounter) counter.incrementNumIterations(); isConverged = checkConvergence(newValue, oldValue, oldSlope, step, eta, nIters, nNonlinearIters); } // End while loop if (isFailed) { if (useCounter) counter.incrementNumFailedLineSearches(); if (recoveryStepType == Constant) step = recoveryStep; if (step == 0.0) { newGrp = oldGrp; newPhi = oldPhi; newValue = oldValue; } else { updateGrp(newGrp, oldGrp, dir, step); newPhi = meritFuncPtr->computef(newGrp); newValue = computeValue(newGrp, newPhi); } } std::string message = (isFailed) ? "(USING RECOVERY STEP!)" : "(STEP ACCEPTED!)"; print.printStep(nIters, step, oldValue, newValue, message, (suffDecrCond != AredPred)); paramsPtr->set("Adjusted Tolerance", 1.0 - step * (1.0 - eta)); if (useCounter) counter.setValues(*paramsPtr); return (!isFailed); }
qreal KisFlowOpacityOption::getDynamicOpacity(const KisPaintInformation& info) const { return computeValue(info); }
void KisSmudgeRadiusOption::apply(KisPainter& painter, const KisPaintInformation& info, qreal scale, qreal posx, qreal posy,KisPaintDeviceSP dev) const { double sliderValue = computeValue(info); int smudgeRadius = ((sliderValue * scale)*0.5)/100.0;//scale is diameter? KoColor color = painter.paintColor(); if (smudgeRadius == 1) { dev->pixel(posx, posy, &color); painter.setPaintColor(color); } else { const KoColorSpace* cs = dev->colorSpace(); int pixelSize = cs->pixelSize(); quint8* data = new quint8[pixelSize]; static quint8** pixels = new quint8*[2]; qint16* weights = new qint16[2]; pixels[1] = new quint8[pixelSize]; pixels[0] = new quint8[pixelSize]; int loop_increment = 1; if(smudgeRadius >= 8) { loop_increment = (2*smudgeRadius)/16; } int i = 0; int k = 0; int j = 0; KisRandomConstAccessorSP accessor = dev->createRandomConstAccessorNG(0, 0); KisCrossDeviceColorPickerInt colorPicker(painter.device(), color); colorPicker.pickColor(posx, posy, color.data()); for (int y = 0; y <= smudgeRadius; y = y + loop_increment) { for (int x = 0; x <= smudgeRadius; x = x + loop_increment) { for(j = 0;j < 2;j++) { if(j == 1) { y = y*(-1); } for(k = 0;k < 2;k++) { if(k == 1) { x = x*(-1); } accessor->moveTo(posx + x, posy + y); memcpy(pixels[1], accessor->rawDataConst(), pixelSize); if(i == 0) { memcpy(pixels[0],accessor->rawDataConst(),pixelSize); } if (x == 0 && y == 0) { // Because the sum of the weights must be 255, // we cheat a bit, and weigh the center pixel differently in order // to sum to 255 in total // It's -(counts -1), because we'll add the center one implicitly // through that calculation weights[1] = (255 - ((i + 1) * (255 /(i+2) )) ); } else { weights[1] = 255 /(i+2); } i++; if (i>smudgeRadius){i=0;} weights[0] = 255 - weights[1]; const quint8** cpixels = const_cast<const quint8**>(pixels); cs->mixColorsOp()->mixColors(cpixels, weights,2, data); memcpy(pixels[0],data,pixelSize); } x = x*(-1); } y = y*(-1); } } KoColor color = KoColor(pixels[0],cs); painter.setPaintColor(color); for (int l = 0; l < 2; l++){ delete[] pixels[l]; } // delete[] pixels; delete[] data; } }
double KisPressureSoftnessOption::apply(const KisPaintInformation & info) const { if (!isChecked()) return 1.0; return computeValue(info); }
double KisPressureRotationOption::apply(const KisPaintInformation & info) const { if (!isChecked()) return m_defaultAngle; return fmod( (1.0 - computeValue(info)) * 2.0 * M_PI + m_defaultAngle, 2.0 * M_PI); }
Value *DataflowAnalyzer::computeValue(const Term *term, const ReachingDefinitions &definitions) { switch (term->kind()) { case Term::INT_CONST: { auto constant = term->asConstant(); Value *value = dataflow().getValue(constant); value->setAbstractValue(constant->value()); value->makeNotStackOffset(); value->makeNotProduct(); value->makeNotReturnAddress(); return value; } case Term::INTRINSIC: { auto intrinsic = term->asIntrinsic(); Value *value = dataflow().getValue(intrinsic); switch (intrinsic->intrinsicKind()) { case Intrinsic::UNKNOWN: /* FALLTHROUGH */ case Intrinsic::UNDEFINED: { value->setAbstractValue(AbstractValue(term->size(), -1, -1)); value->makeNotStackOffset(); value->makeNotProduct(); value->makeNotReturnAddress(); break; } case Intrinsic::ZERO_STACK_OFFSET: { value->setAbstractValue(AbstractValue(term->size(), -1, -1)); value->makeStackOffset(0); value->makeNotProduct(); value->makeNotReturnAddress(); break; } case Intrinsic::RETURN_ADDRESS: { value->setAbstractValue(AbstractValue(term->size(), -1, -1)); value->makeNotStackOffset(); value->makeNotProduct(); value->makeReturnAddress(); break; } default: { log_.warning(tr("%1: Unknown kind of intrinsic: %2.").arg(Q_FUNC_INFO).arg(intrinsic->intrinsicKind())); break; } } return value; } case Term::MEMORY_LOCATION_ACCESS: /* FALLTHROUGH */ case Term::DEREFERENCE: { const auto &memoryLocation = computeMemoryLocation(term, definitions); const auto &reachingDefinitions = computeReachingDefinitions(term, memoryLocation, definitions); return computeValue(term, memoryLocation, reachingDefinitions); } case Term::UNARY_OPERATOR: return computeValue(term->asUnaryOperator(), definitions); case Term::BINARY_OPERATOR: return computeValue(term->asBinaryOperator(), definitions); case Term::CHOICE: { auto choice = term->asChoice(); auto value = dataflow().getValue(choice); auto preferredValue = computeValue(choice->preferredTerm(), definitions); auto defaultValue = computeValue(choice->defaultTerm(), definitions); if (!dataflow().getDefinitions(choice->preferredTerm()).empty()) { *value = *preferredValue; } else { *value = *defaultValue; } return value; } default: { log_.warning(tr("%1: Unknown term kind: %2.").arg(Q_FUNC_INFO).arg(term->kind())); return dataflow().getValue(term); } } }
void DataflowAnalyzer::execute(const Statement *statement, ReachingDefinitions &definitions) { switch (statement->kind()) { case Statement::INLINE_ASSEMBLY: /* * To be completely correct, one should clear reaching definitions. * However, not doing this usually leads to better code. */ break; case Statement::ASSIGNMENT: { auto assignment = statement->asAssignment(); computeValue(assignment->right(), definitions); handleWrite(assignment->left(), computeMemoryLocation(assignment->left(), definitions), definitions); break; } case Statement::JUMP: { auto jump = statement->asJump(); if (jump->condition()) { computeValue(jump->condition(), definitions); } if (jump->thenTarget().address()) { computeValue(jump->thenTarget().address(), definitions); } if (jump->elseTarget().address()) { computeValue(jump->elseTarget().address(), definitions); } break; } case Statement::CALL: { auto call = statement->asCall(); computeValue(call->target(), definitions); break; } case Statement::HALT: { break; } case Statement::TOUCH: { auto touch = statement->asTouch(); switch (touch->accessType()) { case Term::READ: computeValue(touch->term(), definitions); break; case Term::WRITE: handleWrite(touch->term(), computeMemoryLocation(touch->term(), definitions), definitions); break; case Term::KILL: handleKill(computeMemoryLocation(touch->term(), definitions), definitions); break; default: unreachable(); } break; } case Statement::CALLBACK: { statement->asCallback()->function()(); break; } case Statement::REMEMBER_REACHING_DEFINITIONS: { dataflow_.getDefinitions(statement) = definitions; break; } default: log_.warning(tr("%1: Unknown statement kind: %2.").arg(Q_FUNC_INFO).arg(statement->kind())); break; } }
double KisHatchingPressureThicknessOption::apply(const KisPaintInformation & info) const { if (!isChecked()) return 0.5; return computeValue(info); }
double BinaryOption::getValue() const { return computeValue(); }