C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Functions
exprtk_vector_resize_example.cpp File Reference
#include <cstdio>
#include <string>
#include <vector>
#include "exprtk.hpp"
Include dependency graph for exprtk_vector_resize_example.cpp:

Go to the source code of this file.

Functions

template<typename T >
void vector_resize_example ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 77 of file exprtk_vector_resize_example.cpp.

78{
79 vector_resize_example<double>();
80 return 0;
81}

◆ vector_resize_example()

template<typename T >
void vector_resize_example ( )

Definition at line 28 of file exprtk_vector_resize_example.cpp.

29{
30 typedef exprtk::symbol_table<T> symbol_table_t;
31 typedef exprtk::expression<T> expression_t;
32 typedef exprtk::parser<T> parser_t;
33
34 std::vector<T> v0 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
35 std::vector<T> v1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
36
38
39 T n = T(0);
40
41 symbol_table_t symbol_table;
42 symbol_table.add_variable("n", n);
43 symbol_table.add_vector ("v", vv);
44
45 expression_t expression;
46 expression.register_symbol_table(symbol_table);
47
48 parser_t parser;
49
50 const std::string vector_resize_expression =
51 " sum(v) == ((v[]^2 + v[]) / 2) and sum(2v) == (n^2 + n)";
52
53 if (!parser.compile(vector_resize_expression, expression))
54 {
55 printf("Error: %s\tExpression: %s\n",
56 parser.error().c_str(),
57 vector_resize_expression.c_str());
58 return;
59 }
60
61 for (std::size_t i = 1; i <= vv.base_size(); ++i)
62 {
63 vv.set_size(i);
64 vv.rebase(i % 2 ? v0.data() : v1.data());
65
66 n = T(i);
67
68 const T result = expression.value();
69
70 if (result != T(1))
71 {
72 printf("Error: vector size: %d\n", static_cast<unsigned int>(i));
73 }
74 }
75}
void rebase(data_ptr_t data)
Definition exprtk.hpp:4576
std::size_t base_size() const
Definition exprtk.hpp:4594
bool set_size(const std::size_t new_size)
Definition exprtk.hpp:4634
vector_view< T > make_vector_view(T *data, const std::size_t size, const std::size_t offset=0)
Definition exprtk.hpp:4660

References exprtk::vector_view< T >::base_size(), exprtk::make_vector_view(), exprtk::vector_view< T >::rebase(), and exprtk::vector_view< T >::set_size().

Here is the call graph for this function: