void Basis2D::fillCoefficients(const double* u, double* coefficients) { int N1 = basis1->getRank(); int N2 = basis2->getRank(); // The easy coordinates... for (int iCoord2 = 0; iCoord2 < N2; iCoord2++) { int index = functionIndex(0, iCoord2); // Pointer arithmetic. basis1->fillCoefficients(u + index, coefficients + index); } // The complicated coordinates... double coeffs1d[N2]; double coeffs2d[N2]; for (int iCoord1 = 0; iCoord1 < N1; iCoord1++) { // Copy 1d coefficients for (int iCoord2 = 0; iCoord2 < N2; iCoord2++) { int index = functionIndex(iCoord1, iCoord2); coeffs1d[iCoord2] = coefficients[index]; } basis2->fillCoefficients(coeffs1d, coeffs2d); // Copy the 2d coefficients into the coefficients array. for (int iCoord2 = 0; iCoord2 < N2; iCoord2++) { int index = functionIndex(iCoord1, iCoord2); coefficients[index] = coeffs2d[iCoord2]; } } }
/// Returns the name of active parameter i std::string CompositeFunction::nameOfActive(size_t i) const { size_t iFun = functionIndex(i); std::ostringstream ostr; ostr << 'f' << iFun << '.' << m_functions[iFun]->nameOfActive(i - m_paramOffsets[iFun]); return ostr.str(); }
double* Basis2D::tensorInterpolate(const double* u, const double* coord1, int n1, const double* coord2, int n2) { int nCoord1 = basis1->getRank(); int nCoord2 = basis2->getRank(); double* uInterpolatedAlongCoord2[nCoord1]; for (int iCoord1 = 0; iCoord1 < nCoord1; iCoord1++) { double uConstCoord1[nCoord2]; for (int iCoord2 = 0; iCoord2 < nCoord2; iCoord2++) { int index = functionIndex(iCoord1, iCoord2); uConstCoord1[iCoord2] = u[index]; } uInterpolatedAlongCoord2[iCoord1] = basis2->interpolate(uConstCoord1, coord2, n2); } double* uInterpolated = new double[n1*n2]; for (int i2 = 0; i2 < n2; i2++) { double uConstCoord2[nCoord1]; for (int iCoord1 = 0; iCoord1 < nCoord1; iCoord1++) { uConstCoord2[iCoord1] = uInterpolatedAlongCoord2[iCoord1][i2]; } double* uInterpolatedAlongCoord1 = basis1->interpolate(uConstCoord2, coord1, n1); for (int i1 = 0; i1 < n1; i1++) { int index = i2*n1 + i1; // Copied from functionIndex... uInterpolated[index] = uInterpolatedAlongCoord1[i1]; } //delete[] uInterpolatedAlongCoord1; } for (int i = 0; i < nCoord1; i++) delete[] uInterpolatedAlongCoord2[i]; //delete[] uInterpolatedAlongCoord2; return uInterpolated; }
/** Get the tie of i-th parameter * @param i :: The parameter index * @return A pointer to the tie. */ ParameterTie *CompositeFunction::getTie(size_t i) const { auto tie = IFunction::getTie(i); if (tie == nullptr) { size_t iFun = functionIndex(i); tie = m_functions[iFun]->getTie(i - m_paramOffsets[iFun]); } return tie; }
/// Get constraint /// @param i :: the index /// @return A pointer to the constraint IConstraint *CompositeFunction::getConstraint(size_t i) const { auto constraint = IFunction::getConstraint(i); if (constraint == nullptr) { size_t iFun = functionIndex(i); constraint = m_functions[iFun]->getConstraint(i - m_paramOffsets[iFun]); } return constraint; }
/** Removes i-th parameter's tie if it is tied or does nothing. * @param i :: The index of the tied parameter. * @return True if successfull */ bool CompositeFunction::removeTie(size_t i) { bool foundAndRemovedTie = IFunction::removeTie(i); if (!foundAndRemovedTie) { size_t iFun = functionIndex(i); bool res = m_functions[iFun]->removeTie(i - m_paramOffsets[iFun]); return res; } return foundAndRemovedTie; }
/** Returns the index of parameter i as it declared in its function * @param i :: The parameter index * @param recursive :: If true call parameterLocalName recusively until * a non-composite function is reached. * @return The local index of the parameter */ size_t CompositeFunction::parameterLocalIndex(size_t i, bool recursive) const { size_t iFun = functionIndex(i); auto localIndex = i - m_paramOffsets[iFun]; if (recursive) { auto cf = dynamic_cast<const CompositeFunction *>(m_functions[iFun].get()); if (cf) { return cf->parameterLocalIndex(localIndex, recursive); } } return localIndex; }
/** Remove a constraint * @param parName :: The name of a parameter which constarint to remove. */ void CompositeFunction::removeConstraint(const std::string &parName) { auto i = parameterIndex(parName); auto constraint = IFunction::getConstraint(i); if (constraint != nullptr) { IFunction::removeConstraint(parName); } else { size_t iPar = parameterIndex(parName); size_t iFun = functionIndex(iPar); getFunction(iFun)->removeConstraint(parameterLocalName(iPar)); } }
bool QgsExpression::registerFunction( QgsExpressionFunction *function, bool transferOwnership ) { int fnIdx = functionIndex( function->name() ); if ( fnIdx != -1 ) { return false; } QgsExpression::sFunctions.append( function ); if ( transferOwnership ) QgsExpression::sOwnedFunctions.append( function ); return true; }
/** Returns the name of parameter i as it declared in its function * @param i :: The parameter index * @param recursive :: If true call parameterLocalName recusively until * a non-composite function is reached. * @return The pure parameter name (without the function identifier f#.) */ std::string CompositeFunction::parameterLocalName(size_t i, bool recursive) const { size_t iFun = functionIndex(i); auto localIndex = i - m_paramOffsets[iFun]; auto localFunction = m_functions[iFun].get(); if (recursive) { auto cf = dynamic_cast<const CompositeFunction *>(localFunction); if (cf) { return cf->parameterLocalName(localIndex, recursive); } } return localFunction->parameterName(localIndex); }
const double* Basis2D::getDifferentiationMatrices() { if (differentiationMatrices == NULL) { int n1 = basis1->getRank(); int n2 = basis2->getRank(); int n2D = n1*n2; //Instantiate and zero the matrices. delete[] differentiationMatrices; differentiationMatrices = new double[2*n2D*n2D]; for (int i = 0; i < 2*n2D*n2D; i++) differentiationMatrices[i] = 0.; const double* diff1 = basis1->getDifferentiationMatrix(); const double* diff2 = basis2->getDifferentiationMatrix(); for (int iCoord1 = 0; iCoord1 < n1; iCoord1++) { for (int iCoord2 = 0; iCoord2 < n2; iCoord2++) { int iRow = functionIndex(iCoord1,iCoord2); // The derivative along COORD1. // In this case the derivative only depends on function values that // share the same value of coord2. for (int jCoord1 = 0; jCoord1 < n1; jCoord1++) { int iCol = functionIndex(jCoord1, iCoord2); int iMatrix = matrixIndex(iRow, iCol); int i1D = basis1->index(iCoord1, jCoord1); differentiationMatrices[iMatrix] = diff1[i1D]; } // The derivative along COORD2. // In this case the derivative only depends on function values that // share the same value of coord1. for (int jCoord2 = 0; jCoord2 < n2; jCoord2++) { int iCol = functionIndex(iCoord1, jCoord2); int iMatrix = matrixIndex(iRow, iCol); int i1D = basis2->index(iCoord2, jCoord2); differentiationMatrices[iMatrix + n2D*n2D] = diff2[i1D]; } } } } return differentiationMatrices; }
bool QgsExpression::unregisterFunction( const QString &name ) { // You can never override the built in functions. if ( QgsExpression::BuiltinFunctions().contains( name ) ) { return false; } int fnIdx = functionIndex( name ); if ( fnIdx != -1 ) { QgsExpression::sFunctions.removeAt( fnIdx ); return true; } return false; }
/** Get the tie of i-th parameter * @param i :: The parameter index * @return A pointer to the tie. */ ParameterTie *CompositeFunction::getTie(size_t i) const { size_t iFun = functionIndex(i); return m_functions[iFun]->getTie(i - m_paramOffsets[iFun]); }
/** * Attaches a tie to this function. The attached tie is owned by the function. * @param tie :: A pointer to a new tie */ void CompositeFunction::addTie(ParameterTie *tie) { size_t i = getParameterIndex(*tie); size_t iFun = functionIndex(i); m_functions[iFun]->addTie(tie); }
/** Add a constraint * @param ic :: Pointer to a constraint. */ void CompositeFunction::addConstraint(IConstraint *ic) { size_t i = getParameterIndex(*ic); size_t iFun = functionIndex(i); getFunction(iFun)->addConstraint(ic); }
/// Get constraint /// @param i :: the index /// @return A pointer to the constraint IConstraint *CompositeFunction::getConstraint(size_t i) const { size_t iFun = functionIndex(i); return m_functions[iFun]->getConstraint(i - m_paramOffsets[iFun]); }
/** Remove a constraint * @param parName :: The name of a parameter which constarint to remove. */ void CompositeFunction::removeConstraint(const std::string &parName) { size_t iPar = parameterIndex(parName); size_t iFun = functionIndex(iPar); getFunction(iFun)->removeConstraint(parameterLocalName(iPar)); }
bool QgsExpression::isFunctionName( const QString &name ) { return functionIndex( name ) != -1; }
/** * Get the fitting error for a parameter * @param i :: The index of a parameter * @return :: the error */ double CompositeFunction::getError(size_t i) const { size_t iFun = functionIndex(i); return m_functions[iFun]->getError(i - m_paramOffsets[iFun]); }
/// Change status of parameter void CompositeFunction::setParameterStatus(size_t i, IFunction::ParameterStatus status) { size_t iFun = functionIndex(i); m_functions[iFun]->setParameterStatus(i - m_paramOffsets[iFun], status); }
/// Returns the description of active parameter i std::string CompositeFunction::descriptionOfActive(size_t i) const { size_t iFun = functionIndex(i); std::ostringstream ostr; ostr << m_functions[iFun]->descriptionOfActive(i - m_paramOffsets[iFun]); return ostr.str(); }
/** Removes i-th parameter's tie if it is tied or does nothing. * @param i :: The index of the tied parameter. * @return True if successfull */ bool CompositeFunction::removeTie(size_t i) { size_t iFun = functionIndex(i); bool res = m_functions[iFun]->removeTie(i - m_paramOffsets[iFun]); return res; }
/** Sets a new value to the i-th parameter. * @param i :: The parameter index * @param value :: The new value * @param explicitlySet :: A boolean falgging the parameter as explicitly set * (by user) */ void CompositeFunction::setParameter(size_t i, const double &value, bool explicitlySet) { size_t iFun = functionIndex(i); m_functions[iFun]->setParameter(i - m_paramOffsets[iFun], value, explicitlySet); }
/** * Set the fitting error for a parameter * @param i :: The index of a parameter * @param err :: The error value to set */ void CompositeFunction::setError(size_t i, double err) { size_t iFun = functionIndex(i); m_functions[iFun]->setError(i - m_paramOffsets[iFun], err); }
/** Checks if a constraint has been explicitly set * @param i :: The parameter index * @return true if the function is explicitly set */ bool CompositeFunction::isExplicitlySet(size_t i) const { size_t iFun = functionIndex(i); return m_functions[iFun]->isExplicitlySet(i - m_paramOffsets[iFun]); }
/// Get status of parameter IFunction::ParameterStatus CompositeFunction::getParameterStatus(size_t i) const { size_t iFun = functionIndex(i); return m_functions[iFun]->getParameterStatus(i - m_paramOffsets[iFun]); }
/// Value of i-th active parameter. Override this method to make fitted /// parameters different from the declared double CompositeFunction::activeParameter(size_t i) const { size_t iFun = functionIndex(i); return m_functions[iFun]->activeParameter(i - m_paramOffsets[iFun]); }
/** Sets a new description to the i-th parameter. * @param i :: The parameter index * @param description :: The new description */ void CompositeFunction::setParameterDescription( size_t i, const std::string &description) { size_t iFun = functionIndex(i); m_functions[iFun]->setParameterDescription(i - m_paramOffsets[iFun], description); }
/// Set new value of i-th active parameter. Override this method to make fitted /// parameters different from the declared void CompositeFunction::setActiveParameter(size_t i, double value) { size_t iFun = functionIndex(i); m_functions[iFun]->setActiveParameter(i - m_paramOffsets[iFun], value); }
/** Returns the name of parameter i as it declared in its function * @param i :: The parameter index * @return The pure parameter name (without the function identifier f#.) */ std::string CompositeFunction::parameterLocalName(size_t i) const { size_t iFun = functionIndex(i); return m_functions[iFun]->parameterName(i - m_paramOffsets[iFun]); }