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 template<class t_type>
40 {
41 public:
43 {}
45 {
47 t_type value;
48 };
49 void setVariable(const String& var, t_type value)
50 {
51 m_variables[var] = value;
52 if (m_variable_locations.hasKey(var))
53 {
54 Buffer<uint04>& locations = m_variable_locations[var];
55 for (uint04 i = 0; i < locations.size(); i++)
56 m_equation_stack[locations[i]].value = value;
57 }
58 }
60 {
61 for (const auto& variable : m_variables)
62 {
63 Buffer<uint04>& locations = m_variable_locations[variable.first];
64 for (uint04 i = 0; i < locations.size(); i++)
65 m_equation_stack[locations[i]].value = variable.second;
66 }
67 }
68 static t_type solve(t_type left, const char op, t_type right)
69 {
70 switch (op)
71 {
72 case '+': return left + right;
73 case '-': return left - right;
74 case '/': return left / right;
75 case '*': return left * right;
76 case '^': return std::pow(left, right);
77 case '%': return std::fmod(left, right);
78 case 's': return std::sin(right);
79 case 'c': return std::cos(right);
80 case 't': return std::tan(right);
81 case 'q': return std::sqrt(right);
82 case '\0': return right;
83 default: return Constant<t_type>::NaN;
84 }
85 }
86 t_type solve() const
87 {
88 uint04 index = 0;
89 t_type value = solve(cast<t_type>(0), index);
90
91 if (index != m_equation_stack.size())
93 return value;
94 }
95 void add(char operation, t_type value)
96 {
97 EquationPart part{};
98 part.value = value;
99 part.operation = operation;
100 m_equation_stack.add(part);
101 }
102 static uint04 getOrderPriority(const char op)
103 {
104 switch (op)
105 {
106 case '+': return 10;
107 case '-': return 10;
108 case '/': return 20;
109 case '*': return 20;
110 case '^': return 30;
111 case '%': return 30;
112 case 's': return 40;
113 case 'c': return 40;
114 case 't': return 40;
115 case 'q': return 40;
116 default: return Constant<t_type>::NaN;
117 }
118 }
119 void addOrderOfOps(char operation, t_type value)
120 {
121 EquationPart part;
122 part.value = value;
123 part.operation = operation;
124
125 //uint04 priority = getOrderPrioriy(value);
126 uint04 insertion_index = m_equation_stack.size();
127 /*for (insertion_index; insertion_index != 0; --insertion_index)
128 {
129 if (getOrderPrioriy(m_equation_stack[insertion_index - 1].operation) < priority)
130 {
131 if(insertion_index - 1 == 0 || m_equation_stack[insertion_index - 1])
132
133 }
134 }*/
135 m_equation_stack.add(insertion_index, part);
136 }
137 void add(char operation, const String& value)
138 {
139 m_variable_locations[value].add(m_equation_stack.size());
140
141 EquationPart part{};
143 part.operation = operation;
144
145 m_equation_stack.add(part);
146 }
147 t_type solve(t_type start, uint04& current_index) const
148 {
149 for (; current_index < m_equation_stack.size(); current_index++)
150 {
151 if (isNaN(m_equation_stack[current_index].value))
153 const char current = m_equation_stack[current_index].operation;
154 switch (current)
155 {
156 case '\0': start += m_equation_stack[current_index].value; break;
157 case '+': start += m_equation_stack[current_index].value; break;
158 case '-': start -= m_equation_stack[current_index].value; break;
159 case '/': start /= m_equation_stack[current_index].value; break;
160 case '*': start *= m_equation_stack[current_index].value; break;
161 case '^': start = std::pow(start, m_equation_stack[current_index].value); break;
162 case '%': start = std::fmod(start, m_equation_stack[current_index].value); break;
163 case 's':
164 case 'c':
165 case 't':
166 case 'q':
167 case '(':
168 {
169 const char lead_operation = cast<char>(m_equation_stack[current_index].value);
170 t_type inner_solve = solve(cast<t_type>(0), ++current_index);
171 if (current_index >= m_equation_stack.size())
172 return Constant<t_type>::NaN;//no closing ')'
173 switch (current)
174 {
175 case 's': inner_solve = sin(inner_solve); break;
176 case 'c': inner_solve = cos(inner_solve); break;
177 case 't': inner_solve = tan(inner_solve); break;
178 case 'q': inner_solve = sqrt(inner_solve); break;
179 default: break;
180 }
181 start = solve(start, lead_operation, inner_solve);
182 } break;
183 case ')': return start;
184 default: lib_assert(false, "unknown operator");
185 }
186 }
187 return start;
188 }
192 };
193
194}
195
196
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
constexpr t_index_type size() const
Definition Buffer.hpp:1461
Definition Dictionary.h:48
Definition Equation.h:40
void refreshVariables()
Definition Equation.h:59
void add(char operation, t_type value)
Definition Equation.h:95
Equation()
Definition Equation.h:42
static t_type solve(t_type left, const char op, t_type right)
Definition Equation.h:68
void add(char operation, const String &value)
Definition Equation.h:137
void setVariable(const String &var, t_type value)
Definition Equation.h:49
Dictionary< String, t_type > m_variables
Definition Equation.h:190
Buffer< EquationPart > m_equation_stack
Definition Equation.h:191
t_type solve(t_type start, uint04 &current_index) const
Definition Equation.h:147
static uint04 getOrderPriority(const char op)
Definition Equation.h:102
void addOrderOfOps(char operation, t_type value)
Definition Equation.h:119
t_type solve() const
Definition Equation.h:86
Dictionary< String, Buffer< uint04 > > m_variable_locations
Definition Equation.h:189
Definition String.h:40
Definition ACIColor.h:37
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type tan(const Angle< t_type > &angle)
Definition AngleFunctions.h:182
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type cos(const Angle< t_type > &angle)
Definition AngleFunctions.h:154
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
t_type sqrt(const t_type &value)
Definition VectorFunctions.hpp:1309
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type sin(const Angle< t_type > &angle)
Definition AngleFunctions.h:113
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:200
Definition BaseValues.hpp:272
Definition Equation.h:45
char operation
Definition Equation.h:46
t_type value
Definition Equation.h:47