Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying vector literal within ExprTK expression

Tags:

c++

exprtk

I would like to be able to specify a vector literal within an ExprTK expression to pass as a function argument, but I couldn't see what the syntax is or if it was possible. I am trying with "all_true({1,1,0,1,1})".... does someone know if I am using an incorrect syntax or if its just not possible like this?

Example code is below, in which I try 3 ways:

  • "all_true(y)" => using variable instead of literal obviously works (y is double[5] and registered via add_vector)
  • "all_true({1,1,0,1,1})" => This is what I'd like to work! ERROR: ERR238 - Expected '}' instead of: ','
  • "var v[5]:={1,1,0,1,1};all_true(v)" => works as a workaround, but not as neat/intuitive as I would like for end-users!
#include "exprtk.hpp"
#include <stdio.h>

int main(void) {
   typedef exprtk::symbol_table<double> symbol_table_t;
   typedef exprtk::expression<double>   expression_t;
   typedef exprtk::parser<double>       parser_t;
   typedef exprtk::parser_error::type      err_t;

   double y[] = { 1, 1, 0, 1, 1 };

   exprtk::rtl::vecops::package<double> vecops_package;

   symbol_table_t symbol_table;
   symbol_table.add_vector("y",y);
   symbol_table.add_package(vecops_package); 

   expression_t expression;
   expression.register_symbol_table(symbol_table);

   parser_t parser;
   
   // all_true(y)
   if (parser.compile("all_true(y)",expression)) {
       printf("%.1f\n",expression.value());
   }
   else {
      printf("Error: %s\n", parser.error().c_str());
   }
   
   // all_true({1,1,0,1,1})
   if (parser.compile("all_true({1,1,0,1,1})",expression)) {
       printf("%.1f\n",expression.value());
   }
   else {
      printf("Error: %s\n", parser.error().c_str());
   }
   
   // var v[5]:={1,1,0,1,1};all_true(v)
   if (parser.compile("var v[5]:={1,1,0,1,1};all_true(v)",expression)) {
       printf("%.1f\n",expression.value());
   }
   else {
      printf("Error: %s\n", parser.error().c_str());
   }
   
   return 0;
}
like image 717
shancock884 Avatar asked Jun 25 '26 17:06

shancock884


1 Answers

As you have denoted the first two examples work. However the curly-brace syntax "{}" is essentially equivalent to an initialiser list in C++. In short it's not the same type as a vector, and is only supposed to be used when initialising vectors.

If the values of the vector are all known at compile-time, such is the case in the third example, perhaps then the values could simply be passed to the all_true free function, like the following:

all_true(1,1,0,1,1)

The following functions that take vectors have similar input styles:

all_false, all_true, any_true, any_false, count, sum, min, max, avg


btw looks like Parrot is doing something similar: https://developer.parrot.com/docs/sphinx/sensors_alteration_syntax.html

like image 154
ExprTk Developer Avatar answered Jun 28 '26 10:06

ExprTk Developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!