API Documentation
Loading...
Searching...
No Matches
Equation.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Equation
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/String.h>
34#include <NDEVR/Dictionary.h>
35#include <cmath>
36namespace NDEVR
37{
38 /**--------------------------------------------------------------------------------------------------
39 \brief Stores in a compressed way, a formula that can be solved.
40 A formula is made of up variables, numbers, and operations. Formulas can be simplified, or potentially
41 solved to a numeric value.
42 **/
43 template<class t_type>
45 {
46 public:
48 {}
50 {
52 t_type value;
53 };
54 void setVariable(const String& var, t_type value)
55 {
56 m_variables[var] = value;
57 if (m_variable_locations.hasKey(var))
58 {
59 Buffer<uint04>& locations = m_variable_locations[var];
60 for (uint04 i = 0; i < locations.size(); i++)
61 m_equation_stack[locations[i]].value = value;
62 }
63 }
65 {
66 for (const auto& variable : m_variables)
67 {
68 Buffer<uint04>& locations = m_variable_locations[variable.first];
69 for (uint04 i = 0; i < locations.size(); i++)
70 m_equation_stack[locations[i]].value = variable.second;
71 }
72 }
73 static t_type solve(t_type left, const char op, t_type right)
74 {
75 switch (op)
76 {
77 case '+': return left + right;
78 case '-': return left - right;
79 case '/': return left / right;
80 case '*': return left * right;
81 case '^': return std::pow(left, right);
82 case '%': return std::fmod(left, right);
83 case 's': return std::sin(right);
84 case 'c': return std::cos(right);
85 case 't': return std::tan(right);
86 case 'q': return std::sqrt(right);
87 case '\0': return right;
88 default: return Constant<t_type>::Invalid;
89 }
90 }
91 t_type solve() const
92 {
93 uint04 index = 0;
94 t_type value = solve(cast<t_type>(0), index);
95
96 if (index != m_equation_stack.size())
98 return value;
99 }
100 void add(char operation, t_type value)
101 {
102 EquationPart part{};
103 part.value = value;
104 part.operation = operation;
105 m_equation_stack.add(part);
106 }
107 static uint04 getOrderPriority(const char op)
108 {
109 switch (op)
110 {
111 case '+': return 10;
112 case '-': return 10;
113 case '/': return 20;
114 case '*': return 20;
115 case '^': return 30;
116 case '%': return 30;
117 case 's': return 40;
118 case 'c': return 40;
119 case 't': return 40;
120 case 'q': return 40;
121 default: return Constant<t_type>::Invalid;
122 }
123 }
124 void addOrderOfOps(char operation, t_type value)
125 {
126 EquationPart part;
127 part.value = value;
128 part.operation = operation;
129 uint04 insertion_index = m_equation_stack.size();
130 m_equation_stack.add(insertion_index, part);
131 }
132 void add(char operation, const String& value)
133 {
134 m_variable_locations[value].add(m_equation_stack.size());
135
136 EquationPart part{};
138 part.operation = operation;
139
140 m_equation_stack.add(part);
141 }
142 t_type solve(t_type start, uint04& current_index) const
143 {
144 for (; current_index < m_equation_stack.size(); current_index++)
145 {
146 if (IsInvalid(m_equation_stack[current_index].value))
148 const char current = m_equation_stack[current_index].operation;
149 switch (current)
150 {
151 case '\0': start += m_equation_stack[current_index].value; break;
152 case '+': start += m_equation_stack[current_index].value; break;
153 case '-': start -= m_equation_stack[current_index].value; break;
154 case '/': start /= m_equation_stack[current_index].value; break;
155 case '*': start *= m_equation_stack[current_index].value; break;
156 case '^': start = std::pow(start, m_equation_stack[current_index].value); break;
157 case '%': start = std::fmod(start, m_equation_stack[current_index].value); break;
158 case 's':
159 case 'c':
160 case 't':
161 case 'q':
162 case '(':
163 {
164 const char lead_operation = cast<char>(m_equation_stack[current_index].value);
165 t_type inner_solve = solve(cast<t_type>(0), ++current_index);
166 if (current_index >= m_equation_stack.size())
167 return Constant<t_type>::Invalid;//no closing ')'
168 switch (current)
169 {
170 case 's': inner_solve = sin(inner_solve); break;
171 case 'c': inner_solve = cos(inner_solve); break;
172 case 't': inner_solve = tan(inner_solve); break;
173 case 'q': inner_solve = sqrt(inner_solve); break;
174 default: break;
175 }
176 start = solve(start, lead_operation, inner_solve);
177 } break;
178 case ')': return start;
179 default: lib_assert(false, "unknown operator");
180 }
181 }
182 return start;
183 }
187 };
188
189}
190
191
#define lib_assert(expression, message)
Definition LibAssert.h:61
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
constexpr t_index_type size() const
Definition Buffer.hpp:823
A hash-based key-value store, useful for quick associative lookups. Key features include:
Definition Dictionary.h:61
Stores in a compressed way, a formula that can be solved. A formula is made of up variables,...
Definition Equation.h:45
void refreshVariables()
Definition Equation.h:64
void add(char operation, t_type value)
Definition Equation.h:100
Equation()
Definition Equation.h:47
static t_type solve(t_type left, const char op, t_type right)
Definition Equation.h:73
void add(char operation, const String &value)
Definition Equation.h:132
void setVariable(const String &var, t_type value)
Definition Equation.h:54
Dictionary< String, t_type > m_variables
Definition Equation.h:185
Buffer< EquationPart > m_equation_stack
Definition Equation.h:186
t_type solve(t_type start, uint04 &current_index) const
Definition Equation.h:142
static uint04 getOrderPriority(const char op)
Definition Equation.h:107
void addOrderOfOps(char operation, t_type value)
Definition Equation.h:124
t_type solve() const
Definition Equation.h:91
Dictionary< String, Buffer< uint04 > > m_variable_locations
Definition Equation.h:184
The core String class for the NDEVR API.
Definition String.h:69
Definition ACIColor.h:37
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type tan(const Angle< t_type > &angle)
Performs optimized tangent operation on the given angle using pre-computed lookup table for optimal s...
Definition AngleFunctions.h:156
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type cos(const Angle< t_type > &angle)
Performs optimized cosine operation on the given angle using pre-computed lookup table for optimal sp...
Definition AngleFunctions.h:124
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
t_type sqrt(const t_type &value)
Definition VectorFunctions.hpp:1225
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type sin(const Angle< t_type > &angle)
Performs optimized sine operation on the given angle using pre-computed lookup table for optimal spee...
Definition AngleFunctions.h:79
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233
Definition Equation.h:50
char operation
Definition Equation.h:51
t_type value
Definition Equation.h:52