Add ExprTk_BUILD_EXAMPLES to build examples

Also move examples into an examples folder
This commit is contained in:
Spiros Tsalikis
2023-02-25 15:01:05 -05:00
parent 2c35cb4e74
commit 1491ae63ca
21 changed files with 78 additions and 19 deletions

71
examples/CMakeLists.txt Normal file
View File

@ -0,0 +1,71 @@
# **************************************************************
# * C++ Mathematical Expression Toolkit Library *
# * *
# * Author: Arash Partow (1999-2023) *
# * URL: https://www.partow.net/programming/exprtk/index.html *
# * *
# * Copyright notice: *
# * Free use of the Mathematical Expression Toolkit Library is *
# * permitted under the guidelines and in accordance with the *
# * most current version of the MIT License. *
# * http://www.opensource.org/licenses/MIT *
# * *
# **************************************************************
# create executables for each example
add_executable(exprtk_simple_example_01 exprtk_simple_example_01.cpp)
target_link_libraries(exprtk_simple_example_01 ExprTk)
add_executable(exprtk_simple_example_02 exprtk_simple_example_02.cpp)
target_link_libraries(exprtk_simple_example_02 ExprTk)
add_executable(exprtk_simple_example_03 exprtk_simple_example_03.cpp)
target_link_libraries(exprtk_simple_example_03 ExprTk)
add_executable(exprtk_simple_example_04 exprtk_simple_example_04.cpp)
target_link_libraries(exprtk_simple_example_04 ExprTk)
add_executable(exprtk_simple_example_05 exprtk_simple_example_05.cpp)
target_link_libraries(exprtk_simple_example_05 ExprTk)
add_executable(exprtk_simple_example_06 exprtk_simple_example_06.cpp)
target_link_libraries(exprtk_simple_example_06 ExprTk)
add_executable(exprtk_simple_example_07 exprtk_simple_example_07.cpp)
target_link_libraries(exprtk_simple_example_07 ExprTk)
add_executable(exprtk_simple_example_08 exprtk_simple_example_08.cpp)
target_link_libraries(exprtk_simple_example_08 ExprTk)
add_executable(exprtk_simple_example_09 exprtk_simple_example_09.cpp)
target_link_libraries(exprtk_simple_example_09 ExprTk)
add_executable(exprtk_simple_example_10 exprtk_simple_example_10.cpp)
target_link_libraries(exprtk_simple_example_10 ExprTk)
add_executable(exprtk_simple_example_11 exprtk_simple_example_11.cpp)
target_link_libraries(exprtk_simple_example_11 ExprTk)
add_executable(exprtk_simple_example_12 exprtk_simple_example_12.cpp)
target_link_libraries(exprtk_simple_example_12 ExprTk)
add_executable(exprtk_simple_example_13 exprtk_simple_example_13.cpp)
target_link_libraries(exprtk_simple_example_13 ExprTk)
add_executable(exprtk_simple_example_14 exprtk_simple_example_14.cpp)
target_link_libraries(exprtk_simple_example_14 ExprTk)
add_executable(exprtk_simple_example_15 exprtk_simple_example_15.cpp)
target_link_libraries(exprtk_simple_example_15 ExprTk)
add_executable(exprtk_simple_example_16 exprtk_simple_example_16.cpp)
target_link_libraries(exprtk_simple_example_16 ExprTk)
add_executable(exprtk_simple_example_17 exprtk_simple_example_17.cpp)
target_link_libraries(exprtk_simple_example_17 ExprTk)
add_executable(exprtk_simple_example_18 exprtk_simple_example_18.cpp)
target_link_libraries(exprtk_simple_example_18 ExprTk)
add_executable(exprtk_simple_example_19 exprtk_simple_example_19.cpp)
target_link_libraries(exprtk_simple_example_19 ExprTk)

View File

@ -0,0 +1,57 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 1 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void trig_function()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string expression_string =
"clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";
T x;
symbol_table_t symbol_table;
symbol_table.add_variable("x",x);
symbol_table.add_constants();
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_string,expression);
for (x = T(-5); x <= T(+5); x += T(0.001))
{
const T y = expression.value();
printf("%19.15f\t%19.15f\n", x, y);
}
}
int main()
{
trig_function<double>();
return 0;
}

View File

@ -0,0 +1,72 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 2 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void square_wave()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string expr_string =
"a*(4/pi)*"
"((1 /1)*sin( 2*pi*f*t)+(1 /3)*sin( 6*pi*f*t)+"
" (1 /5)*sin(10*pi*f*t)+(1 /7)*sin(14*pi*f*t)+"
" (1 /9)*sin(18*pi*f*t)+(1/11)*sin(22*pi*f*t)+"
" (1/13)*sin(26*pi*f*t)+(1/15)*sin(30*pi*f*t)+"
" (1/17)*sin(34*pi*f*t)+(1/19)*sin(38*pi*f*t)+"
" (1/21)*sin(42*pi*f*t)+(1/23)*sin(46*pi*f*t)+"
" (1/25)*sin(50*pi*f*t)+(1/27)*sin(54*pi*f*t))";
static const T pi = T(3.141592653589793238462643383279502);
const T f = pi / T(10);
const T a = T(10);
T t = T(0);
symbol_table_t symbol_table;
symbol_table.add_variable("t",t);
symbol_table.add_constant("f",f);
symbol_table.add_constant("a",a);
symbol_table.add_constants();
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expr_string,expression);
const T delta = (T(4) * pi) / T(1000);
for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta)
{
const T result = expression.value();
printf("%19.15f\t%19.15f\n", t, result);
}
}
int main()
{
square_wave<double>();
return 0;
}

View File

@ -0,0 +1,59 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 3 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void polynomial()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string expression_string =
"25x^5 - 35x^4 - 15x^3 + 40x^2 - 15x + 1";
const T r0 = T(0);
const T r1 = T(1);
T x = T(0);
symbol_table_t symbol_table;
symbol_table.add_variable("x",x);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_string,expression);
const T delta = T(1.0 / 100.0);
for (x = r0; x <= r1; x += delta)
{
printf("%19.15f\t%19.15f\n", x, expression.value());
}
}
int main()
{
polynomial<double>();
return 0;
}

View File

@ -0,0 +1,87 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 4 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void fibonacci()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
typedef exprtk::function_compositor<T> compositor_t;
typedef typename compositor_t::function function_t;
compositor_t compositor;
compositor
.add(
function_t( // define function: fibonacci(x)
"fibonacci",
" var w := 0; "
" var y := 0; "
" var z := 1; "
" switch "
" { "
" case x == 0 : 0; "
" case x == 1 : 1; "
" default : "
" while ((x -= 1) > 0) "
" { "
" w := z; "
" z := z + y; "
" y := w; "
" z "
" }; "
" } ",
"x"));
T x = T(0);
symbol_table_t& symbol_table = compositor.symbol_table();
symbol_table.add_constants();
symbol_table.add_variable("x",x);
std::string expression_str = "fibonacci(x)";
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_str,expression);
for (std::size_t i = 0; i < 40; ++i)
{
x = static_cast<T>(i);
const T result = expression.value();
printf("fibonacci(%3d) = %10.0f\n",
static_cast<int>(i),
result);
}
}
int main()
{
fibonacci<double>();
return 0;
}

View File

@ -0,0 +1,80 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 5 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
struct myfunc : public exprtk::ifunction<T>
{
using exprtk::ifunction<T>::operator();
myfunc()
: exprtk::ifunction<T>(2)
{ exprtk::disable_has_side_effects(*this); }
inline T operator()(const T& v1, const T& v2)
{
return T(1) + (v1 * v2) / T(3);
}
};
template <typename T>
inline T myotherfunc(T v0, T v1, T v2)
{
return std::abs(v0 - v1) * v2;
}
template <typename T>
void custom_function()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string expression_string =
"myfunc(sin(x / pi), otherfunc(3 * y, x / 2, x * y))";
T x = T(1);
T y = T(2);
myfunc<T> mf;
symbol_table_t symbol_table;
symbol_table.add_variable("x",x);
symbol_table.add_variable("y",y);
symbol_table.add_function("myfunc",mf);
symbol_table.add_function("otherfunc",myotherfunc);
symbol_table.add_constants();
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_string,expression);
const T result = expression.value();
printf("Result: %10.5f\n",result);
}
int main()
{
custom_function<double>();
return 0;
}

View File

@ -0,0 +1,59 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 6 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void vector_function()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string expression_string =
" for (var i := 0; i < min(x[],y[],z[]); i += 1) "
" { "
" z[i] := 3sin(x[i]) + 2log(y[i]); "
" } ";
T x[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5) };
T y[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5) };
T z[] = { T(0.0), T(0.0), T(0.0), T(0.0), T(0.0) };
symbol_table_t symbol_table;
symbol_table.add_vector("x",x);
symbol_table.add_vector("y",y);
symbol_table.add_vector("z",z);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_string,expression);
expression.value();
}
int main()
{
vector_function<double>();
return 0;
}

View File

@ -0,0 +1,70 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 7 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void logic()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string expression_string = "not(A and B) or C";
symbol_table_t symbol_table;
symbol_table.create_variable("A");
symbol_table.create_variable("B");
symbol_table.create_variable("C");
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_string,expression);
printf(" # | A | B | C | %s\n"
"---+---+---+---+-%s\n",
expression_string.c_str(),
std::string(expression_string.size(),'-').c_str());
for (int i = 0; i < 8; ++i)
{
symbol_table.get_variable("A")->ref() = T((i & 0x01) ? 1 : 0);
symbol_table.get_variable("B")->ref() = T((i & 0x02) ? 1 : 0);
symbol_table.get_variable("C")->ref() = T((i & 0x04) ? 1 : 0);
const int result = static_cast<int>(expression.value());
printf(" %d | %d | %d | %d | %d \n",
i,
static_cast<int>(symbol_table.get_variable("A")->value()),
static_cast<int>(symbol_table.get_variable("B")->value()),
static_cast<int>(symbol_table.get_variable("C")->value()),
result);
}
}
int main()
{
logic<double>();
return 0;
}

View File

@ -0,0 +1,89 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 8 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void composite()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
typedef exprtk::parser_error::type error_t;
typedef exprtk::function_compositor<T> compositor_t;
typedef typename compositor_t::function function_t;
compositor_t compositor;
T x = T(1);
T y = T(2);
symbol_table_t& symbol_table = compositor.symbol_table();
symbol_table.add_constants();
symbol_table.add_variable("x",x);
symbol_table.add_variable("y",y);
compositor
.add(
function_t("f","sin(x / pi)","x")); // f(x) = sin(x / pi)
compositor
.add(
function_t("g","3*[f(x) + f(y)]","x","y")); // g(x,y) = 3[f(x) + f(y)]
std::string expression_string = "g(1 + f(x), f(y) / 2)";
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
if (!parser.compile(expression_string,expression))
{
printf("Error: %s\tExpression: %s\n",
parser.error().c_str(),
expression_string.c_str());
for (std::size_t i = 0; i < parser.error_count(); ++i)
{
const error_t error = parser.get_error(i);
printf("Error: %02d Position: %02d Type: [%14s] Msg: %s\tExpression: %s\n",
static_cast<unsigned int>(i),
static_cast<unsigned int>(error.token.position),
exprtk::parser_error::to_str(error.mode).c_str(),
error.diagnostic.c_str(),
expression_string.c_str());
}
return;
}
const T result = expression.value();
printf("%s = %e\n", expression_string.c_str(), result);
}
int main()
{
composite<double>();
return 0;
}

View File

@ -0,0 +1,151 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 9 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void primes()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
typedef exprtk::function_compositor<T> compositor_t;
typedef typename compositor_t::function function_t;
T x = T(0);
symbol_table_t symbol_table;
symbol_table.add_constants();
symbol_table.add_variable("x",x);
compositor_t compositor(symbol_table);
//Mode 1 - if statement based
compositor
.add(
function_t( // define function: is_prime_impl1(x,y)
"is_prime_impl1",
" if (y == 1,true, "
" if (0 == (x % y),false, "
" is_prime_impl1(x,y - 1))) ",
"x","y"));
compositor
.add(
function_t( // define function: is_prime1(x)
"is_prime1",
" if (frac(x) != 0, false, "
" if (x <= 0, false, "
" is_prime_impl1(x,min(x - 1,trunc(sqrt(x)) + 1)))) ",
"x"));
//Mode 2 - switch statement based
compositor
.add(
function_t( // define function: is_prime_impl2(x,y)
"is_prime_impl2",
" switch "
" { "
" case y == 1 : true; "
" case (x % y) == 0 : false; "
" default : is_prime_impl2(x,y - 1); "
" } ",
"x","y"));
compositor
.add(
function_t( // define function: is_prime2(x)
"is_prime2",
" switch "
" { "
" case x <= 0 : false; "
" case frac(x) != 0 : false; "
" default : is_prime_impl2(x,min(x - 1,trunc(sqrt(x)) + 1)); "
" } ",
"x"));
//Mode 3 - switch statement and while-loop based
compositor
.add(
function_t( // define function: is_prime_impl3(x,y)
"is_prime_impl3",
" while (y > 0) "
" { "
" switch "
" { "
" case y == 1 : ~(y := 0,true); "
" case (x % y) == 0 : ~(y := 0,false); "
" default : y := y - 1; "
" } "
" } ",
"x","y"));
compositor
.add(
function_t( // define function: is_prime3(x)
"is_prime3",
" switch "
" { "
" case x <= 0 : false; "
" case frac(x) != 0 : false; "
" default : is_prime_impl3(x,min(x - 1,trunc(sqrt(x)) + 1)); "
" } ",
"x"));
std::string expression_str1 = "is_prime1(x)";
std::string expression_str2 = "is_prime2(x)";
std::string expression_str3 = "is_prime3(x)";
expression_t expression1;
expression_t expression2;
expression_t expression3;
expression1.register_symbol_table(symbol_table);
expression2.register_symbol_table(symbol_table);
expression3.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_str1,expression1);
parser.compile(expression_str2,expression2);
parser.compile(expression_str3,expression3);
for (std::size_t i = 0; i < 100; ++i)
{
x = static_cast<T>(i);
const T result1 = expression1.value();
const T result2 = expression2.value();
const T result3 = expression3.value();
printf("%03d Result1: %c Result2: %c Result3: %c\n",
static_cast<unsigned int>(i),
(result1 == T(1)) ? 'T' : 'F',
(result2 == T(1)) ? 'T' : 'F',
(result3 == T(1)) ? 'T' : 'F');
}
}
int main()
{
primes<double>();
return 0;
}

View File

@ -0,0 +1,91 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 10 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cmath>
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void newton_sqrt()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
typedef exprtk::function_compositor<T> compositor_t;
typedef typename compositor_t::function function_t;
T x = T(0);
symbol_table_t symbol_table;
symbol_table.add_constants();
symbol_table.add_variable("x",x);
compositor_t compositor(symbol_table);
compositor
.add(
function_t( // define function: newton_sqrt(x)
"newton_sqrt",
" switch "
" { "
" case x < 0 : null; "
" case x == 0 : 0; "
" case x == 1 : 1; "
" default: "
" ~{ "
" var z := 100; "
" var sqrt_x := x / 2; "
" repeat "
" if (equal(sqrt_x^2, x)) "
" break[sqrt_x]; "
" else "
" sqrt_x := (1 / 2) * (sqrt_x + (x / sqrt_x)); "
" until ((z -= 1) <= 0); "
" }; "
" } ",
"x"));
const std::string expression_str = "newton_sqrt(x)";
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_str,expression);
for (std::size_t i = 0; i < 100; ++i)
{
x = static_cast<T>(i);
const T result = expression.value();
printf("sqrt(%03d) - Result: %15.13f\tReal: %15.13f\n",
static_cast<unsigned int>(i),
result,
std::sqrt(x));
}
}
int main()
{
newton_sqrt<double>();
return 0;
}

