/
jkmptools.h
1790 lines (1569 loc) · 76.5 KB
/
jkmptools.h
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
/*
Copyright (c) 2008-2016 Jan W. Krieger (<jan@jkrieger.de>)
This software is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License (LGPL) as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License (LGPL) for more details.
You should have received a copy of the GNU Lesser General Public License (LGPL)
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef JKMATHPARSERTOOLS_H
#define JKMATHPARSERTOOLS_H
#include "jkmplib_imexport.h"
//#include "qftools.h"
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>
#include <stack>
#include "extlibs/MersenneTwister.h"
#include <stdint.h>
#include "jkmpbase.h"
#include "jkmpstringtools.h"
class JKMathParser; // forward
/**
* \defgroup jkmpultil utilities for JKMathParser function parser class
* \ingroup jkmplib_mathtools_parser
*/
/*@{*/
#define JKMATHPARSER_VERSION "0.5"
/** possible result types
* - \c jkmpDouble \c =0b00000001=0x01: a floating-point number with double precision. This is
* also used to deal with integers
* - \c jkmpString \c =0b00000010=0x02: a string of characters
* - \c jkmpBool \c =0b00000100=0x04: a boolean value true|false
* - \c jkmpDoubleVector \c =0b00001000=0x08: a double-vector
* - \c jkmpStringVector \c =0b00010000=0x10: a string-vector
* - \c jkmpBoolVector \c =0b00100000=0x20: a boolean-vector
* - \c jkmpVoid \c =0b01000000=0x40: a VOID value (i.e. no value)
* - \c jkmpStruct \c =0b10000000=0x80: a struct
* - \c jkmpList \c =0b100000000=0x100: a list of values
* - \c jkmpDoubleMatrix \c =0b1000000000=0x200: a 2D matrix of numbers
* - \c jkmpBoolMatrix \c =0b10000000000=0x400: a 2D matrix of booleans
* .
*
*
*/
enum jkmpResultType {jkmpDouble=0x01, /*!< \brief a floating-point number with double precision. This is also used to deal with integers */
jkmpString=0x02, /*!< \brief a string of characters */
jkmpBool=0x04, /*!< \brief a boolean value true|false */
jkmpDoubleVector=0x08, /*!< \brief a vector of floating point numbers */
jkmpStringVector=0x10, /*!< \brief a vector of strings */
jkmpBoolVector=0x20, /*!< \brief a vector of booleans */
jkmpVoid=0x40, /*!< \brief a void/non-evaluatable result */
jkmpStruct=0x80, /*!< \brief a struct datatype, i.e. a map string->jkmpResult */
jkmpList=0x100, /*!< \brief a list of jkmpResult datatype */
jkmpDoubleMatrix=0x200, /*!< \brief a 2D matrix of floating point numbers */
jkmpBoolMatrix=0x400, /*!< \brief a 2D matrix of booleans */
};
JKMPLIB_EXPORT JKMP::string jkmpResultTypeToString(jkmpResultType type);
struct jkmpResult; // forward
/** \brief result of any expression */
struct JKMPLIB_EXPORT jkmpResult {
public:
jkmpResult();
~jkmpResult();
jkmpResult(double value);
jkmpResult(int value);
jkmpResult(unsigned int value);
jkmpResult(JKMP::string value);
jkmpResult(bool value);
jkmpResult(const std::vector<double> &value);
jkmpResult(const std::vector<bool> &value);
jkmpResult(const JKMP::stringVector &value);
jkmpResult(const jkmpResult &value);
JKMPLIB_EXPORT jkmpResult& operator=(const jkmpResult &value);
JKMPLIB_EXPORT void set(const jkmpResult &value);
JKMPLIB_EXPORT void setInvalid();
JKMPLIB_EXPORT void setVoid();
/** \brief convert the value this struct representens into a JKMP::string */
JKMPLIB_EXPORT JKMP::string toString(int precision=10) const;
/** \brief convert the value this struct representens into a JKMP::string and adds the name of the datatype in \c [...] */
JKMPLIB_EXPORT JKMP::string toTypeString(int precision=10) const;
/** \brief convert the value this struct to an integer */
JKMPLIB_EXPORT int32_t toInteger() const;
/** \brief convert the value this struct to an integer */
JKMPLIB_EXPORT uint32_t toUInt() const;
JKMPLIB_EXPORT std::vector<uint32_t> toUIntVector() const;
JKMPLIB_EXPORT std::vector<int32_t> toIntVector() const;
/** \brief is this result convertible to integer? */
inline bool isInteger() const {
return (type==jkmpDouble)&&(fabs(num)<4294967296.0)&&(num==round(num));
}
/** \brief is this result convertible to unsigned integer? */
inline bool isUInt() const {
return (type==jkmpDouble)&&(fabs(num)<2147483648.0)&&(num>=0)&&(num==round(num));
}
/** \brief returns the size of the result (number of characters for string, numbers of entries in vectors, 0 for void and 1 else), same as size() */
JKMPLIB_EXPORT size_t length() const;
/** \brief returns the size of the result (number of characters for string, numbers of entries in vectors, 0 for void and 1 else), same as length() */
JKMPLIB_EXPORT size_t size() const;
/** \brief returns \c true, if the datatype is a matrix */
JKMPLIB_EXPORT int dimensions() const;
/** \brief for a mtrix, returns the x-size/number of columns */
JKMPLIB_EXPORT size_t sizeX() const;
/** \brief for a mtrix, returns the y-size/number of rows */
JKMPLIB_EXPORT size_t sizeY() const;
/** \brief for a mtrix, returns the x-size/number of columns */
inline size_t columns() const {
return sizeX();
}
/** \brief for a mtrix, returns the y-size/number of rows */
inline size_t rows() const {
return sizeY();
}
/** \brief returns the number of dimensions in the datatype */
JKMPLIB_EXPORT bool isMatrix() const;
/** \brief is this a double value */
inline bool isDouble() const {
return type==jkmpDouble;
}
inline bool isDoubleVector() const {
return type==jkmpDoubleVector;
}
inline bool isBool() const {
return type==jkmpBool;
}
inline bool isBoolVector() const {
return type==jkmpBoolVector;
}
JKMPLIB_EXPORT bool isUIntVector() const;
JKMPLIB_EXPORT bool isIntVector() const;
/** \brief is this a double value */
inline bool convertsToDouble() const {
return (type==jkmpDouble || type==jkmpBool);
}
inline bool convertsToBool() const {
return (type==jkmpDouble || type==jkmpBool);
}
/** \brief is this a double value */
inline bool convertsToInteger() const {
return (type==jkmpDouble || type==jkmpBool);
}
inline bool convertsToUInt() const {
return ((type==jkmpDouble && num>=0) || type==jkmpBool);
}
JKMPLIB_EXPORT void setDouble(double val);
JKMPLIB_EXPORT void setBoolean(bool val);
JKMPLIB_EXPORT void setString(const JKMP::stringType& val);
JKMPLIB_EXPORT void setString(int size=0, JKMP::charType defaultChar=JKMP::charType(' '));
JKMPLIB_EXPORT void setDoubleVec(const std::vector<double>& val);
JKMPLIB_EXPORT void setDoubleVec(int size=0, double defaultVal=0.0);
JKMPLIB_EXPORT void setDoubleMatrix(int size=0, int cols=1, double defaultVal=0);
template <typename T>
inline void setDoubleVec(T* data, size_t size) {
setDoubleVec(size,0.0);
for (size_t i=0; i<size; i++) {
numVec[i]=double(data[i]);
}
}
JKMPLIB_EXPORT void setStruct(const JKMP::stringVector& items=JKMP::stringVector());
JKMPLIB_EXPORT void setList(int items=0);
JKMPLIB_EXPORT void setList(const std::vector<jkmpResult>& dat);
JKMPLIB_EXPORT void setBoolVec(const std::vector<bool>& val);
JKMPLIB_EXPORT void setBoolVec(int size=0, bool defaultVal=false);
JKMPLIB_EXPORT void setBoolMatrix(int size=0, int cols=1, bool defaultVal=false);
template <typename T>
inline void setBoolVec(T* data, size_t size) {
setBoolVec(size);
for (size_t i=0; i<size; i++) {
boolVec[i]=bool(data[i]);
}
}
JKMPLIB_EXPORT void setStringVec(const JKMP::stringVector& val);
JKMPLIB_EXPORT void setStringVec(int size=0, const JKMP::string& defaultVal=JKMP::string(""));
template <typename T>
inline void setStringVec(T* data, size_t size) {
setStringVec(size);
for (size_t i=0; i<size; i++) {
strVec[i]=data[i];
}
}
/** \brief converst the result to a vector of number (numbers and number vectors are converted!) */
JKMPLIB_EXPORT std::vector<double> asVector() const;
/** \brief converst the result to a vector of number (numbers and number vectors are converted!) */
JKMPLIB_EXPORT JKMP::stringVector asStrVector() const;
/** \brief converst the result to a vector of number (numbers and number vectors are converted!) */
JKMPLIB_EXPORT std::vector<bool> asBoolVector() const;
/** \brief returns \c true, if the result may be converted to a vector of number */
JKMPLIB_EXPORT bool convertsToVector() const;
/** \brief returns \c true, if the result may be converted to a vector of number */
JKMPLIB_EXPORT bool convertsToBoolVector() const;
/** \brief returns \c true, if the result may be converted to a vector of number */
JKMPLIB_EXPORT bool convertsToStringVector() const;
/** \brief converst the result to a vector of integers (numbers and number vectors are converted!) */
JKMPLIB_EXPORT std::vector<int> asIntVector() const;
/** \brief returns \c true, if the result may be converted to a vector of integers */
inline bool convertsToIntVector() const {
return convertsToVector();
}
/** \brief returns \c true if the result is valid and not void */
inline bool isUsableResult() const {
return isValid && (type!=jkmpVoid);
}
/** \brief converst the result to a number (numbers are converted!) */
inline double asNumber() const {
if (type==jkmpDouble) return num;
if (type==jkmpBool) return boolean?1:0;
return NAN;
}
/** \brief converst the result to a number (numbers are converted and from a number vector the first element is returned!) */
inline double asNumberAlsoVector() const {
if (type==jkmpDouble) return num;
if (type==jkmpDoubleVector && numVec.size()>0) return numVec[0];
return NAN;
}
/** \brief converst the result to a number (numbers are converted and from a number vector the first element is returned!) */
inline JKMP::string asStringAlsoVector() const {
if (type==jkmpString) return str;
if (type==jkmpStringVector && strVec.size()>0) return strVec[0];
return JKMP::string();
}
/** \brief converst the result to a number (numbers are converted and from a number vector the first element is returned!) */
inline bool asBooleanAlsoVector() const {
if (type==jkmpBool) return boolean;
if (type==jkmpBoolVector && boolVec.size()>0) return boolVec[0];
return false;
}
/** \brief converst the result to a string (strings are converted!) */
inline JKMP::string asString() const {
if (type==jkmpString) return str;
return JKMP::string();
}
/** \brief converst the result to a boolean (numbers and booleans are converted!) */
inline bool asBool() const {
if (type==jkmpBool) return boolean;
if (type==jkmpDouble) return num!=0;
return false;
}
/** \brief returns the type */
inline jkmpResultType getType() const {
return type;
}
/** \brief returns a string, describing the type of this result */
JKMPLIB_EXPORT JKMP::string typeName() const;
/** \brief returns an invalid result */
JKMPLIB_EXPORT static jkmpResult invalidResult();
/** \brief returns an void result */
JKMPLIB_EXPORT static jkmpResult voidResult();
/** \brief returns an entry from a struct */
JKMPLIB_EXPORT jkmpResult getStructItem(const JKMP::string& item);
/** \brief returns an entry from a struct */
JKMPLIB_EXPORT void setStructItem(const JKMP::string& item, const jkmpResult& value);
/** \brief returns an entry from a struct */
JKMPLIB_EXPORT void setStruct(const JKMP::map<JKMP::string,jkmpResult>& data);
/** \brief returns an entry from a jkmptList */
JKMPLIB_EXPORT jkmpResult getListItem(int item) const;
/** \brief returns an entry from a jkmptList */
JKMPLIB_EXPORT jkmpResult getListItem(int item, const jkmpResult& defaultResult) const;
/** \brief remove an entry from a jkmptList */
JKMPLIB_EXPORT void removeListItem(int item);
/** \brief append an entry to a jkmptList */
JKMPLIB_EXPORT void appendListItem(const jkmpResult& item);
/** \brief insert an entry to a jkmptList */
JKMPLIB_EXPORT void insertListItem(int i, const jkmpResult& item);
JKMPLIB_EXPORT static void add(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void sub(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void mul(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void div(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void mod(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void power(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void bitwiseand(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void bitwiseor(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void logicand(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void logicor(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void logicnot(jkmpResult& result, const jkmpResult& l, JKMathParser *p);
JKMPLIB_EXPORT static void logicnand(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void logicnor(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void logicxor(jkmpResult& result, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void neg(jkmpResult& result, const jkmpResult& l, JKMathParser* p);
JKMPLIB_EXPORT static void bitwisenot(jkmpResult& result, const jkmpResult& l, JKMathParser* p);
JKMPLIB_EXPORT bool operator==(const jkmpResult& other) const;
JKMPLIB_EXPORT bool operator!=(const jkmpResult& other) const;
JKMPLIB_EXPORT static void compareequal(jkmpResult& res, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void comparenotequal(jkmpResult& res, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void comparegreater(jkmpResult& res, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void comparegreaterequal(jkmpResult& res, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void comparesmaller(jkmpResult& res, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
JKMPLIB_EXPORT static void comparesmallerequal(jkmpResult& res, const jkmpResult& l, const jkmpResult& r, JKMathParser* p);
bool isValid;
jkmpResultType type; /*!< \brief type of the result */
JKMP::string str; /*!< \brief contains result if \c type==jkmpString */
double num; /*!< \brief contains result if \c type==jkmpDouble */
bool boolean; /*!< \brief contains result if \c type==jkmpBool */
JKMP::vector<double> numVec; /*!< \brief contains result if \c type==jkmpDoubleVector */
JKMP::stringVector strVec;
JKMP::vector<bool> boolVec;
JKMP::map<JKMP::string,jkmpResult> structData;
JKMP::vector<jkmpResult> listData;
int matrix_columns;
private:
};
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is numeric->numeric, e.g. sin(x). The result is numeric->numeric or num_vector->num_vector
The resulting function will:
- check the number of arguments
- apply the C-function to any number parameter
- apply the C-function item-wise to any number vector parameter
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_NUMERIC_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=1) {\
p->jkmpError(JKMP::_("%1(...) needs exacptly 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return ;\
}\
if (params[0].type==jkmpDouble) {\
r.setDouble(CFUNC(params[0].num));\
} else if(params[0].type==jkmpDoubleVector) {\
r.setDoubleVec(params[0].numVec.size());\
for (size_t i=0; i<params[0].numVec.size(); i++) {\
r.numVec[i]=CFUNC(params[0].numVec[i]);\
}\
} else {\
p->jkmpError(JKMP::_("%1(...) argument has to be a number or vector of numbers").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
}
/** \brief same as JKMATHPARSER_DEFINE_1PARAM_NUMERIC_FUNC(FName, NAME_IN_PARSER, CFUNC), but NAME_IN_PARSER==CFUNC */
#define JKMATHPARSER_DEFINE_1PARAM_NUMERIC_FUNC_SIMPLE(FName, CFUNC) JKMATHPARSER_DEFINE_1PARAM_NUMERIC_FUNC(FName, CFUNC, CFUNC)
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is numeric_vector->numeric_vector (std::vector<double> -> std::vector<double>)
The resulting function will:
- check the number of arguments and their type
- apply the C-function to any number parameter
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_NUMERICVEC_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=1) {\
p->jkmpError(JKMP::_("%1(x) needs exacptly 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector) {\
r.setDoubleVec(CFUNC(params[0].numVec));\
} else {\
p->jkmpError(JKMP::_("%1(x) argument has to be vector of numbers").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C template-function that
is vector->vector it has to accept JKMP::vector and JKMP::vector as arguments! The result of the parser
function will have the same type as the input!
The resulting function will:
- check the number of arguments and their type
- apply the C-function to any number parameter
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_VECTOR_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=1) {\
p->jkmpError(JKMP::_("%1(x) needs exacptly 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector) {\
r.setDoubleVec(CFUNC(params[0].numVec));\
} else if(params[0].type==jkmpStringVector) {\
r.setStringVec(CFUNC(params[0].strVec));\
} else if(params[0].type==jkmpBoolVector) {\
r.setBoolVec(CFUNC(params[0].boolVec));\
} else {\
p->jkmpError(JKMP::_("%1(x) argument has to be a vector of numbers/booleans/strings").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C template-function that
is vector->vector or string->string it has to accept JKMP::vector and JKMP::vector as arguments! The result of the parser
function will have the same type as the input!
The resulting function will:
- check the number of arguments and their type
- apply the C-function to any number parameter
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_VECTORORSTRING_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=1) {\
p->jkmpError(JKMP::_("%1(x) needs exacptly 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector) {\
r.setDoubleVec(CFUNC(params[0].numVec));\
} else if(params[0].type==jkmpStringVector) {\
r.setStringVec(CFUNC(params[0].strVec));\
} else if(params[0].type==jkmpBoolVector) {\
r.setBoolVec(CFUNC(params[0].boolVec));\
} else if(params[0].type==jkmpString) {\
r.setString(CFUNC(params[0].str));\
} else {\
p->jkmpError(JKMP::_("%1(x) argument has to be a vector of numbers/booleans/strings, or a string").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is numeric_vector->numeric (std::vector<double> -> double), e.g. qfstatisticsMedian()
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_VECTONUM_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=1) {\
p->jkmpError(JKMP::_("%1(x) needs exacptly 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector) {\
r.setDouble(CFUNC(params[0].numVec));\
} else {\
p->jkmpError(JKMP::_("%1(x) argument has to be a vector of numbers").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (double*,size_t)->numeric (std::vector<double> -> double), e.g. qfstatisticsMedian()
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_VECTONUM_CFUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=1) {\
p->jkmpError(JKMP::_("%1(x) needs exacptly 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector) {\
r.setDouble(CFUNC(params[0].numVec.data(), params[0].numVec.size()));\
} else {\
p->jkmpError(JKMP::_("%1(x) argument has to be a vector of numbers").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is numeric_vector->numeric (std::vector<double> -> double), e.g. qfstatisticsMedian()
This variant also accepts a single double-number and converts it to a std::vector<double> before evaluating the function.
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_VECORNUMTONUM_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=1) {\
p->jkmpError(JKMP::_("%1(x) needs exacptly 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector) {\
r.setDouble(CFUNC(params[0].numVec));\
} if(params[0].type==jkmpDouble) {\
r.setDouble(CFUNC(JKMP::vector<double>::construct(params[0].num)));\
} else {\
p->jkmpError(JKMP::_("%1(x) argument has to be a vector of numbers or a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (double*,size_t)->numeric (std::vector<double> -> double), e.g. qfstatisticsMedian()
This variant also accepts a single double-number and converts it to a std::vector<double> before evaluating the function.
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_VECORNUMTONUM_CFUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=1) {\
p->jkmpError(JKMP::_("%1(x) needs exacptly 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector) {\
r.setDouble(CFUNC(params[0].numVec.data(), params[0].numVec.size()));\
} if(params[0].type==jkmpDouble) {\
JKMP::vector<double> d(1, params[0].num); \
r.setDouble(CFUNC(d.data(), d.size()));\
} else {\
p->jkmpError(JKMP::_("%1(x) argument has to be a vector of numbers or a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is numeric_vector->numeric (std::vector<double> -> double), e.g. qfstatisticsMedian()
This variant also accepts a single double-number, or a list of numbers and converts it to a std::vector<double> before evaluating the function.
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_VECORNUMSTONUM_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n<1) {\
p->jkmpError(JKMP::_("%1(x1, x2, ...) needs at least 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(n==1 && params[0].type==jkmpDoubleVector) {\
r.setDouble(CFUNC(params[0].numVec));\
} if( n>=1) {\
JKMP::vector<double> d; \
for (unsigned int i=0; i<n; i++) {\
if (params[i].type==jkmpDouble) { \
d<<params[i].num; \
} else if (params[i].type==jkmpDoubleVector) {\
d<<params[i].numVec; \
} else {\
p->jkmpError(JKMP::_("%1(x1, x2, ...) argument %2 has to be a vector of numbers or a number").arg(#NAME_IN_PARSER).arg(i+1));\
r.setInvalid();\
return; \
}\
} \
r.setDouble(CFUNC(d));\
} else {\
p->jkmpError(JKMP::_("%1(x1, x2, ...) argument 1 has to be a vector of numbers or a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (double*,size_t)->numeric (std::vector<double> -> double), e.g. qfstatisticsMedian()
This variant also accepts a single double-number, or a list of numbers and converts it to a std::vector<double> before evaluating the function.
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_1PARAM_VECORNUMSTONUM_CFUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n<1) {\
p->jkmpError(JKMP::_("%1(x1, x2, ...) needs at least 1 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(n==1 && params[0].type==jkmpDoubleVector) {\
r.setDouble(CFUNC(params[0].numVec.data(), params[0].numVec.size()));\
} if( n>=1) {\
JKMP::vector<double> d; \
for (unsigned int i=0; i<n; i++) {\
if (params[i].type==jkmpDouble) { \
d<<params[i].num; \
} else if (params[i].type==jkmpDoubleVector) {\
d<<params[i].numVec; \
} else {\
p->jkmpError(JKMP::_("%1(x1, x2, ...) argument %2 has to be a vector of numbers or a number").arg(#NAME_IN_PARSER).arg(i+1));\
r.setInvalid();\
return; \
}\
} \
r.setDouble(CFUNC(d.data(), d.size()));\
} else {\
p->jkmpError(JKMP::_("%1(x1, x2, ...) argument 1 has to be a vector of numbers or a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (numeric_vector, numeric)->number , e.g. qfstatisticsQuantile()
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_2PARAM1VEC_VECTONUM_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=2) {\
p->jkmpError(JKMP::_("%1(x, p) needs exacptly 2 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDouble) {\
r.setDouble(CFUNC(params[0].numVec, params[1].num));\
} else {\
p->jkmpError(JKMP::_("%1(x, p) argument x has to be a vector of numbers and p a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (double*,size_t, numeric)->number , e.g. qfstatisticsQuantile()
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_2PARAM1VEC_VECTONUM_CFUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=2) {\
p->jkmpError(JKMP::_("%1(x, p) needs exacptly 2 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDouble) {\
r.setDouble(CFUNC(params[0].numVec.data(), params[0].numVec.size(), params[1].num));\
} else {\
p->jkmpError(JKMP::_("%1(x, p) argument x has to be a vector of numbers and p a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (numeric_vector, numeric)->number. If the second argument is also a vector, the function
will be applied element-wise to this vector to build an output vector!
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_2PARAM1VEC2POSVEC_VECTONUM_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=2) {\
p->jkmpError(JKMP::_("%1(x, p) needs exacptly 2 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDouble) {\
r.setDouble(CFUNC(params[0].numVec, params[1].num));\
} else if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDoubleVector) {\
JKMP::vector<double> res=params[1].numVec;\
for (size_t i=0; i<params[1].numvec.size(); i++) {\
res[i]=CFUNC(params[0].numVec, res[i]);\
}\
r.setDoubleVec(res);\
} else {\
p->jkmpError(JKMP::_("%1(x, p) argument x has to be a vector of numbers and p a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (double*,size_t, numeric)->number. If the second argument is also a vector, the function
will be applied element-wise to this vector to build an output vector!
The C-function expects (double* data, int length) and not a std::vector<double> as parameter!)
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_2PARAM1VEC2POSVEC_VECTONUM_CFUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=2) {\
p->jkmpError(JKMP::_("%1(x, p) needs exacptly 2 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDouble) {\
r.setDouble(CFUNC(params[0].numVec.size(), params[0].numVec.size(), params[1].num));\
} else if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDoubleVector) {\
JKMP::vector<double> res=params[1].numVec;\
for (size_t i=0; i<params[1].numvec.size(); i++) {\
res[i]=CFUNC(params[0].numVec.data(), params[0].numVec.size(), res[i]);\
}\
r.setDoubleVec(res);\
} else {\
p->jkmpError(JKMP::_("%1(x, p) argument x has to be a vector of numbers and p a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (numeric_vector, numeric)->number. If the second argument is also a vector, the function
will be applied element-wise to this vector to build an output vector!
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_2PARAM1NUM2VEC_VECTONUM_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=2) {\
p->jkmpError(JKMP::_("%1(x, p) needs exacptly 2 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDouble && params[1].type==jkmpDoubleVector) {\
r.setDouble(CFUNC(params[0].num, params[1].numVec));\
} else if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDoubleVector) {\
JKMP::vector<double> res=params[0].numVec;\
for (size_t i=0; i<res.size(); i++) {\
res[i]=CFUNC(res[i], params[1].numVec);\
}\
r.setDoubleVec(res);\
} else {\
p->jkmpError(JKMP::_("%1(x, p) argument x has to be a number or a vector of numbers and p a vector of number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (numeric, double*,size_t)->number. If the second argument is also a vector, the function
will be applied element-wise to this vector to build an output vector!
The C-function expects (double, double* data, int length) and not a std::vector<double> as parameter!)
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_2PARAM1NUM2VEC_VECTONUM_CFUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=2) {\
p->jkmpError(JKMP::_("%1(x, p) needs exacptly 2 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDouble && params[1].type==jkmpDoubleVector) {\
r.setDouble(CFUNC(params[0].num, params[1].numVec.data(), params[1].numVec.size()));\
} else if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDoubleVector) {\
JKMP::vector<double> res=params[0].numVec;\
for (size_t i=0; i<res.size(); i++) {\
res[i]=CFUNC(res[i], params[1].numVec.data(), params[1].numVec.size());\
}\
r.setDoubleVec(res);\
} else {\
p->jkmpError(JKMP::_("%1(x, p) argument x has to be a number or a vector of numbers and p a vector of number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (numeric_vector, numeric, numeric)->number
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_3PARAM1VEC_VECTONUM_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=3) {\
p->jkmpError(JKMP::_("%1(x, p1, p2) needs exacptly 3 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDouble && params[2].type==jkmpDouble) {\
r.setDouble(CFUNC(params[0].numVec, params[1].num, params[2].num));\
} else {\
p->jkmpError(JKMP::_("%1(x, p) argument x has to be a vector of numbers and p a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (double*,size_t, numeric, numeric)->number
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_3PARAM1VEC_VECTONUM_CFUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=3) {\
p->jkmpError(JKMP::_("%1(x, p1, p2) needs exacptly 3 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDouble && params[2].type==jkmpDouble) {\
r.setDouble(CFUNC(params[0].numVec.data(), params[0].numVec.size(), params[1].num, params[2].num));\
} else {\
p->jkmpError(JKMP::_("%1(x, p) argument x has to be a vector of numbers and p a number").arg(#NAME_IN_PARSER));\
r.setInvalid();\
}\
return; \
}
/*! \brief This macro allows to easily define functions for JKMathParser from a C-function that
is (numeric_vector, numeric_vector)->numeric, e.g. qfstatisticsCorrelationCoefficient()
The resulting function will:
- check the number of arguments and their type
- apply the C-function to the argument
.
\param FName name of the function to declare
\param NAME_IN_PARSER name the function should have in the parser (used for error messages only)
\param CFUNC name of the C function to call
*/
#define JKMATHPARSER_DEFINE_2PARAM2VEC_VECTONUM_FUNC(FName, NAME_IN_PARSER, CFUNC) \
static inline void FName(jkmpResult& r, const jkmpResult* params, unsigned int n, JKMathParser* p){\
if (n!=2) {\
p->jkmpError(JKMP::_("%1(x, y) needs exactly 2 argument").arg(#NAME_IN_PARSER));\
r.setInvalid();\
return; \
}\
if(params[0].type==jkmpDoubleVector && params[1].type==jkmpDoubleVector) {\