18 std::string filename,
char separator,
bool overwrite) {
19 this->values = vals_to_write;
20 this->method = write_method;
21 this->separator = separator;
22 this->overwrite = overwrite;
23 this->filename = filename;
28 std::cout <<
"The found root is " << this->values.row(this->values.rows() - 1)(0) << std::endl;
30 std::unique_ptr<PrinterBase<Eigen::Vector2d>> printer;
31 this->build_printer(printer);
33 for (
int i = 0; i < this->values.rows(); i++) {
34 Eigen::Vector2d row = this->values.row(i);
35 printer->write_values(row);
40 gp->generate_gnuplot_script();
41 std::cout <<
"Gnuplot script generated: " << this->filename <<
".plt\n";
48 switch (this->method) {
50 printer = std::make_unique<PrinterCLI<V>>();
53 printer = std::make_unique<PrinterCSV<V>>(this->filename, this->separator, this->overwrite);
56 printer = std::make_unique<PrinterDAT<V>>(this->filename, this->overwrite);
59 printer = std::make_unique<PrinterGNUPlot<V>>(this->filename, this->overwrite);
62 std::cerr <<
"\033[31mError: Unknown writing method.\033[0m\n";
63 std::exit(EXIT_FAILURE);
69 std::cout <<
"Here are the iterations of the method: " << std::endl;
74 std::cout <<
"x = " << value(0) <<
" --- f(x) = " << value(1) << std::endl;
79 this->filename = fname;
80 this->append = ow_mode;
82 file.open(this->filename, std::ios::trunc);
84 file.open(this->filename, std::ios::app);
88 if (!file.is_open()) {
89 std::cerr <<
"\033[31mError: could not open file " << this->filename <<
" for writing.\033[0m\n";
90 std::exit(EXIT_FAILURE);
101 file << value(0) << this->separator << value(1) << std::endl;
109 file << value(0) <<
" " << value(1) << std::endl;
117 std::string base_name = this->filename;
118 size_t last_dot = base_name.find_last_of(
".");
119 if (last_dot != std::string::npos) {
120 base_name = base_name.substr(0, last_dot);
123 std::string png_file = base_name +
".png";
124 std::string plt_file = base_name +
".plt";
126 std::ofstream script(plt_file);
127 if (!script.is_open()) {
128 std::cerr <<
"\033[31mError: could not open gnuplot script file.\033[0m\n";
129 std::exit(EXIT_FAILURE);
132 script <<
"# Auto-generated gnuplot script\n"
133 <<
"set terminal pngcairo size 1000,800 enhanced font 'Arial,12'\n"
134 <<
"set output '" << png_file <<
"'\n"
135 <<
"set title 'Root-Finding Iterations'\n"
136 <<
"set xlabel 'x'\n"
137 <<
"set ylabel 'f(x)'\n"
139 <<
"plot '" << this->filename
140 <<
"' using 1:2 with linespoints lt rgb 'blue' pt 7 lw 2 title 'Iteration Path'\n";
145 if (std::system(
"which gnuplot > /dev/null 2>&1") == 0) {
146 std::system((
"gnuplot " + plt_file).c_str());
147 std::cout <<
"Gnuplot image generated: " << png_file << std::endl;
149 std::cerr <<
"\033[33mWarning: gnuplot not found. Script generated but PNG not created.\033[0m\n";
150 std::exit(EXIT_FAILURE);
Abstract Printer class.
Definition writer_def.hpp:69
void write_values(const V &value) override
Method to actually print a given value in the output.
PrinterCLI()
Constructor for the PrinterCLI class - just prints out a string.
Definition writer.hpp:68
PrinterCSV(const std::string &fname, char sep, bool ow_mode)
Constructor of the PrinterCSV class.
Definition writer.hpp:95
void write_values(const V &value) override
Writes a given result into the .csv file with the sotred separator.
char separator
Separator for the .csv output file, given at construction time.
Definition writer_def.hpp:134
Class to write on .dat the result - daughter of FilePrinter and Mother of GnuPlotPrinter.
Definition writer_def.hpp:115
void write_values(const V &value) override
Writes a given result into the .dat file.
PrinterDAT(const std::string &fname, bool ow_mode)
Constructor for the PrinterDAT class.
Definition writer.hpp:105
Mother class for all the Printers which print in a file.
Definition writer_def.hpp:97
PrinterFile(const std::string &fname, bool ow_mode)
The constructor for PrinterFile class.
Definition writer.hpp:78
Class Daughter of PrinterDAT to write on .dat (inherited) and produce a gnu plot for the results.
Definition writer_def.hpp:155
PrinterGNUPlot(const std::string &fname, bool ow_mode)
Constructor for the PrinterGNUPlot.
Definition writer.hpp:113
void generate_gnuplot_script() const
Calls the generation of the .png output plot.
Writer(const T &vals_to_write, WritingMethod write_method, std::string filename="output", char separator=',', bool overwrite=true)
Constructor for a Writer object.
void build_printer(std::unique_ptr< PrinterBase< V > > &printer)
Method to convert the generic Printer into a typed one for a specific output destination.
Definition writer.hpp:47
void write()
Method to run the printing loop and correctly initialize the Printer.
Contains definitions for classes Writer and Printer to Write on different output destinations.
WritingMethod
Definition writer_def.hpp:24
@ CONSOLE
Definition writer_def.hpp:24
@ CSV
Definition writer_def.hpp:24
@ DAT
Definition writer_def.hpp:24
@ GNUPLOT
Definition writer_def.hpp:24