QUGaR 0.0.4
Loading...
Searching...
No Matches
impl_funcs_lib.hpp
Go to the documentation of this file.
1// --------------------------------------------------------------------------
2//
3// Copyright (C) 2025-present by Pablo Antolin
4//
5// This file is part of the QUGaR library.
6//
7// SPDX-License-Identifier: MIT
8//
9// --------------------------------------------------------------------------
10
11#ifndef QUGAR_IMPL_FUNCS_LIB_HPP
12#define QUGAR_IMPL_FUNCS_LIB_HPP
13
20
24#include <qugar/point.hpp>
25#include <qugar/types.hpp>
26#include <qugar/vector.hpp>
27
28#include <algoim/interval.hpp>
29
30#include <array>
31#include <memory>
32#include <type_traits>
33
37
38namespace _impl {
45 // NOLINTNEXTLINE (misc-no-recursion)
46 template<class T> constexpr T pow(const T base, unsigned const exponent) noexcept
47 {
48 return (exponent == 0) ? 1 : (base * pow(base, exponent - 1));
49 }
50
51 template<typename V> struct IsAlgoimVector;
52
53 template<typename T, int dim> struct IsAlgoimVector<Vector<T, dim>>
54 {
55 using value = std::integral_constant<bool, true>;
56 };
57
58 template<int dim> struct IsAlgoimVector<Point<dim>>
59 {
60 using value = std::integral_constant<bool, false>;
61 };
62
63 template<typename V, int new_dim> struct NewVector;
64
65 template<typename T, int dim, int new_dim> struct NewVector<Vector<T, dim>, new_dim>
66 {
68 };
69
70 template<int dim, int new_dim> struct NewVector<Point<dim>, new_dim>
71 {
73 };
74
75 template<typename V, int new_dim> using NewVector_t = typename NewVector<V, new_dim>::type;
76
77
78 template<typename V> struct VectorDim;
79
80 template<typename T, int dim_> struct VectorDim<Vector<T, dim_>>
81 {
82 static const int dim = dim_;
83 };
84
85 template<int dim_> struct VectorDim<Point<dim_>>
86 {
87 static const int dim = dim_;
88 };
89
90 template<typename V> using Hessian_t = typename NewVector<V, (VectorDim<V>::dim * (VectorDim<V>::dim + 1)) / 2>::type;
91
92 template<typename V> struct VectorType;
93
94 template<typename T, int dim> struct VectorType<Vector<T, dim>>
95 {
96 using type = T;
97 };
98
99 template<int dim> struct VectorType<Point<dim>>
100 {
101 using type = real;
102 };
103
104 template<typename V> using VectorType_t = typename VectorType<V>::type;
105
106
107}// namespace _impl
108
109
113template<int dim> class FuncWithAffineTransf : public ImplicitFunc<dim>
114{
115public:
119
124
125protected:
128};
129
130
136template<int dim> class Square : public FuncWithAffineTransf<dim>
137{
138public:
140 Square() = default;
141
146 explicit Square(const AffineTransf<dim> &transf);
147
149
155 template<typename T> static int sgn(const T &val);
156};
157
161template<int dim> class DimLinear : public FuncWithAffineTransf<dim>
162{
163public:
165 static const int num_coeffs = _impl::pow(2, dim);
166
170 explicit DimLinear(const std::array<real, num_coeffs> &coefs);
171
177 DimLinear(const std::array<real, num_coeffs> &coefs, const AffineTransf<dim> &transf);
178
180
181private:
183 std::array<real, num_coeffs> coefs_;
184};
185
186
191template<int dim> class TransformedFunction : public FuncWithAffineTransf<dim>
192{
193public:
198 TransformedFunction(const std::shared_ptr<const ImplicitFunc<dim>> &base_func, const AffineTransf<dim> &transf);
199
201
202private:
204 std::shared_ptr<const ImplicitFunc<dim>> base_func_;
205};
206
210template<int dim> class Negative : public ImplicitFunc<dim>
211{
212public:
216 explicit Negative(const std::shared_ptr<const ImplicitFunc<dim>> &func);
217
219
220private:
222 std::shared_ptr<const ImplicitFunc<dim>> func_;
223};
224
228template<int dim> class AddFunctions : public ImplicitFunc<dim>
229{
230public:
235 explicit AddFunctions(const std::shared_ptr<const ImplicitFunc<dim>> &lhs,
236 const std::shared_ptr<const ImplicitFunc<dim>> &rhs);
237
239
240private:
242 std::shared_ptr<const ImplicitFunc<dim>> lhs_;
244 std::shared_ptr<const ImplicitFunc<dim>> rhs_;
245};
246
250template<int dim> class SubtractFunctions : public ImplicitFunc<dim>
251{
252public:
257 explicit SubtractFunctions(const std::shared_ptr<const ImplicitFunc<dim>> &lhs,
258 const std::shared_ptr<const ImplicitFunc<dim>> &rhs);
259
261
262private:
264 std::shared_ptr<const ImplicitFunc<dim>> lhs_;
266 std::shared_ptr<const ImplicitFunc<dim>> rhs_;
267};
268
269
270}// namespace qugar::impl::funcs
271
272
273#endif// QUGAR_IMPL_FUNCS_LIB_HPP
Declaration of affine transformation class.
Class for representing affine transformations.
Definition affine_transf.hpp:46
Domain functions.
Definition domain_function.hpp:41
This function adds two functions together.
Definition impl_funcs_lib.hpp:229
AddFunctions(const std::shared_ptr< const ImplicitFunc< dim > > &lhs, const std::shared_ptr< const ImplicitFunc< dim > > &rhs)
Constructor.
std::shared_ptr< const ImplicitFunc< dim > > lhs_
Left-hand-side operand.
Definition impl_funcs_lib.hpp:242
declare_impl_func_virtual_interface
Definition impl_funcs_lib.hpp:238
std::shared_ptr< const ImplicitFunc< dim > > rhs_
Right-hand-side operand.
Definition impl_funcs_lib.hpp:244
dim-linear function.
Definition impl_funcs_lib.hpp:162
static const int num_coeffs
Number of coefficients.
Definition impl_funcs_lib.hpp:165
std::array< real, num_coeffs > coefs_
Coefficients of the dim-linear expression.
Definition impl_funcs_lib.hpp:183
DimLinear(const std::array< real, num_coeffs > &coefs, const AffineTransf< dim > &transf)
Constructs a new dim-linear function from its coefficients.
DimLinear(const std::array< real, num_coeffs > &coefs)
Constructs a new dim-linear function from its coefficients.
declare_impl_func_virtual_interface
Definition impl_funcs_lib.hpp:179
Function containing an affine transformation.
Definition impl_funcs_lib.hpp:114
FuncWithAffineTransf()
Default constructor. Creates and stores an identity transformation.
FuncWithAffineTransf(const AffineTransf< dim > &transf)
Constructs a new class storing the given transf.
AffineTransf< dim > transf_
Stored affine transformation.
Definition impl_funcs_lib.hpp:127
This function computes the negative of a given function.
Definition impl_funcs_lib.hpp:211
std::shared_ptr< const ImplicitFunc< dim > > func_
Function whose negative is computed.
Definition impl_funcs_lib.hpp:222
Negative(const std::shared_ptr< const ImplicitFunc< dim > > &func)
Constructor.
declare_impl_func_virtual_interface
Definition impl_funcs_lib.hpp:218
Dimension independent square function in domain [-1,1]^dim.
Definition impl_funcs_lib.hpp:137
Square()=default
Constructs a new square aligned with the Cartesian axes.
declare_impl_func_virtual_interface
Definition impl_funcs_lib.hpp:148
static int sgn(const T &val)
Returns the sign of the given value.
Square(const AffineTransf< dim > &transf)
Constructs a new square object transformed according to transf.
This function subtracts two functions.
Definition impl_funcs_lib.hpp:251
std::shared_ptr< const ImplicitFunc< dim > > rhs_
Right-hand-side operand.
Definition impl_funcs_lib.hpp:266
SubtractFunctions(const std::shared_ptr< const ImplicitFunc< dim > > &lhs, const std::shared_ptr< const ImplicitFunc< dim > > &rhs)
Constructor.
declare_impl_func_virtual_interface
Definition impl_funcs_lib.hpp:260
std::shared_ptr< const ImplicitFunc< dim > > lhs_
Left-hand-side operand.
Definition impl_funcs_lib.hpp:264
Creates a new implicit function that is just a base function to which an affine transformation is app...
Definition impl_funcs_lib.hpp:192
std::shared_ptr< const ImplicitFunc< dim > > base_func_
Base function to be transformed.
Definition impl_funcs_lib.hpp:204
TransformedFunction(const std::shared_ptr< const ImplicitFunc< dim > > &base_func, const AffineTransf< dim > &transf)
Constructs a new transformed function.
declare_impl_func_virtual_interface
Definition impl_funcs_lib.hpp:200
Declaration of a few implicit functions template class ready to be consumed by Algoim.
Declaration of macros to ease the definition/implementation of implicit functions.
constexpr T pow(const T base, unsigned const exponent) noexcept
Computes the exponent of a value.
Definition impl_funcs_lib.hpp:46
typename VectorType< V >::type VectorType_t
Definition impl_funcs_lib.hpp:104
typename NewVector< V, new_dim >::type NewVector_t
Definition impl_funcs_lib.hpp:75
typename NewVector< V,(VectorDim< V >::dim *(VectorDim< V >::dim+1))/2 >::type Hessian_t
Definition impl_funcs_lib.hpp:90
Definition impl_funcs_lib.hpp:36
double real
Definition types.hpp:18
::algoim::uvector< T, dim > Vector
Class representing a vector.
Definition vector.hpp:31
Vector< T, dim > Point
Class representing a dim-dimensional Point.
Definition point.hpp:34
Definition and implementation of Point class.
std::integral_constant< bool, false > value
Definition impl_funcs_lib.hpp:60
std::integral_constant< bool, true > value
Definition impl_funcs_lib.hpp:55
Definition impl_funcs_lib.hpp:51
Point< new_dim > type
Definition impl_funcs_lib.hpp:72
Vector< T, new_dim > type
Definition impl_funcs_lib.hpp:67
Definition impl_funcs_lib.hpp:63
Definition impl_funcs_lib.hpp:78
real type
Definition impl_funcs_lib.hpp:101
Definition impl_funcs_lib.hpp:92
Declaration of types.