View File

@ -0,0 +1,70 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 11 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void square_wave2()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string wave_program =
" var r := 0; "
" for (var i := 0; i < 1000; i += 1) "
" { "
" r += (1 / (2i + 1)) * sin((4i + 2) * pi * f * t); "
" }; "
" r *= a * (4 / pi); ";
static const T pi = T(3.141592653589793238462643383279502);
T f = pi / T(10);
T t = T(0);
T a = T(10);
symbol_table_t symbol_table;
symbol_table.add_variable("f",f);
symbol_table.add_variable("t",t);
symbol_table.add_variable("a",a);
symbol_table.add_constants();
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(wave_program,expression);
const T delta = (T(4) * pi) / T(1000);
for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta)
{
const T result = expression.value();
printf("%19.15f\t%19.15f\n", t, result);
}
}
int main()
{
square_wave2<double>();
return 0;
}

View File

@ -0,0 +1,68 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 12 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void bubble_sort()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string bubblesort_program =
" var upper_bound := v[]; "
" var swapped := false; "
" repeat "
" swapped := false; "
" for (var i := 0; i < upper_bound; i += 1) "
" { "
" for (var j := i + 1; j < upper_bound; j += 1) "
" { "
" if (v[i] > v[j]) "
" { "
" v[i] <=> v[j]; "
" swapped := true; "
" }; "
" }; "
" }; "
" upper_bound -= 1; "
" until (not(swapped) or (upper_bound == 0)); ";
T v[] = { T(9.9), T(2.2), T(1.1), T(5.5), T(7.7), T(4.4), T(3.3) };
symbol_table_t symbol_table;
symbol_table.add_vector("v",v);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(bubblesort_program,expression);
expression.value();
}
int main()
{
bubble_sort<double>();
return 0;
}

View File

@ -0,0 +1,99 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 13 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include "exprtk.hpp"
template <typename T>
void savitzky_golay_filter()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string sgfilter_program =
" var weight[9] := "
" { "
" -21, 14, 39, "
" 54, 59, 54, "
" 39, 14, -21 "
" }; "
" "
" if (v_in[] >= weight[]) "
" { "
" var lower_bound := trunc(weight[] / 2); "
" var upper_bound := v_in[] - lower_bound; "
" "
" v_out := 0; "
" "
" for (var i := lower_bound; i < upper_bound; i += 1) "
" { "
" for (var j := -lower_bound; j <= lower_bound; j += 1) "
" { "
" v_out[i] += weight[j + lower_bound] * v_in[i + j]; "
" }; "
" }; "
" "
" v_out /= sum(weight); "
" } ";
const std::size_t n = 1024;
std::vector<T> v_in;
std::vector<T> v_out;
const T pi = T(3.141592653589793238462643383279502);
srand(static_cast<unsigned int>(time(0)));
// Generate a signal with noise.
for (T t = T(-5); t <= T(+5); t += T(10.0 / n))
{
const T noise = T(0.5 * (rand() / (RAND_MAX + 1.0) - 0.5));
v_in.push_back(sin(2.0 * pi * t) + noise);
}
v_out.resize(v_in.size());
symbol_table_t symbol_table;
symbol_table.add_vector("v_in" , v_in );
symbol_table.add_vector("v_out", v_out);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(sgfilter_program,expression);
expression.value();
for (std::size_t i = 0; i < v_out.size(); ++i)
{
printf("%10.6f\t%10.6f\n", v_in[i], v_out[i]);
}
}
int main()
{
savitzky_golay_filter<double>();
return 0;
}

View File

@ -0,0 +1,55 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 14 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void stddev_example()
{
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string stddev_program =
" var x[25] := { "
" 1, 2, 3, 4, 5, "
" 6, 7, 8, 9, 10, "
" 11, 12, 13, 14, 15, "
" 16, 17, 18, 19, 20, "
" 21, 22, 23, 24, 25 "
" }; "
" "
" sqrt(sum([x - avg(x)]^2) / x[]) ";
expression_t expression;
parser_t parser;
parser.compile(stddev_program,expression);
const T stddev = expression.value();
printf("stddev(1..25) = %10.6f\n",stddev);
}
int main()
{
stddev_example<double>();
return 0;
}

