summaryrefslogtreecommitdiff
path: root/a4/octave.cc
diff options
context:
space:
mode:
Diffstat (limited to 'a4/octave.cc')
-rw-r--r--a4/octave.cc90
1 files changed, 90 insertions, 0 deletions
diff --git a/a4/octave.cc b/a4/octave.cc
new file mode 100644
index 0000000..e48fe6c
--- /dev/null
+++ b/a4/octave.cc
@@ -0,0 +1,90 @@
+#include "octave.h"
+
+const Plot& operator<<(std::ostream& out, const Plot& plot)
+{
+ out << "x = [ ";
+ for(const auto& [x,y] : plot.data)
+ {
+ out << x << " ";
+ }
+ out << "]\n";
+
+ out << "y = [ ";
+ for(const auto& [x,y] : plot.data)
+ {
+ out << y << " ";
+ }
+ out << "]\n";
+ out << "scatter(x, y)\n";
+ return plot;
+}
+
+const std::string& Plot::getTitle() const
+{
+ return title;
+}
+
+void Plot::add(const std::pair<double, double>& point)
+{
+ data.push_back(point);
+}
+
+void Plot::add(double y)
+{
+ if(y < 1000.0)
+ {
+ add({x, y});
+ }
+}
+
+void Plot::setX(double x)
+{
+ this->x = x;
+}
+
+Octave::Octave(const std::string& file)
+ : out(file + ".m")
+ , file(file)
+{
+}
+
+Octave::~Octave()
+{
+ if(!out.good())
+ {
+ return;
+ }
+
+ out << "fig = figure()\n";
+ out << "hold on;\n";
+
+ for(const auto& plot : plots)
+ {
+ out << plot;
+ }
+
+ out << "legend ({";
+ for(const auto& plot : plots)
+ {
+ out << "'" << plot.getTitle() << "' " ;
+ }
+ out << "}, 'location', 'east');\n";
+ out << "xlabel ('" << x_axis << "');\n";
+ out << "ylabel ('" << y_axis << "');\n";
+// out << "set(gca, 'yscale', 'log')\n";
+ out << "set(gcf, 'PaperPosition', [0 0 5 5])\n";
+ out << "set(gcf, 'PaperSize', [5 5])\n";
+ out << "print(fig, '" << file << ".pdf');\n";
+}
+
+Plot& Octave::add(const std::string& title)
+{
+ plots.push_back(title);
+ return plots.back();
+}
+
+void Octave::setAxis(const std::string& x_axis, const std::string& y_axis)
+{
+ this->x_axis = x_axis;
+ this->y_axis = y_axis;
+}