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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
|
/***********************************************/
/**
* @file expressionParser.cpp
*
* @brief mathemtical expression parser
*
* @author Torsten Mayer-Guerr
* @author Sebastian Strasser
* @author Andreas Kvas
* @date 2009-05-17
*
*/
/***********************************************/
#include "base/importStd.h"
#include "base/constants.h"
#include "base/time.h"
#include "parser/stringParser.h"
#include "parser/expressionParser.h"
/***** CLASS ***********************************/
/** @brief Mathematcial Expression
* @ingroup parserGroup
* Represented as a tree.
* Can be created by the standard mathemtical operators.
* Example:
* @code ExpressionPtr expr = exprValue(5)+(sin(exprVar(x))^2); @endcode
* expr->string() results in "5+sin(x)^2" */
class Expression
{
public:
enum Priority : UInt {NONE, LOGICAL_OR, LOGICAL_AND, EQUALITY, RELATION, ADDITIVE, MULTIPLICATIVE, UNARY, EXPONENTIAL, FUNCTION, VALUE};
/// Destructor
virtual ~Expression() {}
Expression() = default;
Expression(const Expression &x) = delete;
Expression &operator=(const Expression &x) = delete;
/** @brief Parse an expression given as string.
* Example: "3+5*sin(1.0)+x^2".
* @return expression represented as a tree. */
static ExpressionPtr parse(const std::string &text);
/** @brief Simplifies expression.
* @param varList values of the variables contained in the expression.
* @param[out] resolved expression can be evaluated directly (is a constant value).
* @return Simplified expression. */
virtual ExpressionPtr simplify(VariableList &varList, Bool &resolved) const = 0;
/** @brief Calculate thep result of an expression.
* Example: "5*x" results in 10 if variable x=5 is given.
* @param varList values of the variables contained in the expression. */
virtual Double evaluate(const VariableList &varList) const = 0;
/** @brief Derivative of an expression.
* @param var derivative with respect to this variable. */
virtual ExpressionPtr derivative(const std::string &var) const = 0;
/** @brief Deep copy of an expression. */
virtual ExpressionPtr clone() const = 0;
/** @brief String representation of an expression. */
virtual std::string string() const = 0;
/// Internal.
std::string string(UInt aprio) const;
/// Internal.
virtual UInt priority() const = 0;
};
/***********************************************/
ExpressionPtr sqrt(const ExpressionPtr &ob);
ExpressionPtr exp (const ExpressionPtr &ob);
ExpressionPtr log (const ExpressionPtr &ob);
ExpressionPtr sin (const ExpressionPtr &ob);
ExpressionPtr cos (const ExpressionPtr &ob);
ExpressionPtr tan (const ExpressionPtr &ob);
ExpressionPtr asin(const ExpressionPtr &ob);
ExpressionPtr acos(const ExpressionPtr &ob);
ExpressionPtr atan(const ExpressionPtr &ob);
ExpressionPtr operator+(const ExpressionPtr &l, const ExpressionPtr &r);
ExpressionPtr operator-(const ExpressionPtr &l, const ExpressionPtr &r);
ExpressionPtr operator*(const ExpressionPtr &l, const ExpressionPtr &r);
ExpressionPtr operator/(const ExpressionPtr &l, const ExpressionPtr &r);
ExpressionPtr operator^(const ExpressionPtr &l, const ExpressionPtr &r);
ExpressionPtr operator-(const ExpressionPtr &ob);
/***********************************************/
// Generate named list of constants and functions with one, two or three parameters
class ExpressionFunction0;
class ExpressionFunction1;
class ExpressionFunction2;
class ExpressionFunction3;
class FunctionList
{
public:
static std::vector<std::shared_ptr<ExpressionFunction0>> func0List;
static std::vector<std::shared_ptr<ExpressionFunction1>> func1List;
static std::vector<std::shared_ptr<ExpressionFunction2>> func2List;
static std::vector<std::shared_ptr<ExpressionFunction3>> func3List;
};
std::vector<std::shared_ptr<ExpressionFunction0>> FunctionList::func0List;
std::vector<std::shared_ptr<ExpressionFunction1>> FunctionList::func1List;
std::vector<std::shared_ptr<ExpressionFunction2>> FunctionList::func2List;
std::vector<std::shared_ptr<ExpressionFunction3>> FunctionList::func3List;
#define FUNCLIST0(_name) class FuncList##_name : public FunctionList\
{public: FuncList##_name() {func0List.push_back(std::make_shared<_name>());}};\
static FuncList##_name funcList##_name;
#define FUNCLIST1(_name) class FuncList##_name : public FunctionList\
{public: FuncList##_name() {func1List.push_back(std::make_shared<_name>(ExpressionPtr()));}};\
static FuncList##_name funcList##_name;
#define FUNCLIST2(_name) class FuncList##_name : public FunctionList\
{public: FuncList##_name() {func2List.push_back(std::make_shared<_name>(ExpressionPtr(), ExpressionPtr()));}};\
static FuncList##_name funcList##_name;
#define FUNCLIST3(_name) class FuncList##_name : public FunctionList\
{public: FuncList##_name() {func3List.push_back(std::make_shared<_name>(ExpressionPtr(), ExpressionPtr(), ExpressionPtr()));}};\
static FuncList##_name funcList##_name;
/***********************************************/
class ExpressionValue : public Expression
{
Double value;
public:
explicit ExpressionValue(Double v) : value(v) {}
std::string string() const override;
ExpressionPtr simplify(VariableList &/*varList*/, Bool &resolved) const override {resolved = TRUE; return clone();}
Double evaluate(const VariableList &/*varList*/) const override {return value;}
ExpressionPtr derivative(const std::string &/*var*/) const override {return std::make_shared<ExpressionValue>(0);}
ExpressionPtr clone() const override {return std::make_shared<ExpressionValue>(value);}
UInt priority() const override {return Expression::Priority::VALUE;}
};
inline ExpressionPtr exprValue(Double v) {return std::make_shared<ExpressionValue>(v);}
/***********************************************/
class ExpressionVar : public Expression
{
std::string name;
public:
explicit ExpressionVar(const std::string &_name) : name(_name) {}
std::string string() const override {return name;}
ExpressionPtr simplify(VariableList &varList, Bool &resolved) const override;
Double evaluate(const VariableList &varList) const override;
ExpressionPtr derivative(const std::string &var) const override {return exprValue((var == this->name) ? 1 : 0);}
ExpressionPtr clone() const override {return std::make_shared<ExpressionVar>(name);}
UInt priority() const override {return Expression::Priority::VALUE;}
};
inline ExpressionPtr exprVar(const std::string &name) {return std::make_shared<ExpressionVar>(name);}
/***********************************************/
/***********************************************/
// Template for constants
class ExpressionFunction0 : public Expression
{
protected:
Double value;
ExpressionFunction0(Double v=0) : value(v) {}
public:
virtual ~ExpressionFunction0() {}
virtual std::string name() const = 0;
std::string string() const override {return name()+"()";}
virtual ExpressionPtr create() const = 0;
ExpressionPtr simplify(VariableList &/*varList*/, Bool &resolved) const override {resolved = TRUE; return clone();}
Double evaluate(const VariableList &/*varList*/) const override {return value;}
ExpressionPtr derivative(const std::string &/*var*/) const override {return exprValue(0);}
ExpressionPtr clone() const override {return create();}
UInt priority() const override {return Expression::Priority::FUNCTION;}
};
/***********************************************/
// Template for functions with one argument
class ExpressionFunction1 : public Expression
{
protected:
ExpressionPtr operand;
ExpressionFunction1(const ExpressionPtr &op) : operand(op) {}
public:
virtual ~ExpressionFunction1() {}
virtual std::string name() const {return "operator";}
virtual std::string string() const override {return name()+"("+operand->string()+")";}
virtual ExpressionPtr create(const ExpressionPtr &ob) const = 0;
virtual ExpressionPtr simplify(VariableList &varList, Bool &resolved) const override;
virtual ExpressionPtr derivative(const std::string &/*var*/) const override {throw(Exception("Derivative not defined for \""+name()+"\"."));}
virtual ExpressionPtr clone() const override {return create(operand->clone());}
virtual UInt priority() const override {return Expression::Priority::FUNCTION;}
};
/***********************************************/
// Template for functions with two arguments
class ExpressionFunction2 : public Expression
{
protected:
ExpressionPtr left, right;
ExpressionFunction2(const ExpressionPtr &l, const ExpressionPtr &r) : left(l), right(r) {}
public:
virtual ~ExpressionFunction2() {}
virtual std::string name() const {return "operator";}
virtual std::string string() const override {return name()+"("+left->string()+", "+right->string()+")";}
virtual ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const = 0;
virtual ExpressionPtr simplify(VariableList &varList, Bool &resolved) const override;
virtual ExpressionPtr derivative(const std::string &/*var*/) const override {throw(Exception("Derivative not defined for \""+name()+"\"."));}
virtual ExpressionPtr clone() const override {return create(left->clone(), right->clone());}
virtual UInt priority() const override {return Expression::Priority::FUNCTION;}
};
/***********************************************/
// Template for functions with three arguments
class ExpressionFunction3 : public Expression
{
protected:
ExpressionPtr arg1, arg2, arg3;
ExpressionFunction3(const ExpressionPtr &a1, const ExpressionPtr &a2, const ExpressionPtr &a3) : arg1(a1), arg2(a2), arg3(a3) {}
public:
virtual ~ExpressionFunction3() {}
virtual std::string name() const {return "operator";}
virtual std::string string() const override {return name()+"("+arg1->string()+", "+arg2->string()+", "+arg3->string()+")";}
virtual ExpressionPtr create(const ExpressionPtr &a1, const ExpressionPtr &a2, const ExpressionPtr &a3) const = 0;
virtual ExpressionPtr simplify(VariableList &varList, Bool &resolved) const override;
virtual ExpressionPtr derivative(const std::string &/*var*/) const override {throw(Exception("Derivative not defined for \""+name()+"\"."));}
virtual ExpressionPtr clone() const override {return create(arg1->clone(), arg2->clone(), arg3->clone());}
virtual UInt priority() const override {return Expression::Priority::FUNCTION;}
};
/***********************************************/
/*** constants *********************************/
/***********************************************/
class ExpressionPi : public ExpressionFunction0
{
public:
ExpressionPi() : ExpressionFunction0(PI) {}
std::string name() const override {return "pi";}
ExpressionPtr create() const override {return std::make_shared<ExpressionPi>();}
};
FUNCLIST0(ExpressionPi)
inline ExpressionPtr exprPi() {return std::make_shared<ExpressionPi>();}
/***********************************************/
class ExpressionRho : public ExpressionFunction0
{
public:
ExpressionRho() : ExpressionFunction0(RAD2DEG) {}
std::string name() const override {return "rho";}
ExpressionPtr create() const override {return std::make_shared<ExpressionRho>();}
};
FUNCLIST0(ExpressionRho)
inline ExpressionPtr exprRho() {return std::make_shared<ExpressionRho>();}
/***********************************************/
class ExpressionNan : public ExpressionFunction0
{
public:
ExpressionNan() : ExpressionFunction0(NAN_EXPR) {}
std::string name() const override {return "nan";}
ExpressionPtr create() const override {return std::make_shared<ExpressionNan>();}
};
FUNCLIST0(ExpressionNan)
inline ExpressionPtr exprNan() {return std::make_shared<ExpressionNan>();}
/***********************************************/
class ExpressionLightVelovity : public ExpressionFunction0
{
public:
ExpressionLightVelovity() : ExpressionFunction0(LIGHT_VELOCITY) {}
std::string name() const override {return "c";}
ExpressionPtr create() const override {return std::make_shared<ExpressionLightVelovity>();}
};
FUNCLIST0(ExpressionLightVelovity)
/***********************************************/
class ExpressionGravitationalConstant : public ExpressionFunction0
{
public:
ExpressionGravitationalConstant() : ExpressionFunction0(GRAVITATIONALCONSTANT) {}
std::string name() const override {return "G";}
ExpressionPtr create() const override {return std::make_shared<ExpressionGravitationalConstant>();}
};
FUNCLIST0(ExpressionGravitationalConstant)
/***********************************************/
class ExpressionGM : public ExpressionFunction0
{
public:
ExpressionGM() : ExpressionFunction0(DEFAULT_GM) {}
std::string name() const override {return "GM";}
ExpressionPtr create() const override {return std::make_shared<ExpressionGM>();}
};
FUNCLIST0(ExpressionGM)
/***********************************************/
class ExpressionR : public ExpressionFunction0
{
public:
ExpressionR() : ExpressionFunction0(DEFAULT_R) {}
std::string name() const override {return "R";}
ExpressionPtr create() const override {return std::make_shared<ExpressionR>();}
};
FUNCLIST0(ExpressionR)
/***********************************************/
/*** basic arithmetic operations ***************/
/***********************************************/
class ExpressionNegative : public ExpressionFunction1
{
public:
explicit ExpressionNegative(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string string() const override {return "-"+operand->string(priority());}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionNegative>(ob);}
Double evaluate(const VariableList &v) const override {return -operand->evaluate(v);}
ExpressionPtr derivative(const std::string &var) const override {return -operand->derivative(var);}
UInt priority() const override {return Expression::Priority::UNARY;}
};
inline ExpressionPtr operator-(const ExpressionPtr &ob) {return std::make_shared<ExpressionNegative>(ob);}
/***********************************************/
class ExpressionAdd : public ExpressionFunction2
{
public:
ExpressionAdd(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l, r) {}
std::string string() const override {return left->string(priority()) + " + " + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionAdd>(l, r);}
ExpressionPtr simplify(VariableList &varList, Bool &resolved) const override;
Double evaluate(const VariableList &v) const override {return left->evaluate(v) + right->evaluate(v);}
ExpressionPtr derivative(const std::string &var) const override {return left->derivative(var) + right->derivative(var);}
UInt priority() const override {return Expression::Priority::ADDITIVE;}
};
ExpressionPtr ExpressionAdd::simplify(VariableList &varList, Bool &resolved) const
{
Bool resolved1, resolved2;
ExpressionPtr l = left->simplify(varList, resolved1);
ExpressionPtr r = right->simplify(varList, resolved2);
resolved = resolved1 && resolved2;
if(resolved) return exprValue(create(l, r)->evaluate(varList));
if(resolved1 && (l->evaluate(varList) == 0.)) return r;
if(resolved2 && (r->evaluate(varList) == 0.)) return l;
return create(l, r);
}
inline ExpressionPtr operator+(const ExpressionPtr &l, const ExpressionPtr &r) {return std::make_shared<ExpressionAdd>(l,r);}
/***********************************************/
class ExpressionSub : public ExpressionFunction2
{
public:
ExpressionSub(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l, r) {}
std::string string() const override {return left->string(priority()) + " - " + right->string(priority()+1);}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionSub>(l, r);}
ExpressionPtr simplify(VariableList &varList, Bool &resolved) const override;
Double evaluate(const VariableList &v) const override {return left->evaluate(v) - right->evaluate(v);}
ExpressionPtr derivative(const std::string &var) const override {return left->derivative(var) - right->derivative(var);}
UInt priority() const override {return Expression::Priority::ADDITIVE;}
};
ExpressionPtr ExpressionSub::simplify(VariableList &varList, Bool &resolved) const
{
Bool resolved1, resolved2;
ExpressionPtr l = left->simplify(varList, resolved1);
ExpressionPtr r = right->simplify(varList, resolved2);
resolved = resolved1 && resolved2;
if(resolved) return exprValue(create(l, r)->evaluate(varList));
if(resolved1 && (l->evaluate(varList) == 0.)) return -r;
if(resolved2 && (r->evaluate(varList) == 0.)) return l;
return create(l, r);
}
inline ExpressionPtr operator-(const ExpressionPtr &l, const ExpressionPtr &r) {return std::make_shared<ExpressionSub>(l,r);}
/***********************************************/
class ExpressionMult : public ExpressionFunction2
{
public:
ExpressionMult(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l, r) {}
std::string string() const override {return left->string(priority()) + "*" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionMult>(l, r);}
ExpressionPtr simplify(VariableList &varList, Bool &resolved) const override;
Double evaluate(const VariableList &v) const override {return left->evaluate(v) * right->evaluate(v);}
ExpressionPtr derivative(const std::string &var) const override {return left->derivative(var)*right->clone() + left->clone()*right->derivative(var);}
UInt priority() const override {return Expression::Priority::MULTIPLICATIVE;}
};
ExpressionPtr ExpressionMult::simplify(VariableList &varList, Bool &resolved) const
{
Bool resolved1, resolved2;
ExpressionPtr l = left->simplify(varList, resolved1);
ExpressionPtr r = right->simplify(varList, resolved2);
resolved = resolved1 && resolved2;
if(resolved) return exprValue(create(l, r)->evaluate(varList));
if(resolved1 && (l->evaluate(varList) == 1.)) return r;
if(resolved1 && (l->evaluate(varList) == 0.)) {resolved = TRUE; return exprValue(0);}
if(resolved2 && (r->evaluate(varList) == 1.)) return l;
if(resolved2 && (r->evaluate(varList) == 0.)) {resolved = TRUE; return exprValue(0);}
return create(l, r);
}
inline ExpressionPtr operator*(const ExpressionPtr &l, const ExpressionPtr &r) {return std::make_shared<ExpressionMult>(l,r);}
/***********************************************/
class ExpressionDiv : public ExpressionFunction2
{
public:
ExpressionDiv(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l, r) {}
std::string string() const override {return left->string(priority()) + "/" + right->string(priority()+1);}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionDiv>(l, r);}
ExpressionPtr simplify(VariableList &varList, Bool &resolved) const override;
Double evaluate(const VariableList &v) const override {return left->evaluate(v) / right->evaluate(v);}
ExpressionPtr derivative(const std::string &var) const override {return (left->derivative(var)*right->clone() - left->clone()*right->derivative(var))/(right->clone()^exprValue(2));}
UInt priority() const override {return Expression::Priority::MULTIPLICATIVE;}
};
ExpressionPtr ExpressionDiv::simplify(VariableList &varList, Bool &resolved) const
{
Bool resolved1, resolved2;
ExpressionPtr l = left->simplify(varList, resolved1);
ExpressionPtr r = right->simplify(varList, resolved2);
resolved = resolved1 && resolved2;
if(resolved) return exprValue(create(l, r)->evaluate(varList));
if(resolved1 && (l->evaluate(varList) == 0.)) {resolved = TRUE; return exprValue(0);}
if(resolved2 && (r->evaluate(varList) == 1.)) return l;
return create(l, r);
}
inline ExpressionPtr operator/(const ExpressionPtr &l, const ExpressionPtr &r) {return std::make_shared<ExpressionDiv>(l,r);}
/***********************************************/
class ExpressionPow : public ExpressionFunction2
{
public:
ExpressionPow(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l, r) {}
std::string string() const override {return left->string(priority()) + "^" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionPow>(l, r);}
ExpressionPtr simplify(VariableList &varList, Bool &resolved) const override;
Double evaluate(const VariableList &v) const override {return pow(left->evaluate(v),right->evaluate(v));}
ExpressionPtr derivative(const std::string &var) const override {return left->derivative(var)*right->clone()/left->clone() * (left->clone()^(right->clone()-exprValue(1)));} // this derivative is not completly correct!!!!
UInt priority() const override {return Expression::Priority::EXPONENTIAL;}
};
ExpressionPtr ExpressionPow::simplify(VariableList &varList, Bool &resolved) const
{
Bool resolved1, resolved2;
ExpressionPtr l = left->simplify(varList, resolved1);
ExpressionPtr r = right->simplify(varList, resolved2);
resolved = resolved1 && resolved2;
if(resolved) return exprValue(create(l, r)->evaluate(varList));
if(resolved1 && (l->evaluate(varList) == 0.)) {resolved = TRUE; return exprValue(0);}
if(resolved2 && (r->evaluate(varList) == 1.)) return l;
return create(l, r);
}
inline ExpressionPtr operator^(const ExpressionPtr &l, const ExpressionPtr &r) {return std::make_shared<ExpressionPow>(l,r);}
/***********************************************/
/*** basic comparison operations ***************/
/***********************************************/
class ExpressionLessThan : public ExpressionFunction2
{
public:
ExpressionLessThan(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "operator<";}
std::string string() const override {return left->string(priority()) + "<" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionLessThan>(l, r);}
Double evaluate(const VariableList &varList) const override {return left->evaluate(varList) < right->evaluate(varList) ? 1.0 : 0.0;}
UInt priority() const override {return Expression::Priority::RELATION;}
};
/***********************************************/
class ExpressionLessEqualThan : public ExpressionFunction2
{
public:
ExpressionLessEqualThan(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "operator<=";}
std::string string() const override {return left->string(priority()) + "<=" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionLessEqualThan>(l, r);}
Double evaluate(const VariableList &varList) const override {return left->evaluate(varList) <= right->evaluate(varList) ? 1.0 : 0.0;}
UInt priority() const override {return Expression::Priority::RELATION;}
};
/***********************************************/
class ExpressionGreaterThan : public ExpressionFunction2
{
public:
ExpressionGreaterThan(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "operator>";}
std::string string() const override {return left->string(priority()) + ">" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionGreaterThan>(l, r);}
Double evaluate(const VariableList &varList) const override {return left->evaluate(varList) > right->evaluate(varList) ? 1.0 : 0.0;}
UInt priority() const override {return Expression::Priority::RELATION;}
};
/***********************************************/
class ExpressionGreaterEqualThan : public ExpressionFunction2
{
public:
ExpressionGreaterEqualThan(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "operator>=";}
std::string string() const override {return left->string(priority()) + ">=" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionGreaterEqualThan>(l, r);}
Double evaluate(const VariableList &varList) const override {return left->evaluate(varList) >= right->evaluate(varList) ? 1.0 : 0.0;}
UInt priority() const override {return Expression::Priority::RELATION;}
};
/***********************************************/
class ExpressionEqual : public ExpressionFunction2
{
public:
ExpressionEqual(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "operator==";}
std::string string() const override {return left->string(priority()) + "==" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionEqual>(l, r);}
Double evaluate(const VariableList &varList) const override {return left->evaluate(varList) == right->evaluate(varList) ? 1.0 : 0.0;}
UInt priority() const override {return Expression::Priority::EQUALITY;}
};
/***********************************************/
class ExpressionNotEqual : public ExpressionFunction2
{
public:
ExpressionNotEqual(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "operator!=";}
std::string string() const override {return left->string(priority()) + "!=" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionNotEqual>(l, r);}
Double evaluate(const VariableList &varList) const override {return left->evaluate(varList) != right->evaluate(varList) ? 1.0 : 0.0;}
UInt priority() const override {return Expression::Priority::EQUALITY;}
};
/***********************************************/
/*** logical operators *************************/
/***********************************************/
class ExpressionLogicalNot : public ExpressionFunction1
{
public:
explicit ExpressionLogicalNot(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "operator!";}
std::string string() const override {return "!"+operand->string(priority());}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionLogicalNot>(ob);}
Double evaluate(const VariableList &v) const override {return operand->evaluate(v) == 0.0 ? 1.0 : 0.0;}
UInt priority() const override {return Expression::Priority::UNARY;}
};
/***********************************************/
class ExpressionLogicalAnd : public ExpressionFunction2
{
public:
ExpressionLogicalAnd(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "operator&&";}
std::string string() const override {return left->string(priority()) + "&&" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionLogicalAnd>(l, r);}
Double evaluate(const VariableList &varList) const override {return (left->evaluate(varList)!=0.0 && right->evaluate(varList)!=0.0) ? 1.0 : 0.0;}
UInt priority() const override {return Expression::Priority::LOGICAL_AND;}
};
/***********************************************/
class ExpressionLogicalOr : public ExpressionFunction2
{
public:
ExpressionLogicalOr(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "operator||";}
std::string string() const override {return left->string(priority()) + "||" + right->string(priority());}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionLogicalOr>(l, r);}
Double evaluate(const VariableList &varList) const override {return (left->evaluate(varList)!=0.0 || right->evaluate(varList)!=0.0) ? 1.0 : 0.0;}
UInt priority() const override {return Expression::Priority::LOGICAL_OR;}
};
/***********************************************/
class ExpressionIfThenElse : public ExpressionFunction3
{
public:
ExpressionIfThenElse(const ExpressionPtr &a1, const ExpressionPtr &a2, const ExpressionPtr &a3) : ExpressionFunction3(a1,a2,a3) {}
std::string name() const override {return "if";}
ExpressionPtr create(const ExpressionPtr &a1, const ExpressionPtr &a2, const ExpressionPtr &a3) const override {return std::make_shared<ExpressionIfThenElse>(a1, a2, a3);}
Double evaluate(const VariableList &varList) const override {return arg1->evaluate(varList) != 0.0 ? arg2->evaluate(varList) : arg3->evaluate(varList);}
};
FUNCLIST3(ExpressionIfThenElse)
/***********************************************/
class ExpressionIsNan : public ExpressionFunction1
{
public:
explicit ExpressionIsNan(const ExpressionPtr &a1) : ExpressionFunction1(a1) {}
std::string name() const override {return "isnan";}
ExpressionPtr create(const ExpressionPtr &a1) const override {return std::make_shared<ExpressionIsNan>(a1);}
Double evaluate(const VariableList &varList) const override {return std::isnan(operand->evaluate(varList));}
};
FUNCLIST1(ExpressionIsNan)
/***********************************************/
/*** functions *********************************/
/***********************************************/
class ExpressionSqrt : public ExpressionFunction1
{
public:
explicit ExpressionSqrt(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "sqrt";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionSqrt>(ob);}
Double evaluate(const VariableList &varList) const override {return std::sqrt(operand->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return operand->derivative(var)/(exprValue(2.0)*sqrt(operand->clone()));}
};
FUNCLIST1(ExpressionSqrt)
inline ExpressionPtr sqrt(const ExpressionPtr &ob) {return std::make_shared<ExpressionSqrt>(ob);}
/***********************************************/
class ExpressionExp : public ExpressionFunction1
{
public:
explicit ExpressionExp(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "exp";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionExp>(ob);}
Double evaluate(const VariableList &varList) const override {return std::exp(operand->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return operand->derivative(var)*exp(operand->clone());}
};
FUNCLIST1(ExpressionExp)
inline ExpressionPtr exp(const ExpressionPtr &ob) {return std::make_shared<ExpressionExp>(ob);}
/***********************************************/
class ExpressionLog : public ExpressionFunction1
{
public:
explicit ExpressionLog(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "log";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionExp>(ob);}
Double evaluate(const VariableList &varList) const override {return std::log(operand->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return operand->derivative(var)^exprValue(-1);}
};
FUNCLIST1(ExpressionLog)
inline ExpressionPtr log(const ExpressionPtr &ob) {return std::make_shared<ExpressionLog>(ob);}
/***********************************************/
class ExpressionSin : public ExpressionFunction1
{
public:
explicit ExpressionSin(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "sin";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionSin>(ob);}
Double evaluate(const VariableList &varList) const override {return std::sin(operand->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return operand->derivative(var)*cos(operand->clone());}
};
FUNCLIST1(ExpressionSin)
inline ExpressionPtr sin(const ExpressionPtr &ob) {return std::make_shared<ExpressionSin>(ob);}
/***********************************************/
class ExpressionCos : public ExpressionFunction1
{
public:
explicit ExpressionCos(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "cos";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionCos>(ob);}
Double evaluate(const VariableList &varList) const override {return std::cos(operand->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return -operand->derivative(var)*sin(operand->clone());}
};
FUNCLIST1(ExpressionCos)
inline ExpressionPtr cos(const ExpressionPtr &ob) {return std::make_shared<ExpressionCos>(ob);}
/***********************************************/
class ExpressionTan : public ExpressionFunction1
{
public:
explicit ExpressionTan(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "tan";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionTan>(ob);}
Double evaluate(const VariableList &varList) const override {return std::tan(operand->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return operand->derivative(var)/(cos(operand->clone())^exprValue(2));}
};
FUNCLIST1(ExpressionTan)
inline ExpressionPtr tan(const ExpressionPtr &ob) {return std::make_shared<ExpressionTan>(ob);}
/***********************************************/
class ExpressionAsin : public ExpressionFunction1
{
public:
explicit ExpressionAsin(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "asin";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionAsin>(ob);}
Double evaluate(const VariableList &varList) const override {return std::asin(operand->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return operand->derivative(var)/sqrt(exprValue(1) - ((operand->clone())^exprValue(2)));}
};
FUNCLIST1(ExpressionAsin)
inline ExpressionPtr asin(const ExpressionPtr &ob) {return std::make_shared<ExpressionAsin>(ob);}
/***********************************************/
class ExpressionAcos : public ExpressionFunction1
{
public:
explicit ExpressionAcos(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "acos";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionAcos>(ob);}
Double evaluate(const VariableList &varList) const override {return std::acos(operand->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return -operand->derivative(var)/sqrt(exprValue(1) - ((operand->clone())^exprValue(2)));}
};
FUNCLIST1(ExpressionAcos)
inline ExpressionPtr acos(const ExpressionPtr &ob) {return std::make_shared<ExpressionAcos>(ob);}
/***********************************************/
class ExpressionAtan : public ExpressionFunction1
{
public:
explicit ExpressionAtan(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "atan";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionAtan>(ob);}
Double evaluate(const VariableList &varList) const override {return std::atan(operand->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return operand->derivative(var)/(exprValue(1) + ((operand->clone())^exprValue(2)));}
};
FUNCLIST1(ExpressionAtan)
inline ExpressionPtr atan(const ExpressionPtr &ob) {return std::make_shared<ExpressionAtan>(ob);}
/***********************************************/
class ExpressionAbs : public ExpressionFunction1
{
public:
explicit ExpressionAbs(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "abs";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionAbs>(ob);}
Double evaluate(const VariableList &varList) const override {return std::fabs(operand->evaluate(varList));}
};
FUNCLIST1(ExpressionAbs)
/***********************************************/
class ExpressionRound : public ExpressionFunction1
{
public:
explicit ExpressionRound(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "round";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionRound>(ob);}
Double evaluate(const VariableList &varList) const override {return std::round(operand->evaluate(varList));}
};
FUNCLIST1(ExpressionRound)
/***********************************************/
class ExpressionCeil : public ExpressionFunction1
{
public:
explicit ExpressionCeil(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "ceil";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionCeil>(ob);}
Double evaluate(const VariableList &varList) const override {return std::ceil(operand->evaluate(varList));}
};
FUNCLIST1(ExpressionCeil)
/***********************************************/
class ExpressionFloor : public ExpressionFunction1
{
public:
explicit ExpressionFloor(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "floor";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionFloor>(ob);}
Double evaluate(const VariableList &varList) const override {return std::floor(operand->evaluate(varList));}
};
FUNCLIST1(ExpressionFloor)
/***********************************************/
class ExpressionDeg2Rad : public ExpressionFunction1
{
public:
explicit ExpressionDeg2Rad(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "deg2rad";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionDeg2Rad>(ob);}
Double evaluate(const VariableList &varList) const override {return DEG2RAD * operand->evaluate(varList);}
};
FUNCLIST1(ExpressionDeg2Rad)
/***********************************************/
class ExpressionRad2Deg : public ExpressionFunction1
{
public:
explicit ExpressionRad2Deg(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "rad2deg";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionRad2Deg>(ob);}
Double evaluate(const VariableList &varList) const override {return RAD2DEG * operand->evaluate(varList);}
};
FUNCLIST1(ExpressionRad2Deg)
/***********************************************/
/***********************************************/
class ExpressionAtan2 : public ExpressionFunction2
{
public:
ExpressionAtan2(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "atan2";}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionAtan2>(l, r);}
Double evaluate(const VariableList &varList) const override {return std::atan2(left->evaluate(varList), right->evaluate(varList));}
ExpressionPtr derivative(const std::string &var) const override {return (left->derivative(var)*right->clone() - left->clone()*right->derivative(var))/((left->clone()^exprValue(2))+(right->clone()^exprValue(2)));}
};
FUNCLIST2(ExpressionAtan2)
/***********************************************/
class ExpressionMin : public ExpressionFunction2
{
public:
ExpressionMin(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "min";}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionMin>(l, r);}
Double evaluate(const VariableList &varList) const override {return std::min(left->evaluate(varList), right->evaluate(varList));}
};
FUNCLIST2(ExpressionMin)
/***********************************************/
class ExpressionMax : public ExpressionFunction2
{
public:
ExpressionMax(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "max";}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionMax>(l, r);}
Double evaluate(const VariableList &varList) const override {return std::max(left->evaluate(varList), right->evaluate(varList));}
};
FUNCLIST2(ExpressionMax)
/***********************************************/
class ExpressionMod : public ExpressionFunction2
{
public:
ExpressionMod(const ExpressionPtr &l, const ExpressionPtr &r) : ExpressionFunction2(l,r) {}
std::string name() const override {return "mod";}
ExpressionPtr create(const ExpressionPtr &l, const ExpressionPtr &r) const override {return std::make_shared<ExpressionMod>(l, r);}
Double evaluate(const VariableList &varList) const override {return std::fmod(left->evaluate(varList), right->evaluate(varList));}
};
FUNCLIST2(ExpressionMod)
/***********************************************/
/*** time conversions **************************/
/***********************************************/
class ExpressionNow : public ExpressionFunction0
{
public:
ExpressionNow()
{
std::time_t tt = std::time(nullptr);
std::tm t = *std::localtime(&tt);
value = date2time(t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec).mjd();
}
std::string name() const override {return "now";}
ExpressionPtr create() const override {return std::make_shared<ExpressionNow>();}
};
FUNCLIST0(ExpressionNow)
/***********************************************/
class ExpressionDate2mjd : public ExpressionFunction3
{
public:
ExpressionDate2mjd(const ExpressionPtr &a1, const ExpressionPtr &a2, const ExpressionPtr &a3) : ExpressionFunction3(a1,a2,a3) {}
std::string name() const override {return "date2mjd";}
ExpressionPtr create(const ExpressionPtr &a1, const ExpressionPtr &a2, const ExpressionPtr &a3) const override {return std::make_shared<ExpressionDate2mjd>(a1, a2, a3);}
Double evaluate(const VariableList &varList) const override {return date2time(static_cast<UInt>(arg1->evaluate(varList)), static_cast<UInt>(arg2->evaluate(varList)), static_cast<UInt>(arg3->evaluate(varList))).mjd();}
};
FUNCLIST3(ExpressionDate2mjd)
/***********************************************/
class ExpressionGps2Utc : public ExpressionFunction1
{
public:
explicit ExpressionGps2Utc(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "gps2utc";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionGps2Utc>(ob);}
Double evaluate(const VariableList &varList) const override {return timeGPS2UTC(mjd2time(operand->evaluate(varList))).mjd();}
};
FUNCLIST1(ExpressionGps2Utc)
/***********************************************/
class ExpressionUtc2Gps : public ExpressionFunction1
{
public:
explicit ExpressionUtc2Gps(const ExpressionPtr &ob) : ExpressionFunction1(ob) {}
std::string name() const override {return "utc2gps";}
ExpressionPtr create(const ExpressionPtr &ob) const override {return std::make_shared<ExpressionUtc2Gps>(ob);}
Double evaluate(const VariableList &varList) const override {return timeUTC2GPS(mjd2time(operand->evaluate(varList))).mjd();}
};
FUNCLIST1(ExpressionUtc2Gps)
/***********************************************/
class ExpressionDayOfYear : public ExpressionFunction1
{
public:
explicit ExpressionDayOfYear(const ExpressionPtr &a1) : ExpressionFunction1(a1) {}
std::string name() const override {return "dayofyear";}
ExpressionPtr create(const ExpressionPtr &a1) const override {return std::make_shared<ExpressionDayOfYear>(a1);}
Double evaluate(const VariableList &varList) const override {return static_cast<Double>(mjd2time(operand->evaluate(varList)).dayOfYear());}
};
FUNCLIST1(ExpressionDayOfYear)
/***********************************************/
class ExpressionDecimalYear : public ExpressionFunction1
{
public:
explicit ExpressionDecimalYear(const ExpressionPtr &a1) : ExpressionFunction1(a1) {}
std::string name() const override {return "decimalyear";}
ExpressionPtr create(const ExpressionPtr &a1) const override {return std::make_shared<ExpressionDecimalYear>(a1);}
Double evaluate(const VariableList &varList) const override {return mjd2time(operand->evaluate(varList)).decimalYear();}
};
FUNCLIST1(ExpressionDecimalYear)
/***********************************************/
/***********************************************/
/***********************************************/
std::string ExpressionValue::string() const
{
std::stringstream ss;
ss<<value;
return ss.str();
}
/***********************************************/
std::string Expression::string(UInt aprio) const
{
return (priority()<aprio) ? "("+string()+")" : string();
}
/***********************************************/
Double ExpressionVar::evaluate(const VariableList &varList) const
{
auto variable = varList.find(name);
if(!variable)
{
// Hack to read old constant definition without brackets
std::string name2 = name;
std::transform(name2.begin(), name2.end(), name2.begin(), [](auto c){return std::tolower(c);});
if(name2 == "pi") return PI;
if(name2 == "rho") return RAD2DEG;
if(name2 == "nan") return NAN_EXPR;
throw(Exception("unknown variable: "+name));
}
return variable->evaluate(varList);
}
/***********************************************/
ExpressionPtr ExpressionVar::simplify(VariableList &varList, Bool &resolved) const
{
auto variable = varList.find(name);
if(!variable)
{
// Hack to read old constant definition without brackets
std::string name2 = name;
std::transform(name2.begin(), name2.end(), name2.begin(), [](auto c){return std::tolower(c);});
if(name2 == "pi") return exprPi();
if(name2 == "rho") return exprRho();
if(name2 == "nan") return exprNan();
throw(Exception("unknown variable: "+name));
}
try
{
resolved = TRUE;
return exprValue(variable->evaluate(varList));
}
catch(...)
{
resolved = FALSE;
return clone();
}
}
/***********************************************/
ExpressionPtr ExpressionFunction1::simplify(VariableList &varList, Bool &resolved) const
{
ExpressionPtr expr = create(operand->simplify(varList, resolved));
if(resolved)
return exprValue(expr->evaluate(varList));
return expr;
}
/***********************************************/
ExpressionPtr ExpressionFunction2::simplify(VariableList &varList, Bool &resolved) const
{
Bool resolved1, resolved2;
ExpressionPtr expr = create(left->simplify(varList, resolved1), right->simplify(varList, resolved2));
resolved = resolved1 && resolved2;
if(resolved)
return exprValue(expr->evaluate(varList));
return expr;
}
/***********************************************/
ExpressionPtr ExpressionFunction3::simplify(VariableList &varList, Bool &resolved) const
{
Bool resolved1, resolved2, resolved3;
ExpressionPtr expr = create(arg1->simplify(varList, resolved1), arg2->simplify(varList, resolved2), arg3->simplify(varList, resolved3));
resolved = (resolved1 && resolved2) && resolved3;
if(resolved)
return exprValue(expr->evaluate(varList));
return expr;
}
/***********************************************/
/***********************************************/
class Tokenizer
{
std::string text;
UInt pos, posStart;
Bool unused;
public:
enum Type {END, SINGLE, VALUE, NAME, OPERATOR};
enum OperatorType {NONE, ADD, SUB, MULT, DIV, POW, EQUAL, NOT_EQUAL, LESS, GREATER, LESS_EQUAL, GREATER_EQUAL, LOGICAL_AND, LOGICAL_OR};
Type type;
OperatorType opType;
char c;
Double value;
std::string name;
explicit Tokenizer(const std::string &text);
void get();
void putBack() {unused = TRUE;}
std::string infoString(const std::string &info) const;
};
/***********************************************/
Tokenizer::Tokenizer(const std::string &txt) : text(txt), pos(0), posStart(0), unused(FALSE)
{
get();
putBack();
}
/***********************************************/
void Tokenizer::get()
{
if(unused)
{
unused=FALSE;
return;
}
name.clear();
c = ' ';
value = 0.0;
type = Tokenizer::END;
opType = Tokenizer::NONE;
// eat whitespace
while((pos<text.size()) && isspace(text.at(pos)))
pos++;
if(pos>=text.size())
{
type = Tokenizer::END;
return;
}
posStart = pos;
c = text.at(pos);
// operator? (+,-,..)
if(!(isalnum(c)||(c=='_')))
{
type = Tokenizer::SINGLE;
if(c == '+') opType = Tokenizer::ADD;
if(c == '-') opType = Tokenizer::SUB;
if(c == '*') opType = Tokenizer::MULT;
if(c == '/') opType = Tokenizer::DIV;
if(c == '^') opType = Tokenizer::POW;
if(c == '>')
{
opType = Tokenizer::GREATER;
if(pos+1<text.size() && text.at(pos+1) == '=')
{
opType = Tokenizer::GREATER_EQUAL;
pos++;
}
}
if(c == '<')
{
opType = Tokenizer::LESS;
if(pos+1<text.size() && text.at(pos+1) == '=')
{
opType = Tokenizer::LESS_EQUAL;
pos++;
}
}
if((c == '=') && (pos+1<text.size() && text.at(pos+1) == '='))
{
opType = Tokenizer::EQUAL;
pos++;
}
if((c == '!') && (pos+1<text.size() && text.at(pos+1) == '='))
{
opType = Tokenizer::NOT_EQUAL;
pos++;
}
if((c == '&') && (pos+1<text.size() && text.at(pos+1) == '&'))
{
opType = Tokenizer::LOGICAL_AND;
pos++;
}
if((c == '|') && (pos+1<text.size() && text.at(pos+1) == '|'))
{
opType = Tokenizer::LOGICAL_OR;
pos++;
}
pos++;
return;
}
// number?
if(std::isdigit(c))
{
type = Tokenizer::VALUE;
const char *ptr1 = text.c_str()+pos;
char *ptr2;
value = std::strtod(ptr1, &ptr2);
pos += ptr2-ptr1;
return;
}
// name
type = Tokenizer::NAME;
while((pos<text.size()) && (isalnum(text.at(pos))||(text.at(pos)=='_')))
name.push_back(text.at(pos++));
}
/***********************************************/
std::string Tokenizer::infoString(const std::string &info) const
{
std::stringstream ss;
ss<<"'"<<text<<"', col="<<posStart<<", "<<info<<std::endl
<<std::setfill(' ')<<std::setw(posStart+2)<<std::right<<'^';
return ss.str();
}
/***********************************************/
static ExpressionPtr parse(Tokenizer &token, UInt priority);
static ExpressionPtr operand(Tokenizer &token);
/***********************************************/
ExpressionPtr operand(Tokenizer &token)
{
token.get();
if(token.type==Tokenizer::SINGLE)
{
switch(token.c)
{
case '+':
return parse(token, Expression::Priority::UNARY);
case '-':
return -parse(token, Expression::Priority::UNARY);
case '!':
return std::make_shared<ExpressionLogicalNot>(parse(token, Expression::Priority::UNARY));
case '(':
ExpressionPtr expr = parse(token, 0);
token.get();
if((token.type!=Tokenizer::SINGLE)||(token.c!=')'))
throw(Exception(token.infoString("expected ')'")));
return expr;
}
throw(Exception(token.infoString("unknown token")));
}
else if(token.type==Tokenizer::VALUE)
{
return exprValue(token.value);
}
else if(token.type==Tokenizer::NAME)
{
const std::string name = token.name;
token.get();
if((token.type!=Tokenizer::SINGLE)||(token.c!='('))
{
token.putBack();
return exprVar(name); // new variable
}
// is function/constant without arguments?
for(UInt i=0; i<FunctionList::func0List.size(); i++)
if(name == FunctionList::func0List.at(i)->name())
{
token.get();
if((token.type!=Tokenizer::SINGLE)||(token.c!=')'))
throw(Exception(token.infoString("expected ')'")));
return FunctionList::func0List.at(i)->create();
}
// is Function with one argument?
for(UInt i=0; i<FunctionList::func1List.size(); i++)
if(name == FunctionList::func1List.at(i)->name())
{
ExpressionPtr expr = FunctionList::func1List.at(i)->create(parse(token, 0));
token.get();
if((token.type!=Tokenizer::SINGLE)||(token.c!=')'))
throw(Exception(token.infoString("expected ')'")));
return expr;
}
// is Function with two arguments?
for(UInt i=0; i<FunctionList::func2List.size(); i++)
if(name == FunctionList::func2List.at(i)->name())
{
ExpressionPtr ob1 = parse(token, 0);
token.get();
if((token.type!=Tokenizer::SINGLE)||(token.c!=','))
throw(Exception(token.infoString("expected ','")));
ExpressionPtr ob2 = parse(token, 0);
token.get();
if((token.type!=Tokenizer::SINGLE)||(token.c!=')'))
throw(Exception(token.infoString("expected ')'")));
return FunctionList::func2List.at(i)->create(ob1, ob2);
}
// is Function with three arguments?
for(UInt i=0; i<FunctionList::func3List.size(); i++)
if(name == FunctionList::func3List.at(i)->name())
{
ExpressionPtr ob1 = parse(token, 0);
token.get();
if((token.type!=Tokenizer::SINGLE)||(token.c!=','))
throw(Exception(token.infoString("expected ','")));
ExpressionPtr ob2 = parse(token, 0);
token.get();
if((token.type!=Tokenizer::SINGLE)||(token.c!=','))
throw(Exception(token.infoString("expected ','")));
ExpressionPtr ob3 = parse(token, 0);
token.get();
if((token.type!=Tokenizer::SINGLE)||(token.c!=')'))
throw(Exception(token.infoString("expected ')'")));
return FunctionList::func3List.at(i)->create(ob1, ob2, ob3);
}
throw(Exception(token.infoString("unknown function")));
}
throw(Exception(token.infoString("expected operand")));
}
/***********************************************/
ExpressionPtr parse(Tokenizer &token, UInt priority)
{
UInt priorityNew = Expression::Priority::NONE;
ExpressionPtr ob = operand(token);
do
{
token.get();
if(token.type != Tokenizer::SINGLE)
break;
switch(token.opType)
{
case Tokenizer::ADD:
priorityNew = Expression::Priority::ADDITIVE;
if(priorityNew > priority)
ob = ob + parse(token, priorityNew);
break;
case Tokenizer::SUB:
priorityNew = Expression::Priority::ADDITIVE;
if(priorityNew > priority)
ob = ob - parse(token, priorityNew);
break;
case Tokenizer::MULT:
priorityNew = Expression::Priority::MULTIPLICATIVE;
if(priorityNew > priority)
ob = ob * parse(token, priorityNew);
break;
case Tokenizer::DIV:
priorityNew = Expression::Priority::MULTIPLICATIVE;
if(priorityNew > priority)
ob = ob / parse(token, priorityNew);
break;
case Tokenizer::POW:
priorityNew = Expression::Priority::EXPONENTIAL;
if(priorityNew > priority)
ob = ob ^ parse(token, priorityNew);
break;
case Tokenizer::GREATER:
priorityNew = Expression::Priority::RELATION;
if(priorityNew > priority)
ob = std::make_shared<ExpressionGreaterThan>(ob, parse(token, priorityNew));
break;
case Tokenizer::LESS:
priorityNew = Expression::Priority::RELATION;
if(priorityNew > priority)
ob = std::make_shared<ExpressionLessThan>(ob, parse(token, priorityNew));
break;
case Tokenizer::LESS_EQUAL:
priorityNew = Expression::Priority::RELATION;
if(priorityNew > priority)
ob = std::make_shared<ExpressionLessEqualThan>(ob, parse(token, priorityNew));
break;
case Tokenizer::GREATER_EQUAL:
priorityNew = Expression::Priority::RELATION;
if(priorityNew > priority)
ob = std::make_shared<ExpressionGreaterEqualThan>(ob, parse(token, priorityNew));
break;
case Tokenizer::EQUAL:
priorityNew = Expression::Priority::EQUALITY;
if(priorityNew > priority)
ob = std::make_shared<ExpressionEqual>(ob, parse(token, priorityNew));
break;
case Tokenizer::NOT_EQUAL:
priorityNew = Expression::Priority::EQUALITY;
if(priorityNew > priority)
ob = std::make_shared<ExpressionNotEqual>(ob, parse(token, priorityNew));
break;
case Tokenizer::LOGICAL_AND:
priorityNew = Expression::Priority::LOGICAL_AND;
if(priorityNew > priority)
ob = std::make_shared<ExpressionLogicalAnd>(ob, parse(token, priorityNew));
break;
case Tokenizer::LOGICAL_OR:
priorityNew = Expression::Priority::LOGICAL_OR;
if(priorityNew > priority)
ob = std::make_shared<ExpressionLogicalOr>(ob, parse(token, priorityNew));
break;
case Tokenizer::NONE:
priorityNew = Expression::Priority::NONE;
break;
}
}
while(priority<priorityNew);
token.putBack();
if(!ob)
throw(Exception(token.infoString("unknown error")));
return ob;
}
/***********************************************/
ExpressionPtr Expression::parse(const std::string &text)
{
try
{
Tokenizer token(text);
ExpressionPtr expr = ::parse(token, 0);
if(token.type!=Tokenizer::END)
throw(Exception(token.infoString("unknown token")));
return expr;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
/***********************************************/
Double ExpressionVariable::parse(const std::string &text, const VariableList &varList)
{
try
{
return Expression::parse(text)->evaluate(varList);
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
ExpressionVariable::ExpressionVariable(const std::string &name)
: _name(name), status(UNDEFINED), isSimplified(FALSE) {}
ExpressionVariable::ExpressionVariable(const std::string &name, Double _value)
: _name(name), status(VALUE), value(_value), isSimplified(FALSE) {}
ExpressionVariable::ExpressionVariable(const std::string &name, const std::string &_text)
: _name(name), status(TEXT), text(_text), isSimplified(FALSE) {}
ExpressionVariable::ExpressionVariable(const std::string &name, const std::string &_text, const VariableList &_varList)
: _name(name), status(TEXT), text(_text), varList(_varList), isSimplified(FALSE) {}
ExpressionVariable::ExpressionVariable(const std::string &name, ExpressionPtr _expr, const VariableList &_varList)
: _name(name), status(EXPRESSION), expr(_expr), varList(_varList), isSimplified(FALSE)
{if(!expr) throw(Exception("ExpressionVariable: Null-Pointer."));}
ExpressionVariable::ExpressionVariable(const std::string &name, const std::shared_ptr<Func> &func)
: _name(name), status(FUNC), func(func), isSimplified(FALSE) {}
ExpressionVariable::ExpressionVariable(const ExpressionVariable &x)
: _name(x._name), status(x.status), text(x.text), value(x.value), varList(x.varList), func(x.func), isSimplified(x.isSimplified)
{if(x.expr) expr = x.expr->clone();}
/***********************************************/
std::string ExpressionVariable::getText() const
{
try
{
if(status == VALUE)
{
std::stringstream ss;
ss<<value;
return ss.str();
}
if(status == EXPRESSION)
return expr->string();
if(status == FUNC)
return "{func()}";
if(status == UNDEFINED)
return "{_undefined_}";
// status == TEXT
return text;
}
catch(std::exception &e)
{
GROOPS_RETHROW_EXTRA("Expression("+name()+"')", e)
}
}
/***********************************************/
void ExpressionVariable::parseVariableName()
{
try
{
if(status != TEXT)
throw(Exception("must contain a variable name ('name [= expr]')"));
Bool resolved = TRUE;
auto text_ = StringParser::parse(name(), this->text, varList, resolved);
// parse variable name
auto pos = text_.find('=');
auto nameStr = text_.substr(0, pos);
if(!resolved)
{
resolved = TRUE;
nameStr = StringParser::parse(name(), nameStr, varList, resolved);
}
if(!resolved)
throw(Exception("must contain a variable name ('name [= expr]')"));
Tokenizer token(nameStr);
token.get();
if(token.type!=Tokenizer::NAME)
throw(Exception("must contain a variable name ('name [= expr]')"));
_name = token.name; // set new name
token.get();
if(token.type != Tokenizer::END)
throw(Exception(token.infoString("unknown token")));
if(pos == std::string::npos) // only variable name
{
status = VALUE;
value = 0.;
return;
}
// expression after '='
this->text = text_.substr(pos+1, std::string::npos);
}
catch(std::exception &e)
{
GROOPS_RETHROW_EXTRA("Expression("+name()+" = '"+getText()+"')", e)
}
}
/***********************************************/
void ExpressionVariable::simplify(VariableList &varList)
{
try
{
if(isSimplified)
return;
isSimplified = TRUE;
VariableList varList2 = this->varList;
varList2 += varList;
if(status == TEXT)
{
Bool resolved = TRUE;
std::string text_ = StringParser::parse(name(), this->text, varList2, resolved);
if(!resolved)
return;
expr = Expression::parse(text_);
status = EXPRESSION;
}
if(status == EXPRESSION)
{
Bool resolved;
expr = expr->simplify(varList2, resolved);
if(resolved)
{
value = expr->evaluate(varList2);
status = VALUE;
this->varList.map.clear();
expr = nullptr;
return;
}
// store only used variables
this->varList.map.clear();
for(auto &var : varList2.map)
if(!varList.find(var.first) && var.second->isSimplified)
this->varList.map[var.first] = var.second;
}
}
catch(std::exception &e)
{
GROOPS_RETHROW_EXTRA("Expression("+name()+" = '"+getText()+"')", e)
}
}
/***********************************************/
ExpressionVariablePtr ExpressionVariable::derivative(const std::string &varName, const VariableList &varList) const
{
try
{
if(status == CIRCULAR)
throw(Exception("circular expression"));
if(status == UNDEFINED)
throw(Exception("undefined variable"));
if((status == VALUE) || (status == FUNC))
return std::make_shared<ExpressionVariable>(name(), 0.0);
if(status == EXPRESSION)
return std::make_shared<ExpressionVariable>(name(), expr->derivative(varName), this->varList);
// status == TEXT
VariableList varList2 = this->varList;
varList2 += varList;
Bool resolved = TRUE;
std::string text_ = StringParser::parse(name(), this->text, varList2, resolved);
if(!resolved)
throw(Exception("unresolved variables"));
return std::make_shared<ExpressionVariable>(name(), Expression::parse(text_)->derivative(varName), this->varList);
}
catch(std::exception &e)
{
GROOPS_RETHROW_EXTRA("Expression("+name()+" = '"+getText()+"')", e)
}
}
/***********************************************/
Double ExpressionVariable::evaluate(const VariableList &varList) const
{
try
{
if(status == VALUE)
return value;
if(status == FUNC)
{
if(!func)
throw(Exception("nullptr function"));
status = VALUE;
value = (*func)();
func = nullptr;
return value;
}
if(status == EXPRESSION)
{
if(this->varList.map.empty())
return expr->evaluate(varList);
VariableList varList2 = this->varList;
varList2 += varList;
return expr->evaluate(varList2);
}
if(status == TEXT)
{
VariableList varList2 = this->varList;
varList2 += varList;
Bool resolved = TRUE;
std::string text_ = StringParser::parse(name(), this->text, varList2, resolved);
if(!resolved)
throw(Exception("unresolved variables"));
const Status stat = status;
status = CIRCULAR; // prevent endless searching
Double d = Expression::parse(text_)->evaluate(varList2);
status = stat;
return d;
}
if(status == CIRCULAR)
throw(Exception("circular expression"));
//if(status == UNDEFINED)
throw(Exception("undefined variable"));
}
catch(std::exception &e)
{
GROOPS_RETHROW_EXTRA("Expression("+name()+" = '"+getText()+"')", e)
}
}
/***********************************************/
std::string ExpressionVariable::getParsedText(const VariableList &varList, Bool &resolved) const
{
try
{
if(status == TEXT)
{
status = CIRCULAR; // prevent endless searching
VariableList varList2 = this->varList;
varList2 += varList;
auto result = StringParser::parse(name(), text, varList2, resolved);
status = TEXT;
return result;
}
if(status == UNDEFINED)
return name();
if(status == FUNC)
{
if(!func)
throw(Exception("nullptr function"));
status = VALUE;
value = (*func)();
func = nullptr;
}
if(status == VALUE)
{
std::stringstream ss;
ss<<std::setprecision(std::numeric_limits<decltype(value)>::digits10)<<value;
return ss.str();
}
if(status == EXPRESSION)
return expr->string();
//if(status == CIRCULAR)
throw(Exception("is circular defined"));
}
catch(std::exception &e)
{
GROOPS_RETHROW_EXTRA("Expression("+name()+" = '"+getText()+"')", e)
}
}
/***********************************************/
/***********************************************/
VariableList &VariableList::operator+=(const VariableList &x)
{
for(const auto &var : x.map)
map[var.second->name()] = var.second;
return *this;
}
/***********************************************/
std::shared_ptr<const ExpressionVariable> VariableList::find(const std::string &name) const
{
try
{
auto iter = map.find(name);
if(iter != map.end())
return iter->second;
return ExpressionVariablePtr(nullptr);
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void VariableList::addVariable(ExpressionVariablePtr var)
{
try
{
map[var->name()] = std::make_shared<ExpressionVariable>(*var); // copy
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
ExpressionVariablePtr VariableList::getVariable(const std::string &name)
{
try
{
auto iter = map.find(name);
if(iter != map.end())
{
if(iter->second.use_count() > 1) // not threat save
iter->second = std::make_shared<ExpressionVariable>(*(iter->second)); // copy
return iter->second;
}
// new variable
ExpressionVariablePtr var = std::make_shared<ExpressionVariable>(name);
map[name] = var;
return var;
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void VariableList::setVariable(const std::string &name, Double value) {getVariable(name)->setValue(value);}
void VariableList::setVariable(const std::string &name, const std::string &text) {getVariable(name)->setValue(text);}
void VariableList::undefineVariable(const std::string &name) {getVariable(name)->setUndefined();}
/***********************************************/
|