View File

@ -0,0 +1,93 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 15 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void black_scholes_merton_model()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string bsm_model_program =
" var d1 := (log(s / x) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
" var d2 := d1 - v * sqrt(t); "
" "
" if (callput_flag == 'call') "
" s * ncdf(d1) - x * e^(-r * t) * ncdf(d2); "
" else if (callput_flag == 'put') "
" x * e^(-r * t) * ncdf(-d2) - s * ncdf(-d1); "
" ";
T s = T(60.00); // Stock price
T x = T(65.00); // Strike price
T t = T( 0.25); // Years to maturity
T r = T( 0.08); // Risk free rate
T v = T( 0.30); // Volatility
std::string callput_flag;
static const T e = exprtk::details::numeric::constant::e;
symbol_table_t symbol_table;
symbol_table.add_variable("s",s);
symbol_table.add_variable("x",x);
symbol_table.add_variable("t",t);
symbol_table.add_variable("r",r);
symbol_table.add_variable("v",v);
symbol_table.add_constant("e",e);
symbol_table.add_stringvar("callput_flag",callput_flag);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(bsm_model_program,expression);
{
callput_flag = "call";
const T bsm = expression.value();
printf("BSM(%s,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f) = %10.6f\n",
callput_flag.c_str(),
s, x, t, r, v,
bsm);
}
{
callput_flag = "put";
const T bsm = expression.value();
printf("BSM(%s,%5.3f,%5.3f,%5.3f,%5.3f,%5.3f) = %10.6f\n",
callput_flag.c_str(),
s, x, t, r, v,
bsm);
}
}
int main()
{
black_scholes_merton_model<double>();
return 0;
}

View File

@ -0,0 +1,81 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 16 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <cstdlib>
#include <string>
#include "exprtk.hpp"
template <typename T>
void linear_least_squares()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string linear_least_squares_program =
" if (x[] == y[]) "
" { "
" beta := (sum(x * y) - sum(x) * sum(y) / x[]) / "
" (sum(x^2) - sum(x)^2 / x[]); "
" "
" alpha := avg(y) - beta * avg(x); "
" "
" rmse := sqrt(sum((beta * x + alpha - y)^2) / y[]); "
" } "
" else "
" { "
" alpha := null; "
" beta := null; "
" rmse := null; "
" } ";
T x[] = {T( 1), T( 2), T(3), T( 4), T( 5), T(6), T( 7), T( 8), T( 9), T(10)};
T y[] = {T(8.7), T(6.8), T(6), T(5.6), T(3.8), T(3), T(2.4), T(1.7), T(0.4), T(-1)};
T alpha = T(0);
T beta = T(0);
T rmse = T(0);
symbol_table_t symbol_table;
symbol_table.add_variable("alpha", alpha);
symbol_table.add_variable("beta" , beta );
symbol_table.add_variable("rmse" , rmse );
symbol_table.add_vector ("x" , x );
symbol_table.add_vector ("y" , y );
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(linear_least_squares_program,expression);
expression.value();
printf("alpha: %15.12f\n", alpha);
printf("beta: %15.12f\n", beta );
printf("rmse: %15.12f\n", rmse );
printf("y = %15.12fx + %15.12f\n", beta, alpha);
}
int main()
{
linear_least_squares<double>();
return 0;
}

View File

@ -0,0 +1,77 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 17 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include "exprtk.hpp"
template <typename T>
struct rnd_01 : public exprtk::ifunction<T>
{
using exprtk::ifunction<T>::operator();
rnd_01() : exprtk::ifunction<T>(0)
{ ::srand(static_cast<unsigned int>(time(NULL))); }
inline T operator()()
{
// Note: Do not use this in production
// Result is in the interval [0,1)
return T(::rand() / T(RAND_MAX + 1.0));
}
};
template <typename T>
void monte_carlo_pi()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string monte_carlo_pi_program =
" var experiments[5 * 10^7] := [(rnd_01^2 + rnd_01^2) <= 1]; "
" 4 * sum(experiments) / experiments[]; ";
rnd_01<T> rnd01;
symbol_table_t symbol_table;
symbol_table.add_function("rnd_01",rnd01);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(monte_carlo_pi_program,expression);
const T approximate_pi = expression.value();
const T real_pi = T(3.141592653589793238462643383279502); // or close enough...
printf("pi ~ %20.17f\terror: %20.17f\n",
approximate_pi,
std::abs(real_pi - approximate_pi));
}
int main()
{
monte_carlo_pi<double>();
return 0;
}

