aboutsummaryrefslogtreecommitdiffstats
path: root/libprakpp
diff options
context:
space:
mode:
Diffstat (limited to 'libprakpp')
-rw-r--r--libprakpp/include/prakmath.hpp16
-rw-r--r--libprakpp/include/prakmatrix.hpp4
-rw-r--r--libprakpp/include/praktable.hpp32
3 files changed, 38 insertions, 14 deletions
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);
}