-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.h
95 lines (70 loc) · 2.97 KB
/
sequence.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
/****************************************************************************
** Copyright (c) 2023, Adel Kara Slimane <[email protected]>
**
** This file is part of ZeCalculator's source code.
**
** ZeCalculators is free software: you may copy, redistribute and/or modify it
** under the terms of the GNU Affero General Public License as published by the
** Free Software Foundation, either version 3 of the License, or (at your
** option) any later version.
**
** This file is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include <zecalculator/math_objects/decl/function.h>
/* TODO: update approach as the following:
- Check for validity
- Enable setting custom names for functions and variables
- Performance improvement: flatten trees
*/
namespace zc {
template <parsing::Type type>
class MathWorld;
template <parsing::Type type>
class DynMathObject;
using Vals = std::vector<double>;
template <class T>
struct is_sequence: std::false_type {};
template <class T>
inline constexpr bool is_sequence_v = is_sequence<T>::value;
/// @brief a class that represents a Sequence of single argument
template <parsing::Type type>
class Sequence: public MathObject
{
public:
const std::string& get_equation() const { return equation; }
/// @brief evaluates the sequence at the given index
tl::expected<double, Error> evaluate(double index, eval::Cache* cache = nullptr) const;
/// @brief operator version of evaluate
tl::expected<double, Error> operator () (double index, eval::Cache* cache = nullptr) const;
protected:
// constructor reserved for MathWorld when using add() function
Sequence(MathObject obj, std::string equation);
Sequence(MathObject obj, std::string equation, std::vector<parsing::Parsing<type>> values);
/// @brief evaluation with recursion depth tracking
tl::expected<double, Error> evaluate(double index,
size_t current_recursion_depth,
eval::Cache* cache = nullptr) const;
template <parsing::Type>
friend struct eval::Evaluator;
std::string equation;
/// @brief first values of the sequence
/// @note the last value is the "default" expression
/// @example Fibonacci: values = {parsing_of("1"), parsing_of("1"), parsing_of("u(n-1) + u(n-2)")}
std::vector<parsing::Parsing<type>> values;
template <parsing::Type>
friend class MathWorld;
friend struct EqObject;
};
template <parsing::Type type>
struct is_sequence<Sequence<type>>: std::true_type {};
template <parsing::Type type>
struct is_function<Sequence<type>>: std::true_type {};
}