cgv
Loading...
Searching...
No Matches
mfunc.h
1#pragma once
2
3#include <cgv/math/vec.h>
4
5namespace cgv {
6 namespace math {
7
14template <typename X, typename T>
15class mfunc
16{
17public:
23 virtual ~mfunc() {}
25 virtual unsigned get_nr_independent_variables() const = 0;
27 virtual T evaluate(const pnt_type& p) const = 0;
31 virtual vec_type evaluate_gradient(const pnt_type& p) const {
32 static X epsilon = (X)1e-5;
33 static X inv_2_eps = (X)(0.5/epsilon);
34 unsigned n = p.size();
35 vec_type g(n);
36 pnt_type q(p);
37 for (unsigned i=0; i<n; ++i) {
38 q(i) += epsilon;
39 g(i) = evaluate(q);
40 q(i) = p(i)-epsilon;
41 g(i) -= evaluate(q);
42 g(i) *= inv_2_eps;
43 q(i) = p(i);
44 }
45 return g;
46 }
47};
48
51template <typename X, typename T>
52class v2_func : public mfunc<X,T>
53{
54public:
56 unsigned int get_nr_independent_variables() const { return 2; }
57};
58
61template <typename X, typename T>
62class v3_func : public mfunc<X,T>
63{
64public:
66 unsigned int get_nr_independent_variables() const { return 3; }
67};
68
69 }
70}
interface class for multivariate function with two template arguments:
Definition mfunc.h:16
cgv::math::vec< X > vec_type
vectors must have get_nr_independent_variables() components
Definition mfunc.h:21
cgv::math::vec< X > pnt_type
points must have get_nr_independent_variables() components
Definition mfunc.h:19
virtual vec_type evaluate_gradient(const pnt_type &p) const
interface for evaluation of the gradient of the multivariate function.
Definition mfunc.h:31
virtual unsigned get_nr_independent_variables() const =0
return the number of independent variables that are mapped by the function
virtual T evaluate(const pnt_type &p) const =0
interface for evaluation of the multivariate function
virtual ~mfunc()
virtual destructor
Definition mfunc.h:23
specialization of a multivariate function to two independent variables, which only reimplements the m...
Definition mfunc.h:53
unsigned int get_nr_independent_variables() const
returns 2
Definition mfunc.h:56
specialization of a multivariate function to three independent variables, which only reimplements the...
Definition mfunc.h:63
unsigned int get_nr_independent_variables() const
returns 3
Definition mfunc.h:66
A column vector class.
Definition vec.h:28
unsigned size() const
number of elements
Definition vec.h:59
the cgv namespace
Definition print.h:11