View File

@ -0,0 +1,78 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 18 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void file_io()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string fileio_program =
" var file_name := 'file.txt'; "
" var stream := null; "
" "
" if (stream := open(file_name,'w')) "
" println('Successfully opened file: ' + file_name); "
" else "
" { "
" println('Failed to open file: ' + file_name); "
" return [false]; "
" } "
" "
" var s := 'Hello world...\n'; "
" "
" for (var i := 0; i < 10; i += 1) "
" { "
" write(stream,s); "
" } "
" "
" if (close(stream)) "
" println('Sucessfully closed file: ' + file_name); "
" else "
" { "
" println('Failed to close file: ' + file_name); "
" return [false]; "
" } ";
exprtk::rtl::io::file::package<T> fileio_package;
exprtk::rtl::io::println<T> println;
symbol_table_t symbol_table;
symbol_table.add_function("println",println);
symbol_table.add_package (fileio_package );
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(fileio_program,expression);
printf("Result %10.3f\n",expression.value());
}
int main()
{
file_io<double>();
return 0;
}

View File

@ -0,0 +1,128 @@
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 19 *
* Author: Arash Partow (1999-2023) *
* URL: https://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* https://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
#include "exprtk.hpp"
template <typename T>
class randu : public exprtk::igeneric_function<T>
{
public:
typedef typename exprtk::igeneric_function<T> igfun_t;
typedef typename igfun_t::parameter_list_t parameter_list_t;
typedef typename igfun_t::generic_type generic_type;
typedef typename generic_type::vector_view vector_t;
using exprtk::igeneric_function<T>::operator();
randu()
: exprtk::igeneric_function<T>("V|VTT")
/*
Overloads:
0. V - vector
1. VTT - vector, r0, r1
*/
{ ::srand(static_cast<unsigned int>(time(NULL))); }
inline T operator()(const std::size_t& ps_index, parameter_list_t parameters)
{
vector_t v(parameters[0]);
std::size_t r0 = 0;
std::size_t r1 = v.size() - 1;
if (
(1 == ps_index) &&
!exprtk::rtl::vecops::helper::
load_vector_range<T>::process(parameters, r0, r1, 1, 2, 0)
)
return T(0);
for (std::size_t i = r0; i <= r1; ++i)
{
v[i] = rnd();
}
return T(1);
}
private:
inline T rnd()
{
// Note: Do not use this in production
// Result is in the interval [0,1)
return T(::rand() / T(RAND_MAX + 1.0));
}
};
template <typename T>
void vector_randu()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string vecrandu_program =
" var noise[6] := [0]; "
" "
" if (randu(noise,0,5) == false) "
" { "
" println('Failed to generate noise'); "
" return [false]; "
" } "
" "
" var noisy[6] := signal + (noise - 1/2); "
" "
" for (var i := 0; i < noisy[]; i += 1) "
" { "
" println('noisy[',i,'] = ', noisy[i]); "
" } "
" "
" println('avg: ', avg(noisy)); "
" ";
T signal[] = { T(1.1), T(2.2), T(3.3), T(4.4), T(5.5), T(6.6), T(7.7) };
exprtk::rtl::io::println<T> println;
randu<T> randu;
symbol_table_t symbol_table;
symbol_table.add_vector ("signal" , signal );
symbol_table.add_function("println", println);
symbol_table.add_function("randu" , randu );
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(vecrandu_program,expression);
expression.value();
}
int main()
{
vector_randu<double>();
return 0;
}