-
Notifications
You must be signed in to change notification settings - Fork 0
/
EpsDel.l
138 lines (122 loc) · 3.19 KB
/
EpsDel.l
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
%{
#include <stdio.h>
#include "y.tab.h"
#include "function.h"
#include "float.h"
#include "math.h"
#include <stdbool.h>
#include <string.h>
int yyparse();
#undef ECHO
#define ECHO printf ("[%s]\n", yytext)
%}
%option noyywrap
%%
[ \t\n] ;
"cos" { return COS; }
"sin" { return SIN; }
"tan" { return TAN; }
"sec" { return SEC; }
"csc" { return CSC; }
"cot" { return COT; }
"e" { return E; }
"arcsin" { return ARCSIN; }
"arccos" { return ARCCOS; }
"arctan" { return ARCTAN; }
"arcsec" { return ARCSEC; }
"arccsc" { return ARCCSC; }
"arccot" { return ARCCOT; }
"ln" { return LN; }
"log" { return LOG; }
"sqrt" { return SQRT; }
\( { return LPARENTHESIS; }
\) { return RPARENTHESIS; }
\^ { return EXPONENT; }
\* { return MULTIPLY; }
\/ { return DIVIDE; }
\+ { return ADD; }
\- { return SUBTRACT; }
[xX] { return VARIABLE; }
[0-9\.]* { sscanf(yytext, "%lf", &yylval.fun->dub); return NUMBER; }
. { printf("Invalid input. Exiting.\n"); exit(0); }
%%
bool contains_x(char *func)
{
int i;
for(i = 0; i < strlen(func); i++)
if (func[i] == 'x' || func[i] == 'X')
return true;
return false;
}
function* init_function(double c)
{
function *returner = (function *) calloc(1, sizeof(function));
returner->c = c;
returner->dub = 0;
return returner;
}
void free_func(function *f)
{
free(f);
}
double solve_delta(double epsilon, double fvalue, double c, char *function)
{
double loopNumUp = 0;
double loopNumDown = 0;
double ub = c;
double lb = c;
bool ud = false;
while (1) {
yylval.fun = init_function((ub += 0.0000001));
YY_BUFFER_STATE my_string_buffer = yy_scan_string(function);
yy_switch_to_buffer( my_string_buffer );
yyparse();
yy_delete_buffer(my_string_buffer );
loopNumUp = fabs(yylval.du - fvalue);
if (loopNumUp >= epsilon) {
ud = true;
break;
}
yylval.fun = init_function((lb -= 0.0000001));
my_string_buffer = yy_scan_string(function);
yy_switch_to_buffer( my_string_buffer );
yyparse();
yy_delete_buffer(my_string_buffer );
loopNumDown = fabs(yylval.du - fvalue);
if (loopNumDown >= epsilon) {
ud = false;
break;
}
}
if (ud)
return fabs(ub-c);
else
return fabs(lb-c);
}
int main()
{
char function[88];
double epsilon;
double c;
printf("Please enter a single-variable function (will treat all letters as same variable): ");
scanf("%[^\n]", function);
printf("Function entered: %s\n", function);
printf("Please enter a positive value for epsilon (larger epsilons take more time to compute): ");
scanf("%lf", &epsilon);
printf("Epsilon entered: %.7lf\n", epsilon);
printf("Please enter a value for a point in the domain where you are trying to evaluate a delta: ");
scanf("%lf", &c);
printf("Value entered: %.7lf\n", c);
printf("Please wait while we compute. Thank you!\n");
yylval.fun = init_function(c);
YY_BUFFER_STATE my_string_buffer = yy_scan_string(function);
yy_switch_to_buffer( my_string_buffer );
yyparse();
yy_delete_buffer(my_string_buffer );
double copy;
memcpy(©, &yylval.du, sizeof(double));
if (contains_x(function))
printf("The delta that satisfies this system is less than %.7lf.\n", solve_delta(epsilon, copy, c, function));
else
printf("There is no such delta that satisfies this epsilon.\n");
}