diff options
author | justanothercatgirl <sotov2070@gmail.com> | 2025-04-15 00:51:40 +0300 |
---|---|---|
committer | justanothercatgirl <sotov2070@gmail.com> | 2025-04-15 00:51:40 +0300 |
commit | 45f12ba19761024d335407793ffb8d4823b28149 (patch) | |
tree | da44d238c8db4e8b90cd8e0edcb23cf779d22e1a | |
parent | 5ee716791bf7a8ca74c1670a6887a53ab92141f6 (diff) |
Started doing 207
-rw-r--r-- | 207/Makefile | 18 | ||||
-rw-r--r-- | 207/README.md | 5 | ||||
-rw-r--r-- | 207/compile_flags.txt | 4 | ||||
-rw-r--r-- | 207/data1 | 11 | ||||
-rw-r--r-- | 207/data2 | 7 | ||||
-rw-r--r-- | 207/data3 | 12 | ||||
-rw-r--r-- | 207/data4 | 16 | ||||
-rw-r--r-- | 207/ex1_1.print | 24 | ||||
-rw-r--r-- | 207/ex1_4.print | 34 | ||||
l--------- | 207/include | 1 | ||||
-rw-r--r-- | 207/main.cpp | 86 | ||||
-rw-r--r-- | 207/plots.gp | 16 | ||||
-rw-r--r-- | 207/print.txt | 25 | ||||
-rw-r--r-- | libprakpp/include/prakmath.hpp | 16 | ||||
-rw-r--r-- | libprakpp/include/prakmatrix.hpp | 4 | ||||
-rw-r--r-- | libprakpp/include/praktable.hpp | 32 |
16 files changed, 297 insertions, 14 deletions
diff --git a/207/Makefile b/207/Makefile new file mode 100644 index 0000000..17aed33 --- /dev/null +++ b/207/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 *.print diff --git a/207/README.md b/207/README.md new file mode 100644 index 0000000..96fd716 --- /dev/null +++ b/207/README.md @@ -0,0 +1,5 @@ +<!-- Шаблон для прака --> +<!-- файлы, заканчивающиеся на .plot считаются генерируемыми и удаляются через make clean --> +# Обработка <> прака + + diff --git a/207/compile_flags.txt b/207/compile_flags.txt new file mode 100644 index 0000000..34ae930 --- /dev/null +++ b/207/compile_flags.txt @@ -0,0 +1,4 @@ +-Iinclude +-std=c++2c +-mavx2 + diff --git a/207/data1 b/207/data1 new file mode 100644 index 0000000..0675a6e --- /dev/null +++ b/207/data1 @@ -0,0 +1,11 @@ +n P T V x +1 1110.3 289.9 35.5 ? +2 1091.8 289.9 36.0 ? +3 1089.9 289.9 36.5 ? +4 1074.4 289.9 37.0 ? +5 1062.4 289.9 37.5 ? +6 1050.0 289.9 38.0 ? +7 1038.8 289.9 38.5 ? +8 1024.0 289.9 39.0 ? +9 1012.2 289.9 39.5 ? +10 996.5 289.9 40.0 ? diff --git a/207/data2 b/207/data2 new file mode 100644 index 0000000..c5cabbf --- /dev/null +++ b/207/data2 @@ -0,0 +1,7 @@ +n P T V x +1 991.0 298.33 40.0 ? +2 991.0 302.84 40.5 ? +3 991.7 305.34 41.0 ? +4 991.2 307.78 41.5 ? +5 991.0 310.46 42.0 ? +6 990.8 313.57 42.5 ? diff --git a/207/data3 b/207/data3 new file mode 100644 index 0000000..ca2ca40 --- /dev/null +++ b/207/data3 @@ -0,0 +1,12 @@ +n P T V x +1 990.8 315.40 43.5 ? +2 984.6 316.42 ? ? +3 986.8 317.22 ? ? +4 989.2 317.98 ? ? +5 993.0 319.17 ? ? +6 995.3 320.04 ? ? +7 997.1 321.03 44.0 ? +8 998.5 322.02 ? ? +9 1000.0 323.02 ? ? +10 1000.0 323.85 ? ? +11 998.0 323.91 44.5 ? diff --git a/207/data4 b/207/data4 new file mode 100644 index 0000000..4941d5c --- /dev/null +++ b/207/data4 @@ -0,0 +1,16 @@ +n P T V x +1 997.1 323.61 44.5 ? +2 997.4 323.51 44.0 ? +3 999.0 323.51 43.5 ? +4 1014.2 323.48 43.0 ? +5 1023.3 323.48 42.5 ? +6 1037.3 323.38 42.0 ? +7 1048.0 323.38 41.5 ? +8 1060.0 323.35 41.0 ? +9 1070.7 323.35 40.5 ? +10 1083.8 323.35 40.0 ? +11 1098.5 323.35 39.5 ? +12 1108.8 323.35 39.0 ? +13 1122.9 323.35 38.5 ? +14 1139.0 323.35 38.0 ? +15 1148.1 323.28 37.5 ? diff --git a/207/ex1_1.print b/207/ex1_1.print new file mode 100644 index 0000000..ec5efe0 --- /dev/null +++ b/207/ex1_1.print @@ -0,0 +1,24 @@ +Данные упражнения 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 new file mode 100644 index 0000000..9863903 --- /dev/null +++ b/207/ex1_4.print @@ -0,0 +1,34 @@ +Данные упражнения 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/include b/207/include new file mode 120000 index 0000000..2225752 --- /dev/null +++ b/207/include @@ -0,0 +1 @@ +../libprakpp/include/
\ No newline at end of file diff --git a/207/main.cpp b/207/main.cpp new file mode 100644 index 0000000..6f606bd --- /dev/null +++ b/207/main.cpp @@ -0,0 +1,86 @@ +#include <cmath> +#include <iostream> +#include <ranges> + +#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> &; + +f64p add_nu(table& t) { + t.add_column("nu").apply([](vecarg a) { return a[0] * a[1] / (prak::R<f64> * a[2]); }, {"P", "V", "T"}, "nu"); + return {t.col_avg("nu"), t.col_stddev("nu")}; +} + +void ex1(table &t1, const char *name) { + 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); + 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); + std::ofstream f(std::string("ex1_") + name + ".print"); + f << "Данные упражнения 1"; + t1.print(f); + f.close(); +} + +void ex2(const table &t) { + +} + +void ex3(const table &t) { + +} + +void ex4(void) { + +} + +void ex(void) { + table tables[] = {table("data1"), table("data2"), table("data3"), table("data4")}; + + // approximate how the volume should have changed in ex3 using parabola + std::vector<f64> mrxdata; mrxdata.reserve(9); + std::vector<f64> mrydata; mrydata.reserve(3); + for (size_t i = 0; i < tables[2].rows; ++i) { + if (f64 v; !std::isnan(v = tables[2]["V", i])) { + f64 n = tables[2]["n", i]; + mrxdata.push_back(n*n); + mrxdata.push_back(n); + mrxdata.push_back(1); + mrydata.push_back(v); + } + } + prak::matrix<f64> xs(3, 3, std::move(mrxdata)), ys(3, 1, std::move(mrydata)); + prak::matrix<f64> poly = xs.inv().value() * ys; + for (size_t i = 0; i < tables[2].rows; ++i) + tables[2]["V", i] = (poly.tr() * prak::matrix<f64>(3, 1, {(double)(i+1)*(i+1), (double)(i+1), 1}))[0, 0]; + + std::function<f64(vecarg)> fs[] = { + [](vecarg a) { return a[0] * a[1]; }, + [](vecarg a) { return a[0] / a[1]; }, + [](vecarg a) { return a[0] / a[1]; }, + [](vecarg a) { return a[0] * a[1]; }, + }; + std::vector<std::string> vs[] = { + {"P", "V"}, {"V", "T"}, {"P", "T"}, {"P", "V"}, + }; + for (auto elem : std::views::zip(tables, fs, vs)) + std::get<0>(elem).apply(std::get<1>(elem), std::get<2>(elem), "x"); + ex1(tables[0], "1"); + ex1(tables[3], "4"); + ex2(tables[1]); + ex3(tables[2]); + ex4(); +} + +int main() { + ex(); + return 0; +} diff --git a/207/plots.gp b/207/plots.gp new file mode 100644 index 0000000..f197258 --- /dev/null +++ b/207/plots.gp @@ -0,0 +1,16 @@ +set term pngcairo size 1000, 800 +set grid + +#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 key top left + +#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" + diff --git a/207/print.txt b/207/print.txt new file mode 100644 index 0000000..85be337 --- /dev/null +++ b/207/print.txt @@ -0,0 +1,25 @@ +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/prakmath.hpp b/libprakpp/include/prakmath.hpp index ebd02bf..bb09e74 100644 --- a/libprakpp/include/prakmath.hpp +++ b/libprakpp/include/prakmath.hpp @@ -289,6 +289,22 @@ pvalue<T> function(function_t<T> func, const std::vector<pvalue<T>> &args) { return ret; } +template <typename iterator, typename T = iterator::value_type> +requires requires(iterator a){ ++a; *a; } && (std::integral<T> || std::floating_point<T>) +T discrete_integral_trapezoid(iterator X_start, iterator X_end, iterator Y_start, iterator Y_end) +{ + T ret = 0; + for (T Xl = *X_start, Yl = *Y_start; + X_start != X_end && Y_start != Y_end; + ++X_start, ++Y_start) + { + ret += std::abs(*X_start - Xl) * (*Y_start + Yl) / 2; // first iteration will add 0 + Xl = *X_start; + Yl = *Y_start; + } + return ret; +} + /// calculate least-squares linear approximation to fit data /// ax+b = y (ss is an error of Y value) template <std::floating_point T> diff --git a/libprakpp/include/prakmatrix.hpp b/libprakpp/include/prakmatrix.hpp index 53d3b4b..f1ccddc 100644 --- a/libprakpp/include/prakmatrix.hpp +++ b/libprakpp/include/prakmatrix.hpp @@ -14,7 +14,7 @@ struct dimension_error : public std::logic_error { }; /// A class to represent a matrix. Works as an extension to std::vector, so all methods of std::vector are available on this class -/// BIG TODO: replave `throw` with `std::optional` return type +/// BIG TODO: replace `throw` with `std::optional` return type template <typename T> struct matrix : public std::vector<T> { size_t rows, cols; @@ -75,7 +75,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>> + 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) diff --git a/libprakpp/include/praktable.hpp b/libprakpp/include/praktable.hpp index f00e7b9..1870ec6 100644 --- a/libprakpp/include/praktable.hpp +++ b/libprakpp/include/praktable.hpp @@ -222,6 +222,14 @@ public: return data.at(names.size() * row + column); } + const dtype & SUBSCR_OPRTR (const std::string &column, size_t row) const noexcept(false) { + size_t i = index(column); + return data.at(names.size() * row + index(column)); + } + + const dtype & SUBSCR_OPRTR (size_t column, size_t row) const { + return data.at(names.size() * row + column); + } // prints a table. defaults to using std::cout, but any std::ostream can be passed in it. void print(std::ostream &stream = std::cout) const { stream << "columns: " << columns << ", rows: " << rows << std::endl; @@ -493,7 +501,7 @@ public: // 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::optional<dtype> sigma_fixed) + least_squares_linear(std::string x, std::string y, std::optional<std::string> sigma, std::optional<dtype> sigma_fixed) const noexcept(false) { if (sigma.has_value() == sigma_fixed.has_value()) throw std::invalid_argument("sigma and sigma_fixed can't both have (no) value"); @@ -501,9 +509,9 @@ public: prak::vector<dtype> _y(rows); prak::vector<dtype> _s(rows); - 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()); + 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)); std::pair<prak::pvalue<dtype>, prak::pvalue<dtype>> ret; @@ -512,31 +520,31 @@ public: } // calculate an average of the column - dtype col_avg(const std::string &column) { + dtype col_avg(const std::string &column) const { dtype accum = dtype{}; - for (auto it = begin(column); it != end(column); ++it) + for (auto it = cbegin(column); it != cend(column); ++it) accum += *it; return accum / rows; } - dtype col_max(const std::string &column) { + dtype col_max(const std::string &column) const { dtype max = dtype{}; - for (auto it = begin(column); it != end(column); ++it) + for (auto it = cbegin(column); it != cend(column); ++it) max = max < *it ? *it : max; return max; } - dtype col_min(const std::string &column) { + dtype col_min(const std::string &column) const { dtype min = dtype{}; - for (auto it = begin(column); it != end(column); ++it) + for (auto it = cbegin(column); it != cend(column); ++it) min = min > *it ? *it : min; return min; } // calculate standard deviation of the column - dtype col_stddev(const std::string &column) { + dtype col_stddev(const std::string &column) const { dtype accum = dtype{}; dtype avg = col_avg(column); - for (auto it = begin(column); it != end(column); ++it) + for (auto it = cbegin(column); it != cend(column); ++it) accum += (*it - avg)*(*it - avg); return std::sqrt(accum / rows); } |