void VectorialFunction::parse()
{
  clear();

  for(Uint i = 0; i < m_functions.size(); ++i)
  {
    FunctionParser* ptr = new FunctionParser();
    ptr->AddConstant("pi", Consts::pi());
    m_parsers.push_back(ptr);

    // CFinfo << "Parsing Function: \'" << m_functions[i] << "\' Vars: \'" << m_vars << "\'\n" << CFendl;
    ptr->Parse(m_functions[i],m_vars);

    if ( ptr->GetParseErrorType() !=  FunctionParser::FP_NO_ERROR )
    {
      std::string msg("ParseError in VectorialFunction::parse(): ");
      msg += " Error [" +std::string(ptr->ErrorMsg()) + "]";
      msg += " Function [" + m_functions[i] + "]";
      msg += " Vars: ["    + m_vars + "]";
      throw common::ParsingFailed (FromHere(),msg);
    }
  }

  m_result.resize(m_functions.size());
  m_is_parsed = true;
}
Exemple #2
0
void Excitation::CalcCustomExcitation(double f0, int nTS, string signal)
{
	if (dT==0) return;
	if (nTS<=0) return;

	Length = (unsigned int)(nTS);
//	cerr << "Operator::CalcSinusExcitation: Length of the excite signal: " << ExciteLength << " timesteps" << endl;
	delete[] Signal_volt;
	delete[] Signal_curr;
	Signal_volt = new FDTD_FLOAT[Length+1];
	Signal_curr = new FDTD_FLOAT[Length+1];
	Signal_volt[0]=0.0;
	Signal_curr[0]=0.0;
	FunctionParser fParse;
	fParse.AddConstant("pi", 3.14159265358979323846);
	fParse.AddConstant("e", 2.71828182845904523536);
	fParse.Parse(signal,"t");
	if (fParse.GetParseErrorType()!=FunctionParser::FP_NO_ERROR)
	{
		cerr << "Operator::CalcCustomExcitation: Function Parser error: " << fParse.ErrorMsg() << endl;
		exit(1);
	}
	double vars[1];
	for (unsigned int n=1; n<Length+1; ++n)
	{
		vars[0] = (n-1)*dT;
		Signal_volt[n] = fParse.Eval(vars);
		vars[0] += 0.5*dT;
		Signal_curr[n] = fParse.Eval(vars);
	}

	m_f_max = f0;
	m_foi = f0;
	SetNyquistNum( CalcNyquistNum(f0,dT) );
}
double ribi::gtst::ParametersGroupReAssign::CalculateNextPeriodPayoff(const double average_payoff) const
{
  FunctionParser f;
  //Parse the formula
  f.Parse(m_next_period_payoff_function,"p");
  assert(f.GetParseErrorType()== FunctionParser::FP_NO_ERROR);

  //Evaluate the parsed formula
  const double payoffs[1] = { average_payoff };
  const double payoff_for_reaching_next_period = f.Eval(payoffs);

  if (f.EvalError()!=0)
  {
    std::clog
      << "Function \'"
      << m_next_period_payoff_function
      << "\'could not be evaluated"
      << " for a payoff of "
      << average_payoff
      << '\n';
    return 0.0;
  }

  return payoff_for_reaching_next_period;
}
///Parse a line
void ribi::gtst::ParametersGroupAssign::Parse(const std::string& s)
{
  if (s.size() > 19 && s.substr(0,19) == "message_unassigned=")
  {
    const std::string t = s.substr(19,s.size()-19);
    SetMessageUnassigned(t);
    return;
  }
  else if (s.size() > 17 && s.substr(0,17) == "message_assigned=")
  {
    const std::string t = s.substr(17,s.size()-17);
    SetMessageAssigned(t);
    return;
  }
  else if (s.size() > 24 && s.substr(0,24) == "waiting_payoff_function=")
  {
    static_assert(sizeof("waiting_payoff_function=") == 24 + 1,"Assume size 24");
    const std::string t = s.substr(24,s.size()-24);
    FunctionParser f;
    //Parse the formula
    f.Parse(t,"p");
    if (f.GetParseErrorType()!= FunctionParser::FP_NO_ERROR)
    {
      throw std::runtime_error("group_assign_waiting_payoff_function could not be parsed, use for example \'sqrt(p)+10\'");
    }
    SetWaitingPayoffFunction(t);
    return;
  }

  throw std::runtime_error(
    (std::string("Unparsable parameter file line: group_assign_") + s).c_str());

}
void ribi::gtst::ParametersGroupReAssign::SetNextPeriodPayoffFunction(const std::string& function)
{
  FunctionParser f;
  //Parse the formula
  f.Parse(function,"p");
  if (f.GetParseErrorType()!= FunctionParser::FP_NO_ERROR)
  {
    throw std::runtime_error("finished_earnings_function could not be parsed, use for example \'sqrt(p)+10\'");
  }
  m_next_period_payoff_function = function;
}
///Parse a line
void ribi::gtst::ParametersGroupReAssign::Parse(const std::string& s)
{
  if (s.size() > 9 && s.substr(0,9) == "duration=")
  {
    const std::string t = s.substr(9,s.size()-9);
    try
    {
      std::stoi(t);
    }
    catch (std::exception&)
    {
      throw std::runtime_error("group_reassign_duration must be an integer");
    }
    const int time = std::stoi(t);
    if (time < 0) throw std::runtime_error("group_reassign_duration must be zero or posive");
    SetDuration(time);
    return;
  }
  else if (s.size() > 18 && s.substr(0,18) == "number_of_periods=")
  {
    const std::string t = s.substr(18,s.size()-18);
    try
    {
      std::stoi(t);
    }
    catch (std::exception&)
    {
      throw std::runtime_error("group_reassign_number_of_periods must be an integer");
    }
    const int n_periods = std::stoi(t);
    if (n_periods < 0) throw std::runtime_error("group_reassign_number_of_periods must be zero or posive");
    SetNumberOfPeriods(n_periods);
    return;
  }
  else if (s.size() > 28 && s.substr(0,28) == "next_period_payoff_function=")
  {
    static_assert(sizeof("next_period_payoff_function=") == 28 + 1,"Assume size 28");
    const std::string t = s.substr(28,s.size()-28);
    FunctionParser f;
    //Parse the formula
    f.Parse(t,"p");
    if (f.GetParseErrorType()!= FunctionParser::FP_NO_ERROR)
    {
      throw std::runtime_error("group_reassign_next_period_payoff_function could not be parsed, use for example \'sqrt(p)+10\'");
    }
    SetNextPeriodPayoffFunction(t);
    return;
  }

  throw std::runtime_error(
    (std::string("Unparsable parameter file line: group_reassign_") + s).c_str());

}
QtDialog::QtDialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::QtDialog),
  m_curve(new QwtPlotCurve("Sine")),
  m_plot(new QwtPlot(QwtText("CppQwtExample1")))
{
  ui->setupUi(this);

  assert(!this->layout());
  QLayout * const my_layout = new QVBoxLayout;
  this->setLayout(my_layout);

  #ifndef NDEBUG
  my_layout->addWidget(new QLabel("DEBUG"));
  #else
  my_layout->addWidget(new QLabel("RELEASE"));
  #endif

  my_layout->addWidget(new QLabel( ("GCC version: " + GetGccVersion()).c_str()));
  //my_layout->addWidget(new QLabel( ("Qt Creator version: " + GetQtCreatorVersion()).c_str()));
  my_layout->addWidget(new QLabel( ("STL version: " + GetStlVersion()).c_str()));
  my_layout->addWidget(new QLabel( ("Boost version: " + GetBoostVersion()).c_str()));


  {
    FunctionParser f;
    f.Parse("x * x","x");
    assert(f.GetParseErrorType()== FunctionParser::FP_NO_ERROR);
    const double xs[1] = { M_PI };
    const double y = f.Eval(xs);
    assert(f.EvalError()==0);
    my_layout->addWidget(new QLabel("Warp's function parser version: 4.5.1"));
  }
  {
    m_plot->setGeometry(0,0,640,400);
    m_plot->setAxisScale(QwtPlot::xBottom, 0.0,2.0 * M_PI);
    m_plot->setAxisScale(QwtPlot::yLeft,-1.0,1.0);
    std::vector<double> xs;
    std::vector<double> ys;
    for (double x = 0; x < 2.0 * M_PI; x+=(M_PI / 10.0))
    {
      xs.push_back(x);
      ys.push_back(std::sin(x));
    }
    QwtPointArrayData * const data = new QwtPointArrayData(&xs[0],&ys[0],xs.size());
    m_curve->setData(data);
    m_curve->attach(m_plot);
    m_plot->replot();
    my_layout->addWidget(m_plot);
  }
}
ribi::FunctionPlotterMainDialog::FunctionPlotterMainDialog(
  const std::string& formula,
  const double x_min,
  const double x_max,
  const int n_cols
) : m_x(std::vector<double>(n_cols,0.0)),
    m_y(std::vector<double>(n_cols,0.0))
{
  #ifndef NDEBUG
  assert(Rescale(2.0,1.0,5.0,0.0,100.0) >= 24.9999 && Rescale(2.0,1.0,5.0,0.0,100.0) < 25.0001);
  #endif

  FunctionParser f;

  f.Parse(formula,"x");
  if (f.GetParseErrorType()!= FunctionParser::FP_NO_ERROR)
  {
    throw std::runtime_error("Function cannot not be parsed");
  }

  if (x_min >= x_max)
  {
    throw std::runtime_error("Value of x_min must be smaller than x_max");
  }

  //Evaluate the function in a 2D std::vector
  const double n_cols_d = static_cast<double>(n_cols);

  for (int x = 0; x!=n_cols; ++x)
  {
    const double xD = static_cast<double>(x);
    const double x_scaled = Rescale(xD,0.0,n_cols_d,x_min,x_max);
    const double xs[1] = { x_scaled };
    const double y = f.Eval(xs);
    if (!f.EvalError())
    {
      m_y[x] = y;
    }
    else
    {
      m_y[x] = 0.0;
    }
    m_x[x] = x_scaled;
  }

}
Exemple #9
0
//_____________________________________________________________________________
//
void tf_set_formula()
//-----------------------------------------------------------------------------
{
  // Parse transfer function formula
  if( tf_fun_formula[0] == '\0' ) return ;
  FunctionParser fparser ;
  int res = fparser.Parse( (const char*)tf_fun_formula, "x" ) ;
  if( fparser.GetParseErrorType() != FunctionParser::NO_SYNTAX_ERROR )
  {
    printf( "transfer formula error: %s\n\t%s\n\t% *d\n", fparser.ErrorMsg(), tf_fun_formula, res, 1 ) ;
    return ;
  }


  // Retreives the color map
  if( tf_colmap < 0 || tf_colmap >= CMAP_NB ) return ;
  const float *colmap = cmaps[tf_colmap] ;

  // allocate the transfer function
  int tf_size = tf_sym ? 512 : 256 ;
  vsvr.tf_set_intern() ;
  vsvr.tf_set_size( tf_size ) ;
  vsvr.tf_alloc() ;

  // fills the texture
  float  x = 0.0f ;
  float dx = 1.0f / (tf_size-1) ;
  for( int i = 0 ; i < tf_size ; ++i, x += dx )
  {
    int k = i ;
    // symmetric
    if( i > 255 ) k = 511 - i ;
    // inverse
    if( tf_inv ) k = 255 - k ;
    const float *col = colmap + 3*k ;

    float a = fparser.Eval( &x ) ;
    if( a < 0.0f ) a = 0.0f ;
    if( a > 1.0f ) a = 1.0f ;

    vsvr.tf_set( i, col[0], col[1], col[2], a*opacity ) ;
  }

  force_reload = true ;
}
void ribi::QtToolSurfacePlotterMainDialog::OnAnyChange()
{
  try { boost::lexical_cast<double>(ui->edit_minx->text().toStdString()); }
  catch (boost::bad_lexical_cast&)
  {
    this->setWindowTitle("Value of x_min is not a valid double"); return;
  }
  try { boost::lexical_cast<double>(ui->edit_miny->text().toStdString()); }
  catch (boost::bad_lexical_cast&)
  {
    this->setWindowTitle("Value of y_min is not a valid double"); return;
  }
  try { boost::lexical_cast<double>(ui->edit_maxx->text().toStdString()); }
  catch (boost::bad_lexical_cast&)
  {
    this->setWindowTitle("Value of x_max is not a valid double"); return;
  }
  try { boost::lexical_cast<double>(ui->edit_maxy->text().toStdString()); }
  catch (boost::bad_lexical_cast&)
  {
    this->setWindowTitle("Value of y_max is not a valid double"); return;
  }

  FunctionParser f;

  //Parse the formula
  f.Parse(ui->edit_equation->text().toStdString().c_str(),"x,y");
  if (f.GetParseErrorType()!= FunctionParser::FP_NO_ERROR)
  {
    this->setWindowTitle("Function cannot not be parsed"); return;
  }


  const double x_min = boost::lexical_cast<double>(ui->edit_minx->text().toStdString());
  const double y_min = boost::lexical_cast<double>(ui->edit_miny->text().toStdString());
  const double x_max = boost::lexical_cast<double>(ui->edit_maxx->text().toStdString());
  const double y_max = boost::lexical_cast<double>(ui->edit_maxy->text().toStdString());

  if (x_min >= x_max)
  {
    this->setWindowTitle("Value of x_min must be smaller than x_max"); return;
  }

  if (y_min >= y_max)
  {
    this->setWindowTitle("Value of y_min must be smaller than y_max"); return;
  }

  //Evaluate the function in a 2D std::vector
  const int n_rows = ui->surfaceplotwidget->height();
  const int n_cols = ui->surfaceplotwidget->width();
  std::vector<std::vector<double> > v(n_rows,std::vector<double>(n_cols,0.0));
  const double n_rows_d = static_cast<double>(n_rows);
  const double n_cols_d = static_cast<double>(n_cols);

  for (int y = 0; y!=n_rows; ++y)
  {
    const double yD = static_cast<double>(y);
    const double y_scaled = Rescale(yD,0.0,n_rows_d,y_min,y_max);
    for (int x = 0; x!=n_cols; ++x)
    {
      const double xD = static_cast<double>(x);
      const double x_scaled = Rescale(xD,0.0,n_cols_d,x_min,x_max);
      const double xs[2] = { x_scaled,y_scaled };
      const double z = f.Eval(xs);
      if (!f.EvalError())
      {
        v[y][x] = z;
      }
      else
      {
        v[y][x] = 0.0;
      }
    }
  }

  this->setWindowTitle("Function plotted successfully");

  //Plot the 2D std::vector
  ui->surfaceplotwidget->SetSurfaceGrey(v);
}