aboutsummaryrefslogtreecommitdiffstats
path: root/libprakpp/include/praktable.hpp
diff options
context:
space:
mode:
authorjustanothercatgirl <sotov@twistea.su>2025-04-17 12:48:23 +0300
committerjustanothercatgirl <sotov@twistea.su>2025-04-17 12:48:23 +0300
commit0bd2ab4d1f9e8fdbf55ed11d61fe2e75720a334a (patch)
tree59f91bead63e9f68f74dafd73b68491286eabcf7 /libprakpp/include/praktable.hpp
parent45f12ba19761024d335407793ffb8d4823b28149 (diff)
finished 206, 207
Diffstat (limited to 'libprakpp/include/praktable.hpp')
-rw-r--r--libprakpp/include/praktable.hpp37
1 files changed, 33 insertions, 4 deletions
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 {