This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.cpp
2250 lines (2092 loc) · 84.6 KB
/
Function.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "stdafx.h"
std::wstring functionList[78] = {//list of all functions recognized by the 'evaluate()' function of the Function class
L"abs(", L"acos(", L"acosh(", L"arg(", L"asin(", L"asinh(", L"atan(", L"atan2(", L"atanh(", L"BesselFunctionFirstKind(",
L"BesselFunctionSecondKind(", L"beta", L"cbrt(", L"Ci(", L"conj(", L"ceil(", L"combination(", L"cos(", L"cosh(",
L"cot(", L"csc(", L"digamma(", L"doubleFactorial(", L"Ei(", L"erf(", L"erfc", L"exp(", L"exp2(",
L"factorial(", L"fallingFactorial(", L"floor(", L"gamma(", L"hypergeometricFunction(", L"hypot(",
L"if(", L"imag(", L"int(", L"inverseErfc", L"LambertW(", L"Li(", L"log(", L"log2(", L"log10(", L"logGamma(",
L"logit(", L"logisticFunction(",
L"max(", L"min(", L"permutation(", L"prime(", L"sumPrimes(", L"productPrimes(", L"primeCountingFunction(",
L"polar(", L"pow(", L"polygamma(", L"real(", L"regularModifiedBesselFunctionFirstKind(",
L"rect(", L"risingFactorial(", L"sec(", L"Si(", L"sin(", L"sinc(",
L"sinh(", L"sphericalBesselFunctionFirstKind(",
L"squarewave(", L"sgn(", L"sqrt(", L"step(", L"StirlingNumberFirstKind(", L"StirlingNumberSecondKind(",
L"tan(", L"tanh(", L"tri(", L"trunc(", L"WrightOmegaFunction(",
L"zeta("
};
unsigned int functionCount = 78;
Function::Function() {}
Function::~Function() {}
Function::Function(std::wstring str) { //input of format f(x,y,w,z...) = x*3 + exp(-3*y*z) - ...
if (str.find(L"=") != std::wstring::npos) { //case: string is 'f(x) = 2*x + exp(-y)'
while (str[0] != '(') { str = str.substr(1); }//clip off everything leading up to the variable definition
str = str.substr(1);
for (int i = 0; i < str.find(L"=") + 1; ++i) {
if (isLetter(str[i]) == true) { variables.push_back(str[i]); }
}
function = str.substr(str.find(L"=") + 1);
}
else { function = str; } //case: string has no variables, i.e. '23-12+exp(3)'
}
Function::Function(std::wstring vars, std::wstring funct) {
for (int i = 0; i < vars.size(); ++i) {
if (isLetter(vars[i]) == true) { variables.push_back(vars[i]); }
}
function = funct;
}
Function::Function(std::vector <wchar_t> vars, std::wstring functs) {
function = functs;
variables = vars;
}
Function::Function(std::vector<std::wstring> vec) {
function = vec[0];
variables = varsToChars(vec[1]);
}
//class methods
//=============
void Function::clear() {
variables.clear();
function.clear();
}
std::vector<wchar_t> Function::varsToChars(std::wstring vars) {
std::vector<wchar_t> variables;
for (int i = 0; i < vars.size(); ++i) {
if (isLetter(vars[i]) == true && vars[i] != ',') { variables.push_back(vars[i]); }
}
return variables;
}
std::vector<wchar_t> Function::combineVariables(std::vector<wchar_t> v1, std::vector<wchar_t> v2) {
for (int i = 0; i < v2.size(); ++i) {
bool skip = 0;
for (int j = 0; j < v1.size(); ++j) {//if any char in v2 is the same as a char in v1, do not append it to v1
if (v2[i] == v1[j]) {
skip = 1;
}
}
if (skip == 0) {//if the char is not a duplicate, append it to v1
v1.push_back(v2[i]);
}
}
return v1;
}
bool Function::variablesMatch(Function a, Function b) {
if (a.variables.size() != b.variables.size()) { return false; }
for (int i = 0; i < a.variables.size(); ++i) {
bool findVar = 0;
for (int j = 0; j < b.variables.size(); ++j) {
if (a.variables[i] == b.variables[j]) { findVar = 1; }
}
if (findVar == 0) { return false; }
}
//if all else fails, return true:
return true;
}
//ARITHMETIC METHODS
//===================
Function Function::abs(Function a) {
std::wstring func = L"abs(";
func.append(a.function);
func.append(L")");
return Function(a.variables, func);
}
Function Function::add(Function a, Function b) {
a.function.append(L"+(");
a.function.append(b.function);
a.function.append(L")");
return Function(combineVariables(a.variables, b.variables), a.function);
}
Function Function::subtract(Function a, Function b) {
a.function.append(L"-(");
a.function.append(b.function);
a.function.append(L")");
return Function(combineVariables(a.variables, b.variables), a.function);
}
Function Function::multiply(Function a, Function b) {
a.function.append(L"*(");
a.function.append(b.function);
a.function.append(L")");
return Function(combineVariables(a.variables, b.variables), a.function);
}
Function Function::divide(Function a, Function b) {
a.function.append(L"/(");
a.function.append(b.function);
a.function.append(L")");
return Function(combineVariables(a.variables, b.variables), a.function);
}
Function Function::inverse(Function a) {
std::wstring func = L"1/(";
func.append(a.function);
func.append(L")");
return Function(a.variables, func);
}
Function Function::exponent(Function a, Function b)
{
a.function.append(L"^(");
a.function.append(b.function);
a.function.append(L")");
return Function(combineVariables(a.variables, b.variables), a.function);
}
//OVERLOADED OPERATORS
//====================
Function& Function::operator*=(Function& rhs) { *this = multiply(*this, rhs); return *this; }
Function& Function::operator/=(Function& rhs) { *this = divide(*this, rhs); return *this; }
Function& Function::operator+=(Function& rhs) { *this = add(*this, rhs); return *this; }
Function& Function::operator-=(Function& rhs) { *this = subtract(*this, rhs); return *this; }
Function& Function::operator*(Function& rhs) { return multiply(*this, rhs); }
Function& Function::operator/(Function& rhs) { return divide(*this, rhs); }
Function& Function::operator+(Function& rhs) { return add(*this, rhs); }
Function& Function::operator-(Function& rhs) { return subtract(*this, rhs); }
std::wostream& operator<<(std::wostream& os, Function& rhs) {
os << rhs.toString();
return os;
}
std::wstring Function::replaceVariable(std::wstring str, wchar_t replace, std::wstring replacer) {
for (int i = 0; i < str.length(); ++i) {//run replace loop ONCE only
std::wstring sub = str.substr(i, str.length());
int pos = sub.find(replace) + i;
if (sub.find(replace) == std::wstring::npos || pos < 0 || sub.find(replace) >= str.length() - 1) {
//break if done replacing
i = str.length() + 1;
pos = std::wstring::npos;//set position to NULL for string type
}
if (
(pos > 0 && pos < str.length() && isLetter(sub[sub.find(replace) + 1]) == false && isLetter(sub[sub.find(replace) - 1]) == false) ||
(pos == 0 && isLetter(sub[sub.find(replace) + 1]) == false) ||
(pos == str.length() && isLetter(sub[sub.find(replace) - 1]) == false)
)
{
str.erase(pos, 1);
str.insert(pos, replacer);
i = pos + replacer.length();
}
}
return str;
}
Function Function::compose(Function a, Function b) {
//variables of both functions must match
if (variablesMatch(a, b) == false) { return Function(); }
std::wstring replacer = L"(";
replacer.append(b.function);
replacer.append(L")");
for (int i = 0; i < a.variables.size(); ++i) {
a.function = replaceVariable(a.function, a.variables[i], replacer);
}
return a;
}
double Function::evalFunction(std::wstring str) {//convert string to double if the string is a function of the kind that is parsable
if (str.length() < 2) { return 0; }//all function names have more than 2 letters in a row
if (str.find(L"abs(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"abs(") + 1));
return std::abs(tmp);
}
if (str.find(L"acos(") != std::wstring::npos && str.find(L"acosh(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"acos(") + 1));
return acos(tmp);
}
if (str.find(L"acosh(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"acosh(") + 1));
return acosh(tmp);
}
/* if (str.find(L"arg(L") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"arg(L") + 1));
return acos(tmp);
}*/
if (str.find(L"asin(") != std::wstring::npos && str.find(L"asinh(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"asin(") + 1));
return asin(tmp);
}
if (str.find(L"asinh(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"asinh(") + 1));
return asinh(tmp);
}
if (str.find(L"atan(") != std::wstring::npos && str.find(L"atan2(") == std::wstring::npos && str.find(L"atanh(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"atan(") + 1));
return atan(tmp);
}
if (str.find(L"atan2(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return atan2(tmp[1], tmp[0]);
}
if (str.find(L"atanh(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"atanh(") + 1));
return atanh(tmp);
}
if (
str.find(L"BesselFunctionFirstKind(") != std::wstring::npos
&& str.find(L"sphericalBesselFunctionFirstKind") == std::wstring::npos
&& str.find(L"regularModifiedBesselFunctionFirstKind(") == std::wstring::npos
) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return BesselFunction1stKind(tmp[0], tmp[1]);
}
if (str.find(L"BesselFunctionSecondKind(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return BesselFunction2ndKind(tmp[0], tmp[1]);
}
if (str.find(L"beta(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return betaFunction(tmp);
}
/* if (str.find(L"cbrt(L") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"cbrt(L") + 1));
return acos(tmp);
}*/
if (str.find(L"combination(L") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"combination(L") + 1));
if (tmp.size() == 2) { return combination(tmp[0], tmp[1]); }
else { return 0; }
}
/*if (str.find(L"conj(L") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"conj(L") + 1));
return conj(tmp);
}*/
if (str.find(L"ceil(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"ceil(") + 1));
return ceil(tmp);
}
if (str.find(L"Ci(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return Ci(tmp);
}
if (str.find(L"cos(") != std::wstring::npos && str.find(L"acos(") == std::wstring::npos && str.find(L"cosh(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"cos(") + 1));
return cos(tmp);
}
if (str.find(L"cosh(") != std::wstring::npos && str.find(L"acosh(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"cosh(L") + 1));
return cosh(tmp);
}
if (str.find(L"cot(") != std::wstring::npos && str.find(L"coth(") == std::wstring::npos && str.find(L"acot(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"cot(") + 1));
return cot(tmp);
}
if (str.find(L"csc(") != std::wstring::npos && str.find(L"acsc(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"csc(") + 1));
return (csc(tmp));
}
if (str.find(L"digamma(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return digamma(tmp);
}
if (str.find(L"doubleFactorial(") != std::wstring::npos && str.find(L"doubleFactorial(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return doubleFactorial(tmp);
}
if (str.find(L"Ei(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return Ei(tmp);
}
if (str.find(L"erf(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return errorFunction(tmp);
}
if (str.find(L"erfc(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"erfc(") + 1));
return errorFunctionComplement(tmp);
}
if (str.find(L"exp(") != std::wstring::npos && str.find(L"exp2(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"exp(") + 1));
return exp(tmp);
}
if (str.find(L"exp2(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"exp2(") + 1));
return exp2(tmp);
}
if (str.find(L"factorial(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"factorial(") + 1));
return factorial(tmp);
}
if (str.find(L"fallingFactorial(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return fallingFactorial(tmp[0], tmp[1]);
}
if (str.find(L"floor(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"floor(") + 1));
return floor(tmp);
}
if (str.find(L"gamma(") != std::wstring::npos &&
str.find(L"digamma(") == std::wstring::npos &&
str.find(L"polygamma(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"gamma(") + 1));
return gamma(tmp);
}
if (str.find(L"hypergeometricFunction(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return hypergeometricFunction(tmp);
}
if (str.find(L"hypot(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return sqrt(pow(tmp[0], 2) + pow(tmp[1], 2));
}
/*if (str.find(L"if(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"acos(L") + 1));
return acos(tmp);
}*/
/* if (str.find(L"imag(") != std::wstring::npos) {
ComplexNumber tmp = ParseComplexNumber(str.substr(str.find(L"imag(L") + 1));
return tmp.Im;
}*/
if (str.find(L"int(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"int(") + 1));
return (int)(tmp);
}
if (str.find(L"inverseErfc(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"inverseErfc(") + 1));
return inverseErfc(tmp);
}
if (str.find(L"LambertW(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return LambertW(tmp);
}
if (str.find(L"Li(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return Li(tmp);
}
if (str.find(L"log(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
if (tmp.size() == 1) { return log(tmp[0]); }//standard log base e
if (tmp.size() == 2) { return logarithm(tmp[0],tmp[1]); }//log w/different base
return 0;
}
if (str.find(L"log2(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"log2(") + 1));
return log2(tmp);
}
if (str.find(L"log10(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"log10(") + 1));
return log10(tmp);
}
if (str.find(L"logGamma(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return logGamma(tmp);
}
if (str.find(L"logisticFunction(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"logisticFunction(") + 1));
return logisticFunction(tmp);
}
if (str.find(L"logit(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"logit(") + 1));
return logit(tmp);
}
/*if (str.find(L"max(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"acos(L") + 1));
return acos(tmp);
}
if (str.find(L"min(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"acos(L") + 1));
return acos(tmp);
}
*/ if (str.find(L"permutation(L") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(L") + 1));
if (tmp.size() == 2) { return permutation(tmp[0], tmp[1]); }
else { return 0; }
}
/* if (str.find(L"polar(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"acos(L") + 1));
return acos(tmp);
}*/
if (str.find(L"polygamma(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return polygamma(tmp[0], tmp[1]);
}
if (str.find(L"pow(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return pow(tmp[0], tmp[1]);
}
if (str.find(L"prime(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return prime(tmp);
}
if (str.find(L"sumPrimes(") != std::wstring::npos) {
int tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return sumPrimes(tmp);
}
if (str.find(L"productPrimes(") != std::wstring::npos) {
int tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return productPrimes(tmp);
}
if (str.find(L"primeCountingFunction(") != std::wstring::npos) {
int tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return primeCountingFunction(tmp);
}
/* if (str.find(L"real(") != std::wstring::npos) {
ComplexNumber tmp = ParseComplexNumber(str.substr(str.find(L"acos(L") + 1));
return tmp.Re;
}*/
if (str.find(L"rect(") != std::wstring::npos) {
std::wstring s = str.substr(str.find(L"rect(L"));
s = s.substr(str.find(L"(L") + 1, str.find(L")") - 1);
double tmp = ParseInputNumber(s);
return rect(tmp);
}
if (str.find(L"regularModifiedBesselFunctionFirstKind(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return regularModifiedBesselFunction1stKind(tmp[0], tmp[1]);
}
if (str.find(L"risingFactorial(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return risingFactorial(tmp[0], tmp[1]);
}
if (str.find(L"sec(") != std::wstring::npos && str.find(L"asec(") == std::wstring::npos && str.find(L"sech(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"sec(") + 1));
return sec(tmp);
}
if (str.find(L"Si(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return Si(tmp);
}
if (str.find(L"sin(") != std::wstring::npos && str.find(L"asin(") == std::wstring::npos && str.find(L"sinh(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"sin(") + 1));
return sin(tmp);
}
if (str.find(L"sinc(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return sinc(tmp);
}
if (str.find(L"sinh(") != std::wstring::npos && str.find(L"asinh(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"sinh(") + 1));
return sinh(tmp);
}
if (str.find(L"sgn(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"sgn(") + 1));
return sgn(tmp);
}
if (str.find(L"sphericalBesselFunctionFirstKind(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return sphericalBesselFunction1stKind(tmp[0], tmp[1]);
}
if (str.find(L"sqrt(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"sqrt(") + 1));
return sqrt(tmp);
}
if (str.find(L"squarewave(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"squarewave(") + 1));
return squarewave(tmp);
}
if (str.find(L"step(") != std::wstring::npos) {//Heaviside step function
std::wstring s = str.substr(str.find(L"step("));
s = s.substr(str.find(L"(") + 1, str.find(L")") - 1);
double tmp = ParseInputNumber(s);
return step(tmp);
}
if (str.find(L"StirlingNumberFirstKind(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return StirlingNumber1stKind(tmp[0], tmp[1]);
}
if (str.find(L"StirlingNumberSecondKind(") != std::wstring::npos) {
std::vector<double> tmp = ParseNInputNumbers(str.substr(str.find(L"(") + 1));
return StirlingNumber2ndKind(tmp[0], tmp[1]);
}
if (str.find(L"tan(") != std::wstring::npos && str.find(L"tanh(") == std::wstring::npos && str.find(L"atan(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"tan(") + 1));
return tan(tmp);
}
if (str.find(L"tanh(") != std::wstring::npos && str.find(L"atanh(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"tanh(") + 1));
return tanh(tmp);
}
if (str.find(L"tri(") != std::wstring::npos) {
std::wstring s = str.substr(str.find(L"tri("));
s = s.substr(str.find(L"(") + 1, str.find(L")") - 1);
double tmp = ParseInputNumber(s);
return tri(tmp);
}
/*if (str.find(L"trunc(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"acos(") + 1));
return acos(tmp);
}*/
if (str.find(L"WrightOmegaFunction(") != std::wstring::npos && str.find(L"WrightOmegaFunction(") == std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"WrightOmegaFunction(") + 1));
return tanh(tmp);
}
if (str.find(L"zeta(") != std::wstring::npos) {
double tmp = ParseInputNumber(str.substr(str.find(L"(") + 1));
return RiemannZetaFunction(tmp);
}
return 0; //else, if none of the other functions match, return NULL
}
int findFunction(std::wstring in) {//test a string to see if it has a function in it
//search defined functions
for (int i = 0; i < functionCount; ++i) {
if (in.find(functionList[i]) != std::wstring::npos) {
return i;//return index of function in array of pre-defined functions
}
}
return -1; //if nothing is thrown, return -1 by default
}
bool hasFunction(std::wstring in) {//test a string to see if it has a function in it
//defined functions
for (int i = 0; i < functionCount; ++i) {
if (in.find(functionList[i]) != std::wstring::npos) {
return true;
}
}
return false; //if nothing is thrown, return false by default
}
bool hasOperator(std::wstring in) {//test a string to see if it has an operator in it (does not include parenthesis)
if (
//defined operators
in.find(L"+") != std::wstring::npos ||
in.find(L"-") != std::wstring::npos ||
in.find(L"*") != std::wstring::npos ||
in.find(L"/") != std::wstring::npos ||
in.find(L"^") != std::wstring::npos ||
in.find(L"%") != std::wstring::npos
) {
return true;
}
//defined functions
for (int i = 0; i < functionCount; ++i) {
if (in.find(functionList[i]) != std::wstring::npos) {
return true;
}
}
return false; //if nothing is thrown, return false by default
}
int findComplexNumber(std::wstring str) {//return index if ComplexNumber is found, -1 if not
for (int i = 0; i <= str.length(); ++i) {
if (i == 0 && str[i] == L'i' && isLetter(str[i + 1]) == false) { return 0; }
if (i > 0 && str[i] == L'i' && (isNumber(str[i - 1]) == true || str[i - 1] == L'+' || str[i - 1] == L'-')) {
bool halfway = false;//indicates when you are past the '+' or '-' sign joining the real and imaginary parts of the string
for (int j = i - 1; j >= 0; --j) {
if (j == 1 && isNumber(str[j]) == true && isNumber(str[j - 1]) == false && halfway == true) { return 1; }
if (j == 1 && str[0] == L'-' && halfway == true) { return 0; }
if (j == 0 && halfway == true) { return 0; }
if (halfway == true && isNumber(str[j]) == false){
if (str[j] == L'-') { return j; }
if (str[j] != L'-' && str[j] != L'.') {
return j + 1;
}
}
if ((str[j] == L'+' || str[j] == L'-') && halfway == false) { halfway = true; }
}
}
}
return std::wstring::npos;//if all else fails return null position value
}
std::wstring getNearestComplexNumber(std::wstring str) {
int i = findComplexNumber(str);
int i2 = str.substr(i).find(L"i") + 1;
if (str.substr(i).find(L"i") < 0) { return NULL; }
else {
std::wstring cn = str.substr(i, i2);
while (cn[0] != L'-' && isNumber(cn[0]) == false) { cn = cn.substr(1); }
while (cn[cn.length() - 1] != L'i') { cn.pop_back(); }
return cn;
}
return NULL;
}
std::wstring getNearestDouble(std::wstring str) {
int i = findNearestDouble(str);
if (str.size() == 1 && i == 0) {
std::wstring s1 = L" ";
s1[0] = (str[0]);
return s1;
}
int i2 = i;
for (int j = i+1; j < str.size(); ++j) {
if (isNumber(str[j]) == false && str[j] != L'.') {
i2 = j - 1;
if (i == 0 && j != 0 && j + 1 < str.size()) { ++i2; }
j = str.size() + 1;
}
if (j == str.size() - 1 && isNumber(str[j])==true) { i2 = str.size(); j += 2; }
}
if (i<0 || i2<i) { return NULL; }
else {
std::wstring cn = str.substr(i, i2-i+1);
while (cn[0] != L'-' && isNumber(cn[0]) == false) { cn = cn.substr(1); }
return cn;
}
return NULL;
}
std::wstring removeDoubles(std::wstring str) {
if (findNearestDouble(str) < 0) { return str; }
while (findNearestDouble(str) >= 0) {
std::wstring cpnum = getNearestDouble(str);
str.erase(findNearestDouble(str), cpnum.length());
}
return str;
}
std::wstring removeComplexNumbers(std::wstring str) {
if (findComplexNumber(str) < 0) { return str; }
while (findComplexNumber(str) >= 0) {
std::wstring cpnum = getNearestComplexNumber(str);
str.erase(findComplexNumber(str), cpnum.length());
}
return str;
}
int countDoubles(std::wstring str) {//returns number of ComplexNumbers present in a string
if (str == L"" || str.size() == 0) { return 0; }//handle null string
int count = 0;
while (findNearestDouble(str) >= 0) {
std::wstring cpnum = getNearestDouble(str);
size_t end = cpnum.size();
size_t start = findNearestDouble(str);
str.erase(findNearestDouble(str), cpnum.size());
++count;
}
return count;
}
int countComplexNumbers(std::wstring str) {//returns number of ComplexNumbers present in a string
if (str == L"" || str.size() == 0) { return 0; }//handle null string
int count = 0;
while (findComplexNumber(str) >= 0) {
std::wstring cpnum = getNearestComplexNumber(str);
str.erase(findComplexNumber(str), cpnum.size());
++count;
}
return count;
}
int countOperators(std::wstring str) {//returns number of operators present in a string
if (findComplexNumber(str)>0) { str = removeComplexNumbers(str); }
str = removeDoubles(str);
if (str == L"" || str.size() == 0) { return 0; }//handle null string
int count = 0;
for (int i = 0; i < str.length(); ++i) {
if (isOperatorNotParen(str[i]) == true) { ++count; }
}
return count;
}
bool hasOperatorNotComplexNumber(std::wstring in) {//test a string to see if it has an operator in it (does not include parenthesis)
if (in.size() == 0) { return false; }//handle null string input
in = removeComplexNumbers(in);
if (//find defined operators
in.find(L"*") != std::wstring::npos ||
in.find(L"/") != std::wstring::npos ||
in.find(L"^") != std::wstring::npos ||
in.find(L"%") != std::wstring::npos
) {
return true;
}
//defined functions
for (int i = 0; i < functionCount; ++i) {
if (in.find(functionList[i]) != std::wstring::npos) {
return true;
}
}
//finally, check to see if there is an operator not attached to a complexNumber
if (in.find(L"+") != std::wstring::npos) {//check '+' operator, rejecting any complex number of form '1+2i'
int i = in.find(L'+');
for (i + 1; i < in.length(); ++i) {
if (isNumber(in[i]) == false && in[i]!=L',') {
if (in[i] == L'i') {
in = in.substr(i);
i = in.find(L'+');
}
else {
return true;
}
}
}
}
if (in.find(L"-") != std::wstring::npos) {
bool halfway = false;
int i = in.find(L'-');
for (i + 1; i < in.length(); ++i) {
if (halfway == true && i == in.length() - 1) { return true; }
if (isNumber(in[i]) == false && in[i] != L'.' && in[i]!=L',') {
if (halfway == false && (in[i] == L'-' || in[i] == L'+')) { halfway = true; }
if (in[i] == L'i') {
in = in.substr(i + 1);
i = in.find(L'+');
int i2 = in.find(L'-');
if (i2 >= 0 && i2 < i) { i = i2; }
halfway = false;
}
}
}
}
return false; //if nothing is thrown, return false by default
}
int findOperator(std::wstring in) {//gives position of nearest operator (does not include parenthesis)
int a = std::wstring::npos;
if (in.find(L"+") >= 0) { a = ((int)in.find(L"+") < a) ? in.find(L"+") : a; }
if (in.find(L"-") >= 0) { a = ((int)in.find(L"-") < a) ? in.find(L"-") : a; }
if (in.find(L"*") >= 0) { a = ((int)in.find(L"*") < a) ? in.find(L"*") : a; }
if (in.find(L"/") >= 0) { a = ((int)in.find(L"/") < a) ? in.find(L"/") : a; }
if (in.find(L"^") >= 0) { a = ((int)in.find(L"^") < a) ? in.find(L"^") : a; }
if (in.find(L"%") >= 0) { a = ((int)in.find(L"%") < a) ? in.find(L"%") : a; }
return a;
}
int findNearestDouble(std::wstring str) {
int start = 0;
for (int i = 0; i < str.length(); ++i) {
if (isNumber(str[i]) == true) {
bool isNegativeNumber = false;
if (i > 0 && str[i - 1] == L'-') {
int j = i - 1;
while (j >= 0 && str[j] != L'(' && isNumber(str[j]) == false) {
if ( (j>0 && str[j - 1] == L'(') || j==0 ) { isNegativeNumber = true; j = -1; }
--j;
}
}
start = i;
if (isNegativeNumber == true) { start = i - 1; }
return start;
}
}
return std::wstring::npos;
}
std::wstring convertDoubleToComplexNumber(std::wstring str, int start, int end) {
std::wstring replacer = L"(";
replacer.append(ComplexNumber(ParseInputNumber(str.substr(start, end - start))).toStringBothParts());
replacer.append(L")");
str.erase(start, end - start);
str.insert(start, replacer);
return str;
}
std::wstring convertDoubleToComplex(std::wstring funct) {
//first, convert any double values in the string to ComplexNumbers
bool foundNum = false;
int start = 0;
int end = 0;
for (int i = 0; i < funct.length(); ++i) {
if (funct[i] == L'i') { //if it is a complex number, skip
start = end = i;
foundNum = false;
}
if (i == funct.length() - 1 && foundNum == true && isNumber(funct[i])==true) {
end = i;
funct = convertDoubleToComplexNumber(funct, start, end);
start = end = i;
foundNum = false;
}
if (isNumber(funct[i]) == false && start != end && funct[i] != L'.' && findComplexNumber(funct.substr(i - 1)) != 0) {//find end of double
if (foundNum == true) {
end = i;
funct = convertDoubleToComplexNumber(funct, start, end);
}
start = end = i;
foundNum = false;
}
if (isNumber(funct[i]) == true) {//if the char is a number, include it in the range
bool isNegativeNumber = false;
if (i > 0 && funct[i - 1] == L'-') {
int j = i-1;
while (j > 0 && funct[j] != L'(' && isNumber(funct[j]) == false) {
if (funct[j - 1] == L'(') { isNegativeNumber = true; j = -1; }
--j;
}
}
if (start == end) {
start = i;
if (isNegativeNumber == true) {
start = i - 1;
}
foundNum = true;
}
end = i + 1;
if (i == funct.length() - 1 && foundNum == true) {
funct = convertDoubleToComplexNumber(funct, start, end);
foundNum = false;
i += end - start;
}
}
}
return funct;
}
std::wstring processTwoTermOperation(std::wstring funct, std::wstring tempstr) {
double x = calculateArithmetic(tempstr);
std::wstring replc = (L"(");
replc.append(to_stringPrecision(x));
replc.append(L")");
funct = replaceString(funct, tempstr, replc);
return funct;
}
std::wstring processTwoTermOperationComplex(std::wstring funct, std::wstring tempstr) {
ComplexNumber x = calculateArithmeticComplex(tempstr);
std::wstring replc = (L"(");
replc.append(x.toStringBothParts());
replc.append(L")");
funct = replaceString(funct, tempstr, replc);
return funct;
}
bool isOperableExpression(std::wstring tempstr) {
if (countDoubles(tempstr) == 2 && countOperators(tempstr) == 1) { return true; }
return false;
}
bool isOperableExpressionComplex(std::wstring tempstr) {
if (countComplexNumbers(tempstr) == 2 && countOperators(tempstr) == 1){return true;}
return false;
}
std::wstring processInnerTerms(std::wstring str) {
if (str.find(L'(') == std::wstring::npos) { return str; }
bool hasOperableTerms = true;
while (hasOperableTerms == true) {
std::vector<std::wstring> exps = loadExpressionsIntoArray(str);
for (int i = 0; i < exps.size(); ++i) {
if (countDoubles(exps[i]) >=2 && countOperators(exps[i])>=1) {
str = processTwoTermOperation(str, exps[i]);
i = exps.size() + 1;
}
if (i == exps.size() - 1) { hasOperableTerms = false; }
}
}
return str;
}
std::wstring processInnerTermsComplex(std::wstring str) {
bool hasOperableTerms = true;
while (hasOperableTerms == true) {
std::vector<std::wstring> exps = loadExpressionsIntoArray(str);
for (int i = 0; i < exps.size(); ++i) {
if (countComplexNumbers(exps[i]) >= 2 && countOperators(exps[i]) >= 1) {
str = processTwoTermOperationComplex(str, exps[i]);
i = exps.size() + 1;
}
if (i == exps.size() - 1) { hasOperableTerms = false; }
}
}
return str;
}
double Function::evaluate(std::vector<double> vals) {
/* NOTE:
Suitable arithmetic operators are +,-,*,/,^. Suitable functions are found in evalFunction method.
This function will properly evaluate a function of the form f(x,y) = x^2 without error. */
/*=================
|DECLARE VARIABLES|
=================*/
//make sure when using to_string precision or other functions which truncate values, we do as little truncation as possible
int prcs = precision;
precision = 30;
std::wstring funct = function;//local copy of function string, necessary as we will erase/replace parts of the string as we go
funct = removeSpaces(funct);//remove all spaces in function string
funct = replaceConstants(funct);//replace all system constants in string
//========================================
/*================================
|REPLACE VARIABLES WITH CONSTANTS|
================================*/
//replace all variables with their double value from double* vals
if (vals.size() > 0) {
for (int i = 0; i < variables.size(); ++i) {
std::wstring val = to_stringPrecision(vals[i]);//double values for variable made into a string
//check string for spots to replace char, then perform replace function
for (int j = 0; j < funct.size(); ++j) {
if (j == 0 && funct[j] == variables[i] && isLetter(funct[j + 1]) == false) {
//std::wcout << L"replacing char #" << j << L"...\n";//debug
funct.erase(j, 1);
funct.insert(j, val);
j = 0;
}
if (j>0 && j<funct.size() - 1 && funct[j] == variables[i] && isLetter(funct[j + 1]) == false && isLetter(funct[j - 1]) == false) {
//std::wcout << L"replacing char #" << j << L"...\n";//debug
funct.erase(j, 1);
funct.insert(j, val);
}
if (j == funct.size() - 1 && funct[j] == variables[i] && isLetter(funct[j - 1]) == false) {
//std::wcout << L"replacing char #" << j << L"...\n";//debug
funct.erase(j, 1);
funct.insert(j, val);
}
}
}
}//================================================================
//remove any double '-' signs that might have resulted accidentally
for (int i = 0; i < funct.size() - 1; ++i) {
if (funct[i] == L'-' && funct[i + 1] == L'-') {
funct[i] = L' ';
funct[i + 1] = L'+';
++i;
}
}
funct = removeSpaces(funct);
/*================
|HANDLE FUNCTIONS|
================*/
bool searchForFuncts = hasFunction(funct);
while (searchForFuncts == true) {
bool foundOne = false;
for (int i = 0; i < funct.size() - 1; ++i) {
topOfTheFunctsLoop:
if (isLetter(funct[i]) == true && isLetter(funct[i + 1]) == true) { //find functions, which have more than one letter in a row
std::wstring tempstr = funct.substr(i);
tempstr = tempstr.substr(0, findMatchingParen(tempstr, tempstr.find(L"("))+1); //save function (i.e. 'exp(12.2)') as a substr to evaluate
if (hasFunction(tempstr.substr(tempstr.find(L"("))) == true) {//keep searching for the innermost paren without a function inside
i += tempstr.find(L"(")-1;//skip past the function (i.e. i+=4 if outer function is 'cos(' for example)
goto topOfTheFunctsLoop;
}
std::wstring toReplace = tempstr;
//make sure there's nothing within the parenthesis that needs to be simplified (i.e. 'sin(12-2)')
int posit = tempstr.find(L'(');
if (hasOperator(tempstr.substr(posit+1, findMatchingParen(tempstr,posit)+ 1)) == true && countDoubles(tempstr)>1) {
int opers = 0;
for (int k = 0; k < tempstr.length() - 2; ++k) {
if (isOperator(tempstr[k]) == true) {
if (tempstr[k] == '-') {
if (k > 0) {
if (isNumber(tempstr[k - 1]) == true && isNumber(tempstr[k + 1]) == true) {
++opers;
}
}
}
else { ++opers; }
}
}
if (opers>0) {
size_t a = tempstr.find(L"(") + 1;
size_t b = tempstr.find(L")") - 1;
std::wstring chk = tempstr.substr(a, b);
chk = replaceChar(chk, L')', L' ');
chk = replaceChar(chk, L'(', L' ');
chk = removeSpaces(chk);
Function f2(chk);
double p = f2.evaluate(0.0);
std::wstring replc = to_stringPrecision(p);
tempstr = replaceString(tempstr, chk, replc);
}
}
//now evaluate the function, get the double value, and replace the whole thing in the original string (i.e. 'sin(0)' --> '0')