当前位置: 首页>>代码示例>>C++>>正文


C++ MyParser::DefineConst方法代码示例

本文整理汇总了C++中MyParser::DefineConst方法的典型用法代码示例。如果您正苦于以下问题:C++ MyParser::DefineConst方法的具体用法?C++ MyParser::DefineConst怎么用?C++ MyParser::DefineConst使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MyParser的用法示例。


在下文中一共展示了MyParser::DefineConst方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: removeDataSingularities

void NonLinearFit::removeDataSingularities()
{
	MyParser parser;
	for (int i = 0; i < d_p; i++){
		double param = gsl_vector_get(d_param_init, i);
		parser.DefineVar(d_param_names[i].ascii(), &param);
	}

	QMapIterator<QString, double> it(d_constants);
 	while (it.hasNext()) {
     	it.next();
		parser.DefineConst(it.key().ascii(), it.value());
 	}

	double xvar;
	parser.DefineVar("x", &xvar);
	parser.SetExpr(d_formula.ascii());

    for (int i = 0; i < d_n; i++){
    	xvar = d_x[i];
    	try {
			parser.EvalRemoveSingularity(&xvar);
    	} catch(MyParser::Pole){
			QApplication::restoreOverrideCursor();
			QMessageBox::critical((ApplicationWindow *)parent(), QObject::tr("QtiPlot"),
			QObject::tr("Ignored data point at x = %1.").arg(xvar));

    		removePole(i);
    	}
    }
}
开发者ID:BackupTheBerlios,项目名称:qtiplot-svn,代码行数:31,代码来源:NonLinearFit.cpp

示例2: calculateFitCurveData

void NonLinearFit::calculateFitCurveData(double *X, double *Y)
{
	MyParser parser;
	for (int i=0; i<d_p; i++)
		parser.DefineVar(d_param_names[i].ascii(), &d_results[i]);

	QMapIterator<QString, double> i(d_constants);
 	while (i.hasNext()) {
     	i.next();
		parser.DefineConst(i.key().ascii(), i.value());
 	}

	double x;
	parser.DefineVar("x", &x);
	parser.SetExpr(d_formula.ascii());

	if (d_gen_function){
		double X0 = d_x[0];
		double step = (d_x[d_n - 1] - X0)/(d_points - 1);
		for (int i=0; i<d_points; i++){
		    x = X0 + i*step;
			X[i] = x;
			Y[i] = parser.EvalRemoveSingularity(&x, false);
		}
	} else {
		for (int i=0; i<d_points; i++) {
		    x = d_x[i];
			X[i] = x;
			Y[i] = parser.EvalRemoveSingularity(&x, false);
		}
	}
}
开发者ID:BackupTheBerlios,项目名称:qtiplot-svn,代码行数:32,代码来源:NonLinearFit.cpp

示例3: setFormula

bool NonLinearFit::setFormula(const QString& s, bool guess)
{
	if (s.isEmpty()){
		QMessageBox::critical((ApplicationWindow *)parent(),  tr("QtiPlot - Input function error"),
				tr("Please enter a valid non-empty expression! Operation aborted!"));
		d_init_err = true;
		return false;
	}

	if (d_formula == s)
		return true;

	if (guess)
		setParametersList(guessParameters(s));
	if (!d_p){
		QMessageBox::critical((ApplicationWindow *)parent(), tr("QtiPlot - Fit Error"),
				tr("There are no parameters specified for this fit operation. Please define a list of parameters first!"));
		d_init_err = true;
		return false;
	}

	try {
		double *param = new double[d_p];
		MyParser parser;
		double xvar;
		parser.DefineVar("x", &xvar);
		for (int k = 0; k < (int)d_p; k++){
			param[k] = gsl_vector_get(d_param_init, k);
			parser.DefineVar(d_param_names[k].ascii(), &param[k]);
		}

		QMapIterator<QString, double> i(d_constants);
 		while (i.hasNext()) {
     		i.next();
			parser.DefineConst(i.key().ascii(), i.value());
 		}

		parser.SetExpr(s.ascii());
		parser.Eval() ;
		delete[] param;
	} catch(mu::ParserError &e){
		QMessageBox::critical((ApplicationWindow *)parent(),  tr("QtiPlot - Input function error"), QString::fromStdString(e.GetMsg()));
		d_init_err = true;
		return false;
	}

	d_init_err = false;
	d_formula = s;
	return true;
}
开发者ID:BackupTheBerlios,项目名称:qtiplot-svn,代码行数:50,代码来源:NonLinearFit.cpp

