shancock884
shancock884

Reputation: 51

Specifying vector literal within ExprTK expression

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:

#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;
}

Upvotes: 5

Views: 84

Answers (1)

ExprTk Developer
ExprTk Developer

Reputation: 1726

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

Upvotes: 11

Related Questions