-
Notifications
You must be signed in to change notification settings - Fork 22
[Math] Add least squares method #1591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 19 commits
47bc414
db367cf
c9dd6fd
04d385c
c13398a
979b3e5
e7468f3
594076f
9487349
39d0094
a10b2d7
b6a165a
5e31c24
1c42e54
9ce02cd
3e6e72f
247e17d
33629bd
002ea61
ee1090f
5e6c1ca
2d7614b
90d5570
e4d071f
0ebcc10
cd3fd51
49fdfb6
ef2f577
087764a
d2cbdd1
e5122ac
65187ce
fa09329
d6fe7bd
e94cf79
638e268
659e8fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| /** | ||
| * @file integrator.hpp | ||
| * @author David Fang (david.fang@ikmail.com) | ||
| * @author Timothée David--Cléris (tim.shamrock@proton.me) | ||
| * @brief | ||
| * | ||
|
|
@@ -30,4 +31,47 @@ namespace shammath { | |
| return acc; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Euler solving of ODE | ||
| * The ode has the form | ||
| * \f{eqnarray*}{ | ||
| * u'(x) &=& f(u,x) \\ | ||
| * u(x_0) &=& u_0 | ||
| * \f} | ||
| * and will be solved between start and end with step $\mathrm{d}t$. | ||
| * | ||
| * @param start Lower bound of integration | ||
| * @param end Higher bound of integration | ||
| * @param step Step of integration $\mathrm{d}t$ | ||
| * @param ode Ode function $f$ | ||
| * @param x0 Initial coordinate $x_0$ | ||
| * @param u0 Initial value $u_0$ | ||
| */ | ||
| template<class T, class Lambda> | ||
| inline constexpr std::pair<std::vector<T>, std::vector<T>> euler_ode( | ||
| T start, T end, T step, Lambda &&ode, T x0, T u0) { | ||
| std::vector<T> U = {u0}; | ||
| std::vector<T> X = {x0}; | ||
|
|
||
| T u_prev = u0; | ||
| T u = u0; | ||
| for (T x = x0 + step; x < end; x += step) { | ||
| u = u_prev + ode(u_prev, x) * step; | ||
| X.push_back(x); | ||
| U.push_back(u); | ||
| u_prev = u; | ||
| }; | ||
| u_prev = u0; | ||
| std::vector<T> X_backward, U_backward; | ||
| for (T x = x0 - step; x > start; x -= step) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| u = u_prev - ode(u_prev, x) * step; | ||
| X_backward.push_back(x); | ||
| U_backward.push_back(u); | ||
| u_prev = u; | ||
| } | ||
| X.insert(X.begin(), X_backward.rbegin(), X_backward.rend()); | ||
| U.insert(U.begin(), U_backward.rbegin(), U_backward.rend()); | ||
| return {X, U}; | ||
| } | ||
|
|
||
| } // namespace shammath | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
|
|
||
| /** | ||
| * @file matrix.hpp | ||
| * @author David Fang (david.fang@ikmail.com) | ||
| * @author Léodasce Sewanou (leodasce.sewanou@ens-lyon.fr) | ||
| * @author Timothée David--Cléris (tim.shamrock@proton.me) | ||
| * @author Yann Bernard (yann.bernard@univ-grenoble-alpes.fr) | ||
|
|
@@ -68,8 +69,8 @@ namespace shammath { | |
| /// check if this matrix is equal to another one at a given precison | ||
| bool equal_at_precision(const mat<T, m, n> &other, const T precision) const { | ||
| bool res = true; | ||
| for (auto i = 0; i < m; i++) { | ||
| for (auto j = 0; j < n; j++) { | ||
| for (int i = 0; i < m; i++) { | ||
| for (int j = 0; j < n; j++) { | ||
| if (sham::abs(data[i * n + j] - other.data[i * n + j]) >= precision) { | ||
| res = false; | ||
| } | ||
|
|
@@ -120,6 +121,97 @@ namespace shammath { | |
| bool operator==(const vec<T, n> &other) { return data == other.data; } | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Matrix class based on std::vector storage and mdspan | ||
DavidFang03 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * @tparam T the type of the matrix entries | ||
| */ | ||
| template<class T> | ||
| class mat_d { | ||
| public: | ||
| /// The matrix data | ||
| std::vector<T> data; | ||
| int rows; | ||
| int columns; | ||
|
|
||
| mat_d(int rows, int columns) : rows(rows), columns(columns), data(rows * columns) {} | ||
|
|
||
| /// Get the matrix data as a mdspan | ||
| inline constexpr auto get_mdspan() { | ||
| return std::mdspan<T, std::dextents<size_t, 2>>(data.data(), rows, columns); | ||
| } | ||
|
|
||
| /// const overload | ||
| inline constexpr auto get_mdspan() const { | ||
| return std::mdspan<const T, std::dextents<size_t, 2>>(data.data(), rows, columns); | ||
| } | ||
|
|
||
| /// Access the matrix entry at position (i, j) | ||
| inline constexpr T &operator()(int i, int j) { return get_mdspan()(i, j); } | ||
|
|
||
| /// const overload | ||
| inline constexpr const T &operator()(int i, int j) const { return get_mdspan()(i, j); } | ||
|
|
||
| /// Check if this matrix is equal to another one | ||
| bool operator==(const mat_d<T> &other) const { return data == other.data; } | ||
|
|
||
| inline mat_d &operator+=(const mat_d &other) { | ||
| #pragma unroll | ||
|
||
| for (size_t i = 0; i < get_mdspan().extent(0) * get_mdspan().extent(1); i++) { | ||
| data[i] += other.data[i]; | ||
| } | ||
| return *this; | ||
| } | ||
|
|
||
| /// check if this matrix is equal to another one at a given precison | ||
| bool equal_at_precision(const mat_d<T> &other, const T precision) const { | ||
| bool res = true; | ||
| for (int i = 0; i < rows; i++) { | ||
| for (int j = 0; j < columns; j++) { | ||
| if (sham::abs(data[i * columns + j] - other.data[i * columns + j]) | ||
| >= precision) { | ||
| res = false; | ||
|
||
| } | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Vector class based on std::vector storage and mdspan | ||
| * @tparam T the type of the vector entries | ||
| * @tparam n the number of entries | ||
| */ | ||
| template<class T> | ||
| class vec_d { | ||
| public: | ||
| /// The vector data | ||
| std::vector<T> data; | ||
| int size; | ||
|
|
||
| vec_d(int size) : size(size), data(size) {} | ||
|
|
||
| /// Get the vector data as a mdspan | ||
| inline constexpr auto get_mdspan() { | ||
| return std::mdspan<T, std::dextents<size_t, 1>>(data.data(), size); | ||
| } | ||
|
|
||
| /// Get the vector data as a mdspan of a matrix with one column | ||
| inline constexpr auto get_mdspan_mat_col() { | ||
| return std::mdspan<T, std::dextents<size_t, 2>>(data.data(), size, 1); | ||
| } | ||
|
|
||
| /// Get the vector data as a mdspan of a matrix with one row | ||
| inline constexpr auto get_mdspan_mat_row() { | ||
| return std::mdspan<T, std::dextents<size_t, 2>>(data.data(), 1, size); | ||
| } | ||
|
|
||
| /// Access the vector entry at position i | ||
| inline constexpr T &operator[](int i) { return get_mdspan()(i); } | ||
|
|
||
| /// Check if this vector is equal to another one | ||
| bool operator==(const vec_d<T> &other) { return data == other.data; } | ||
DavidFang03 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| } // namespace shammath | ||
|
|
||
| template<class T, int m, int n> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using floating-point numbers directly in loop conditions like
x < endcan lead to precision issues, potentially causing the loop to terminate one step early or run one step too long due to accumulated floating-point errors. It's generally safer to iterate a fixed number of times or use a small epsilon for comparison, e.g.,x < end - std::numeric_limits<T>::epsilon() * std::abs(end) * Nwhere N is the number of steps.