示例4: eval

double NonLinearFit::eval(double *par, double x)
{
	MyParser parser;
	for (int i=0; i<d_p; i++)
		parser.DefineVar(d_param_names[i].ascii(), &par[i]);

	QMapIterator<QString, double> i(d_constants);
 	while (i.hasNext()) {
     	i.next();
		parser.DefineConst(i.key().ascii(), i.value());
 	}

	parser.DefineVar("x", &x);
	parser.SetExpr(d_formula.ascii());
    return parser.EvalRemoveSingularity(&x, false);
}
开发者ID:BackupTheBerlios,项目名称:qtiplot-svn,代码行数:16,代码来源:NonLinearFit.cpp

示例5: user_d

double user_d(const gsl_vector * x, void *params) {
    int n = ((struct FitData *)params)->n;
    int p = ((struct FitData *)params)->p;
    double *X = ((struct FitData *)params)->X;
    double *Y = ((struct FitData *)params)->Y;
    double *sigma = ((struct FitData *)params)->sigma;

	NonLinearFit *fitter = (NonLinearFit *)((struct FitData *) params)->fitter;
	const char *function = fitter->formula().ascii();
	QStringList parNames = fitter->parameterNames();

    double val=0;
    MyParser parser;
    try {
        double *parameters = new double[p];
        double xvar;
        parser.DefineVar("x", &xvar);
        for (int i=0; i < p; i++) {
            parameters[i]=gsl_vector_get(x,i);
            parser.DefineVar(parNames[i].ascii(), &parameters[i]);
        }

		QMapIterator<QString, double> i(fitter->constants());
 		while (i.hasNext()){
     		i.next();
			parser.DefineConst(i.key().ascii(), i.value());
 		}

        parser.SetExpr(function);
        for (int j = 0; j < n; j++) {
            xvar = X[j];
			double s = 1.0/sqrt(sigma[j]);
            try {
				double t = (parser.EvalRemoveSingularity(&xvar) - Y[j])/s;
				val += t*t;
			} catch (MyParser::Pole) {
				return GSL_POSINF; //weird, I know. blame gsl.
			}
        }
        delete[] parameters;
    } catch (mu::ParserError &e) {
        QMessageBox::critical(0,"QtiPlot - Input function error",QString::fromStdString(e.GetMsg()));
        return GSL_EINVAL;
    }
    return val;
}
开发者ID:kuzavas,项目名称:qtiplot,代码行数:46,代码来源:fit_gsl.cpp

示例6: user_df

int user_df(const gsl_vector *x, void *params, gsl_matrix *J) {
    int n = ((struct FitData *)params)->n;
    int p = ((struct FitData *)params)->p;
    double *X = ((struct FitData *)params)->X;
    double *sigma = ((struct FitData *)params)->sigma;

	NonLinearFit *fitter = (NonLinearFit *)((struct FitData *) params)->fitter;
	const char *function = fitter->formula().ascii();
	QStringList parNames = fitter->parameterNames();

	try {
        double *param = new double[p];
        MyParser parser;
        double xvar;
        parser.DefineVar("x", &xvar);
        for (int k=0; k<p; k++) {
            param[k] = gsl_vector_get(x,k);
            parser.DefineVar(parNames[k].ascii(), &param[k]);
        }

		QMapIterator<QString, double> i(fitter->constants());
 		while (i.hasNext()){
     		i.next();
			parser.DefineConst(i.key().ascii(), i.value());
 		}

        parser.SetExpr(function);
        for (int i = 0; i < n; i++) {
            xvar = X[i];
			double s = 1.0/sqrt(sigma[i]);
            for (int j = 0; j < p; j++)
	        try {
				gsl_matrix_set (J, i, j, 1.0/s*parser.DiffRemoveSingularity(&xvar, &param[j], param[j]));
			} catch (MyParser::Pole) {
				return GSL_ESING;
			}
        }
        delete[] param;
    } catch (mu::ParserError &) {
        return GSL_EINVAL;
    }
    return GSL_SUCCESS;
}
开发者ID:kuzavas,项目名称:qtiplot,代码行数:43,代码来源:fit_gsl.cpp

