Tatooine
finite_differences_coefficients.h
Go to the documentation of this file.
1#ifndef TATOOINE_FINITE_DIFFERENCES_COEFFICIENTS_H
2#define TATOOINE_FINITE_DIFFERENCES_COEFFICIENTS_H
3//==============================================================================
4#include <tatooine/mat.h>
5#include <tatooine/vec.h>
6//==============================================================================
7namespace tatooine {
8//==============================================================================
11//==============================================================================
13auto finite_differences_coefficients(std::size_t const derivative_order,
14 floating_point auto const... xs) {
15 constexpr auto N = sizeof...(xs);
16 using real_type = common_type<std::decay_t<decltype(xs)>...>;
17 auto V = mat<real_type, N, N>::vander(xs...);
18 V = transposed(V);
19 auto b = vec<real_type, N>::zeros();
20 b(derivative_order) =
21 static_cast<real_type>(gcem::factorial(derivative_order));
22 return *solve(V, b);
23}
24//------------------------------------------------------------------------------
26template <typename Tensor, floating_point Real, std::size_t N>
27auto finite_differences_coefficients(std::size_t const derivative_order,
29 auto V = mat<Real, N, N>::vander(v);
30 V = transposed(V);
31 auto b = vec<Real, N>::zeros();
32 b(derivative_order) = static_cast<Real>(gcem::factorial<std::size_t>(derivative_order));
33 return *solve(V, b);
34}
35//------------------------------------------------------------------------------
37template <typename Tensor, floating_point Real, std::size_t N>
38auto finite_differences_coefficients(std::size_t const derivative_order,
39 vec<Real, N> const& v) {
40 auto V = mat<Real, N, N>::vander(v);
41 V = transposed(V);
42 auto b = vec<Real, N>::zeros();
43 b(derivative_order) = gcem::factorial(derivative_order);
44 return *solve(V, b);
45}
46//------------------------------------------------------------------------------
48template <floating_point_range R>
49requires (!static_tensor<R>)
50auto finite_differences_coefficients(std::size_t const derivative_order,
51 R const& v) {
52 using real_type = std::ranges::range_value_t<R>;
53 auto const V = transposed(tensor<real_type>::vander(v));
54 auto b = tensor<real_type>::zeros(std::ranges::size(v));
55 b(derivative_order) =
56 static_cast<real_type>(gcem::factorial(derivative_order));
57 return solve(V, b)->internal_container();
58}
59//==============================================================================
61//==============================================================================
62} // namespace tatooine
63//==============================================================================
64#endif
Definition: concepts.h:30
auto finite_differences_coefficients(std::size_t const derivative_order, floating_point auto const ... xs)
See What is this? for an explanation.
Definition: finite_differences_coefficients.h:13
Definition: algorithm.h:6
typename common_type_impl< Ts... >::type common_type
Definition: common_type.h:23
auto solve(polynomial< Real, 1 > const &p) -> std::vector< Real >
solve a + b*x
Definition: polynomial.h:187
auto transposed(dynamic_tensor auto &&t)
Definition: transposed_tensor.h:170
Definition: base_tensor.h:23
static auto constexpr vander(fixed_size_vec< N > auto const &v)
Definition: mat.h:60
Definition: tensor.h:17
static constexpr auto zeros()
Definition: tensor.h:144
Definition: vec.h:12
static auto constexpr zeros()
Definition: vec.h:26