C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_vector_resize_example.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Vector Resize Example *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <cstdio>
21#include <string>
22#include <vector>
23
24#include "exprtk.hpp"
25
26
27template <typename T>
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}
76
77int main()
78{
79 vector_resize_example<double>();
80 return 0;
81}
82
83
84/*
85 Build:
86 c++ -pedantic-errors -Wall -Wextra -Werror -O3 -DNDEBUG -o vector_resize_example01 vector_resize_example01.cpp -L/usr/lib -lstdc++ -lm
87*/
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
void vector_resize_example()
vector_view< T > make_vector_view(T *data, const std::size_t size, const std::size_t offset=0)
Definition exprtk.hpp:4660