diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | 206/Makefile | 18 | ||||
-rw-r--r-- | 206/README.md | 5 | ||||
-rw-r--r-- | 206/compile_flags.txt | 4 | ||||
-rw-r--r-- | 206/data1 | 7 | ||||
-rw-r--r-- | 206/data2 | 27 | ||||
-rw-r--r-- | 206/final.pdf | bin | 0 -> 96431 bytes | |||
l--------- | 206/include | 1 | ||||
-rw-r--r-- | 206/main.cpp | 51 | ||||
-rw-r--r-- | 206/plots.gp | 38 | ||||
-rw-r--r-- | 207/data3 | 2 | ||||
-rw-r--r-- | 207/ex1_1.print | 24 | ||||
-rw-r--r-- | 207/ex1_4.print | 34 | ||||
-rw-r--r-- | 207/main.cpp | 65 | ||||
-rw-r--r-- | 207/plots.gp | 86 | ||||
-rw-r--r-- | 207/print.txt | 25 | ||||
-rw-r--r-- | libprakpp/include/prakcommon.hpp | 6 | ||||
-rw-r--r-- | libprakpp/include/prakmath.hpp | 22 | ||||
-rw-r--r-- | libprakpp/include/prakmatrix.hpp | 22 | ||||
-rw-r--r-- | libprakpp/include/praktable.hpp | 37 | ||||
-rw-r--r-- | libprakpp/tests/matrix.cpp | 14 | ||||
-rw-r--r-- | tempalte/Makefile | 2 |
22 files changed, 377 insertions, 114 deletions
@@ -5,5 +5,6 @@ __pycache__/ *.log *.out *.plot +*.print vtek3/*.data */main diff --git a/206/Makefile b/206/Makefile new file mode 100644 index 0000000..224506f --- /dev/null +++ b/206/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 $< &>/dev/null + +run_main: main + ./main + +main: main.cpp include/* + $(CXX) -o $@ $< $(CFLAGS) + +clean: + rm -fr main *.png *.plot diff --git a/206/README.md b/206/README.md new file mode 100644 index 0000000..96fd716 --- /dev/null +++ b/206/README.md @@ -0,0 +1,5 @@ +<!-- Шаблон для прака --> +<!-- файлы, заканчивающиеся на .plot считаются генерируемыми и удаляются через make clean --> +# Обработка <> прака + + diff --git a/206/compile_flags.txt b/206/compile_flags.txt new file mode 100644 index 0000000..34ae930 --- /dev/null +++ b/206/compile_flags.txt @@ -0,0 +1,4 @@ +-Iinclude +-std=c++2c +-mavx2 + diff --git a/206/data1 b/206/data1 new file mode 100644 index 0000000..c4b5435 --- /dev/null +++ b/206/data1 @@ -0,0 +1,7 @@ +L t1 t2 t3 t St st +0.1 280 279 280 ? ? ? +0.2 568 569 567 ? ? ? +0.3 857 858 857 ? ? ? +0.4 1150 1150 1149 ? ? ? +0.5 1432 1431 1430 ? ? ? +0.6 1731 1730 1729 ? ? ? diff --git a/206/data2 b/206/data2 new file mode 100644 index 0000000..24cafe0 --- /dev/null +++ b/206/data2 @@ -0,0 +1,27 @@ +t T sqrt(T) tau v +51.0 ? ? 1660 ? +50.0 ? ? 1664 ? +48.8 ? ? 1666 ? +47.9 ? ? 1668 ? +47.0 ? ? 1671 ? +46.0 ? ? 1673 ? +45.0 ? ? 1675 ? +44.0 ? ? 1678 ? +43.0 ? ? 1680 ? +42.0 ? ? 1683 ? +41.0 ? ? 1686 ? +40.0 ? ? 1688 ? +39.0 ? ? 1690 ? +38.0 ? ? 1692 ? +37.0 ? ? 1695 ? +36.0 ? ? 1698 ? +35.0 ? ? 1700 ? +34.0 ? ? 1702 ? +33.0 ? ? 1705 ? +32.0 ? ? 1708 ? +31.0 ? ? 1711 ? +30.0 ? ? 1713 ? +28.8 ? ? 1717 ? +28.0 ? ? 1719 ? +27.2 ? ? 1722 ? +26.0 ? ? 1725 ? diff --git a/206/final.pdf b/206/final.pdf Binary files differnew file mode 100644 index 0000000..f15f111 --- /dev/null +++ b/206/final.pdf diff --git a/206/include b/206/include new file mode 120000 index 0000000..2225752 --- /dev/null +++ b/206/include @@ -0,0 +1 @@ +../libprakpp/include/
\ No newline at end of file diff --git a/206/main.cpp b/206/main.cpp new file mode 100644 index 0000000..8c94af3 --- /dev/null +++ b/206/main.cpp @@ -0,0 +1,51 @@ +#include <iostream> + +#include "include/praktable.hpp" +#include "include/prakphys.hpp" + +using table = prak::table<f64>; +using f64p = prak::pvalue<f64>; +using f64v = std::vector<f64>; +using vecarg = const std::vector<f64> &; +using argvec = const std::vector<f64> &; + +void ex1(std::string file) { + table t(file); + f64 tmsgm = 1; + t /*.multiply_column("t1", 1e-6) + .multiply_column("t2", 1e-6) + .multiply_column("t3", 1e-6)*/ + .apply(prak::avg<f64>, {"t1", "t2", "t3"}, "t") + .apply(prak::stddev<f64>, {"t1", "t2", "t3"}, "St") + .apply([&](vecarg a) {return std::sqrt(a[0]*a[0] + tmsgm * tmsgm); }, "St", "st") + .write_plot("ex1.plot", "L", "t", "st") + .print(); + auto [A, B] = t.least_squares_linear("L", "t", "st", std::nullopt); + A = A * 1e-6; + B = B * 1e-6; + f64p v = 1.0 / A; + std::cout var(A) var(B) var(v); +} + +void ex2(std::string file) { + table t(file); + f64 L0 = 0.6, mu = 29e-3; + t .apply(prak::add<f64, conv::C_to_K<f64>>, "t", "T") + .apply([](vecarg a){ return std::sqrt(a[0]); }, "T", "sqrt(T)") + .multiply_column("tau", 1e-6) + .apply([&](vecarg a) { return L0 / a[0]; }, "tau", "v") + .write_plot("ex2.plot", "sqrt(T)", "v", std::nullopt) + .add_row({-273.15, 0, 0, 0, 0}) + .print(); + auto A = t.least_squares_prop("sqrt(T)", "v", std::nullopt, 0.01); + f64p gamma = A * A * mu / prak::R<f64>; + f64p Cv = prak::R<f64> / (gamma - 1.0), i = 2.0 / (gamma - 1.0); + f64p tau = 1.2041 * Cv * t.col_avg("v") * t.col_avg("v") / 4.0 / 1e6 / 0.0259; + std::cout var(L0) var(A) var(mu) var(gamma) var(Cv) var(i) var(tau); +} + +int main() { + ex1("data1"); + ex2("data2"); + return 0; +} diff --git a/206/plots.gp b/206/plots.gp new file mode 100644 index 0000000..5460021 --- /dev/null +++ b/206/plots.gp @@ -0,0 +1,38 @@ +set term pngcairo size 1000, 800 +set grid + +f1(x) = a1*x+b1 +fit f1(x) 'ex1.plot' using 1:2:3 yerr via a1, b1 +f2(x) = a2 * x +fit f2(x) 'ex2.plot' using 1:2 via a2 + +set output 'ex1.png' +set title "Зависимость времени прохода волны от расстояния" +set xlabel "L, м" +set ylabel "t, мкc" +set key top left + +set xrange[0:0.7] + +set label sprintf("A = %.4g\n B =%.4g", a1 / 10e6, b1 / 10e6) at graph 0.2, 0.9 front boxed +plot 'ex1.plot' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \ + f1(x) title "t(L)" lc rgb "red" + + +set output 'ex2.png' +set title "Зависимость скорости от корня из температуры" +set xlabel "T^{1/2}, K^{1/2}" +set ylabel "v, м/c" +set key top left + +unset xrange +unset label +set label sprintf("A = %.4g", a2) at graph 0.2, 0.9 front boxed +plot 'ex2.plot' using 1:2 notitle lc 0 pt 1 lw 2, \ + f2(x) title "v(T^{1/2})" lc rgb "red" + + +set output 'ex2_0.png' +set title "Зависимость скорости от корня из температуры (проход через 0)" +set xrange[0:19] +replot @@ -1,5 +1,5 @@ n P T V x -1 990.8 315.40 43.5 ? +1 980.8 315.40 43.5 ? 2 984.6 316.42 ? ? 3 986.8 317.22 ? ? 4 989.2 317.98 ? ? diff --git a/207/ex1_1.print b/207/ex1_1.print deleted file mode 100644 index ec5efe0..0000000 --- a/207/ex1_1.print +++ /dev/null @@ -1,24 +0,0 @@ -Данные упражнения 1columns: 6, rows: 10 -_______________________________________________________________________________ -|n |P |T |V |x |nu | -|____________|____________|____________|____________|____________|____________| -|1 |1110.3 |289.9 |35.5 |39415.7 |16.3526 | -|____________|____________|____________|____________|____________|____________| -|2 |1091.8 |289.9 |36 |39304.8 |16.3066 | -|____________|____________|____________|____________|____________|____________| -|3 |1089.9 |289.9 |36.5 |39781.4 |16.5043 | -|____________|____________|____________|____________|____________|____________| -|4 |1074.4 |289.9 |37 |39752.8 |16.4925 | -|____________|____________|____________|____________|____________|____________| -|5 |1062.4 |289.9 |37.5 |39840 |16.5286 | -|____________|____________|____________|____________|____________|____________| -|6 |1050 |289.9 |38 |39900 |16.5535 | -|____________|____________|____________|____________|____________|____________| -|7 |1038.8 |289.9 |38.5 |39993.8 |16.5924 | -|____________|____________|____________|____________|____________|____________| -|8 |1024 |289.9 |39 |39936 |16.5685 | -|____________|____________|____________|____________|____________|____________| -|9 |1012.2 |289.9 |39.5 |39981.9 |16.5875 | -|____________|____________|____________|____________|____________|____________| -|10 |996.5 |289.9 |40 |39860 |16.5369 | -|____________|____________|____________|____________|____________|____________| diff --git a/207/ex1_4.print b/207/ex1_4.print deleted file mode 100644 index 9863903..0000000 --- a/207/ex1_4.print +++ /dev/null @@ -1,34 +0,0 @@ -Данные упражнения 1columns: 6, rows: 15 -_______________________________________________________________________________ -|n |P |T |V |x |nu | -|____________|____________|____________|____________|____________|____________| -|1 |997.1 |323.61 |44.5 |44371 |16.4908 | -|____________|____________|____________|____________|____________|____________| -|2 |997.4 |323.51 |44 |43885.6 |16.3155 | -|____________|____________|____________|____________|____________|____________| -|3 |999 |323.51 |43.5 |43456.5 |16.156 | -|____________|____________|____________|____________|____________|____________| -|4 |1014.2 |323.48 |43 |43610.6 |16.2148 | -|____________|____________|____________|____________|____________|____________| -|5 |1023.3 |323.48 |42.5 |43490.2 |16.17 | -|____________|____________|____________|____________|____________|____________| -|6 |1037.3 |323.38 |42 |43566.6 |16.2034 | -|____________|____________|____________|____________|____________|____________| -|7 |1048 |323.38 |41.5 |43492 |16.1757 | -|____________|____________|____________|____________|____________|____________| -|8 |1060 |323.35 |41 |43460 |16.1653 | -|____________|____________|____________|____________|____________|____________| -|9 |1070.7 |323.35 |40.5 |43363.3 |16.1293 | -|____________|____________|____________|____________|____________|____________| -|10 |1083.8 |323.35 |40 |43352 |16.1251 | -|____________|____________|____________|____________|____________|____________| -|11 |1098.5 |323.35 |39.5 |43390.8 |16.1395 | -|____________|____________|____________|____________|____________|____________| -|12 |1108.8 |323.35 |39 |43243.2 |16.0846 | -|____________|____________|____________|____________|____________|____________| -|13 |1122.9 |323.35 |38.5 |43231.7 |16.0803 | -|____________|____________|____________|____________|____________|____________| -|14 |1139 |323.35 |38 |43282 |16.0991 | -|____________|____________|____________|____________|____________|____________| -|15 |1148.1 |323.28 |37.5 |43053.8 |16.0176 | -|____________|____________|____________|____________|____________|____________| diff --git a/207/main.cpp b/207/main.cpp index 6f606bd..5424940 100644 --- a/207/main.cpp +++ b/207/main.cpp @@ -17,28 +17,70 @@ f64p add_nu(table& t) { } void ex1(table &t1, const char *name) { + // t = const f64 DA = prak::discrete_integral_trapezoid(t1.cbegin("V"), t1.cend("V"), t1.cbegin("P"), t1.cend("P")); f64 DS = DA / t1.col_avg("T"); f64p nu = add_nu(t1); + t1 .add_columns({"DAi", "DSi", "lnViV0"}) + .apply([&t1](vecarg a) -> f64 { + return prak::discrete_integral_trapezoid( + t1.begin("V"), std::find(t1.begin("V"), t1.end("V"), a[1])+1, + t1.begin("P"), std::find(t1.begin("P"), t1.end("P"), a[0])+1 + ); + }, {"P", "V"}, "DAi") + .apply([&t1](vecarg a){return a[0] / a[1];}, {"DAi", "T"}, "DSi") + .apply([&t1](vecarg a){ return std::log(a[0] / t1["V", 0]); }, "V", "lnViV0") + .apply([](vecarg a) {return std::abs(a[0]); }, "lnViV0", "lnViV0"); f64p DS_teor = nu * prak::R<f64> * std::abs(std::log(*(t1.end("V")-1) / *t1.begin("V"))); - std::cout << "Упражнение 1\n" var(DA) var(DS) var(nu) var(DS_teor) << std::endl; - t1.write_plot(std::string("P(V)") + name + ".plot", "V", "P", std::nullopt); - t1.write_plot(std::string("PV(V)") + name + ".plot", "V", "x", std::nullopt); + f64p A = std::abs(t1.least_squares_prop("lnViV0", "DSi", std::nullopt, 0.1)); + f64p R = A / nu; + std::cout << "Упражнение 1\n" var(DA) var(DS) var(nu) var(DS_teor) << "доп данные (упражнение 4):\n" + var(A) var(R) << std::endl; + t1 .write_plot(std::string("PV") + name + ".plot", "V", "P", std::nullopt) + .write_plot(std::string("PVV") + name + ".plot", "V", "x", std::nullopt) + .write_plot(std::string("DSilnViV0_Tconst") + name + ".plot", "lnViV0", "DSi", std::nullopt); std::ofstream f(std::string("ex1_") + name + ".print"); - f << "Данные упражнения 1"; + f << "Данные упражнения 1 (T = const)\n"; t1.print(f); f.close(); } -void ex2(const table &t) { - +void ex2(table &t) { + // p = const + std::cout << "Упражнение 2\n"; + std::ofstream f("ex2.print"); + f << "Данные упражнения 2 (P = const)\n"; + f64p nu = add_nu(t); + t .add_columns({"lnTiT0", "DSi"}) + .apply([&t](vecarg a) -> f64 {return std::log(a[0] / t["T", 0]); }, "T", "lnTiT0") + .apply([&t, &nu](vecarg a) -> f64 { + return nu.val * (prak::R<f64> * std::log(a[1] / t["V", 0]) + 5.0/2 * prak::R<f64> * a[2]); + }, {"T", "V", "lnTiT0"}, "DSi") + .write_plot("DSilnTiT0_Pconst.plot", "lnTiT0", "DSi", std::nullopt) + .write_plot("VTV.plot", "V", "x", std::nullopt) + .print(f); + f64p A = t.least_squares_prop("lnTiT0", "DSi", std::nullopt, 0.1); + f64p Cp = A / nu; + std::cout var(nu) var(A) var(Cp); } -void ex3(const table &t) { - -} - -void ex4(void) { +void ex3(table &t) { + // v = const + std::cout << "Упражнение 3\n"; + std::ofstream f("ex3.print"); + f << "Данные упражнения 3 (V = const)\n"; + f64p nu = add_nu(t); + t .add_columns({"lnTiT0", "DSi"}) + .apply([&t](vecarg a) -> f64 {return std::log(a[0] / t["T", 0]); }, "T", "lnTiT0") + .apply([&t, &nu](vecarg a) -> f64 { + return nu.val * (-prak::R<f64> * std::log(a[0] / t["P", 0]) + 7.0/2 * prak::R<f64> * a[1]); + }, {"P", "lnTiT0"}, "DSi") + .write_plot("DSilnTiT0_Vconst.plot", "lnTiT0", "DSi", std::nullopt) + .write_plot("PTP.plot", "P", "x", std::nullopt) + .print(f); + auto [A, B] = t.least_squares_linear("lnTiT0", "DSi", std::nullopt, 0.1); + f64p Cv = A / nu; + std::cout var(nu) var(A) var(Cv); } @@ -77,7 +119,6 @@ void ex(void) { ex1(tables[3], "4"); ex2(tables[1]); ex3(tables[2]); - ex4(); } int main() { diff --git a/207/plots.gp b/207/plots.gp index f197258..09b2442 100644 --- a/207/plots.gp +++ b/207/plots.gp @@ -1,16 +1,84 @@ set term pngcairo size 1000, 800 set grid +f1(x) = a1*x +f2(x) = a2*x +f3(x) = a3*x+b3 +f4(x) = a4*x+b4 -#f1(x) = a1*x+b1 -#fit f1(x) '.plot' using 1:2:3 yerr via a1, b1 -set output '' -set title "" -set xlabel "" -set ylabel "" +set output 'ex4.png' +fit f1(x) 'DSilnViV0_Tconst1.plot' using 1:2 via a1 +fit f2(x) 'DSilnViV0_Tconst4.plot' using 1:2 via a2 + +set title "Зависимость при разных температурах" +set xlabel "ед." +set ylabel "S_i, Дж/К" + +plot 'DSilnViV0_Tconst1.plot' notitle lc 0 pt 1 lw 2, \ + f1(x) title " (T = 289 K)" lc rgb "magenta",\ + 'DSilnViV0_Tconst4.plot' notitle lc 0 pt 1 lw 2, \ + f2(x) title " (T = 3232.5 K)" lc rgb "red" + + +set output 'ex1.png' +unset label +set title "зависимости P(V), PV(V) в изотермическом процессе при разных температурах" +set xlabel "V, м^3" +set ylabel "P, Па" +set y2tics +set y2label "PV, Па * м^3" set key top left +set yrange[950:1200] +set y2range[35000:45000] + +plot 'PV1.plot' notitle axis x1y1 lc 0 pt 1 lw 2, \ + 'PV1.plot' with lines axis x1y1 smooth csplines title "P(V) (T = 289 K)" lc rgb "orange", \ + 'PVV1.plot' notitle axis x1y2 lc 0 pt 1 lw 2, \ + 'PVV1.plot' with lines axis x1y2 smooth csplines title "PV(V) (T = 289 K)" lc rgb "magenta", \ + 'PV4.plot' notitle axis x1y1 lc 0 pt 1 lw 2, \ + 'PV4.plot' with lines axis x1y1 smooth csplines title "P(V) (T = 323.5 K)" lc rgb "red", \ + 'PVV4.plot' notitle axis x1y2 lc 0 pt 1 lw 2, \ + 'PVV4.plot' with lines axis x1y2 smooth csplines title "PV(V) (T = 323.5 K)" lc rgb "green" + + +set output 'ex23_mnk.png' +fit f1(x) 'DSilnTiT0_Pconst.plot' using 1:2 via a1 +fit f2(x) 'DSilnTiT0_Vconst.plot' using 1:2 via a2 + +unset xrange +unset yrange +unset x2tics +unset y2label +unset y2range +set title "зависимости при P = const, V = const" +set xlabel "ед." +set ylabel "S_i, Дж/К" + +set label sprintf("(A)_P = %.4g\n (A)_V = %.4g", a1, a2) at graph 0.2, 0.9 front boxed +plot 'DSilnTiT0_Vconst.plot' notitle lc rgb "green" pt 1 lw 3,\ + 'DSilnTiT0_Pconst.plot' notitle lc rgb "magenta" pt 2 lw 3,\ + f2(x) title "V = const" lc rgb "green",\ + f1(x) title "P = const" lc rgb "magenta" +unset label + +set output 'ex2.png' +set title "Зависимость V/T от V при P = const" +set xlabel "V, м^3" +set ylabel "V/T, м^3/К" +set yrange[0.13:0.14] + +plot 'VTV.plot' notitle lc 0 pt 1 lw 2,\ + 'VTV.plot' title "V/T (V)" lc rgb "magenta" with lines smooth csplines + + + +set output 'ex3.png' -#set label sprintf("A = %.4g\n B = %.4g", a1, b1) at graph 0.2, 0.9 front boxed -#plot '.plot' using 1:2:3 with yerrorbars notitle lc 0 pt 1 lw 2, \ -# f1(x) title "" lc rgb "red" +set title "Зависимость P/T (P) при V = const (по большей части)" +set xlabel "P, Па" +set ylabel "P/T, Па/K" +set yrange[3:3.2] +set xrange [980:1001] +plot 'PTP.plot' notitle lc 0 pt 1 lw 2,\ + 'PTP.plot' title "P/T (P)" lc rgb "magenta" with lines smooth path diff --git a/207/print.txt b/207/print.txt deleted file mode 100644 index 85be337..0000000 --- a/207/print.txt +++ /dev/null @@ -1,25 +0,0 @@ -columns: 5, rows: 10 -__________________________________________________________________ -|n |P |T |V |x | -|____________|____________|____________|____________|____________| -|1 |1110.3 |289.9 |35.5 |39415.7 | -|____________|____________|____________|____________|____________| -|2 |1091.8 |289.9 |36 |39304.8 | -|____________|____________|____________|____________|____________| -|3 |1089.9 |289.9 |36.5 |39781.4 | -|____________|____________|____________|____________|____________| -|4 |1074.4 |289.9 |37 |39752.8 | -|____________|____________|____________|____________|____________| -|5 |1062.4 |289.9 |37.5 |39840 | -|____________|____________|____________|____________|____________| -|6 |1050 |289.9 |38 |39900 | -|____________|____________|____________|____________|____________| -|7 |1038.8 |289.9 |38.5 |39993.8 | -|____________|____________|____________|____________|____________| -|8 |1024 |289.9 |39 |39936 | -|____________|____________|____________|____________|____________| -|9 |1012.2 |289.9 |39.5 |39981.9 | -|____________|____________|____________|____________|____________| -|10 |996.5 |289.9 |40 |39860 | -|____________|____________|____________|____________|____________| - diff --git a/libprakpp/include/prakcommon.hpp b/libprakpp/include/prakcommon.hpp index 074e290..dcafecf 100644 --- a/libprakpp/include/prakcommon.hpp +++ b/libprakpp/include/prakcommon.hpp @@ -1,5 +1,7 @@ #pragma once +#include <cstddef> +#include <cstdint> #include <cmath> #include <functional> #include <vector> @@ -132,3 +134,7 @@ template <typename T> struct pvalue<T> operator-(const T &a, const struct pvalue template <typename T> std::ostream &operator<<(std::ostream &os, const struct pvalue<T> &p) { return os << p.val << "±" << p.err; } } // namespace prak + +namespace std { +template <typename T> struct prak::pvalue<T> abs(const struct prak::pvalue<T> &p) { return {abs(p.val), abs(p.err)}; } +} diff --git a/libprakpp/include/prakmath.hpp b/libprakpp/include/prakmath.hpp index bb09e74..d43c11f 100644 --- a/libprakpp/include/prakmath.hpp +++ b/libprakpp/include/prakmath.hpp @@ -321,7 +321,6 @@ void least_squares_linear( << ", s.size() = " << ss.size() << std::endl; return; } - [[assume(xs.size() == ys.size() && ys.size() == ss.size())]]; size_t sz = xs.size(); prak::vector<T> ones(sz); @@ -364,6 +363,27 @@ void least_squares_linear( b.err = sqrt(d1 / D); } +template <std::floating_point T> +void least_squares_prop( + const prak::vector<T> &xs, + const prak::vector<T> &ys, + const prak::vector<T> &ss, + struct pvalue<T> &a) { + assert("Different-sized vectors passed to least squares method" && xs.size() == ys.size()); + size_t s = xs.size(); + prak::vector<T> buf(s), si(s); + vmul(ss.data(), ss.data(), si.data(), s); + + vmul(xs.data(), ys.data(), buf.data(), s); + vdiv(buf.data(), ss.data(), buf.data(), s); + T D1 = vsum(buf.data(), s); + + vmul(xs.data(), xs.data(), buf.data(), s); + vdiv(buf.data(), ss.data(), buf.data(), s); + T D2 = vsum(buf.data(), s); + a.val = D1/D2; + a.err = 1/D2; +} /// May throw std::bad_optional_access template <typename T> diff --git a/libprakpp/include/prakmatrix.hpp b/libprakpp/include/prakmatrix.hpp index f1ccddc..2f87401 100644 --- a/libprakpp/include/prakmatrix.hpp +++ b/libprakpp/include/prakmatrix.hpp @@ -2,6 +2,7 @@ #include "prakcommon.hpp" +#include <iomanip> #include <cstring> #include <ostream> #include <optional> @@ -75,7 +76,7 @@ struct matrix : public std::vector<T> { } /// multiply matrix by a scalar template <typename Arg> - std::enable_if_t<std::is_arithmetic_v<Arg>, struct matrix<T>>::type + typename std::enable_if_t<std::is_arithmetic_v<Arg>, struct matrix<T>>::type operator*(const Arg other) const { struct matrix<T> ret(rows, cols); for (size_t i = 0; i < this->size(); ++i) @@ -161,12 +162,27 @@ struct matrix : public std::vector<T> { } return ret; } + + void add_row(size_t index, std::vector<T> row_data) { + ++rows; + this->resize(rows * cols); + memmove(this->data() + cols * (index+1), this->data() + cols * index, (rows - index - 1) * cols * sizeof (T)); + for (size_t i = 0; i < cols; ++i) + this->operator[](index * cols + i) = row_data[i]; + } + void add_column(size_t index, std::vector<T> column_data) { + this->reserve(rows * (cols + 1)); + for (size_t i = 0; i < rows; ++i) + this->insert(this->begin() + (cols + 1) * i + index, column_data[i]); + ++cols; + } std::ostream &print(std::ostream &out = std::cout, size_t width = 10) const { + std::cout.setf(std::ios::left); for (size_t row = 0; row < rows; ++row) { for (size_t col = 0; col < cols; ++col) - out << "|" << std::setw(width) << SUBSCR_OPRTR(row, col); - std::cout << "|\n"; + out /*<< "|"*/ << std::setw(width) << SUBSCR_OPRTR(row, col); + std::cout << "\n"; } return out; } diff --git a/libprakpp/include/praktable.hpp b/libprakpp/include/praktable.hpp index 1870ec6..aabcb8e 100644 --- a/libprakpp/include/praktable.hpp +++ b/libprakpp/include/praktable.hpp @@ -105,10 +105,10 @@ public: iterator operator--(int) { iterator ret = *this; --(*this); return ret; } iterator &operator+(int x) { data_index += columns * x; return *this; } iterator &operator-(int x) { data_index -= columns * x; return *this; } - std::strong_ordering operator<=>(const iterator& other) { + std::strong_ordering operator<=>(const iterator& other) const { return parent == other.parent && col_index == other.col_index && data_index <=> other.data_index; } - bool operator==(const iterator& other) { + bool operator==(const iterator& other) const { return parent == other.parent && col_index == other.col_index && data_index == other.data_index; } reference operator*() { return parent->data[data_index]; }; @@ -270,6 +270,18 @@ public: // apply a function to several columns and store result in another column // function must accept std::vector or arguments + table &apply(function_type function, std::initializer_list<std::string> _args, std::optional<std::string> result) { + std::vector<std::string> args = _args; + size_t result_index = result.has_value() ? index(*result) : 0; + for (size_t i = 0; i < rows; ++i) { + std::vector<dtype> v(args.size()); + for (size_t j = 0; j < args.size(); ++j) + v[j] = SUBSCR_OPRTR(args[j], i); + if (result.has_value()) data[columns * i + result_index] = function(v); + else (void)function(v); + } + return *this; + } table &apply(function_type function, stringvec args, std::optional<std::string> result) { size_t result_index = result.has_value() ? index(*result) : 0; for (size_t i = 0; i < rows; ++i) { @@ -281,7 +293,6 @@ public: } return *this; } - table& apply(function_type function, const std::string& arg, std::optional<std::string> result) { size_t result_index = result.has_value() ? index(*result) : 0; for (size_t i = 0; i < rows; ++i) { @@ -411,11 +422,12 @@ public: // Appends a row 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) { + table& 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); + return *this; } @@ -518,6 +530,23 @@ public: prak::least_squares_linear<dtype>(_x, _y, _s, ret.first, ret.second); return ret; } + prak::pvalue<dtype> + least_squares_prop(std::string x, std::string y, std::optional<std::string> sigma, std::optional<dtype> sigma_fixed) const { + 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); + + std::copy(cbegin(x), cend(x), _x.begin()); + std::copy(cbegin(y), cend(y), _y.begin()); + if (sigma.has_value()) std::copy(cbegin(*sigma), cend(*sigma), _s.begin()); + else _s = prak::vector<dtype>(rows, static_cast<dtype>(*sigma_fixed)); + + prak::pvalue<dtype> ret; + prak::least_squares_prop(_x, _y, _s, ret); + return ret; + } // calculate an average of the column dtype col_avg(const std::string &column) const { diff --git a/libprakpp/tests/matrix.cpp b/libprakpp/tests/matrix.cpp new file mode 100644 index 0000000..dffecef --- /dev/null +++ b/libprakpp/tests/matrix.cpp @@ -0,0 +1,14 @@ +#include "../include/prakmatrix.hpp" + +int main() { + using namespace prak; + prak::matrix<const char*> a(2, 3, { + "раз", "два", "три", + "4", "5s", "6" + }); + std::cout << "\x1b[0;91m" << a; + a.add_row(1, {"6", "6", "6"}); + std::cout << "\x1b[0;92m" << a; + a.add_column(0, {"9", "9", "9"}); + std::cout << "\x1b[0;93m" << a; +} diff --git a/tempalte/Makefile b/tempalte/Makefile index 224506f..3f1ad56 100644 --- a/tempalte/Makefile +++ b/tempalte/Makefile @@ -15,4 +15,4 @@ main: main.cpp include/* $(CXX) -o $@ $< $(CFLAGS) clean: - rm -fr main *.png *.plot + rm -fr main *.png *.plot *.print *.log |