aboutsummaryrefslogtreecommitdiffstats
path: root/libprakpp/include
diff options
context:
space:
mode:
Diffstat (limited to 'libprakpp/include')
-rw-r--r--libprakpp/include/prakcommon.hpp41
-rw-r--r--libprakpp/include/prakmath.hpp42
-rw-r--r--libprakpp/include/prakphys.hpp28
3 files changed, 82 insertions, 29 deletions
diff --git a/libprakpp/include/prakcommon.hpp b/libprakpp/include/prakcommon.hpp
index c9bced3..47c793c 100644
--- a/libprakpp/include/prakcommon.hpp
+++ b/libprakpp/include/prakcommon.hpp
@@ -75,24 +75,6 @@ fequal(T x, T y, std::size_t ulps = 1)
return std::fabs(x - y) <= ulps * std::ldexp(std::numeric_limits<T>::epsilon(), exp);
}
-template<typename T>
-T sum(const std::vector<T> &args) {
- T res{};
- for (const T& x : args) res += x;
- return res;
-}
-
-template<typename T>
-T prod(const std::vector<T> &args) {
- T res = 1;
- for (const T& x : args) res *= x;
- return res;
-}
-
-template <std::floating_point T>
-T hypot(const std::vector<T> &args) {
- return std::sqrt(args[0]*args[0] + args[1]*args[1]);
-}
/// prints a vector
template <typename T>
@@ -124,16 +106,17 @@ using vector = std::vector<T, align_alloc<T>>;
/// prak value / pair value: a value with an error
template <typename T>
struct pvalue { T val, err; };
-
-template <typename T>
-struct pvalue<T> operator*(const struct pvalue<T> &v, T a) {
- return pvalue<T>{v.val * a, v.err * a};
-}
-
-template <typename T>
-std::ostream &operator<<(std::ostream &os, const struct pvalue<T> &p) {
- /* return os << "value {" << p.val << "±" << p.err << "}"; */
- return os << p.val << "±" << p.err;
-}
+template <typename T> struct pvalue<T> operator*(const struct pvalue<T> &v, const T &a) { return {v.val * a, v.err * a}; }
+template <typename T> struct pvalue<T> operator*(const T &a, const struct pvalue<T> &v) { return {v.val * a, v.err * a}; }
+template <typename T> struct pvalue<T> operator*(const struct pvalue<T> &a, const struct pvalue<T> &b) { return {a.val * b.val, a.val * b.val * std::hypot(a.err/a.val, b.err/b.val)}; }
+template <typename T> struct pvalue<T> operator/(const struct pvalue<T> &v, const T &a) { return {v.val / a, v.err / a}; }
+template <typename T> struct pvalue<T> operator/(const struct pvalue<T> &a, const struct pvalue<T> &b) { return {a.val / b.val, a.val / b.val * std::hypot(a.err/a.val, b.err/b.val)}; }
+template <typename T> struct pvalue<T> operator+(const struct pvalue<T> &a, const struct pvalue<T> &b) { return {a.val + b.val, std::hypot(a.err, b.err)}; }
+template <typename T> struct pvalue<T> operator-(const struct pvalue<T> &a, const struct pvalue<T> &b) { return {a.val - b.val, std::hypot(a.err, b.err)}; }
+template <typename T> struct pvalue<T> operator+(const struct pvalue<T> &a, const T &b) { return {a.val + b, a.err}; }
+template <typename T> struct pvalue<T> operator-(const struct pvalue<T> &a, const T &b) { return {a.val - b, a.err}; }
+template <typename T> struct pvalue<T> operator+(const T &a, const struct pvalue<T> &b) { return {a + b.val, b.err}; }
+template <typename T> struct pvalue<T> operator-(const T &a, const struct pvalue<T> &b) { return {a - b.val, b.err}; }
+template <typename T> std::ostream &operator<<(std::ostream &os, const struct pvalue<T> &p) { return os << p.val << "±" << p.err; }
} // namespace prak
diff --git a/libprakpp/include/prakmath.hpp b/libprakpp/include/prakmath.hpp
index 54b902d..ebd02bf 100644
--- a/libprakpp/include/prakmath.hpp
+++ b/libprakpp/include/prakmath.hpp
@@ -4,6 +4,7 @@
#include <concepts>
#include <iomanip>
#include <numeric>
+#include <functional>
#ifdef __x86_64__
#include <immintrin.h>
@@ -186,6 +187,45 @@ scalar:
return linear + finalize(buf);
}
+
+template<arithmetic T>
+T sum(const std::vector<T> &args) {
+ T res{};
+ for (const T& x : args) res += x;
+ return res;
+}
+template<std::floating_point T>
+T prod(const std::vector<T> &args) {
+ T res = 1;
+ for (const T& x : args) res *= x;
+ return res;
+}
+template <std::floating_point T>
+T hypot(const std::vector<T> &args) {
+ return std::sqrt(args[0]*args[0] + args[1]*args[1]);
+}
+template <arithmetic T, class op>
+struct generic_arith {
+ T operator()(const std::vector<T> &args) {
+ return std::accumulate(args.cbegin(), args.cend(), T{}, op{});
+ }
+};
+template <arithmetic T>
+struct plus: public generic_arith<T, std::plus<T>>{};
+template <arithmetic T>
+struct minus: public generic_arith<T, std::minus<T>>{};
+template <arithmetic T>
+T diff(const std::vector<T> &args) { return std::abs(args[0] - args[1]); }
+template <arithmetic T>
+T quot(const std::vector<T> &args) { return args[0] / args[1]; }
+template <arithmetic T> T log(const std::vector<T> &arg){ return std::log(arg[0]); }
+template <std::floating_point T> T inv(const std::vector<T> &arg){ return 1/arg[0]; }
+template <typename T, T val> T add(const std::vector<T> &arg) { return val + arg[0]; }
+template <typename T, T val> T sub(const std::vector<T> &arg) { return arg[0] - val; }
+template <arithmetic T, T val> T mul(const std::vector<T> &arg) {return arg[0] * val; }
+template <arithmetic T, T val> T div(const std::vector<T> &arg) {return arg[0] / val; }
+
+
template <arithmetic T>
T avg(const std::vector<T> &args) {
T init{};
@@ -195,6 +235,8 @@ T avg(const std::vector<T> &args) {
return init / args.size();
}
+// Actually calculates not the standard deviation, but standard
+// deviation OF the mean. This means additionally dividing by \sqrt{N-1}
template <arithmetic T>
T stddev(const std::vector<T> &args) {
T sum = {};
diff --git a/libprakpp/include/prakphys.hpp b/libprakpp/include/prakphys.hpp
new file mode 100644
index 0000000..1eaef5a
--- /dev/null
+++ b/libprakpp/include/prakphys.hpp
@@ -0,0 +1,28 @@
+#pragma once
+
+
+
+#define TP template <typename T = double> constexpr const T
+
+namespace conv {
+TP C_to_K = 273.15; // Celsius-Kelvin
+TP K_to_C = -273.15;
+TP J_to_eV = 6.2415e+18;
+TP eV_to_J = 1 / 6.2415e+18;
+} // namespace conv
+
+namespace prak {
+TP g = 9.81;
+TP c = 299792458;
+TP h = 6.62607015e-34;
+TP hh = 1.054571817e-34;
+TP k = 1.380649e-23;
+TP G = 6.67430151515e-11;
+TP e = 1.602176634e-19;
+TP m_e = 1.6021766342828e-31;
+TP m_p = 1.672621925955252e-27;
+TP m_n = 1.674927500568585e-27;
+TP R = 8.31446261815324;
+TP N_A = 6.02214076e23;
+
+} // namespace prak