aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjustanothercatgirl <sotov2070@gmail.com>2024-11-24 19:47:38 +0300
committerjustanothercatgirl <sotov2070@gmail.com>2024-11-24 19:47:38 +0300
commit8237f82ebd2299addc6dec837d5f850cc5fd1b5c (patch)
tree06f32b24512f54a466e4bd6a0e3d51d0bcf68aef
parent33e3d9540237b0faef3c7d9aeab6dedbccbe3707 (diff)
parentfcf77faf19dcb9d75b7842ec7138551bd73418cf (diff)
Merge remote-tracking branch 'origin/master'
-rw-r--r--101/Makefile18
-rw-r--r--101/README.md5
-rw-r--r--101/compile_flags.txt4
-rw-r--r--101/ex111
-rw-r--r--101/ex211
-rw-r--r--101/ex311
l---------101/include1
-rwxr-xr-x101/mainbin0 -> 621352 bytes
-rw-r--r--101/main.cpp137
-rw-r--r--101/mass20
-rw-r--r--101/plots.gp14
-rw-r--r--107/Makefile18
-rw-r--r--107/README.md5
-rw-r--r--107/compile_flags.txt4
-rw-r--r--107/data15
-rw-r--r--107/data215
-rw-r--r--107/data225
-rwxr-xr-x107/mainbin0 -> 335376 bytes
-rw-r--r--107/main.cpp46
-rw-r--r--107/plots.gp14
-rw-r--r--compile_flags.txt4
-rw-r--r--libprakpp/include/prakcommon.hpp19
-rw-r--r--libprakpp/include/prakmath.hpp24
-rw-r--r--libprakpp/include/prakmatrix.hpp2
-rw-r--r--libprakpp/include/praktable.hpp80
-rwxr-xr-xvtek3/mainbin655552 -> 0 bytes
26 files changed, 447 insertions, 16 deletions
diff --git a/101/Makefile b/101/Makefile
new file mode 100644
index 0000000..eb309ff
--- /dev/null
+++ b/101/Makefile
@@ -0,0 +1,18 @@
+
+CFLAGS = -std=c++2c -mavx -Iinclude -ggdb
+
+.PHONY: all run_main clean gnuplot
+
+run: gnuplot
+
+gnuplot: plots.gp run_main
+ gnuplot $<
+
+run_main: main
+ ./main
+
+main: main.cpp include/*
+ $(CXX) -o $@ $< $(CFLAGS)
+
+clean:
+ rm -fr main *.png *.plot
diff --git a/101/README.md b/101/README.md
new file mode 100644
index 0000000..96fd716
--- /dev/null
+++ b/101/README.md
@@ -0,0 +1,5 @@
+<!-- Шаблон для прака -->
+<!-- файлы, заканчивающиеся на .plot считаются генерируемыми и удаляются через make clean -->
+# Обработка <> прака
+
+
diff --git a/101/compile_flags.txt b/101/compile_flags.txt
new file mode 100644
index 0000000..34ae930
--- /dev/null
+++ b/101/compile_flags.txt
@@ -0,0 +1,4 @@
+-Iinclude
+-std=c++2c
+-mavx2
+
diff --git a/101/ex1 b/101/ex1
new file mode 100644
index 0000000..6c38a0b
--- /dev/null
+++ b/101/ex1
@@ -0,0 +1,11 @@
+N a1 a2 a3 a sa acal sacal
+1 0.13346 0.13171 0.132942 ? ? ? ?
+2 0.282912 0.282706 0.28394 ? ? ? ?
+3 0.429228 0.428942 0.429562 ? ? ? ?
+4 0.569562 0.567884 0.56956 ? ? ? ?
+5 0.7058 0.704906 0.70637 ? ? ? ?
+6 0.838736 0.821824 0.84288 ? ? ? ?
+7 0.969494 0.967614 0.9688 ? ? ? ?
+8 1.095644 1.094602 1.094976 ? ? ? ?
+9 1.217494 1.21835 1.219444 ? ? ? ?
+10 1.33011 1.324222 1.335216 ? ? ? ?
diff --git a/101/ex2 b/101/ex2
new file mode 100644
index 0000000..c6750fc
--- /dev/null
+++ b/101/ex2
@@ -0,0 +1,11 @@
+N m1 m2 a1 a2 a3 a
+1 ? ? 0.133954 0.132362 0.135396 ?
+2 ? ? 0.125120 0.129666 0.129296 ?
+3 ? ? 0.121898 0.123508 0.122264 ?
+4 ? ? 0.119830 0.119476 0.116626 ?
+5 ? ? 0.110080 0.111944 0.112238 ?
+6 ? ? 0.108780 0.111320 0.110016 ?
+7 ? ? 0.108712 0.106222 0.107966 ?
+8 ? ? 0.104368 0.105308 0.105832 ?
+9 ? ? 0.099952 0.102050 0.101092 ?
+10 ? ? 0.100834 0.100822 0.101342 ?
diff --git a/101/ex3 b/101/ex3
new file mode 100644
index 0000000..6b37ca3
--- /dev/null
+++ b/101/ex3
@@ -0,0 +1,11 @@
+N m1 m2 dm a1 a2 a3 a
+1 ? ? ? 0.101242 0.100606 0.100698 ?
+2 ? ? ? 0.324394 0.343446 0.345500 ?
+3 ? ? ? 0.587238 0.587220 0.564608 ?
+4 ? ? ? 0.828416 0.830074 0.815484 ?
+5 ? ? ? 1.063504 1.071230 1.070982 ?
+6 ? ? ? 1.305656 1.305208 1.307460 ?
+7 ? ? ? 1.550900 1.548880 1.550336 ?
+8 ? ? ? 1.794500 1.792756 1.788620 ?
+9 ? ? ? 2.032050 2.032032 2.034266 ?
+10 ? ? ? 2.269558 2.272532 2.273842 ?
diff --git a/101/include b/101/include
new file mode 120000
index 0000000..2225752
--- /dev/null
+++ b/101/include
@@ -0,0 +1 @@
+../libprakpp/include/ \ No newline at end of file
diff --git a/101/main b/101/main
new file mode 100755
index 0000000..cb77291
--- /dev/null
+++ b/101/main
Binary files differ
diff --git a/101/main.cpp b/101/main.cpp
new file mode 100644
index 0000000..54d956d
--- /dev/null
+++ b/101/main.cpp
@@ -0,0 +1,137 @@
+#include <iostream>
+
+#include "include/praktable.hpp"
+
+using table = prak::table<f64>;
+using vecarg = const std::vector<f64> &;
+using f64v = std::vector<f64>;
+using f64p = prak::pvalue<f64>;
+
+const f64 g = 9.81571; // moscow local acceleration
+const f64 m_0 = 113.63 / 1000.0;
+const f64 m_s = 0.01 / 1000.0;
+
+f64p avg_mass(void) {
+ std::ifstream f("mass");
+ f64 acc = 0;
+ u32 cnt = 0;
+ for (f64 tmp; f >> tmp; acc += tmp, ++cnt);
+ f64 avg = acc / cnt;
+ f.clear();
+ f.seekg(0, std::ios::beg);
+ f64 stddev_acc = 0;
+ for (f64 tmp; f >> tmp; stddev_acc += (tmp-avg)*(tmp-avg));
+ return {.val = avg / 1000.0, .err = std::sqrt(stddev_acc / cnt / (cnt - 1)) / 1000.0};
+}
+
+// args[0] = dm
+// args[1] = m
+f64 acal(vecarg args) {
+ return g * args[0] / args[1];
+}
+
+f64 get_g(vecarg args) {
+ f64 C = args[0],
+ m_bl = args[1],
+ m = args[2],
+ m0 = args[3],
+ a = args[4];
+ return C * (a*m_bl + m + m0);
+}
+
+f64 get_M(vecarg args) {
+ f64 D = args[0],
+ m_bl = args[1],
+ m = args[2],
+ m0 = args[3],
+ a = args[4],
+ R = args[5];
+ return D * (a*m_bl + m + m0) * R;
+}
+
+void ex1(f64p m) {
+ table t("ex1");
+ t .apply(prak::avg<f64>, {"a1", "a2", "a3"}, "a")
+ .apply(prak::stddev<f64>, {"a1", "a2", "a3"}, "sa")
+ .apply([m](vecarg arg) -> f64 {
+ return acal({arg[0] * m.val, (m_0 + arg[0]*m.val)});
+ }, {"N"}, "acal")
+ .apply([m](vecarg arg) -> f64 {
+ f64v args = {arg[0] * m.val, m_0 + arg[0]*m.val};
+ f64v sigmas = {m.err * arg[0], m_s};
+ return prak::sigma<f64>(acal, args, sigmas);
+ }, {"N"}, "sacal");
+ t.write_plot("ex1_exp.plot", "N", "a", "sa");
+ t.write_plot("ex1_calc.plot", "N", "acal", "sacal");
+ std::cout << "Упражнение 1: " << t << std::endl;
+}
+
+void ex2(f64p m) {
+ table in("ex2");
+ in .apply([m] (vecarg a) { return m.val * (a[0] - 1); }, {"N"}, "m1")
+ .apply([m] (vecarg a) { return m.val * a[0]; }, {"N"}, "m2")
+ .apply(prak::avg<f64>, {"a1", "a2", "a3"}, "a");
+ table out({"N", "M", "1/M", "a_exp", "a_cal", "da(%)"}, in.rows, NAN);
+ {
+ int N = 0;
+ for (auto it = out.begin("N"); it != out.end("N"); ++it) *it = ++N;
+ for (size_t i = 0; i < in.rows; ++i) out["a_exp", i] = in["a", i];
+ out .apply([m] (vecarg a) { return (2*a[0] - 1)*m.val + m_0; }, {"N"}, "M")
+ .apply([m] (vecarg a) { return acal({m.val, a[0]}); }, {"M"}, "a_cal")
+ .apply([ ] (vecarg a) { return std::abs(a[0]-a[1])/a[0]*100; }, {"a_cal", "a_exp"}, "da(%)")
+ .apply([ ] (vecarg a) { return 1.0/a[0]; }, {"M"}, "1/M");
+ }
+ std::cout << "Таблица 3: " << in << "\nТаблица 4: " << out << std::endl;
+
+ out.write_plot("ex2_exp.plot", "1/M", "a_exp", std::nullopt);
+ out.write_plot("ex2_cal.plot", "1/M", "a_cal", std::nullopt);
+ out.write_plot("ex2_dev.plot", "1/M", "da(%)", std::nullopt);
+}
+
+void ex3(f64p m) {
+ table in("ex3");
+ in .apply([m] (vecarg a) { return (10 - a[0]) * m.val; }, {"N"}, "m1")
+ .apply([m] (vecarg a) { return ( 9 + a[0]) * m.val; }, {"N"}, "m2")
+ .apply([m] (vecarg a) { return (2 * a[0] - 1) * m.val; }, {"N"}, "dm")
+ .apply(prak::avg<f64>, {"a1", "a2", "a3"}, "a");
+ in.add_column("sa", {});
+ in.apply(prak::stddev<f64>, {"a1", "a2", "a3"}, "sa");
+ table out({"N", "dm", "M", "1/M", "a_exp", "a_cal", "da(%)"}, in.rows, NAN);
+ {
+ int N = 0;
+ for (auto it = out.begin("N"); it != out.end("N"); ++it) *it = ++N;
+ for (size_t i = 0; i < in.rows; ++i) out["a_exp", i] = in["a", i];
+ out.fill_column("M", 19 * m.val + m_0);
+ out .apply([m] (vecarg a) { return (2 * a[0] - 1) * m.val; }, {"N"}, "dm")
+ .apply([ ] (vecarg a) { return acal({a[0], a[1]}); }, {"dm", "M"}, "a_cal")
+ .apply([ ] (vecarg a) { return std::abs(a[0]-a[1])/a[0]*100; }, {"a_cal", "a_exp"}, "da(%)")
+ .apply([ ] (vecarg a) { return 1.0/a[0]; }, {"M"}, "1/M");
+ }
+ std::cout << "Таблица 5: " << in << "\nТаблица 6: " << out << std::endl;
+ out.write_plot("ex3_exp.plot", "dm", "a_exp", std::nullopt);
+ out.write_plot("ex3_cal.plot", "dm", "a_cal", std::nullopt);
+ out.write_plot("ex3_dev.plot", "dm", "da(%)", std::nullopt);
+ auto [C, D] = out.least_squares_linear("dm", "a_exp", std::nullopt, in.col_avg("sa"));
+ f64p g, M;
+ f64v g_args = {C.val, 0.0175, m.val * 19, m_0, 0.3},
+ g_sigmas = {C.err, 0.0005, m.err * 19, m_s, 0.1};
+ g.val = get_g(g_args);
+ g.err = prak::sigma<f64>(get_g, g_args, g_sigmas);
+ f64v M_args = {D.val, 0.0175, m.val * 19, m_0, 0.3, 0.025},
+ M_sigmas = {D.err, 0.0005, m.err * 19, m_s, 0.0, 0.001};
+ M.val = get_M(M_args);
+ M.err = prak::sigma<f64>(get_M, M_args, M_sigmas);
+ std::cout << "Результаты регрессии:\n\tC = " << C << "\tD = " << D << '\n'
+ << "Полученное значение g:\n\tg = " << g << '\n'
+ << "Полученное значение M:\n\tM = " << M << std::endl;
+}
+
+int main() {
+ f64p m = avg_mass();
+ std::cout << "Средняя масса = " << m << std::endl;
+ ex1(m);
+ ex2(m);
+ ex3(m);
+
+ return 0;
+}
diff --git a/101/mass b/101/mass
new file mode 100644
index 0000000..2e48479
--- /dev/null
+++ b/101/mass
@@ -0,0 +1,20 @@
+1.88
+1.87
+1.87
+1.85
+1.86
+1.88
+1.88
+1.88
+1.88
+1.87
+1.87
+1.88
+1.88
+1.89
+1.89
+1.89
+1.86
+1.86
+1.86
+1.86
diff --git a/101/plots.gp b/101/plots.gp
new file mode 100644
index 0000000..702408a
--- /dev/null
+++ b/101/plots.gp
@@ -0,0 +1,14 @@
+set term pngcairo size 1000, 800
+set tmargin at screen 0.95
+
+f1(x) = a1*x+b1
+fit f1(x) '.plot' using 1:2:3 yerr via a1, b1
+
+set output ''
+set label "" at graph 0.5, graph 1.025 center
+set xlabel ""
+set ylabel ""
+
+plot '.plot' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \
+ f1(x) title "" lc rgb "red", \
+
diff --git a/107/Makefile b/107/Makefile
new file mode 100644
index 0000000..eb309ff
--- /dev/null
+++ b/107/Makefile
@@ -0,0 +1,18 @@
+
+CFLAGS = -std=c++2c -mavx -Iinclude -ggdb
+
+.PHONY: all run_main clean gnuplot
+
+run: gnuplot
+
+gnuplot: plots.gp run_main
+ gnuplot $<
+
+run_main: main
+ ./main
+
+main: main.cpp include/*
+ $(CXX) -o $@ $< $(CFLAGS)
+
+clean:
+ rm -fr main *.png *.plot
diff --git a/107/README.md b/107/README.md
new file mode 100644
index 0000000..96fd716
--- /dev/null
+++ b/107/README.md
@@ -0,0 +1,5 @@
+<!-- Шаблон для прака -->
+<!-- файлы, заканчивающиеся на .plot считаются генерируемыми и удаляются через make clean -->
+# Обработка <> прака
+
+
diff --git a/107/compile_flags.txt b/107/compile_flags.txt
new file mode 100644
index 0000000..34ae930
--- /dev/null
+++ b/107/compile_flags.txt
@@ -0,0 +1,4 @@
+-Iinclude
+-std=c++2c
+-mavx2
+
diff --git a/107/data1 b/107/data1
new file mode 100644
index 0000000..419b8c2
--- /dev/null
+++ b/107/data1
@@ -0,0 +1,5 @@
+__name__ b sb a1 a2 a3 a sa mu smu
+b1_weigh 0.1 ? 0.0315 0.033 0.037 ? ? ? ?
+b1 0.1 ? 0.032 0.028 0.027 ? ? ? ?
+b2_weigh 0.1 ? 0.035 0.029 0.03 ? ? ? ?
+b2 0.1 ? 0.036 0.033 0.027 ? ? ? ?
diff --git a/107/data21 b/107/data21
new file mode 100644
index 0000000..5198f10
--- /dev/null
+++ b/107/data21
@@ -0,0 +1,5 @@
+__name__ b sb a1 a2 a3 a sa mu smu
+b1 0.165 ? 0.046 0.048 0.049 ? ? ? ?
+b1_weig 0.169 ? 0.047 0.046 0.046 ? ? ? ?
+b2 0.203 ? 0.062 0.063 0.063 ? ? ? ?
+b2_weig 0.195 ? 0.058 0.059 0.059 ? ? ? ?
diff --git a/107/data22 b/107/data22
new file mode 100644
index 0000000..e7614ef
--- /dev/null
+++ b/107/data22
@@ -0,0 +1,5 @@
+__name__ b sb a1 a2 a3 a sa mu smu
+b1 0.158 ? 0.046 0.048 0.047 ? ? ? ?
+b1_weig 0.1 ? 0.024 0.029 0.029 ? ? ? ?
+b2 0.1 ? 0.031 0.033 0.033 ? ? ? ?
+b2_weig 0.1 ? 0.030 0.029 0.030 ? ? ? ?
diff --git a/107/main b/107/main
new file mode 100755
index 0000000..3d5308e
--- /dev/null
+++ b/107/main
Binary files differ
diff --git a/107/main.cpp b/107/main.cpp
new file mode 100644
index 0000000..e731a06
--- /dev/null
+++ b/107/main.cpp
@@ -0,0 +1,46 @@
+#include "include/praktable.hpp"
+#include <functional>
+
+using table = prak::table<double>;
+
+double mu(const std::vector<double> &ab) {
+ return ab[0] / ab[1];
+}
+
+void proc_table(table &t) {
+ using namespace std::placeholders;
+
+ t.fill_column("sb", 0.003); // 3 millimeters
+ t.apply(prak::avg<double>, {"a1", "a2", "a3"}, "a");
+ t.apply([](const std::vector<double>& args) -> double {
+ return std::sqrt(std::pow(prak::stddev<double>(args), 2) + 0.003*0.003);
+ }, {"a1", "a2", "a3"}, "sa"); // error = sqrt(stddev^2 + systerr^2)
+ t.apply(mu, {"a", "b"}, "mu");
+ t.apply([](const std::vector<double> &arg) -> double {
+ std::vector<double> args = {arg[0], arg[1]};
+ std::vector<double> sargs = {arg[2], arg[3]};
+ return prak::sigma<double>(mu, args, sargs);
+ }, {"a", "b", "sa", "sb"}, "smu");
+}
+
+void ex1(void) {
+ table t;
+ t.read("data1");
+ proc_table(t);
+ std::cout << "Таблица для 1 упражнения: " << t;
+}
+void ex2(void) {
+ table a1, a2;
+ a1.read("data21");
+ a2.read("data22");
+ proc_table(a1);
+ proc_table(a2);
+ std::cout << "Таблица для 2.1: " << a1;
+ std::cout << "Таблица для 2.2: " << a2;
+}
+
+int main() {
+ ex1();
+ ex2();
+ return 0;
+}
diff --git a/107/plots.gp b/107/plots.gp
new file mode 100644
index 0000000..702408a
--- /dev/null
+++ b/107/plots.gp
@@ -0,0 +1,14 @@
+set term pngcairo size 1000, 800
+set tmargin at screen 0.95
+
+f1(x) = a1*x+b1
+fit f1(x) '.plot' using 1:2:3 yerr via a1, b1
+
+set output ''
+set label "" at graph 0.5, graph 1.025 center
+set xlabel ""
+set ylabel ""
+
+plot '.plot' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \
+ f1(x) title "" lc rgb "red", \
+
diff --git a/compile_flags.txt b/compile_flags.txt
new file mode 100644
index 0000000..34ae930
--- /dev/null
+++ b/compile_flags.txt
@@ -0,0 +1,4 @@
+-Iinclude
+-std=c++2c
+-mavx2
+
diff --git a/libprakpp/include/prakcommon.hpp b/libprakpp/include/prakcommon.hpp
index 8d1b001..c58040e 100644
--- a/libprakpp/include/prakcommon.hpp
+++ b/libprakpp/include/prakcommon.hpp
@@ -5,6 +5,25 @@
#include <vector>
#include <iostream>
+typedef void i0;
+typedef int8_t i8;
+typedef uint8_t u8;
+typedef int16_t i16;
+typedef uint16_t u16;
+typedef int32_t i32;
+typedef uint32_t u32;
+typedef int64_t i64;
+typedef uint64_t u64;
+typedef ssize_t isz;
+typedef size_t usz;
+typedef float f32;
+typedef double f64;
+#if __SIZEOF_LONG_DOUBLE__ == 16
+typedef long double f128;
+#else
+typedef long double _f64;
+#endif
+
#if defined(_MSC_VER) || !defined(__cpp_multidimensional_subscript) || __cplusplus < 202110L
#warning "can not use multidimentional subscript operator: falling back to `operator()`"
#undef MDSUBSCRIPT
diff --git a/libprakpp/include/prakmath.hpp b/libprakpp/include/prakmath.hpp
index 3a4e4db..a5eb0b8 100644
--- a/libprakpp/include/prakmath.hpp
+++ b/libprakpp/include/prakmath.hpp
@@ -2,6 +2,7 @@
#include <cmath>
#include <concepts>
+#include <numeric>
#ifdef __x86_64__
#include <immintrin.h>
@@ -184,6 +185,25 @@ scalar:
return linear + finalize(buf);
}
+template <arithmetic T>
+T avg(const std::vector<T> &args) {
+ T init{};
+ for (const T &x : args) {
+ init += x;
+ }
+ return init / args.size();
+}
+
+template <arithmetic T>
+T stddev(const std::vector<T> &args) {
+ T sum = {};
+ const T a = avg<T>(args);
+ for (T el : args) {
+ sum += (a-el)*(a-el);
+ }
+ return std::sqrt(sum/args.size()/(args.size()-1));
+}
+
// take a derivative of function `f` with arguments `at` and with differentiable variable being at index `varidx`
template <std::floating_point T>
T deriv4(function_t<T> f, std::vector<T> /*I actually do want a copy here*/ at, size_t varidx) {
@@ -204,7 +224,7 @@ T deriv4(function_t<T> f, std::vector<T> /*I actually do want a copy here*/ at,
/// get error of the calculated value
template <std::floating_point T>
-T sigma(function_t<T> f, const std::vector<T> &args, const std::vector<T> sigmas) noexcept(false) {
+T sigma(function_t<T> f, const std::vector<T> &args, const std::vector<T> &sigmas) noexcept(false) {
if (args.size() != sigmas.size()) throw dimension_error("function prak::sigma : Args.size() does not match sigmas.size()");
T sum = 0;
for (size_t i = 0; i < args.size(); ++i) {
@@ -277,7 +297,7 @@ void least_squares_linear(
/// May throw std::bad_optional_access
template <typename T>
std::enable_if<std::is_arithmetic_v<T>, std::vector<pvalue<T>>>
-polynomial_regression(
+polynominal_regression(
size_t degree,
std::vector<T> data_x,
std::vector<T> data_y,
diff --git a/libprakpp/include/prakmatrix.hpp b/libprakpp/include/prakmatrix.hpp
index 87b38b4..53d3b4b 100644
--- a/libprakpp/include/prakmatrix.hpp
+++ b/libprakpp/include/prakmatrix.hpp
@@ -3,8 +3,8 @@
#include "prakcommon.hpp"
#include <cstring>
-#include <iomanip>
#include <ostream>
+#include <optional>
#include <vector>
namespace prak {
diff --git a/libprakpp/include/praktable.hpp b/libprakpp/include/praktable.hpp
index f32d00d..70f6d49 100644
--- a/libprakpp/include/praktable.hpp
+++ b/libprakpp/include/praktable.hpp
@@ -1,5 +1,6 @@
#pragma once
+#include <cassert>
#include <cstddef>
#include <functional>
#include <algorithm>
@@ -96,7 +97,7 @@ public:
iterator operator++(int) { iterator ret = *this; ++(*this); return ret; }
bool operator==(iterator other) { return data_index == other.data_index && parent == other.parent && col_index == other.col_index; }
bool operator!=(iterator other) { return data_index != other.data_index || parent != other.parent || col_index != other.col_index; }
- value_type operator*() { return parent->data[data_index]; };
+ value_type &operator*() { return parent->data[data_index]; };
};
/// Optional rownames: names of rows
std::vector<std::string> opt_rownames;
@@ -108,16 +109,23 @@ public:
/// default constructor
table() = default;
+ explicit table(const std::vector<std::string> &columns, size_t rows, const dtype &deflt)
+ : rows{rows}, columns{columns.size()}
+ {
+ names = columns;
+ data = std::vector<dtype>(rows * columns.size(), deflt);
+ }
+
/// Data: array of ararys, not freed automatically.
- explicit table(const std::vector<std::string> &strings, dtype **data, size_t rows /*size_t columns = strings.size()*/)
- : rows{rows}, columns{strings.size()}
+ explicit table(const std::vector<std::string> &columns, dtype **data, size_t rows /*size_t columns = strings.size()*/)
+ : rows{rows}, columns{columns.size()}
{
- names = strings;
+ names = columns;
- data = std::vector<dtype>(rows * strings.size());
+ data = std::vector<dtype>(rows * columns.size());
for (size_t i = 0; i < rows; ++i)
- for (size_t j = 0; j < strings.size(); ++j)
- data[i * strings.size() + j] = data[i][j];
+ for (size_t j = 0; j < columns.size(); ++j)
+ data[i * columns.size() + j] = data[i][j];
}
/// Strings: names for columns
@@ -137,10 +145,16 @@ public:
}
}
+ explicit table(const std::string &file) {
+ read(file);
+ }
+
iterator begin(std::string column) { return iterator(this, column); }
iterator end(std::string column) { return iterator(this, column, rows); }
- dtype & SUBSCR_OPRTR (const std::string &column, size_t row) {
+ dtype & SUBSCR_OPRTR (const std::string &column, size_t row) noexcept(false) {
+ size_t i = index(column);
+ if (i == columns) throw std::out_of_range("Column " + column + " does not exist");
return data.at(names.size() * row + index(column));
}
@@ -196,12 +210,14 @@ public:
v[j] = SUBSCR_OPRTR(args[j], i);
if (result.has_value()) data[columns * i + result_index] = function(v);
else (void)function(v);
- }
+ }
+ /* print(std::cerr); */
return *this;
}
/// adds a column with name `name` and data `column_data`
void add_column(std::string name, std::vector<dtype> column_data) {
+ if (column_data.size() == 0) column_data = std::vector<dtype>(rows, dtype{});
std::vector<dtype> data_new(rows * (++columns));
for (size_t row = 0; row < rows; ++row) {
@@ -215,6 +231,7 @@ public:
/// Appends a column to the table. if name is set, appends it to `opt_rownames`
void add_row(std::vector<dtype> values, std::optional<std::string> name = std::nullopt) {
+ if (values.size() == 0) values = std::vector<dtype>(columns, dtype{});
data.resize(columns * (++rows));
std::copy_n(values.cbegin(), columns, data.end() - columns);
if (name.has_value()) opt_rownames.push_back(*name);
@@ -224,7 +241,7 @@ public:
t.print(os);
return os;
}
-
+
/// Reads a table from a file in a format:
/// ```
/// col1 col2 col3 ...
@@ -251,7 +268,9 @@ public:
names.push_back(buffer);
std::vector<dtype> tmp_row(names.size());
+ int __i = 0;
while (!(f >> std::ws).eof()) {
+ ++__i;
if (read_names) {
f >> buffer;
opt_rownames.push_back(buffer);
@@ -269,9 +288,25 @@ public:
}
columns = names.size();
}
+
+ /// Reads a table from a file specified by `path`.
+ /// For details, refer to documentation of `void read(std::ifstream&)` overload
+ void read(const std::string &path) {
+ std::ifstream f(path);
+ read(f);
+ }
+
+ /// Fills a specified column with the same value `v`
+ void fill_column(const std::string &column, dtype v) {
+ apply([&v](const std::vector<dtype>& _) -> dtype { return v; }, {}, column);
+ }
/// returns an std::pair with coefficients A and B in that order
- std::pair<prak::pvalue<dtype>, prak::pvalue<dtype>> least_squares_linear(std::string x, std::string y, std::optional<std::string> sigma = std::nullopt) {
+ std::pair<prak::pvalue<dtype>, prak::pvalue<dtype>>
+ least_squares_linear(std::string x, std::string y, std::optional<std::string> sigma, std::optional<dtype> sigma_fixed)
+ noexcept(false) {
+ if (sigma.has_value() == sigma_fixed.has_value())
+ throw std::invalid_argument("sigma and sigma_fixed can't both have (no) value");
prak::vector<dtype> _x(rows);
prak::vector<dtype> _y(rows);
prak::vector<dtype> _s(rows);
@@ -279,13 +314,26 @@ public:
std::copy(begin(x), end(x), _x.begin());
std::copy(begin(y), end(y), _y.begin());
if (sigma.has_value()) std::copy(begin(*sigma), end(*sigma), _s.begin());
- else _s = prak::vector<dtype>(rows, static_cast<dtype>(1));
+ else _s = prak::vector<dtype>(rows, static_cast<dtype>(*sigma_fixed));
std::pair<prak::pvalue<dtype>, prak::pvalue<dtype>> ret;
prak::least_squares_linear<dtype>(_x, _y, _s, ret.first, ret.second);
return ret;
}
+ /// calculate an average of the column
+ dtype col_avg(const std::string &column) {
+ dtype accum = dtype{};
+ for (auto it = begin(column); it != end(column); ++it)
+ accum += *it;
+ return accum / rows;
+ }
+
+ /// calculate standard deviation of the column
+ dtype col_stddev(const std::string &column) {
+ assert(0);
+ }
+
/// Serialize data in format `data[args[0]][i] data[args[1]][i] data[args[2]][i]...`
void print_plot(const stringvec &args, std::ostream &out = std::cout) const {
std::vector<size_t> offsets(args.size());
@@ -307,11 +355,17 @@ public:
if (yss.has_value()) ssi = index(*yss);
for (size_t row = 0; row < rows; ++row) {
size_t offset = columns * row;
- out << data[offset + xsi] << ' ' << data[offset + ysi];
+ out << data.at(offset + xsi) << ' ' << data.at(offset + ysi);
if (ssi != nosigma) out << ' ' << data[offset+ssi];
out << std::endl;
}
}
+
+ /// Serialize data into a file `file`. For details, refer to documentation for overload with std::ifstream as an argument
+ void write_plot(const std::string &file, const std::string &xs, const std::string &ys, std::optional<std::string> yss = std::nullopt) const {
+ std::ofstream out(file);
+ write_plot(xs, ys, yss, out);
+ }
void plot_png(
const std::string output_filename,
diff --git a/vtek3/main b/vtek3/main
deleted file mode 100755
index 08e6ab8..0000000
--- a/vtek3/main
+++ /dev/null
Binary files differ