-
Notifications
You must be signed in to change notification settings - Fork 11
/
exprtk_gnuplot.cpp
172 lines (133 loc) · 4.06 KB
/
exprtk_gnuplot.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* ExprTk GNUPlot Example *
* Author: Arash Partow (1999-2024) *
* 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 *
* SPDX-License-Identifier: MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "exprtk.hpp"
class exprtk_gnuplot_fx
{
public:
exprtk_gnuplot_fx()
: min_x_(0.0)
, max_x_(0.0)
, min_y_(0.0)
, max_y_(0.0)
{}
exprtk_gnuplot_fx& set_title(const std::string& title)
{
title_ = title;
return *this;
}
exprtk_gnuplot_fx& set_domain(const double min_x, const double max_x)
{
min_x_ = min_x;
max_x_ = max_x;
return *this;
}
exprtk_gnuplot_fx& set_expression(const std::string& expression)
{
expression_ = expression;
return *this;
}
bool plot()
{
if (!generate_data())
return false;
else
return generate_gp_script();
}
private:
bool generate_gp_script()
{
std::ofstream stream("plot.gp");
if (!stream)
{
return false;
}
stream << "set term png\n";
stream << "set output 'plot.png'\n";
stream << "set xrange[" << min_x_ << ":" << max_x_ <<"]\n";
stream << "set yrange[" << min_y_ << ":" << max_y_ <<"]\n";
stream << "set xzeroaxis\n";
stream << "set yzeroaxis\n";
stream << "plot 'data.dat' using 1:2:(1.0) smooth unique title '" << title_ <<"'\n";
return true;
}
bool generate_data()
{
typedef exprtk::symbol_table<double> symbol_table_t;
typedef exprtk::expression<double> expression_t;
typedef exprtk::parser<double> parser_t;
double x = 0.0;
symbol_table_t symbol_table;
symbol_table.add_constants();
symbol_table.add_variable("x",x);
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
if (!parser.compile(expression_,expression))
{
return false;
}
std::ofstream stream("data.dat");
if (!stream)
{
return false;
}
min_y_ = +std::numeric_limits<double>::max();
max_y_ = -std::numeric_limits<double>::max();
stream << std::setprecision(10);
const double increment = std::min(0.00005,std::abs(max_x_ - min_x_) / 1000.0);
for (x = min_x_; x <= max_x_; x += increment)
{
const double y = expression.value();
if (y < min_y_) min_y_ = y;
else if (y > max_y_) max_y_ = y;
stream << x << "\t" << y << "\n";
}
const double diff_y = std::abs(max_y_ - min_y_);
const double perc7_5 = diff_y * 0.075; //7.5%
min_y_ -= perc7_5;
max_y_ += perc7_5;
return true;
}
std::string title_;
std::string expression_;
double min_x_;
double max_x_;
double min_y_;
double max_y_;
};
int main()
{
exprtk_gnuplot_fx plotter;
plotter
.set_expression("clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)")
.set_domain(-5,+5)
.set_title("ExprTk GNUPlot Example");
plotter.plot();
return 0;
}
/*
Build and Run:
1. c++ -ansi -pedantic-errors -Wall -Wextra -Werror -Wno-long-long -O3 -DNDEBUG -o exprtk_gnuplot exprtk_gnuplot.cpp -lstdc++
2. ./exprtk_gnuplot
3. gnuplot plot.gp
*/