symbolic4
expression.h
Go to the documentation of this file.
1 
2 /*
3 
4  Copyright (c) 2019 Hannes Eberhard
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 
24  */
25 
26 #ifndef expression_h
27 #define expression_h
28 
29 #include "symbolic4.h"
30 
31 typedef enum {
39 
40 typedef enum {
41 
43 
47 
54 
64 
69 
78 
85 
87 
93 
98 
100 
101 typedef enum {
106 
107 typedef struct numeric_value {
108  uintmax_t numerator;
109  uintmax_t denominator;
110 } numeric_value;
111 
112 typedef struct expression {
113 
116 
117  int8_t sign;
119  uint8_t child_count;
120 
121 #ifdef DEBUG_MODE
122  struct expression* children[50];
123 #else
125 #endif
126 
127  union {
128  char symbolic[10];
130  } value;
131 
132 } expression;
133 
134 extern char* keyword_strings[];
136 
137 expression* new_expression(expression_type type, expression_identifier identifier, uint8_t child_count, ...);
138 expression* new_literal(int8_t sign, uintmax_t numerator, uintmax_t denominator);
139 expression* new_symbol(expression_identifier identifier, const char* value);
141 expression* copy_expression(const expression* source);
143 void free_expression(expression* source, bool persistent);
144 void free_expressions(uint8_t expression_count, ...);
145 void free_all_except(expression* source);
146 void append_child(expression* parent, expression* child);
147 void set_parents(expression* source);
148 bool expressions_are_identical(const expression* a, expression* b, bool persistent);
149 bool expressions_are_equivalent(const expression* a, expression* b, bool persistent);
150 bool expression_is_greater_than(const expression* a, expression* b, bool persistent);
151 bool expression_is_smaller_than(const expression* a, expression* b, bool persistent);
152 bool expression_is_constant(const expression* source);
153 bool symbol_is_constant(const expression* source);
154 int8_t expression_contains_division(const expression* source);
155 bool expression_is_reziprocal(const expression* source);
156 bool expression_is_numerical(const expression* source);
157 uint8_t count_occurrences(const expression* haystack, expression* needle, bool persistent);
158 void collect_symbols(expression* symbols, const expression* source);
159 void remove_child_at_index(expression* source, uint8_t index);
160 void remove_null_children(expression* source);
162 void merge_nested_lists(expression* source, bool recursive);
163 uint8_t swap_expressions(expression* a, expression* b);
164 void replace_occurences(expression* source, const expression* child, const expression* replacement);
165 void replace_null_with_zero(expression* source);
166 void order_children(expression* source);
167 expression* guess_symbol(const expression* source, const char* custom_priorities, uint8_t rank);
168 expression* get_symbol(const expression* source);
169 expression* double_to_literal(double source);
170 double literal_to_double(expression* source);
172 const char* get_expression_string(expression_identifier identifier);
174 void expression_to_string(char* buffer, const expression* source, expression_to_string_format format);
175 #ifdef DEBUG_MODE
176 void print_expression(const expression* source);
177 #endif
178 
179 #endif /* expression_h */
EXPI_SOLVE
Definition: expression.h:66
EXPI_LIST
Definition: expression.h:90
EXPI_NULL
Definition: expression.h:42
EXPT_FUNCTION
Definition: expression.h:35
expressions_are_identical
bool expressions_are_identical(const expression *a, expression *b, bool persistent)
Definition: expression.c:467
numeric_value::denominator
uintmax_t denominator
Definition: expression.h:109
keyword_identifiers
expression_identifier keyword_identifiers[]
Definition: expression.c:91
symbolic4.h
copy_expression
expression * copy_expression(const expression *source)
Returns a deep copy of an expression.
Definition: expression.c:286
EXPI_LEFT_PARENTHESIS
Definition: expression.h:95
EXPI_LN
Definition: expression.h:56
replace_occurences
void replace_occurences(expression *source, const expression *child, const expression *replacement)
Definition: expression.c:688
EXPI_EXPONENTATION
Definition: expression.h:53
EXPI_ARCTAN
Definition: expression.h:63
replace_expression
void replace_expression(expression *a, expression *b)
Definition: expression.c:309
symbol_is_constant
bool symbol_is_constant(const expression *source)
Definition: expression.c:611
expression::symbolic
char symbolic[10]
Definition: expression.h:128
expression::type
expression_type type
Definition: expression.h:114
EXPI_POLYNOMIAL_DENSE
Definition: expression.h:89
EXPI_SYMBOL
Definition: expression.h:45
expression_identifier
expression_identifier
Definition: expression.h:40
EXPI_ABS
Definition: expression.h:55
EXPI_COMMA
Definition: expression.h:94
expression
Definition: expression.h:112
expression::identifier
expression_identifier identifier
Definition: expression.h:115
expression_contains_division
int8_t expression_contains_division(const expression *source)
Definition: expression.c:622
numeric_value
struct numeric_value numeric_value
EXPI_COS
Definition: expression.h:59
EXPT_CONTROL
Definition: expression.h:37
EXPT_STRUCTURE
Definition: expression.h:36
EXPI_V_TRIPLE_PRODUCT
Definition: expression.h:84
EXPI_RIGHT_PARENTHESIS
Definition: expression.h:96
EXPI_V_MAG
Definition: expression.h:79
ETSF_INFIX
Definition: expression.h:103
free_expression
void free_expression(expression *source, bool persistent)
Definition: expression.c:315
expression::sign
int8_t sign
Definition: expression.h:117
get_symbol
expression * get_symbol(const expression *source)
Definition: expression.c:878
EXPI_V_NORMALIZED
Definition: expression.h:80
EXPI_LOG
Definition: expression.h:57
EXPT_VALUE
Definition: expression.h:33
EXPI_STATIONARY_POINTS
Definition: expression.h:73
EXPI_TANGENT
Definition: expression.h:75
expression
struct expression expression
remove_child_at_index
void remove_child_at_index(expression *source, uint8_t index)
Definition: expression.c:397
expression_is_reziprocal
bool expression_is_reziprocal(const expression *source)
Definition: expression.c:636
free_expressions
void free_expressions(uint8_t expression_count,...)
Definition: expression.c:338
EXPI_ARCSIN
Definition: expression.h:61
expression::value
union expression::@0 value
EXPI_V_CROSS_PRODUCT
Definition: expression.h:83
EXPI_NORMAL
Definition: expression.h:76
literal_to_double_symbol
void literal_to_double_symbol(expression *source)
Definition: expression.c:912
EXPI_EXTENSION
Definition: expression.h:92
literal_to_double
double literal_to_double(expression *source)
Definition: expression.c:908
guess_symbol
expression * guess_symbol(const expression *source, const char *custom_priorities, uint8_t rank)
Definition: expression.c:830
ETSF_TIKZ
Definition: expression.h:104
EXPI_DERIVATIVE
Definition: expression.h:70
expression_is_numerical
bool expression_is_numerical(const expression *source)
Definition: expression.c:646
embed_in_list_if_necessary
void embed_in_list_if_necessary(expression *source)
Definition: expression.c:417
double_to_literal
expression * double_to_literal(double source)
Definition: expression.c:896
EXPI_PARSE
Definition: expression.h:97
free_all_except
void free_all_except(expression *source)
Definition: expression.c:353
keyword_strings
char * keyword_strings[]
Definition: expression.c:28
new_expression
expression * new_expression(expression_type type, expression_identifier identifier, uint8_t child_count,...)
Allocates and initializes a new expression with the arguments provided.
Definition: expression.c:183
EXPI_MULTIPLICATION
Definition: expression.h:51
EXPI_SIN
Definition: expression.h:58
new_symbol
expression * new_symbol(expression_identifier identifier, const char *value)
Allocates and initializes a new symbol/variable expression.
Definition: expression.c:252
EXPI_INFLECTION_POINTS
Definition: expression.h:74
set_parents
void set_parents(expression *source)
Definition: expression.c:455
EXPI_FACTORS
Definition: expression.h:67
EXPI_POLYNOMIAL_SPARSE
Definition: expression.h:88
merge_nested_lists
void merge_nested_lists(expression *source, bool recursive)
Definition: expression.c:424
EXPT_OPERATION
Definition: expression.h:34
get_expression_identifier
expression_identifier get_expression_identifier(const char *string)
Returns the identifier corresponding to the keyword string.
Definition: expression.c:987
expression::parent
struct expression * parent
Definition: expression.h:118
expression_type
expression_type
Definition: expression.h:31
new_literal
expression * new_literal(int8_t sign, uintmax_t numerator, uintmax_t denominator)
Allocates and initializes a new literal expression.
Definition: expression.c:225
expression::numeric
numeric_value numeric
Definition: expression.h:129
EXPI_AREA
Definition: expression.h:72
order_children
void order_children(expression *source)
Definition: expression.c:769
EXPI_SUBTRACTION
Definition: expression.h:50
count_occurrences
uint8_t count_occurrences(const expression *haystack, expression *needle, bool persistent)
Definition: expression.c:664
collect_symbols
void collect_symbols(expression *symbols, const expression *source)
Definition: expression.c:815
new_trigonometic_periodicity
expression * new_trigonometic_periodicity(uint8_t period)
Definition: expression.c:258
ETSF_NULL
Definition: expression.h:102
expressions_are_equivalent
bool expressions_are_equivalent(const expression *a, expression *b, bool persistent)
Definition: expression.c:526
expression_to_string
void expression_to_string(char *buffer, const expression *source, expression_to_string_format format)
Serializes an expression into a specified format.
Definition: expression.c:1031
EXPI_EQUATION
Definition: expression.h:48
EXPI_TAN
Definition: expression.h:60
EXPI_ARCCOS
Definition: expression.h:62
swap_expressions
uint8_t swap_expressions(expression *a, expression *b)
Definition: expression.c:718
get_expression_string
const char * get_expression_string(expression_identifier identifier)
Returns the keyword string corresponding to the identifier.
Definition: expression.c:950
EXPI_APPROXIMATE
Definition: expression.h:86
EXPI_VARIABLE
Definition: expression.h:46
EXPI_MATRIX
Definition: expression.h:91
EXPI_V_DOT_PRODUCT
Definition: expression.h:82
numeric_value
Definition: expression.h:107
remove_null_children
void remove_null_children(expression *source)
Definition: expression.c:403
EXPI_ADDITION
Definition: expression.h:49
EXPI_INTEGRAL
Definition: expression.h:71
EXPI_VALUE
Definition: expression.h:68
expression_to_string_format
expression_to_string_format
Definition: expression.h:101
expression_is_constant
bool expression_is_constant(const expression *source)
Definition: expression.c:593
append_child
void append_child(expression *parent, expression *child)
Appends a child to an expression.
Definition: expression.c:383
expression_is_smaller_than
bool expression_is_smaller_than(const expression *a, expression *b, bool persistent)
Definition: expression.c:570
EXPT_NULL
Definition: expression.h:32
expression_is_greater_than
bool expression_is_greater_than(const expression *a, expression *b, bool persistent)
Definition: expression.c:547
EXPI_V_ANGLE
Definition: expression.h:81
expression::child_count
uint8_t child_count
Definition: expression.h:119
expression::children
struct expression ** children
Definition: expression.h:124
EXPI_ANGLE
Definition: expression.h:77
EXPI_SIMPLIFY
Definition: expression.h:65
replace_null_with_zero
void replace_null_with_zero(expression *source)
Definition: expression.c:703
numeric_value::numerator
uintmax_t numerator
Definition: expression.h:108
EXPI_DIVISION
Definition: expression.h:52
EXPI_LITERAL
Definition: expression.h:44