示例7: loadData

bool FunctionCurve::loadData(int points, bool xLog10Scale)
{
    if (!points)
        points = dataSize();

	double *X = (double *)malloc(points*sizeof(double));
	if (!X){
		QMessageBox::critical(0, QObject::tr("QtiPlot - Memory Allocation Error"),
		QObject::tr("Not enough memory, operation aborted!"));
		return false;
	}
	double *Y = (double *)malloc(points*sizeof(double));
	if (!Y){
		QMessageBox::critical(0, QObject::tr("QtiPlot - Memory Allocation Error"),
		QObject::tr("Not enough memory, operation aborted!"));
		free(X);
		return false;
	}

	double step = (d_to - d_from)/(double)(points - 1.0);
	if (d_function_type == Normal){
		MyParser parser;
		double x = d_from;
		try {
			parser.DefineVar(d_variable.ascii(), &x);
			QMapIterator<QString, double> i(d_constants);
			while (i.hasNext()){
				i.next();
				parser.DefineConst(i.key().ascii(), i.value());
			}
			parser.SetExpr(d_formulas[0].ascii());

			int lastButOne = points - 1;
			try {
				double xl = x, xr;
				double y = parser.EvalRemoveSingularity(&x, false);
				bool wellDefinedFunction = true;
				if (!gsl_finite(y)){// try to find a first well defined point (might help for some not really bad functions)
					wellDefinedFunction = false;
					for (int i = 0; i < lastButOne; i++){
						xl = x;
						x += step;
						xr = x;
						y = parser.Eval();
						if (gsl_finite(y)){
							wellDefinedFunction = true;
							int iter = 0;
							double x0 = x, y0 = y;
							while(fabs(xr - xl)/step > 1e-15 && iter < points){
								x = 0.5*(xl + xr);
								y = parser.Eval();
								if (gsl_finite(y)){
									xr = x;
									x0 = x;
									y0 = y;
								} else
									xl = x;
								iter++;
							}
							d_from = x0;
							X[0] = x0;
							Y[0] = y0;
							step = (d_to - d_from)/(double)(lastButOne);
							break;
						}
					}
					if (!wellDefinedFunction){
						QMessageBox::critical(0, QObject::tr("QtiPlot"),
						QObject::tr("The function %1 is not defined in the specified interval!").arg(d_formulas[0]));
						free(X); free(Y);
						return false;
					}
				} else {
					X[0] = d_from;
					Y[0] = y;
				}
			} catch (MyParser::Pole) {}

			ScaleEngine *sc_engine = 0;
			if (plot())
				sc_engine = (ScaleEngine *)plot()->axisScaleEngine(xAxis());

			if (xLog10Scale || (d_from > 0 && d_to > 0 && sc_engine &&
				sc_engine->type() == ScaleTransformation::Log10)){
				step = log10(d_to/d_from)/(double)(points - 1);
				for (int i = 1; i < lastButOne; i++ ){
					x = d_from*pow(10, i*step);
					X[i] = x;
					try {
						Y[i] = parser.EvalRemoveSingularity(&x, false);
					} catch (MyParser::Pole){}
				}
			} else {
				for (int i = 1; i < lastButOne; i++ ){
					x += step;
					X[i] = x;
					try {
						Y[i] = parser.EvalRemoveSingularity(&x, false);
					} catch (MyParser::Pole){}
				}
//.........这里部分代码省略.........
开发者ID:BackupTheBerlios,项目名称:qtiplot-svn,代码行数:101,代码来源:FunctionCurve.cpp


注:本文中的MyParser::DefineConst方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。