readme.txt 191 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917
  1. C++ Mathematical Expression Toolkit Library Documentation
  2. Section 00 - Introduction
  3. Section 01 - Capabilities
  4. Section 02 - Example Expressions
  5. Section 03 - Copyright Notice
  6. Section 04 - Downloads & Updates
  7. Section 05 - Installation
  8. Section 06 - Compilation
  9. Section 07 - Compiler Compatibility
  10. Section 08 - Built-In Operations & Functions
  11. Section 09 - Fundamental Types
  12. Section 10 - Components
  13. Section 11 - Compilation Options
  14. Section 12 - Expression Structures
  15. Section 13 - Variable, Vector & String Definition
  16. Section 14 - Vector Processing
  17. Section 15 - User Defined Functions
  18. Section 16 - Expression Dependents
  19. Section 17 - Hierarchies Of Symbol Tables
  20. Section 18 - Unknown Unknowns
  21. Section 19 - Enabling & Disabling Features
  22. Section 20 - Expression Return Values
  23. Section 21 - Compilation Errors
  24. Section 22 - Runtime Library Packages
  25. Section 23 - Helpers & Utils
  26. Section 24 - Benchmarking
  27. Section 25 - Exprtk Notes
  28. Section 26 - Simple Exprtk Example
  29. Section 27 - Build Options
  30. Section 28 - Files
  31. Section 29 - Language Structure
  32. [SECTION 00 - INTRODUCTION]
  33. The C++ Mathematical Expression Toolkit Library (ExprTk) is a simple
  34. to use, easy to integrate and extremely efficient run-time
  35. mathematical expression parsing and evaluation engine. The parsing
  36. engine supports numerous forms of functional and logic processing
  37. semantics and is easily extensible.
  38. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. [SECTION 01 - CAPABILITIES]
  40. The ExprTk expression evaluator supports the following fundamental
  41. arithmetic operations, functions and processes:
  42. (00) Types: Scalar, Vector, String
  43. (01) Basic operators: +, -, *, /, %, ^
  44. (02) Assignment: :=, +=, -=, *=, /=, %=
  45. (03) Equalities &
  46. Inequalities: =, ==, <>, !=, <, <=, >, >=
  47. (04) Logic operators: and, mand, mor, nand, nor, not, or, shl, shr,
  48. xnor, xor, true, false
  49. (05) Functions: abs, avg, ceil, clamp, equal, erf, erfc, exp,
  50. expm1, floor, frac, log, log10, log1p, log2,
  51. logn, max, min, mul, ncdf, nequal, root,
  52. round, roundn, sgn, sqrt, sum, swap, trunc
  53. (06) Trigonometry: acos, acosh, asin, asinh, atan, atanh, atan2,
  54. cos, cosh, cot, csc, sec, sin, sinc, sinh,
  55. tan, tanh, hypot, rad2deg, deg2grad, deg2rad,
  56. grad2deg
  57. (07) Control
  58. structures: if-then-else, ternary conditional, switch-case,
  59. return-statement
  60. (08) Loop statements: while, for, repeat-until, break, continue
  61. (09) String
  62. processing: in, like, ilike, concatenation
  63. (10) Optimisations: constant-folding, simple strength reduction and
  64. dead code elimination
  65. (11) Calculus: numerical integration and differentiation
  66. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. [SECTION 02 - EXAMPLE EXPRESSIONS]
  68. The following is a short listing of infix format based mathematical
  69. expressions that can be parsed and evaluated using the ExprTk library.
  70. (01) sqrt(1 - (3 / x^2))
  71. (02) clamp(-1, sin(2 * pi * x) + cos(y / 2 * pi), +1)
  72. (03) sin(2.34e-3 * x)
  73. (04) if(((x[2] + 2) == 3) and ((y + 5) <= 9),1 + w, 2 / z)
  74. (05) inrange(-2,m,+2) == if(({-2 <= m} and [m <= +2]),1,0)
  75. (06) ({1/1}*[1/2]+(1/3))-{1/4}^[1/5]+(1/6)-({1/7}+[1/8]*(1/9))
  76. (07) a * exp(2.2 / 3.3 * t) + c
  77. (08) z := x + sin(2.567 * pi / y)
  78. (09) u := 2.123 * {pi * z} / (w := x + cos(y / pi))
  79. (10) 2x + 3y + 4z + 5w == 2 * x + 3 * y + 4 * z + 5 * w
  80. (11) 3(x + y) / 2.9 + 1.234e+12 == 3 * (x + y) / 2.9 + 1.234e+12
  81. (12) (x + y)3.3 + 1 / 4.5 == [x + y] * 3.3 + 1 / 4.5
  82. (13) (x + y[i])z + 1.1 / 2.7 == (x + y[i]) * z + 1.1 / 2.7
  83. (14) (sin(x / pi) cos(2y) + 1) == (sin(x / pi) * cos(2 * y) + 1)
  84. (15) 75x^17 + 25.1x^5 - 35x^4 - 15.2x^3 + 40x^2 - 15.3x + 1
  85. (16) (avg(x,y) <= x + y ? x - y : x * y) + 2.345 * pi / x
  86. (17) while (x <= 100) { x -= 1; }
  87. (18) x <= 'abc123' and (y in 'AString') or ('1x2y3z' != z)
  88. (19) ((x + 'abc') like '*123*') or ('a123b' ilike y)
  89. (20) sgn(+1.2^3.4z / -5.6y) <= {-7.8^9 / -10.11x }
  90. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91. [SECTION 03 - COPYRIGHT NOTICE]
  92. Free use of the C++ Mathematical Expression Toolkit Library is
  93. permitted under the guidelines and in accordance with the most current
  94. version of the MIT License.
  95. http://www.opensource.org/licenses/MIT
  96. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. [SECTION 04 - DOWNLOADS & UPDATES]
  98. The most recent version of the C++ Mathematical Expression Toolkit
  99. Library including all updates and tests can be found at the following
  100. locations:
  101. (a) Download: http://www.partow.net/programming/exprtk/index.html
  102. (b) Repository: https://github.com/ArashPartow/exprtk
  103. https://github.com/ArashPartow/exprtk-extras
  104. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105. [SECTION 05 - INSTALLATION]
  106. The header file exprtk.hpp should be placed in a project or system
  107. include path (e.g: /usr/include/).
  108. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  109. [SECTION 06 - COMPILATION]
  110. (a) For a complete build: make clean all
  111. (b) For a PGO build: make clean pgo
  112. (c) To strip executables: make strip_bin
  113. (d) Execute valgrind check: make valgrind_check
  114. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115. [SECTION 07 - COMPILER COMPATIBILITY]
  116. ExprTk has been built error and warning free using the following set
  117. of C++ compilers:
  118. (*) GNU Compiler Collection (3.5+)
  119. (*) Intel C++ Compiler (8.x+)
  120. (*) Clang/LLVM (1.1+)
  121. (*) PGI C++ (10.x+)
  122. (*) Microsoft Visual Studio C++ Compiler (8.1+)
  123. (*) IBM XL C/C++ (9.x+)
  124. (*) C++ Builder (XE4+)
  125. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  126. [SECTION 08 - BUILT-IN OPERATIONS & FUNCTIONS]
  127. (0) Arithmetic & Assignment Operators
  128. +----------+---------------------------------------------------------+
  129. | OPERATOR | DEFINITION |
  130. +----------+---------------------------------------------------------+
  131. | + | Addition between x and y. (eg: x + y) |
  132. +----------+---------------------------------------------------------+
  133. | - | Subtraction between x and y. (eg: x - y) |
  134. +----------+---------------------------------------------------------+
  135. | * | Multiplication between x and y. (eg: x * y) |
  136. +----------+---------------------------------------------------------+
  137. | / | Division between x and y. (eg: x / y) |
  138. +----------+---------------------------------------------------------+
  139. | % | Modulus of x with respect to y. (eg: x % y) |
  140. +----------+---------------------------------------------------------+
  141. | ^ | x to the power of y. (eg: x ^ y) |
  142. +----------+---------------------------------------------------------+
  143. | := | Assign the value of x to y. Where y is either a variable|
  144. | | or vector type. (eg: y := x) |
  145. +----------+---------------------------------------------------------+
  146. | += | Increment x by the value of the expression on the right |
  147. | | hand side. Where x is either a variable or vector type. |
  148. | | (eg: x += abs(y - z)) |
  149. +----------+---------------------------------------------------------+
  150. | -= | Decrement x by the value of the expression on the right |
  151. | | hand side. Where x is either a variable or vector type. |
  152. | | (eg: x[i] -= abs(y + z)) |
  153. +----------+---------------------------------------------------------+
  154. | *= | Assign the multiplication of x by the value of the |
  155. | | expression on the righthand side to x. Where x is either|
  156. | | a variable or vector type. |
  157. | | (eg: x *= abs(y / z)) |
  158. +----------+---------------------------------------------------------+
  159. | /= | Assign the division of x by the value of the expression |
  160. | | on the right-hand side to x. Where x is either a |
  161. | | variable or vector type. (eg: x[i + j] /= abs(y * z)) |
  162. +----------+---------------------------------------------------------+
  163. | %= | Assign x modulo the value of the expression on the right|
  164. | | hand side to x. Where x is either a variable or vector |
  165. | | type. (eg: x[2] %= y ^ 2) |
  166. +----------+---------------------------------------------------------+
  167. (1) Equalities & Inequalities
  168. +----------+---------------------------------------------------------+
  169. | OPERATOR | DEFINITION |
  170. +----------+---------------------------------------------------------+
  171. | == or = | True only if x is strictly equal to y. (eg: x == y) |
  172. +----------+---------------------------------------------------------+
  173. | <> or != | True only if x does not equal y. (eg: x <> y or x != y) |
  174. +----------+---------------------------------------------------------+
  175. | < | True only if x is less than y. (eg: x < y) |
  176. +----------+---------------------------------------------------------+
  177. | <= | True only if x is less than or equal to y. (eg: x <= y) |
  178. +----------+---------------------------------------------------------+
  179. | > | True only if x is greater than y. (eg: x > y) |
  180. +----------+---------------------------------------------------------+
  181. | >= | True only if x greater than or equal to y. (eg: x >= y) |
  182. +----------+---------------------------------------------------------+
  183. (2) Boolean Operations
  184. +----------+---------------------------------------------------------+
  185. | OPERATOR | DEFINITION |
  186. +----------+---------------------------------------------------------+
  187. | true | True state or any value other than zero (typically 1). |
  188. +----------+---------------------------------------------------------+
  189. | false | False state, value of exactly zero. |
  190. +----------+---------------------------------------------------------+
  191. | and | Logical AND, True only if x and y are both true. |
  192. | | (eg: x and y) |
  193. +----------+---------------------------------------------------------+
  194. | mand | Multi-input logical AND, True only if all inputs are |
  195. | | true. Left to right short-circuiting of expressions. |
  196. | | (eg: mand(x > y, z < w, u or v, w and x)) |
  197. +----------+---------------------------------------------------------+
  198. | mor | Multi-input logical OR, True if at least one of the |
  199. | | inputs are true. Left to right short-circuiting of |
  200. | | expressions. (eg: mor(x > y, z < w, u or v, w and x)) |
  201. +----------+---------------------------------------------------------+
  202. | nand | Logical NAND, True only if either x or y is false. |
  203. | | (eg: x nand y) |
  204. +----------+---------------------------------------------------------+
  205. | nor | Logical NOR, True only if the result of x or y is false |
  206. | | (eg: x nor y) |
  207. +----------+---------------------------------------------------------+
  208. | not | Logical NOT, Negate the logical sense of the input. |
  209. | | (eg: not(x and y) == x nand y) |
  210. +----------+---------------------------------------------------------+
  211. | or | Logical OR, True if either x or y is true. (eg: x or y) |
  212. +----------+---------------------------------------------------------+
  213. | xor | Logical XOR, True only if the logical states of x and y |
  214. | | differ. (eg: x xor y) |
  215. +----------+---------------------------------------------------------+
  216. | xnor | Logical XNOR, True iff the biconditional of x and y is |
  217. | | satisfied. (eg: x xnor y) |
  218. +----------+---------------------------------------------------------+
  219. | & | Similar to AND but with left to right expression short |
  220. | | circuiting optimisation. (eg: (x & y) == (y and x)) |
  221. +----------+---------------------------------------------------------+
  222. | | | Similar to OR but with left to right expression short |
  223. | | circuiting optimisation. (eg: (x | y) == (y or x)) |
  224. +----------+---------------------------------------------------------+
  225. (3) General Purpose Functions
  226. +----------+---------------------------------------------------------+
  227. | FUNCTION | DEFINITION |
  228. +----------+---------------------------------------------------------+
  229. | abs | Absolute value of x. (eg: abs(x)) |
  230. +----------+---------------------------------------------------------+
  231. | avg | Average of all the inputs. |
  232. | | (eg: avg(x,y,z,w,u,v) == (x + y + z + w + u + v) / 6) |
  233. +----------+---------------------------------------------------------+
  234. | ceil | Smallest integer that is greater than or equal to x. |
  235. +----------+---------------------------------------------------------+
  236. | clamp | Clamp x in range between r0 and r1, where r0 < r1. |
  237. | | (eg: clamp(r0,x,r1)) |
  238. +----------+---------------------------------------------------------+
  239. | equal | Equality test between x and y using normalised epsilon |
  240. +----------+---------------------------------------------------------+
  241. | erf | Error function of x. (eg: erf(x)) |
  242. +----------+---------------------------------------------------------+
  243. | erfc | Complimentary error function of x. (eg: erfc(x)) |
  244. +----------+---------------------------------------------------------+
  245. | exp | e to the power of x. (eg: exp(x)) |
  246. +----------+---------------------------------------------------------+
  247. | expm1 | e to the power of x minus 1, where x is very small. |
  248. | | (eg: expm1(x)) |
  249. +----------+---------------------------------------------------------+
  250. | floor | Largest integer that is less than or equal to x. |
  251. | | (eg: floor(x)) |
  252. +----------+---------------------------------------------------------+
  253. | frac | Fractional portion of x. (eg: frac(x)) |
  254. +----------+---------------------------------------------------------+
  255. | hypot | Hypotenuse of x and y (eg: hypot(x,y) = sqrt(x*x + y*y))|
  256. +----------+---------------------------------------------------------+
  257. | iclamp | Inverse-clamp x outside of the range r0 and r1. Where |
  258. | | r0 < r1. If x is within the range it will snap to the |
  259. | | closest bound. (eg: iclamp(r0,x,r1) |
  260. +----------+---------------------------------------------------------+
  261. | inrange | In-range returns 'true' when x is within the range r0 |
  262. | | and r1. Where r0 < r1. (eg: inrange(r0,x,r1) |
  263. +----------+---------------------------------------------------------+
  264. | log | Natural logarithm of x. (eg: log(x)) |
  265. +----------+---------------------------------------------------------+
  266. | log10 | Base 10 logarithm of x. (eg: log10(x)) |
  267. +----------+---------------------------------------------------------+
  268. | log1p | Natural logarithm of 1 + x, where x is very small. |
  269. | | (eg: log1p(x)) |
  270. +----------+---------------------------------------------------------+
  271. | log2 | Base 2 logarithm of x. (eg: log2(x)) |
  272. +----------+---------------------------------------------------------+
  273. | logn | Base N logarithm of x. where n is a positive integer. |
  274. | | (eg: logn(x,8)) |
  275. +----------+---------------------------------------------------------+
  276. | max | Largest value of all the inputs. (eg: max(x,y,z,w,u,v)) |
  277. +----------+---------------------------------------------------------+
  278. | min | Smallest value of all the inputs. (eg: min(x,y,z,w,u)) |
  279. +----------+---------------------------------------------------------+
  280. | mul | Product of all the inputs. |
  281. | | (eg: mul(x,y,z,w,u,v,t) == (x * y * z * w * u * v * t)) |
  282. +----------+---------------------------------------------------------+
  283. | ncdf | Normal cumulative distribution function. (eg: ncdf(x)) |
  284. +----------+---------------------------------------------------------+
  285. | nequal | Not-equal test between x and y using normalised epsilon |
  286. +----------+---------------------------------------------------------+
  287. | pow | x to the power of y. (eg: pow(x,y) == x ^ y) |
  288. +----------+---------------------------------------------------------+
  289. | root | Nth-Root of x. where n is a positive integer. |
  290. | | (eg: root(x,3) == x^(1/3)) |
  291. +----------+---------------------------------------------------------+
  292. | round | Round x to the nearest integer. (eg: round(x)) |
  293. +----------+---------------------------------------------------------+
  294. | roundn | Round x to n decimal places (eg: roundn(x,3)) |
  295. | | where n > 0 and is an integer. |
  296. | | (eg: roundn(1.2345678,4) == 1.2346) |
  297. +----------+---------------------------------------------------------+
  298. | sgn | Sign of x, -1 where x < 0, +1 where x > 0, else zero. |
  299. | | (eg: sgn(x)) |
  300. +----------+---------------------------------------------------------+
  301. | sqrt | Square root of x, where x >= 0. (eg: sqrt(x)) |
  302. +----------+---------------------------------------------------------+
  303. | sum | Sum of all the inputs. |
  304. | | (eg: sum(x,y,z,w,u,v,t) == (x + y + z + w + u + v + t)) |
  305. +----------+---------------------------------------------------------+
  306. | swap | Swap the values of the variables x and y and return the |
  307. | <=> | current value of y. (eg: swap(x,y) or x <=> y) |
  308. +----------+---------------------------------------------------------+
  309. | trunc | Integer portion of x. (eg: trunc(x)) |
  310. +----------+---------------------------------------------------------+
  311. (4) Trigonometry Functions
  312. +----------+---------------------------------------------------------+
  313. | FUNCTION | DEFINITION |
  314. +----------+---------------------------------------------------------+
  315. | acos | Arc cosine of x expressed in radians. Interval [-1,+1] |
  316. | | (eg: acos(x)) |
  317. +----------+---------------------------------------------------------+
  318. | acosh | Inverse hyperbolic cosine of x expressed in radians. |
  319. | | (eg: acosh(x)) |
  320. +----------+---------------------------------------------------------+
  321. | asin | Arc sine of x expressed in radians. Interval [-1,+1] |
  322. | | (eg: asin(x)) |
  323. +----------+---------------------------------------------------------+
  324. | asinh | Inverse hyperbolic sine of x expressed in radians. |
  325. | | (eg: asinh(x)) |
  326. +----------+---------------------------------------------------------+
  327. | atan | Arc tangent of x expressed in radians. Interval [-1,+1] |
  328. | | (eg: atan(x)) |
  329. +----------+---------------------------------------------------------+
  330. | atan2 | Arc tangent of (x / y) expressed in radians. [-pi,+pi] |
  331. | | eg: atan2(x,y) |
  332. +----------+---------------------------------------------------------+
  333. | atanh | Inverse hyperbolic tangent of x expressed in radians. |
  334. | | (eg: atanh(x)) |
  335. +----------+---------------------------------------------------------+
  336. | cos | Cosine of x. (eg: cos(x)) |
  337. +----------+---------------------------------------------------------+
  338. | cosh | Hyperbolic cosine of x. (eg: cosh(x)) |
  339. +----------+---------------------------------------------------------+
  340. | cot | Cotangent of x. (eg: cot(x)) |
  341. +----------+---------------------------------------------------------+
  342. | csc | Cosecant of x. (eg: csc(x)) |
  343. +----------+---------------------------------------------------------+
  344. | sec | Secant of x. (eg: sec(x)) |
  345. +----------+---------------------------------------------------------+
  346. | sin | Sine of x. (eg: sin(x)) |
  347. +----------+---------------------------------------------------------+
  348. | sinc | Sine cardinal of x. (eg: sinc(x)) |
  349. +----------+---------------------------------------------------------+
  350. | sinh | Hyperbolic sine of x. (eg: sinh(x)) |
  351. +----------+---------------------------------------------------------+
  352. | tan | Tangent of x. (eg: tan(x)) |
  353. +----------+---------------------------------------------------------+
  354. | tanh | Hyperbolic tangent of x. (eg: tanh(x)) |
  355. +----------+---------------------------------------------------------+
  356. | deg2rad | Convert x from degrees to radians. (eg: deg2rad(x)) |
  357. +----------+---------------------------------------------------------+
  358. | deg2grad | Convert x from degrees to gradians. (eg: deg2grad(x)) |
  359. +----------+---------------------------------------------------------+
  360. | rad2deg | Convert x from radians to degrees. (eg: rad2deg(x)) |
  361. +----------+---------------------------------------------------------+
  362. | grad2deg | Convert x from gradians to degrees. (eg: grad2deg(x)) |
  363. +----------+---------------------------------------------------------+
  364. (5) String Processing
  365. +----------+---------------------------------------------------------+
  366. | FUNCTION | DEFINITION |
  367. +----------+---------------------------------------------------------+
  368. | = , == | All common equality/inequality operators are applicable |
  369. | !=, <> | to strings and are applied in a case sensitive manner. |
  370. | <=, >= | In the following example x, y and z are of type string. |
  371. | < , > | (eg: not((x <= 'AbC') and ('1x2y3z' <> y)) or (z == x) |
  372. +----------+---------------------------------------------------------+
  373. | in | True only if x is a substring of y. |
  374. | | (eg: x in y or 'abc' in 'abcdefgh') |
  375. +----------+---------------------------------------------------------+
  376. | like | True only if the string x matches the pattern y. |
  377. | | Available wildcard characters are '*' and '?' denoting |
  378. | | zero or more and zero or one matches respectively. |
  379. | | (eg: x like y or 'abcdefgh' like 'a?d*h') |
  380. +----------+---------------------------------------------------------+
  381. | ilike | True only if the string x matches the pattern y in a |
  382. | | case insensitive manner. Available wildcard characters |
  383. | | are '*' and '?' denoting zero or more and zero or one |
  384. | | matches respectively. |
  385. | | (eg: x ilike y or 'a1B2c3D4e5F6g7H' ilike 'a?d*h') |
  386. +----------+---------------------------------------------------------+
  387. | [r0:r1] | The closed interval [r0,r1] of the specified string. |
  388. | | eg: Given a string x with a value of 'abcdefgh' then: |
  389. | | 1. x[1:4] == 'bcde' |
  390. | | 2. x[ :5] == x[:10 / 2] == 'abcdef' |
  391. | | 3. x[2 + 1: ] == x[3:] =='defgh' |
  392. | | 4. x[ : ] == x[:] == 'abcdefgh' |
  393. | | 5. x[4/2:3+2] == x[2:5] == 'cdef' |
  394. | | |
  395. | | Note: Both r0 and r1 are assumed to be integers, where |
  396. | | r0 <= r1. They may also be the result of an expression, |
  397. | | in the event they have fractional components truncation |
  398. | | will be performed. (eg: 1.67 --> 1) |
  399. +----------+---------------------------------------------------------+
  400. | := | Assign the value of x to y. Where y is a mutable string |
  401. | | or string range and x is either a string or a string |
  402. | | range. eg: |
  403. | | 1. y := x |
  404. | | 2. y := 'abc' |
  405. | | 3. y := x[:i + j] |
  406. | | 4. y := '0123456789'[2:7] |
  407. | | 5. y := '0123456789'[2i + 1:7] |
  408. | | 6. y := (x := '0123456789'[2:7]) |
  409. | | 7. y[i:j] := x |
  410. | | 8. y[i:j] := (x + 'abcdefg'[8 / 4:5])[m:n] |
  411. | | |
  412. | | Note: For options 7 and 8 the shorter of the two ranges |
  413. | | will denote the number characters that are to be copied.|
  414. +----------+---------------------------------------------------------+
  415. | + | Concatenation of x and y. Where x and y are strings or |
  416. | | string ranges. eg |
  417. | | 1. x + y |
  418. | | 2. x + 'abc' |
  419. | | 3. x + y[:i + j] |
  420. | | 4. x[i:j] + y[2:3] + '0123456789'[2:7] |
  421. | | 5. 'abc' + x + y |
  422. | | 6. 'abc' + '1234567' |
  423. | | 7. (x + 'a1B2c3D4' + y)[i:2j] |
  424. +----------+---------------------------------------------------------+
  425. | += | Append to x the value of y. Where x is a mutable string |
  426. | | and y is either a string or a string range. eg: |
  427. | | 1. x += y |
  428. | | 2. x += 'abc' |
  429. | | 3. x += y[:i + j] + 'abc' |
  430. | | 4. x += '0123456789'[2:7] |
  431. +----------+---------------------------------------------------------+
  432. | <=> | Swap the values of x and y. Where x and y are mutable |
  433. | | strings. (eg: x <=> y) |
  434. +----------+---------------------------------------------------------+
  435. | [] | The string size operator returns the size of the string |
  436. | | being actioned. |
  437. | | eg: |
  438. | | 1. 'abc'[] == 3 |
  439. | | 2. var max_str_length := max(s0[],s1[],s2[],s3[]) |
  440. | | 3. ('abc' + 'xyz')[] == 6 |
  441. | | 4. (('abc' + 'xyz')[1:4])[] == 4 |
  442. +----------+---------------------------------------------------------+
  443. (6) Control Structures
  444. +----------+---------------------------------------------------------+
  445. |STRUCTURE | DEFINITION |
  446. +----------+---------------------------------------------------------+
  447. | if | If x is true then return y else return z. |
  448. | | eg: |
  449. | | 1. if (x, y, z) |
  450. | | 2. if ((x + 1) > 2y, z + 1, w / v) |
  451. | | 3. if (x > y) z; |
  452. | | 4. if (x <= 2*y) { z + w }; |
  453. +----------+---------------------------------------------------------+
  454. | if-else | The if-else/else-if statement. Subject to the condition |
  455. | | branch the statement will return either the value of the|
  456. | | consequent or the alternative branch. |
  457. | | eg: |
  458. | | 1. if (x > y) z; else w; |
  459. | | 2. if (x > y) z; else if (w != u) v; |
  460. | | 3. if (x < y) { z; w + 1; } else u; |
  461. | | 4. if ((x != y) and (z > w)) |
  462. | | { |
  463. | | y := sin(x) / u; |
  464. | | z := w + 1; |
  465. | | } |
  466. | | else if (x > (z + 1)) |
  467. | | { |
  468. | | w := abs (x - y) + z; |
  469. | | u := (x + 1) > 2y ? 2u : 3u; |
  470. | | } |
  471. +----------+---------------------------------------------------------+
  472. | switch | The first true case condition that is encountered will |
  473. | | determine the result of the switch. If none of the case |
  474. | | conditions hold true, the default action is assumed as |
  475. | | the final return value. This is sometimes also known as |
  476. | | a multi-way branch mechanism. |
  477. | | eg: |
  478. | | switch |
  479. | | { |
  480. | | case x > (y + z) : 2 * x / abs(y - z); |
  481. | | case x < 3 : sin(x + y); |
  482. | | default : 1 + x; |
  483. | | } |
  484. +----------+---------------------------------------------------------+
  485. | while | The structure will repeatedly evaluate the internal |
  486. | | statement(s) 'while' the condition is true. The final |
  487. | | statement in the final iteration will be used as the |
  488. | | return value of the loop. |
  489. | | eg: |
  490. | | while ((x -= 1) > 0) |
  491. | | { |
  492. | | y := x + z; |
  493. | | w := u + y; |
  494. | | } |
  495. +----------+---------------------------------------------------------+
  496. | repeat/ | The structure will repeatedly evaluate the internal |
  497. | until | statement(s) 'until' the condition is true. The final |
  498. | | statement in the final iteration will be used as the |
  499. | | return value of the loop. |
  500. | | eg: |
  501. | | repeat |
  502. | | y := x + z; |
  503. | | w := u + y; |
  504. | | until ((x += 1) > 100) |
  505. +----------+---------------------------------------------------------+
  506. | for | The structure will repeatedly evaluate the internal |
  507. | | statement(s) while the condition is true. On each loop |
  508. | | iteration, an 'incrementing' expression is evaluated. |
  509. | | The conditional is mandatory whereas the initialiser |
  510. | | and incrementing expressions are optional. |
  511. | | eg: |
  512. | | for (var x := 0; (x < n) and (x != y); x += 1) |
  513. | | { |
  514. | | y := y + x / 2 - z; |
  515. | | w := u + y; |
  516. | | } |
  517. +----------+---------------------------------------------------------+
  518. | break | Break terminates the execution of the nearest enclosed |
  519. | break[] | loop, allowing for the execution to continue on external|
  520. | | to the loop. The default break statement will set the |
  521. | | return value of the loop to NaN, where as the return |
  522. | | based form will set the value to that of the break |
  523. | | expression. |
  524. | | eg: |
  525. | | while ((i += 1) < 10) |
  526. | | { |
  527. | | if (i < 5) |
  528. | | j -= i + 2; |
  529. | | else if (i % 2 == 0) |
  530. | | break; |
  531. | | else |
  532. | | break[2i + 3]; |
  533. | | } |
  534. +----------+---------------------------------------------------------+
  535. | continue | Continue results in the remaining portion of the nearest|
  536. | | enclosing loop body to be skipped. |
  537. | | eg: |
  538. | | for (var i := 0; i < 10; i += 1) |
  539. | | { |
  540. | | if (i < 5) |
  541. | | continue; |
  542. | | j -= i + 2; |
  543. | | } |
  544. +----------+---------------------------------------------------------+
  545. | return | Return immediately from within the current expression. |
  546. | | With the option of passing back a variable number of |
  547. | | values (scalar, vector or string). eg: |
  548. | | 1. return [1]; |
  549. | | 2. return [x, 'abx']; |
  550. | | 3. return [x, x + y,'abx']; |
  551. | | 4. return []; |
  552. | | 5. if (x < y) |
  553. | | return [x, x - y, 'result-set1', 123.456]; |
  554. | | else |
  555. | | return [y, x + y, 'result-set2']; |
  556. +----------+---------------------------------------------------------+
  557. | ?: | Ternary conditional statement, similar to that of the |
  558. | | above denoted if-statement. |
  559. | | eg: |
  560. | | 1. x ? y : z |
  561. | | 2. x + 1 > 2y ? z + 1 : (w / v) |
  562. | | 3. min(x,y) > z ? (x < y + 1) ? x : y : (w * v) |
  563. +----------+---------------------------------------------------------+
  564. | ~ | Evaluate each sub-expression, then return as the result |
  565. | | the value of the last sub-expression. This is sometimes |
  566. | | known as multiple sequence point evaluation. |
  567. | | eg: |
  568. | | ~(i := x + 1, j := y / z, k := sin(w/u)) == (sin(w/u))) |
  569. | | ~{i := x + 1; j := y / z; k := sin(w/u)} == (sin(w/u))) |
  570. +----------+---------------------------------------------------------+
  571. | [*] | Evaluate any consequent for which its case statement is |
  572. | | true. The return value will be either zero or the result|
  573. | | of the last consequent to have been evaluated. |
  574. | | eg: |
  575. | | [*] |
  576. | | { |
  577. | | case (x + 1) > (y - 2) : x := z / 2 + sin(y / pi); |
  578. | | case (x + 2) < abs(y + 3) : w / 4 + min(5y,9); |
  579. | | case (x + 3) == (y * 4) : y := abs(z / 6) + 7y; |
  580. | | } |
  581. +----------+---------------------------------------------------------+
  582. | [] | The vector size operator returns the size of the vector |
  583. | | being actioned. |
  584. | | eg: |
  585. | | 1. v[] |
  586. | | 2. max_size := max(v0[],v1[],v2[],v3[]) |
  587. +----------+---------------------------------------------------------+
  588. Note: In the tables above, the symbols x, y, z, w, u and v where
  589. appropriate may represent any of one the following:
  590. 1. Literal numeric/string value
  591. 2. A variable
  592. 3. A vector element
  593. 4. A vector
  594. 5. A string
  595. 6. An expression comprised of [1], [2] or [3] (eg: 2 + x / vec[3])
  596. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  597. [SECTION 09 - FUNDAMENTAL TYPES]
  598. ExprTk supports three fundamental types which can be used freely in
  599. expressions. The types are as follows:
  600. (1) Scalar
  601. (2) Vector
  602. (3) String
  603. (1) Scalar Type
  604. The scalar type is a singular numeric value. The underlying type is
  605. that used to specialise the ExprTk components (float, double, long
  606. double, MPFR et al).
  607. (2) Vector Type
  608. The vector type is a fixed size sequence of contiguous scalar values.
  609. A vector can be indexed resulting in a scalar value. Operations
  610. between a vector and scalar will result in a vector with a size equal
  611. to that of the original vector, whereas operations between vectors
  612. will result in a vector of size equal to that of the smaller of the
  613. two. In both mentioned cases, the operations will occur element-wise.
  614. (3) String Type
  615. The string type is a variable length sequence of 8-bit chars. Strings
  616. can be assigned and concatenated to one another, they can also be
  617. manipulated via sub-ranges using the range definition syntax. Strings
  618. however can not interact with scalar or vector types.
  619. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  620. [SECTION 10 - COMPONENTS]
  621. There are three primary components, that are specialised upon a given
  622. numeric type, which make up the core of ExprTk. The components are as
  623. follows:
  624. (1) Symbol Table exprtk::symbol_table<NumericType>
  625. (2) Expression exprtk::expression<NumericType>
  626. (3) Parser exprtk::parser<NumericType>
  627. (1) Symbol Table
  628. A structure that is used to store references to variables, constants
  629. and functions that are to be used within expressions. Furthermore in
  630. the context of composited recursive functions the symbol table can
  631. also be thought of as a simple representation of a stack specific for
  632. the expression(s) that reference it. The following is a list of the
  633. types a symbol table can handle:
  634. (a) Numeric variables
  635. (b) Numeric constants
  636. (c) Numeric vector elements
  637. (d) String variables
  638. (e) String constants
  639. (f) Functions
  640. (g) Vararg functions
  641. During the compilation process if an expression is found to require
  642. any of the elements noted above, the expression's associated
  643. symbol_table will be queried for the element and if present a
  644. reference to the element will be embedded within the expression's AST.
  645. This allows for the original element to be modified independently of
  646. the expression instance and to also allow the expression to be
  647. evaluated using the current value of the element.
  648. Note: Any variable reference provided to a given symbol_table
  649. instance, must have a life time at least as long as the life-time of
  650. the symbol_table instance. In the event the variable reference is
  651. invalidated before the symbol_table or any dependent expression
  652. instances have been destructed, then any associated expression
  653. evaluations or variable referencing via the symbol_table instance will
  654. result in undefined behaviour.
  655. The following bit of code instantiates a symbol_table and expression
  656. instance, then proceeds to demonstrate various ways in which
  657. references to variables can be added to the symbol_table, and how
  658. those references are subsequently invalidated resulting in various
  659. forms of undefined behaviour.
  660. typedef exprtk::symbol_table<double> symbol_table_t;
  661. symbol_table_t symbol_table;
  662. expression_t expression;
  663. {
  664. double x = 123.4567;
  665. symbol_table.add_variable("x", x);
  666. } // Reference to variable x has been invalidated
  667. std::deque<double> y {1.1, 2.2, 3.3};
  668. symbol_table.add_variable("y", y.back());
  669. y.pop_back(); // Reference to variable y has been invalidated
  670. std::vector<double> z {4.4, 5.5, 6.6};
  671. symbol_table.add_variable("z", z.front());
  672. z.erase(z.begin());
  673. // Reference to variable z has been invalidated
  674. double* w = new double(123.456);
  675. symbol_table.add_variable("w", *w);
  676. delete w; // Reference to variable w has been invalidated
  677. const std::string expression_str = "x + y / z * w";
  678. // Compilation of expression will succeed
  679. parser.compile(expression_str,expression);
  680. expression.value();
  681. // Evaluation will result in undefined behaviour
  682. symbol_table.get_variable("x")->ref() = 135.791;
  683. // Assignment will result in undefined behaviour
  684. The example below demonstrates the relationship between variables,
  685. symbol_table and expression. Note the variables are modified as they
  686. normally would in a program, and when the expression is evaluated the
  687. current values assigned to the variables will be used.
  688. typedef exprtk::symbol_table<double> symbol_table_t;
  689. typedef exprtk::expression<double> expression_t;
  690. typedef exprtk::parser<double> parser_t;
  691. symbol_table_t symbol_table;
  692. expression_t expression;
  693. parser_t parser;
  694. double x = 0;
  695. double y = 0;
  696. std::string expression_string = "x * y + 3";
  697. symbol_table.add_variable("x",x);
  698. symbol_table.add_variable("y",y);
  699. expression.register_symbol_table(symbol_table);
  700. parser.compile(expression_string,expression);
  701. x = 1.0;
  702. y = 2.0;
  703. expression.value(); // 1 * 2 + 3
  704. x = 3.7;
  705. expression.value(); // 3.7 * 2 + 3
  706. y = -9.0;
  707. expression.value(); // 3.7 * -9 + 3
  708. // 'x * -9 + 3' for x in range of [0,100) in steps of 0.0001
  709. for (x = 0.0; x < 100.0; x += 0.0001)
  710. {
  711. expression.value(); // x * -9 + 3
  712. }
  713. Note: It is possible to register multiple symbol_tables with a single
  714. expression object. In the event an expression has multiple symbol
  715. tables, and where there exists conflicts between symbols, the
  716. compilation stage will resolve the conflicts based on the order of
  717. registration of the symbol_tables to the expression. For a more
  718. expansive discussion please review section [17 - Hierarchies Of
  719. Symbol Tables]
  720. typedef exprtk::symbol_table<double> symbol_table_t;
  721. typedef exprtk::expression<double> expression_t;
  722. typedef exprtk::parser<double> parser_t;
  723. symbol_table_t symbol_table0;
  724. symbol_table_t symbol_table1;
  725. expression_t expression;
  726. parser_t parser;
  727. double x0 = 123.0;
  728. double x1 = 678.0;
  729. std::string expression_string = "x + 1";
  730. symbol_table0.add_variable("x",x0);
  731. symbol_table1.add_variable("x",x1);
  732. expression.register_symbol_table(symbol_table0);
  733. expression.register_symbol_table(symbol_table1);
  734. parser.compile(expression_string,expression);
  735. expression.value(); // 123 + 1
  736. The symbol table supports adding references to external instances of
  737. types that can be accessed within expressions via the following
  738. methods:
  739. 1. bool add_variable (const std::string& name, scalar_t&)
  740. 2. bool add_constant (const std::string& name, const scalar_t&)
  741. 3. bool add_stringvar(const std::string& name, std::string&)
  742. 4. bool add_vector (const std::string& name, vector_type&)
  743. Note: The 'vector' type must be comprised from a contiguous array of
  744. scalars with a size that is larger than zero. The vector type itself
  745. can be any one of the following:
  746. 1. std::vector<scalar_t>
  747. 2. scalar_t(&v)[N]
  748. 3. scalar_t* and array size
  749. 4. exprtk::vector_view<scalar_t>
  750. When registering a variable, vector, string or function with an
  751. instance of a symbol_table, the call to 'add_...' may fail and return
  752. a false result due to one or more of the following reasons:
  753. 1. Variable name contains invalid characters or is ill-formed
  754. 2. Variable name conflicts with a reserved word (eg: 'while')
  755. 3. Variable name conflicts with a previously registered variable
  756. 4. A vector of size (length) zero is being registered
  757. 5. A free function exceeding fifteen parameters is being registered
  758. 6. The symbol_table instance is in an invalid state
  759. (2) Expression
  760. A structure that holds an abstract syntax tree or AST for a specified
  761. expression and is used to evaluate said expression. Evaluation of the
  762. expression is accomplished by performing a post-order traversal of the
  763. AST. If a compiled Expression uses variables or user defined
  764. functions, it will have an associated Symbol Table, which will contain
  765. references to said variables, functions or strings. An example AST
  766. structure for the denoted expression is as follows:
  767. Expression: z := (x + y^-2.345) * sin(pi / min(w - 7.3,v))
  768. [Root]
  769. |
  770. [Assignment]
  771. ________/ \_____
  772. / \
  773. Variable(z) [Multiplication]
  774. ____________/ \___________
  775. / \
  776. / [Unary-Function(sin)]
  777. [Addition] |
  778. ____/ \____ [Division]
  779. / \ ___/ \___
  780. Variable(x) [Exponentiation] / \
  781. ______/ \______ Constant(pi) [Binary-Function(min)]
  782. / \ ____/ \____
  783. Variable(y) [Negation] / \
  784. | / Variable(v)
  785. Constant(2.345) /
  786. /
  787. [Subtraction]
  788. ____/ \____
  789. / \
  790. Variable(w) Constant(7.3)
  791. The above denoted AST will be evaluated in the following order:
  792. (01) Load Variable (z) (10) Load Constant (7.3)
  793. (02) Load Variable (x) (11) Subtraction (09 & 10)
  794. (03) Load Variable (y) (12) Load Variable (v)
  795. (04) Load Constant (2.345) (13) Min (11 & 12)
  796. (05) Negation (04) (14) Division (08 & 13)
  797. (06) Exponentiation (03 & 05) (15) Sin (14)
  798. (07) Addition (02 & 06) (16) Multiplication (07 & 15)
  799. (08) Load Constant (pi) (17) Assignment (01 & 16)
  800. (09) Load Variable (w)
  801. Generally an expression in ExprTk can be thought of as a free function
  802. similar to those found in imperative languages. This form of pseudo
  803. function will have a name, it may have a set of one or more inputs and
  804. will return at least one value as its result. Furthermore the function
  805. when invoked, may cause a side-effect that changes the state of the
  806. host program.
  807. As an example the following is a pseudo-code definition of a free
  808. function that performs a computation taking four inputs, modifying one
  809. of them and returning a value based on some arbitrary calculation:
  810. ResultType foo(InputType x, InputType y, InputType z, InputType w)
  811. {
  812. w = 2 * x^y + z; // Side-Effect
  813. return abs(x - y) / z; // Return Result
  814. }
  815. Given the above definition the following is a functionally equivalent
  816. version using ExprTk:
  817. const std::string foo_str = " w := 2 * x^y + z; "
  818. " abs(x - y) / z; ";
  819. T x, y, z, w;
  820. symbol_table_t symbol_table;
  821. symbol_table.add_variable("x",x);
  822. symbol_table.add_variable("y",y);
  823. symbol_table.add_variable("z",z);
  824. symbol_table.add_variable("w",w);
  825. expression_t foo;
  826. foo.register_symbol_table(symbol_table);
  827. parser_t parser;
  828. if (!parser.compile(foo_str,foo))
  829. {
  830. // Error in expression...
  831. return;
  832. }
  833. T result = foo.value();
  834. (3) Parser
  835. A component which takes as input a string representation of an
  836. expression and attempts to compile said input with the result being an
  837. instance of Expression. If an error is encountered during the
  838. compilation process, the parser will stop compiling and return an
  839. error status code, with a more detailed description of the error(s)
  840. and its location within the input provided by the 'get_error'
  841. interface.
  842. Note: The exprtk::expression and exprtk::symbol_table components are
  843. reference counted entities. Copy constructing or assigning to or from
  844. either component will result in a shallow copy and a reference count
  845. increment, rather than a complete replication. Furthermore the
  846. expression and symbol_table components being Default-Constructible,
  847. Copy-Constructible and Copy-Assignable make them compatible with
  848. various C++ standard library containers and adaptors such as
  849. std::vector, std::map, std::stack etc.
  850. The following is an example of two unique expressions, after having
  851. being instantiated and compiled, one expression is assigned to the
  852. other. The diagrams depict their initial and post assignment states,
  853. including which control block each expression references and their
  854. associated reference counts.
  855. exprtk::expression e0; // constructed expression, eg: x + 1
  856. exprtk::expression e1; // constructed expression, eg: 2z + y
  857. +-----[ e0 cntrl block]----+ +-----[ e1 cntrl block]-----+
  858. | 1. Expression Node 'x+1' | | 1. Expression Node '2z+y' |
  859. | 2. Ref Count: 1 |<-+ | 2. Ref Count: 1 |<-+
  860. +--------------------------+ | +---------------------------+ |
  861. | |
  862. +--[ e0 expression]--+ | +--[ e1 expression]--+ |
  863. | 1. Reference to ]------+ | 1. Reference to ]-------+
  864. | e0 Control Block | | e1 Control Block |
  865. +--------------------+ +--------------------+
  866. e0 = e1; // e0 and e1 are now 2z+y
  867. +-----[ e1 cntrl block]-----+
  868. | 1. Expression Node '2z+y' |
  869. +----------->| 2. Ref Count: 2 |<----------+
  870. | +---------------------------+ |
  871. | |
  872. | +--[ e0 expression]--+ +--[ e1 expression]--+ |
  873. +---[ 1. Reference to | | 1. Reference to ]---+
  874. | e1 Control Block | | e1 Control Block |
  875. +--------------------+ +--------------------+
  876. The reason for the above complexity and restrictions of deep copies
  877. for the expression and symbol_table components is because expressions
  878. may include user defined variables or functions. These are embedded as
  879. references into the expression's AST. When copying an expression, said
  880. references need to also be copied. If the references are blindly
  881. copied, it will then result in two or more identical expressions
  882. utilizing the exact same references for variables. This obviously is
  883. not the default assumed scenario and will give rise to non-obvious
  884. behaviours when using the expressions in various contexts such as
  885. multi-threading et al.
  886. The prescribed method for cloning an expression is to compile it from
  887. its string form. Doing so will allow the 'user' to properly consider
  888. the exact source of user defined variables and functions.
  889. Note: The exprtk::parser is a non-copyable and non-thread safe
  890. component, and should only be shared via either a reference, a shared
  891. pointer or a std::ref mechanism, and considerations relating to
  892. synchronisation taken into account where appropriate. The parser
  893. represents an object factory, specifically a factory of expressions,
  894. and generally should not be instantiated solely on a per expression
  895. compilation basis.
  896. The following diagram and example depicts the flow of data and
  897. operations for compiling multiple expressions via the parser and
  898. inserting the newly minted exprtk::expression instances into a
  899. std::vector.
  900. +----[exprtk::parser]---+
  901. | Expression Factory |
  902. | parser_t::compile(...)|
  903. +--> ~.~.~.~.~.~.~.~.~.~ ->--+
  904. | +-----------------------+ |
  905. Expressions in | | Expressions as
  906. string form A V exprtk::expression
  907. | | instances
  908. [s0:'x+1']--->--+ | | +-[e0: x+1]
  909. | | | |
  910. [s1:'2z+y']-->--+--+ +->+-[e1: 2z+y]
  911. | |
  912. [s2:'sin(k+w)']-+ +-[e2: sin(k+w)]
  913. const std::string expression_str[3]
  914. = { "x + 1", "2x + y", "sin(k + w)" };
  915. std::vector<expression_t> expression_list;
  916. parser_t parser;
  917. expression_t expression;
  918. symbol_table_t symbol_table;
  919. expression.register_symbol_table(symbol_table);
  920. for (std::size_t i = 0; i < 3; ++i)
  921. {
  922. if (parser.compile(expression_str[i],expression))
  923. {
  924. expression_list.push_back(expression);
  925. }
  926. else
  927. std::cout << "Error in " << expression_str[i] << "\n";
  928. }
  929. for (auto& e : expression_list)
  930. {
  931. e.value();
  932. }
  933. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  934. [SECTION 11 - COMPILATION OPTIONS]
  935. The exprtk::parser when being instantiated takes as input a set of
  936. options to be used during the compilation process of expressions.
  937. An example instantiation of exprtk::parser where only the joiner,
  938. commutative and strength reduction options are enabled is as follows:
  939. typedef exprtk::parser<NumericType>::settings_t settings_t;
  940. std::size_t compile_options = settings_t::e_joiner +
  941. settings_t::e_commutative_check +
  942. settings_t::e_strength_reduction;
  943. parser_t parser(compile_options);
  944. Currently seven types of compile time options are supported, and
  945. enabled by default. The options and their explanations are as follows:
  946. (1) Replacer
  947. (2) Joiner
  948. (3) Numeric Check
  949. (4) Bracket Check
  950. (5) Sequence Check
  951. (6) Commutative Check
  952. (7) Strength Reduction Check
  953. (1) Replacer (e_replacer)
  954. Enable replacement of specific tokens with other tokens. For example
  955. the token "true" of type symbol will be replaced with the numeric
  956. token of value one.
  957. (a) (x < y) == true ---> (x < y) == 1
  958. (b) false == (x > y) ---> 0 == (x > y)
  959. (2) Joiner (e_joiner)
  960. Enable joining of multi-character operators that may have been
  961. incorrectly disjoint in the string representation of the specified
  962. expression. For example the consecutive tokens of ">" "=" will become
  963. ">=" representing the "greater than or equal to" operator. If not
  964. properly resolved the original form will cause a compilation error.
  965. The following is a listing of the scenarios that the joiner can
  966. handle:
  967. (a) '>' '=' ---> '>=' (gte)
  968. (b) '<' '=' ---> '<=' (lte)
  969. (c) '=' '=' ---> '==' (equal)
  970. (d) '!' '=' ---> '!=' (not-equal)
  971. (e) '<' '>' ---> '<>' (not-equal)
  972. (f) ':' '=' ---> ':=' (assignment)
  973. (g) '+' '=' ---> '+=' (addition assignment)
  974. (h) '-' '=' ---> '-=' (subtraction assignment)
  975. (i) '*' '=' ---> '*=' (multiplication assignment)
  976. (j) '/' '=' ---> '/=' (division assignment)
  977. (k) '%' '=' ---> '%=' (modulo assignment)
  978. (l) '+' '-' ---> '-' (subtraction)
  979. (m) '-' '+' ---> '-' (subtraction)
  980. (n) '-' '-' ---> '+' (addition)
  981. (o) '<=' '>' ---> '<=>' (swap)
  982. An example of the transformation that takes place is as follows:
  983. (a) (x > = y) and (z ! = w) ---> (x >= y) and (z != w)
  984. (3) Numeric Check (e_numeric_check)
  985. Enable validation of tokens representing numeric types so as to catch
  986. any errors prior to the costly process of the main compilation step
  987. commencing.
  988. (4) Bracket Check (e_bracket_check)
  989. Enable the check for validating the ordering of brackets in the
  990. specified expression.
  991. (5) Sequence Check (e_sequence_check)
  992. Enable the check for validating that sequences of either pairs or
  993. triplets of tokens make sense. For example the following sequence of
  994. tokens when encountered will raise an error:
  995. (a) (x + * 3) ---> sequence error
  996. (6) Commutative Check (e_commutative_check)
  997. Enable the check that will transform sequences of pairs of tokens that
  998. imply a multiplication operation. The following are some examples of
  999. such transformations:
  1000. (a) 2x ---> 2 * x
  1001. (b) 25x^3 ---> 25 * x^3
  1002. (c) 3(x + 1) ---> 3 * (x + 1)
  1003. (d) (x + 1)4 ---> (x + 1) * 4
  1004. (e) 5foo(x,y) ---> 5 * foo(x,y)
  1005. (f) foo(x,y)6 + 1 ---> foo(x,y) * 6 + 1
  1006. (g) (4((2x)3)) ---> 4 * ((2 * x) * 3)
  1007. (h) w(x) + (y)z ---> w * x + y * z
  1008. (7) Strength Reduction Check (e_strength_reduction)
  1009. Enable the use of strength reduction optimisations during the
  1010. compilation process. In ExprTk strength reduction optimisations
  1011. predominantly involve transforming sub-expressions into other forms
  1012. that are algebraically equivalent yet less costly to compute. The
  1013. following are examples of the various transformations that can occur:
  1014. (a) (x / y) / z ---> x / (y * z)
  1015. (b) (x / y) / (z / w) ---> (x * w) / (y * z)
  1016. (c) (2 * x) - (2 * y) ---> 2 * (x - y)
  1017. (d) (2 / x) / (3 / y) ---> (2 / 3) / (x * y)
  1018. (e) (2 * x) * (3 * y) ---> (2 * 3) * (x * y)
  1019. Note:
  1020. When using strength reduction in conjunction with expressions whose
  1021. inputs or sub-expressions may result in values nearing either of the
  1022. bounds of the underlying numeric type (eg: double), there may be the
  1023. possibility of a decrease in the precision of results.
  1024. In the following example the given expression which represents an
  1025. attempt at computing the average between x and y will be transformed
  1026. as follows:
  1027. (0.5 * x) + (y * 0.5) ---> 0.5 * (x + y)
  1028. There may be situations where the above transformation will cause
  1029. numerical overflows and that the original form of the expression is
  1030. desired over the strength reduced form. In these situations it is best
  1031. to turn off strength reduction optimisations or to use a type with a
  1032. larger numerical bound.
  1033. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1034. [SECTION 12 - EXPRESSION STRUCTURES]
  1035. Exprtk supports mathematical expressions in numerous forms based on a
  1036. simple imperative programming model. This section will cover the
  1037. following topics related to general structure and programming of
  1038. expression using ExprTk:
  1039. (1) Multi-Statement Expressions
  1040. (2) Statements And Side-Effects
  1041. (3) Conditional Statements
  1042. (4) Special Functions
  1043. (1) Multi-Statement Expressions
  1044. Expressions in ExprTk can be comprised of one more statements, which
  1045. may sometimes be called sub-expressions. The following are two
  1046. examples of expressions stored in std::string variables, the first a
  1047. single statement and the second a multi-statement expression:
  1048. std::string single_statement = " z := x + y ";
  1049. std::string multi_statement = " var temp := x; "
  1050. " x := y + z; "
  1051. " y := temp; ";
  1052. In a multi-statement expression, the final statement will determine
  1053. the overall result of the expression. In the following multi-statement
  1054. expression, the result of the expression when evaluated will be '2.3',
  1055. which will also be the value stored in the 'y' variable.
  1056. z := x + y;
  1057. y := 2.3;
  1058. As demonstrated in the expression above, statements within an
  1059. expression are separated using the semi-colon ';' operator. In the
  1060. event two statements are not separated by a semi-colon, and the
  1061. implied multiplication feature is active (enabled by default), the
  1062. compiler will assume a multiplication operation between the two
  1063. statements.
  1064. In the following example we have a multi-statement expression composed
  1065. of two variable definitions and initialisations for variables x and y
  1066. and two seemingly separate mathematical operations.
  1067. var x:= 2;
  1068. var y:= 3;
  1069. x + 1
  1070. y * 2
  1071. However the result of the expression will not be 6 as may have been
  1072. assumed based on the calculation of 'y * 2', but rather the result
  1073. will be 8. This is because the compiler will have conjoined the two
  1074. mathematical statements into one via a multiplication operation. The
  1075. expression when compiled will actually evaluate as the following:
  1076. var x:= 2;
  1077. var y:= 3;
  1078. x + 1 * y * 2; // 2 + 1 * 3 * 2 == 8
  1079. In ExprTk any valid statement will itself return a value. This value
  1080. can further be used in conjunction with other statements. This
  1081. includes language structures such as if-statements, loops (for, while)
  1082. and the switch statement. Typically the last statement executed in the
  1083. given construct (conditional, loop etc), will be the value that is
  1084. returned.
  1085. In the following example, the return value of the expression will be
  1086. 11, which is the sum of the variable 'x' and the final value computed
  1087. within the loop body on its last iteration:
  1088. var x := 1;
  1089. x + for (var i := x; i < 10; i += 1)
  1090. {
  1091. i / 2;
  1092. i + 1;
  1093. }
  1094. (2) Statements And Side-Effects
  1095. Statements themselves may have side effects, which in-turn effect the
  1096. proceeding statements in multi-statement expressions.
  1097. A statement is said to have a side-effect if it causes the state of
  1098. the expression to change in some way - this includes but is not
  1099. limited to the modification of the state of external variables used
  1100. within the expression. Currently the following actions being present
  1101. in a statement will cause it to have a side-effect:
  1102. (a) Assignment operation (explicit or potentially)
  1103. (b) Invoking a user-defined function that has side-effects
  1104. The following are examples of expressions where the side-effect status
  1105. of the statements (sub-expressions) within the expressions have been
  1106. noted:
  1107. +-+----------------------+------------------------------+
  1108. |#| Expression | Side Effect Status |
  1109. +-+----------------------+------------------------------+
  1110. |0| x + y | False |
  1111. +-+----------------------+------------------------------+
  1112. |1| z := x + y | True - Due to assignment |
  1113. +-+----------------------+------------------------------+
  1114. |2| abs(x - y) | False |
  1115. +-+----------------------+------------------------------+
  1116. |3| abs(x - y); | False |
  1117. | | z := (x += y); | True - Due to assignments |
  1118. +-+----------------------+------------------------------+
  1119. |4| abs(x - y); | False |
  1120. | | z := (x += y); | True - Due to assignments |
  1121. +-+----------------------+------------------------------+
  1122. |5| var t := abs(x - y); | True - Due to initialisation |
  1123. | | t + x; | False |
  1124. | | z := (x += y); | True - Due to assignments |
  1125. +-+----------------------+------------------------------+
  1126. |6| foo(x - y) | True - user defined function |
  1127. +-+----------------------+------------------------------+
  1128. Note: In example 6 from the above set, it is assumed the user defined
  1129. function foo has been registered as having a side_effect. By default
  1130. all user defined functions are assumed to have side-effects, unless
  1131. they are configured in their constructors to not have side-effects
  1132. using the 'disable_has_side_effects' free function. For more
  1133. information review Section 15 - User Defined Functions sub-section 7
  1134. Function Side-Effects.
  1135. At this point we can see that there will be expressions composed of
  1136. certain kinds of statements that when executed will not effect the
  1137. nature of the expression's result. These statements are typically
  1138. called 'dead code'. These statements though not effecting the final
  1139. result will still be executed and as such they will consume processing
  1140. time that could otherwise be saved. As such ExprTk attempts to detect
  1141. and remove such statements from expressions.
  1142. The 'Dead Code Elimination' (DCE) optimisation process, which is
  1143. enabled by default, will remove any statements that are determined to
  1144. not have a side effect in a multi-statement expression, excluding the
  1145. final or last statement.
  1146. By default the final statement in an expression will always be present
  1147. regardless of its side-effect status, as it is the statement whose
  1148. value will be used as the result of the expression.
  1149. In order to further explain the actions taken during the DCE process,
  1150. lets review the following expression:
  1151. var x := 2; // Statement 1
  1152. var y := x + 2; // Statement 2
  1153. x + y; // Statement 3
  1154. y := x + 3y; // Statement 4
  1155. x - y; // Statement 5
  1156. The above expression has five statements. Three of them (1, 2 and 4)
  1157. actively have side-effects. The first two are variable declaration and
  1158. initialisations, where as the third is due to an assignment operation.
  1159. There are two statements (3 and 5), that do not explicitly have
  1160. side-effects, however the latter, statement 5, is the final statement
  1161. in the expression and hence will be assumed to have a side-effect.
  1162. During compilation when the DCE optimisation is applied to the above
  1163. expression, statement 3 will be removed from the expression, as it has
  1164. no bearing on the final result of expression, the rest of the
  1165. statements will all remain. The optimised form of the expression is as
  1166. follows:
  1167. var x := 2; // Statement 1
  1168. var y := x + 2; // Statement 2
  1169. y := x + 3y; // Statement 3
  1170. x - y; // Statement 4
  1171. (3) Conditional Statements (If-Then-Else)
  1172. ExprTk support two forms of conditional branching or otherwise known
  1173. as if-statements. The first form, is a simple function based
  1174. conditional statement, that takes exactly three input expressions:
  1175. condition, consequent and alternative. The following is an example
  1176. expression that utilises the function based if-statement.
  1177. x := if (y < z, y + 1, 2 * z)
  1178. In the example above, if the condition 'y < z' is true, then the
  1179. consequent 'y + 1' will be evaluated, its value will be returned and
  1180. subsequently assigned to the variable 'x'. Otherwise the alternative
  1181. '2 * z' will be evaluated and its value will be returned. This is
  1182. essentially the simplest form of an if-then-else statement. A simple
  1183. variation of the expression where the value of the if-statement is
  1184. used within another statement is as follows:
  1185. x := 3 * if (y < z, y + 1, 2 * z) / 2
  1186. The second form of if-statement resembles the standard syntax found in
  1187. most imperative languages. There are two variations of the statement:
  1188. (a) If-Statement
  1189. (b) If-Then-Else Statement
  1190. (a) If-Statement
  1191. This version of the conditional statement returns the value of the
  1192. consequent expression when the condition expression is true, else it
  1193. will return a quiet NaN value as its result.
  1194. Example 1:
  1195. x := if (y < z) y + 3;
  1196. Example 2:
  1197. x := if (y < z)
  1198. {
  1199. y + 3
  1200. }
  1201. The two example expressions above are equivalent. If the condition
  1202. 'y < z' is true, the 'x' variable will be assigned the value of the
  1203. consequent 'y + 3', otherwise it will be assigned the value of quiet
  1204. NaN. As previously discussed, if-statements are value returning
  1205. constructs, and if not properly terminated using a semi-colon, will
  1206. end-up combining with the next statement via a multiplication
  1207. operation. The following example will NOT result in the expected value
  1208. of 'w + x' being returned:
  1209. x := if (y < z) y + 3 // missing semi-colon ';'
  1210. w + x
  1211. When the above supposed multi-statement expression is compiled, the
  1212. expression will have a multiplication inserted between the two
  1213. 'intended' statements resulting in the unanticipated expression:
  1214. x := (if (y < z) y + 3) * w + x
  1215. The solution to the above situation is to simply terminate the
  1216. conditional statement with a semi-colon as follows:
  1217. x := if (y < z) y + 3;
  1218. w + x
  1219. (b) If-Then-Else Statement
  1220. The second variation of the if-statement is to allow for the use of
  1221. Else and If-Else cascading statements. Examples of such statements are
  1222. as follows:
  1223. Example 1: Example 2: Example 3:
  1224. if (x < y) if (x < y) if (x > y + 1)
  1225. z := x + 3; { y := abs(x - z);
  1226. else y := z + x; else
  1227. y := x - z; z := x + 3; {
  1228. } y := z + x;
  1229. else z := x + 3;
  1230. y := x - z; };
  1231. Example 4: Example 5: Example 6:
  1232. if (2 * x < max(y,3)) if (x < y) if (x < y or (x + z) > y)
  1233. { z := x + 3; {
  1234. y := z + x; else if (2y != z) z := x + 3;
  1235. z := x + 3; { y := x - z;
  1236. } z := x + 3; }
  1237. else if (2y - z) y := x - z; else if (abs(2y - z) >= 3)
  1238. y := x - z; } y := x - z;
  1239. else else
  1240. x * x; {
  1241. z := abs(x * x);
  1242. x * y * z;
  1243. };
  1244. In the case where there is no final else statement and the flow
  1245. through the conditional arrives at this final point, the same rules
  1246. apply to this form of if-statement as to the previous. That is a quiet
  1247. NaN will be returned as the result of the if-statement. Furthermore
  1248. the same requirements of terminating the statement with a semi-colon
  1249. apply.
  1250. (4) Special Functions
  1251. The purpose of special functions in ExprTk is to provide compiler
  1252. generated equivalents of common mathematical expressions which can be
  1253. invoked by using the 'special function' syntax (eg: $f12(x,y,z) or
  1254. $f82(x,y,z,w)).
  1255. Special functions dramatically decrease the total evaluation time of
  1256. expressions which would otherwise have been written using the common
  1257. form by reducing the total number of nodes in the evaluation tree of
  1258. an expression and by also leveraging the compiler's ability to
  1259. correctly optimise such expressions for a given architecture.
  1260. 3-Parameter 4-Parameter
  1261. +-------------+-------------+ +--------------+------------------+
  1262. | Prototype | Operation | | Prototype | Operation |
  1263. +-------------+-------------+ +--------------+------------------+
  1264. $f00(x,y,z) | (x + y) / z $f48(x,y,z,w) | x + ((y + z) / w)
  1265. $f01(x,y,z) | (x + y) * z $f49(x,y,z,w) | x + ((y + z) * w)
  1266. $f02(x,y,z) | (x + y) - z $f50(x,y,z,w) | x + ((y - z) / w)
  1267. $f03(x,y,z) | (x + y) + z $f51(x,y,z,w) | x + ((y - z) * w)
  1268. $f04(x,y,z) | (x - y) + z $f52(x,y,z,w) | x + ((y * z) / w)
  1269. $f05(x,y,z) | (x - y) / z $f53(x,y,z,w) | x + ((y * z) * w)
  1270. $f06(x,y,z) | (x - y) * z $f54(x,y,z,w) | x + ((y / z) + w)
  1271. $f07(x,y,z) | (x * y) + z $f55(x,y,z,w) | x + ((y / z) / w)
  1272. $f08(x,y,z) | (x * y) - z $f56(x,y,z,w) | x + ((y / z) * w)
  1273. $f09(x,y,z) | (x * y) / z $f57(x,y,z,w) | x - ((y + z) / w)
  1274. $f10(x,y,z) | (x * y) * z $f58(x,y,z,w) | x - ((y + z) * w)
  1275. $f11(x,y,z) | (x / y) + z $f59(x,y,z,w) | x - ((y - z) / w)
  1276. $f12(x,y,z) | (x / y) - z $f60(x,y,z,w) | x - ((y - z) * w)
  1277. $f13(x,y,z) | (x / y) / z $f61(x,y,z,w) | x - ((y * z) / w)
  1278. $f14(x,y,z) | (x / y) * z $f62(x,y,z,w) | x - ((y * z) * w)
  1279. $f15(x,y,z) | x / (y + z) $f63(x,y,z,w) | x - ((y / z) / w)
  1280. $f16(x,y,z) | x / (y - z) $f64(x,y,z,w) | x - ((y / z) * w)
  1281. $f17(x,y,z) | x / (y * z) $f65(x,y,z,w) | ((x + y) * z) - w
  1282. $f18(x,y,z) | x / (y / z) $f66(x,y,z,w) | ((x - y) * z) - w
  1283. $f19(x,y,z) | x * (y + z) $f67(x,y,z,w) | ((x * y) * z) - w
  1284. $f20(x,y,z) | x * (y - z) $f68(x,y,z,w) | ((x / y) * z) - w
  1285. $f21(x,y,z) | x * (y * z) $f69(x,y,z,w) | ((x + y) / z) - w
  1286. $f22(x,y,z) | x * (y / z) $f70(x,y,z,w) | ((x - y) / z) - w
  1287. $f23(x,y,z) | x - (y + z) $f71(x,y,z,w) | ((x * y) / z) - w
  1288. $f24(x,y,z) | x - (y - z) $f72(x,y,z,w) | ((x / y) / z) - w
  1289. $f25(x,y,z) | x - (y / z) $f73(x,y,z,w) | (x * y) + (z * w)
  1290. $f26(x,y,z) | x - (y * z) $f74(x,y,z,w) | (x * y) - (z * w)
  1291. $f27(x,y,z) | x + (y * z) $f75(x,y,z,w) | (x * y) + (z / w)
  1292. $f28(x,y,z) | x + (y / z) $f76(x,y,z,w) | (x * y) - (z / w)
  1293. $f29(x,y,z) | x + (y + z) $f77(x,y,z,w) | (x / y) + (z / w)
  1294. $f30(x,y,z) | x + (y - z) $f78(x,y,z,w) | (x / y) - (z / w)
  1295. $f31(x,y,z) | x * y^2 + z $f79(x,y,z,w) | (x / y) - (z * w)
  1296. $f32(x,y,z) | x * y^3 + z $f80(x,y,z,w) | x / (y + (z * w))
  1297. $f33(x,y,z) | x * y^4 + z $f81(x,y,z,w) | x / (y - (z * w))
  1298. $f34(x,y,z) | x * y^5 + z $f82(x,y,z,w) | x * (y + (z * w))
  1299. $f35(x,y,z) | x * y^6 + z $f83(x,y,z,w) | x * (y - (z * w))
  1300. $f36(x,y,z) | x * y^7 + z $f84(x,y,z,w) | x*y^2 + z*w^2
  1301. $f37(x,y,z) | x * y^8 + z $f85(x,y,z,w) | x*y^3 + z*w^3
  1302. $f38(x,y,z) | x * y^9 + z $f86(x,y,z,w) | x*y^4 + z*w^4
  1303. $f39(x,y,z) | x * log(y)+z $f87(x,y,z,w) | x*y^5 + z*w^5
  1304. $f40(x,y,z) | x * log(y)-z $f88(x,y,z,w) | x*y^6 + z*w^6
  1305. $f41(x,y,z) | x * log10(y)+z $f89(x,y,z,w) | x*y^7 + z*w^7
  1306. $f42(x,y,z) | x * log10(y)-z $f90(x,y,z,w) | x*y^8 + z*w^8
  1307. $f43(x,y,z) | x * sin(y)+z $f91(x,y,z,w) | x*y^9 + z*w^9
  1308. $f44(x,y,z) | x * sin(y)-z $f92(x,y,z,w) | (x and y) ? z : w
  1309. $f45(x,y,z) | x * cos(y)+z $f93(x,y,z,w) | (x or y) ? z : w
  1310. $f46(x,y,z) | x * cos(y)-z $f94(x,y,z,w) | (x < y) ? z : w
  1311. $f47(x,y,z) | x ? y : z $f95(x,y,z,w) | (x <= y) ? z : w
  1312. $f96(x,y,z,w) | (x > y) ? z : w
  1313. $f97(x,y,z,w) | (x >= y) ? z : w
  1314. $f98(x,y,z,w) | (x == y) ? z : w
  1315. $f99(x,y,z,w) | x*sin(y)+z*cos(w)
  1316. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1317. [SECTION 13 - VARIABLE, VECTOR & STRING DEFINITION]
  1318. ExprTk supports the definition of expression local variables, vectors
  1319. and strings. The definitions must be unique as shadowing is not
  1320. allowed and object life-times are based on scope. Definitions use the
  1321. following general form:
  1322. var <name> := <initialiser>;
  1323. (1) Variable Definition
  1324. Variables are of numeric type denoting a single value. They can be
  1325. explicitly initialised to a value, otherwise they will be defaulted to
  1326. zero. The following are examples of variable definitions:
  1327. (a) Initialise x to zero
  1328. var x;
  1329. (b) Initialise y to three
  1330. var y := 3;
  1331. (c) Initialise z to the expression
  1332. var z := if (max(1, x + y) > 2, w, v);
  1333. (2) Vector Definition
  1334. Vectors are arrays of a common numeric type. The elements in a vector
  1335. can be explicitly initialised, otherwise they will all be defaulted to
  1336. zero. The following are examples of vector definitions:
  1337. (a) Initialise all values to zero
  1338. var x[3];
  1339. (b) Initialise all values to zero
  1340. var x[3] := {};
  1341. (c) Initialise all values to given expression
  1342. var x[3] := [123 + 3y + sin(w / z)];
  1343. (d) Initialise the first two values, all other elements to zero
  1344. var x[3] := { 1 + x[2], sin(y[0] / x[]) + 3 };
  1345. (e) Initialise the first three (all) values
  1346. var x[3] := { 1, 2, 3 };
  1347. (f) Initialise vector from a vector
  1348. var x[4] := { 1, 2, 3, 4 };
  1349. var y[3] := x;
  1350. (g) Initialise vector from a smaller vector
  1351. var x[3] := { 1, 2, 3 };
  1352. var y[5] := x; // 1, 2, 3, ??, ??
  1353. (h) Non-initialised vector
  1354. var x[3] := null; // ?? ?? ??
  1355. (i) Error as there are too many initialisers
  1356. var x[3] := { 1, 2, 3, 4 };
  1357. (j) Error as a vector of size zero is not allowed.
  1358. var x[0];
  1359. (3) String Definition
  1360. Strings are sequences comprised of 8-bit characters. They can only be
  1361. defined with an explicit initialisation value. The following are
  1362. examples of string variable definitions:
  1363. (a) Initialise to a string
  1364. var x := 'abc';
  1365. (b) Initialise to an empty string
  1366. var x := '';
  1367. (c) Initialise to a string expression
  1368. var x := 'abc' + '123';
  1369. (d) Initialise to a string range
  1370. var x := 'abc123'[2:4];
  1371. (e) Initialise to another string variable
  1372. var x := 'abc';
  1373. var y := x;
  1374. (f) Initialise to another string variable range
  1375. var x := 'abc123';
  1376. var y := x[2:4];
  1377. (g) Initialise to a string expression
  1378. var x := 'abc';
  1379. var y := x + '123';
  1380. (h) Initialise to a string expression range
  1381. var x := 'abc';
  1382. var y := (x + '123')[1:3];
  1383. (4) Return Value
  1384. Variable and vector definitions have a return value. In the case of
  1385. variable definitions, the value to which the variable is initialised
  1386. will be returned. Where as for vectors, the value of the first element
  1387. (eg: v[0]) will be returned.
  1388. 8 == ((var x := 7;) + 1)
  1389. 4 == (var y[3] := {4, 5, 6};)
  1390. (5) Variable/Vector Assignment
  1391. The value of a variable can be assigned to a vector and a vector or a
  1392. vector expression can be assigned to a variable.
  1393. (a) Variable To Vector:
  1394. Every element of the vector is assigned the value of the variable
  1395. or expression.
  1396. var x := 3;
  1397. var y[3] := { 1, 2, 3 };
  1398. y := x + 1;
  1399. (b) Vector To Variable:
  1400. The variable is assigned the value of the first element of the
  1401. vector (aka vec[0])
  1402. var x := 3;
  1403. var y[3] := { 1, 2, 3 };
  1404. x := y + 1;
  1405. Note: During the expression compilation phase, tokens are classified
  1406. based on the following priorities:
  1407. (a) Reserved keywords or operators (+, -, and, or, etc)
  1408. (b) Base functions (abs, sin, cos, min, max etc)
  1409. (c) Symbol table variables
  1410. (d) Expression local defined variables
  1411. (e) Symbol table functions
  1412. (f) Unknown symbol resolver based variables
  1413. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1414. [SECTION 14 - VECTOR PROCESSING]
  1415. ExprTk provides support for various forms of vector oriented
  1416. arithmetic, inequalities and processing. The various supported pairs
  1417. are as follows:
  1418. (a) vector and vector (eg: v0 + v1)
  1419. (b) vector and scalar (eg: v + 33)
  1420. (c) scalar and vector (eg: 22 * v)
  1421. The following is a list of operations that can be used in conjunction
  1422. with vectors:
  1423. (a) Arithmetic: +, -, *, /, %
  1424. (b) Exponentiation: vector ^ scalar
  1425. (c) Assignment: :=, +=, -=, *=, /=, %=, <=>
  1426. (d) Inequalities: <, <=, >, >=, ==, =, equal
  1427. (e) Boolean logic: and, nand, nor, or, xnor, xor
  1428. (f) Unary operations:
  1429. abs, acos, acosh, asin, asinh, atan, atanh, ceil, cos, cosh,
  1430. cot, csc, deg2grad, deg2rad, erf, erfc, exp, expm1, floor,
  1431. frac, grad2deg, log, log10, log1p, log2, rad2deg, round, sec,
  1432. sgn, sin, sinc, sinh, sqrt, swap, tan, tanh, trunc
  1433. (g) Aggregate and Reduce operations:
  1434. avg, max, min, mul, dot, dotk, sum, sumk, count, all_true,
  1435. all_false, any_true, any_false
  1436. (h) Transformation operations:
  1437. copy, rotate-left/right, shift-left/right, sort, nth_element
  1438. (i) BLAS-L1:
  1439. axpy, axpby, axpyz, axpbyz, axpbz
  1440. Note: When one of the above described operations is being performed
  1441. between two vectors, the operation will only span the size of the
  1442. smallest vector. The elements of the larger vector outside of the
  1443. range will not be included. The operation itself will be processed
  1444. element-wise over values the smaller of the two ranges.
  1445. The following simple example demonstrates the vector processing
  1446. capabilities by computing the dot-product of the vectors v0 and v1 and
  1447. then assigning it to the variable v0dotv1:
  1448. var v0[3] := { 1, 2, 3 };
  1449. var v1[3] := { 4, 5, 6 };
  1450. var v0dotv1 := sum(v0 * v1);
  1451. The following is a for-loop based implementation that is equivalent to
  1452. the previously mentioned dot-product computation expression:
  1453. var v0[3] := { 1, 2, 3 };
  1454. var v1[3] := { 4, 5, 6 };
  1455. var v0dotv1;
  1456. for (var i := 0; i < min(v0[],v1[]); i += 1)
  1457. {
  1458. v0dotv1 += (v0[i] * v1[i]);
  1459. }
  1460. Note: When the aggregate or reduction operations denoted above are
  1461. used in conjunction with a vector or vector expression, the return
  1462. value is not a vector but rather a single value.
  1463. var x[3] := { 1, 2, 3 };
  1464. sum(x) == 6
  1465. sum(1 + 2x) == 15
  1466. avg(3x + 1) == 7
  1467. min(1 / x) == (1 / 3)
  1468. max(x / 2) == (3 / 2)
  1469. sum(x > 0 and x < 5) == x[]
  1470. When utilizing external user defined vectors via the symbol table as
  1471. opposed to expression local defined vectors, the typical 'add_vector'
  1472. method from the symbol table will register the entirety of the vector
  1473. that is passed. The following example attempts to evaluate the sum of
  1474. elements of the external user defined vector within a typical yet
  1475. trivial expression:
  1476. std::string reduce_program = " sum(2 * v + 1) ";
  1477. std::vector<T> v0 { T(1.1), T(2.2), ..... , T(99.99) };
  1478. symbol_table_t symbol_table;
  1479. symbol_table.add_vector("v",v);
  1480. expression_t expression;
  1481. expression.register_symbol_table(symbol_table);
  1482. parser_t parser;
  1483. parser.compile(reduce_program,expression);
  1484. T sum = expression.value();
  1485. For the most part, this is a very common use-case. However there may
  1486. be situations where one may want to evaluate the same vector oriented
  1487. expression many times over, but using different vectors or sub ranges
  1488. of the same vector of the same size to that of the original upon every
  1489. evaluation.
  1490. The usual solution is to either recompile the expression for the new
  1491. vector instance, or to copy the contents from the new vector to the
  1492. symbol table registered vector and then perform the evaluation. When
  1493. the vectors are large or the re-evaluation attempts are numerous,
  1494. these solutions can become rather time consuming and generally
  1495. inefficient.
  1496. std::vector<T> v1 { T(2.2), T(2.2), ..... , T(2.2) };
  1497. std::vector<T> v2 { T(3.3), T(3.3), ..... , T(3.3) };
  1498. std::vector<T> v3 { T(4.4), T(4.4), ..... , T(4.4) };
  1499. std::vector<std::vector<T>> vv { v1, v2, v3 };
  1500. ...
  1501. T sum = T(0);
  1502. for (auto& new_vec : vv)
  1503. {
  1504. v = new_vec; // update vector
  1505. sum += expression.value();
  1506. }
  1507. A solution to the above 'efficiency' problem, is to use the
  1508. exprtk::vector_view object. The vector_view is instantiated with a
  1509. size and backing based upon a vector. Upon evaluations if the backing
  1510. needs to be 'updated' to either another vector or sub-range, the
  1511. vector_view instance can be efficiently rebased, and the expression
  1512. evaluated as normal.
  1513. exprtk::vector_view<T> view = exprtk::make_vector_view(v,v.size());
  1514. symbol_table_t symbol_table;
  1515. symbol_table.add_vector("v",view);
  1516. ...
  1517. T sum = T(0);
  1518. for (auto& new_vec : vv)
  1519. {
  1520. view.rebase(new_vec.data()); // update vector
  1521. sum += expression.value();
  1522. }
  1523. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1524. [SECTION 15 - USER DEFINED FUNCTIONS]
  1525. ExprTk provides a means whereby custom functions can be defined and
  1526. utilised within expressions. The concept requires the user to
  1527. provide a reference to the function coupled with an associated name
  1528. that will be invoked within expressions. Functions may take numerous
  1529. inputs but will always return a single value of the underlying numeric
  1530. type.
  1531. During expression compilation when required the reference to the
  1532. function will be obtained from the associated symbol_table and be
  1533. embedded into the expression.
  1534. There are five types of function interface:
  1535. +---+----------------------+--------------+----------------------+
  1536. | # | Name | Return Type | Input Types |
  1537. +---+----------------------+--------------+----------------------+
  1538. | 1 | ifunction | Scalar | Scalar |
  1539. | 2 | ivararg_function | Scalar | Scalar |
  1540. | 3 | igeneric_function | Scalar | Scalar,Vector,String |
  1541. | 4 | igeneric_function II | String | Scalar,Vector,String |
  1542. | 5 | igeneric_function III| String/Scalar| Scalar,Vector,String |
  1543. | 6 | function_compositor | Scalar | Scalar |
  1544. +---+----------------------+--------------+----------------------+
  1545. (1) ifunction
  1546. This interface supports zero to 20 input parameters of only the scalar
  1547. type (numbers). The usage requires a custom function be derived from
  1548. ifunction and to override one of the 21 function operators. As part of
  1549. the constructor the custom function will define how many parameters it
  1550. expects to handle. The following example defines a 3 parameter
  1551. function called 'foo':
  1552. template <typename T>
  1553. struct foo : public exprtk::ifunction<T>
  1554. {
  1555. foo() : exprtk::ifunction<T>(3)
  1556. {}
  1557. T operator()(const T& v1, const T& v2, const T& v3)
  1558. {
  1559. return T(1) + (v1 * v2) / T(v3);
  1560. }
  1561. };
  1562. (2) ivararg_function
  1563. This interface supports a variable number of scalar arguments as input
  1564. into the function. The function operator interface uses a std::vector
  1565. specialised upon type T to facilitate parameter passing. The following
  1566. example defines a vararg function called 'boo':
  1567. template <typename T>
  1568. struct boo : public exprtk::ivararg_function<T>
  1569. {
  1570. inline T operator()(const std::vector<T>& arglist)
  1571. {
  1572. T result = T(0);
  1573. for (std::size_t i = 0; i < arglist.size(); ++i)
  1574. {
  1575. result += arglist[i] / arglist[i > 0 ? (i - 1) : 0];
  1576. }
  1577. return result;
  1578. }
  1579. };
  1580. (3) igeneric_function
  1581. This interface supports a variable number of arguments and types as
  1582. input into the function. The function operator interface uses a
  1583. std::vector specialised upon the type_store type to facilitate
  1584. parameter passing.
  1585. Scalar <-- function(i_0, i_1, i_2....., i_N)
  1586. The fundamental types that can be passed into the function as
  1587. parameters and their views are as follows:
  1588. (1) Scalar - scalar_view
  1589. (2) Vector - vector_view
  1590. (3) String - string_view
  1591. The above denoted type views provide non-const reference-like access
  1592. to each parameter, as such modifications made to the input parameters
  1593. will persist after the function call has completed. The following
  1594. example defines a generic function called 'too':
  1595. template <typename T>
  1596. struct too : public exprtk::igeneric_function<T>
  1597. {
  1598. typedef typename exprtk::igeneric_function<T>::parameter_list_t
  1599. parameter_list_t;
  1600. too()
  1601. {}
  1602. inline T operator()(parameter_list_t parameters)
  1603. {
  1604. for (std::size_t i = 0; i < parameters.size(); ++i)
  1605. {
  1606. ...
  1607. }
  1608. return T(0);
  1609. }
  1610. };
  1611. In the example above, the input 'parameters' to the function operator,
  1612. parameter_list_t, is a type of std::vector of type_store. Each
  1613. type_store instance has a member called 'type' which holds the
  1614. enumeration pertaining to the underlying type of the type_store. There
  1615. are three type enumerations:
  1616. (1) e_scalar - literals, variables, vector elements, expressions
  1617. eg: 123.456, x, vec[3x + 1], 2x + 3
  1618. (2) e_vector - vectors, vector expressions
  1619. eg: vec1, 2 * vec1 + vec2 / 3
  1620. (3) e_string - strings, string literals and range variants of both
  1621. eg: 'AString', s0, 'AString'[x:y], s1[1 + x:] + 'AString'
  1622. Each of the parameters can be accessed using its designated view. A
  1623. typical loop for processing the parameters is as follows:
  1624. inline T operator()(parameter_list_t parameters)
  1625. {
  1626. typedef typename exprtk::igeneric_function<T>::generic_type
  1627. generic_type;
  1628. typedef typename generic_type::scalar_view scalar_t;
  1629. typedef typename generic_type::vector_view vector_t;
  1630. typedef typename generic_type::string_view string_t;
  1631. for (std::size_t i = 0; i < parameters.size(); ++i)
  1632. {
  1633. generic_type& gt = parameters[i];
  1634. if (generic_type::e_scalar == gt.type)
  1635. {
  1636. scalar_t x(gt);
  1637. ...
  1638. }
  1639. else if (generic_type::e_vector == gt.type)
  1640. {
  1641. vector_t vector(gt);
  1642. ...
  1643. }
  1644. else if (generic_type::e_string == gt.type)
  1645. {
  1646. string_t string(gt);
  1647. ...
  1648. }
  1649. }
  1650. return T(0);
  1651. }
  1652. Most often than not a custom generic function will require a specific
  1653. sequence of parameters, rather than some arbitrary sequence of types.
  1654. In those situations, ExprTk can perform compile-time type checking to
  1655. validate that function invocations are carried out using the correct
  1656. sequence of parameters. Furthermore performing the checks at compile
  1657. -time rather than at run-time (aka every time the function is invoked)
  1658. will result in expression evaluation performance gains.
  1659. Compile-time type checking of input parameters can be requested by
  1660. passing a string to the constructor of the igeneric_function that
  1661. represents the required sequence of parameter types. When no parameter
  1662. sequence is provided, it is implied the function can accept a variable
  1663. number of parameters comprised of any of the fundamental types.
  1664. Each fundamental type has an associated character. The following is a
  1665. listing of said characters and their meanings:
  1666. (1) T - Scalar
  1667. (2) V - Vector
  1668. (3) S - String
  1669. (4) Z - Zero or no parameters
  1670. (5) ? - Any type (Scalar, Vector or String)
  1671. (6) * - Wildcard operator
  1672. (7) | - Parameter sequence delimiter
  1673. No other characters other than the seven denoted above may be included
  1674. in the parameter sequence definition. If any such invalid characters
  1675. do exist, registration of the associated generic function to a symbol
  1676. table ('add_function' method) will fail. If the parameter sequence is
  1677. modified resulting in it becoming invalid after having been added to
  1678. the symbol table but before the compilation step, a compilation error
  1679. will be incurred.
  1680. The following example demonstrates a simple generic function
  1681. implementation with a user specified parameter sequence:
  1682. template <typename T>
  1683. struct moo : public exprtk::igeneric_function<T>
  1684. {
  1685. typedef typename exprtk::igeneric_function<T>::parameter_list_t
  1686. parameter_list_t;
  1687. moo()
  1688. : exprtk::igeneric_function<T>("SVTT")
  1689. {}
  1690. inline T operator()(parameter_list_t parameters)
  1691. {
  1692. ...
  1693. }
  1694. };
  1695. In the example above the generic function 'moo' expects exactly four
  1696. parameters in the following sequence:
  1697. (1) String
  1698. (2) Vector
  1699. (3) Scalar
  1700. (4) Scalar
  1701. Note: The 'Z' or no parameter option may not be used in conjunction
  1702. with any other type option in a parameter sequence. When incorporated
  1703. in the parameter sequence list, the no parameter option indicates that
  1704. the function may be invoked without any parameters being passed. For
  1705. more information refer to the section: 'Zero Parameter Functions'
  1706. (4) igeneric_function II
  1707. This interface is identical to the igeneric_function, in that in can
  1708. consume an arbitrary number of parameters of varying type, but the
  1709. difference being that the function returns a string and as such is
  1710. treated as a string when invoked within expressions. As a result the
  1711. function call can alias a string and interact with other strings in
  1712. situations such as concatenation and equality operations.
  1713. String <-- function(i_0, i_1, i_2....., i_N)
  1714. The following example defines a generic function named 'toupper' with
  1715. the string return type function operator being explicitly overridden:
  1716. template <typename T>
  1717. struct toupper : public exprtk::igeneric_function<T>
  1718. {
  1719. typedef exprtk::igeneric_function<T> igenfunct_t
  1720. typedef typename igenfunct_t::generic_type generic_t;
  1721. typedef typename igenfunct_t::parameter_list_t parameter_list_t;
  1722. typedef typename generic_t::string_view string_t;
  1723. toupper()
  1724. : exprtk::igeneric_function<T>("S",igenfunct_t::e_rtrn_string)
  1725. {}
  1726. inline T operator()(std::string& result,
  1727. parameter_list_t parameters)
  1728. {
  1729. result.clear();
  1730. string_t string(params[0]);
  1731. for (std::size_t i = 0; i < string.size(); ++i)
  1732. {
  1733. result += std::toupper(string[i]);
  1734. }
  1735. return T(0);
  1736. }
  1737. };
  1738. In the example above the generic function 'toupper' expects only one
  1739. input parameter of type string, as noted by the parameter sequence
  1740. string passed during the constructor. Furthermore a second parameter
  1741. is passed to the constructor indicating that it should be treated as a
  1742. string returning function - by default it is assumed to be a scalar
  1743. returning function.
  1744. When executed, the function will return as a result a copy of the
  1745. input string converted to uppercase form. An example expression using
  1746. the toupper function registered as the symbol 'toupper' is as follows:
  1747. "'ABCDEF' == toupper('aBc') + toupper('DeF')"
  1748. Note: When adding a string type returning generic function to a symbol
  1749. table the 'add_function' is invoked. The example below demonstrates
  1750. how this can be done:
  1751. toupper<T> tu;
  1752. exprtk::symbol_table<T> symbol_table;
  1753. symbol_table.add_function("toupper",tu);
  1754. Note: Two further refinements to the type checking facility are the
  1755. possibilities of a variable number of common types which can be
  1756. accomplished by using a wildcard '*' and a special 'any type' which is
  1757. done using the '?' character. It should be noted that the wildcard
  1758. operator is associated with the previous type in the sequence and
  1759. implies one or more of that type.
  1760. template <typename T>
  1761. struct zoo : public exprtk::igeneric_function<T>
  1762. {
  1763. typedef typename exprtk::igeneric_function<T>::parameter_list_t
  1764. parameter_list_t;
  1765. zoo()
  1766. : exprtk::igeneric_function<T>("SVT*V?")
  1767. {}
  1768. inline T operator()(parameter_list_t parameters)
  1769. {
  1770. ...
  1771. }
  1772. };
  1773. In the example above the generic function 'zoo' expects at least five
  1774. parameters in the following sequence:
  1775. (1) String
  1776. (2) Vector
  1777. (3) One or more Scalars
  1778. (4) Vector
  1779. (5) Any type (one type of either a scalar, vector or string)
  1780. A final piece of type checking functionality is available for the
  1781. scenarios where a single function name is intended to be used for
  1782. multiple distinct parameter sequences, another name for this feature
  1783. is function overloading. The parameter sequences are passed to the
  1784. constructor as a single string delimited by the pipe '|' character.
  1785. Two specific overrides of the function operator are provided one for
  1786. standard generic functions and one for string returning functions. The
  1787. overrides are as follows:
  1788. // Scalar <-- function(psi,i_0,i_1,....,i_N)
  1789. inline T operator()(const std::size_t& ps_index,
  1790. parameter_list_t parameters)
  1791. {
  1792. ...
  1793. }
  1794. // String <-- function(psi,i_0,i_1,....,i_N)
  1795. inline T operator()(const std::size_t& ps_index,
  1796. std::string& result,
  1797. parameter_list_t parameters)
  1798. {
  1799. ...
  1800. }
  1801. When the function operator is invoked the 'ps_index' parameter will
  1802. have as its value the index of the parameter sequence that matches the
  1803. specific invocation. This way complex and time consuming type checking
  1804. conditions need not be executed in the function itself but rather a
  1805. simple and efficient dispatch to a specific implementation for that
  1806. particular parameter sequence can be performed.
  1807. template <typename T>
  1808. struct roo : public exprtk::igeneric_function<T>
  1809. {
  1810. typedef typename exprtk::igeneric_function<T>::parameter_list_t
  1811. parameter_list_t;
  1812. moo()
  1813. : exprtk::igeneric_function<T>("SVTT|SS|TTV|S?V*S")
  1814. {}
  1815. inline T operator()(const std::size_t& ps_index,
  1816. parameter_list_t parameters)
  1817. {
  1818. ...
  1819. }
  1820. };
  1821. In the example above there are four distinct parameter sequences that
  1822. can be processed by the generic function 'roo'. Any other parameter
  1823. sequences will cause a compilation error. The four valid sequences are
  1824. as follows:
  1825. Sequence-0 Sequence-1 Sequence-2 Sequence-3
  1826. 'SVTT' 'SS' 'TTV' 'S?V*S'
  1827. (1) String (1) String (1) Scalar (1) String
  1828. (2) Vector (2) String (2) Scalar (2) Any Type
  1829. (3) Scalar (3) Vector (3) One or more Vectors
  1830. (4) Scalar (4) String
  1831. (5) igeneric_function III
  1832. In this section we will discuss an extension of the igeneric_function
  1833. interface that will allow for the overloading of a user defined custom
  1834. function, where by it can return either a scalar or string value type
  1835. depending on the input parameter sequence with which the function is
  1836. invoked.
  1837. template <typename T>
  1838. struct foo : public exprtk::igeneric_function<T>
  1839. {
  1840. typedef typename exprtk::igeneric_function<T>::parameter_list_t
  1841. parameter_list_t;
  1842. foo()
  1843. : exprtk::igeneric_function<T>
  1844. (
  1845. "T:T|S:TS",
  1846. igfun_t::e_rtrn_overload
  1847. )
  1848. {}
  1849. // Scalar value returning invocations
  1850. inline T operator()(const std::size_t& ps_index,
  1851. parameter_list_t parameters)
  1852. {
  1853. ...
  1854. }
  1855. // String value returning invocations
  1856. inline T operator()(const std::size_t& ps_index,
  1857. std::string& result,
  1858. parameter_list_t& parameters)
  1859. {
  1860. ...
  1861. }
  1862. };
  1863. In the example above the custom user defined function "foo" can be
  1864. invoked by using either one of two input parameter sequences, which
  1865. are defined as follows:
  1866. Sequence-0 Sequence-1
  1867. 'T' -> T 'TS' -> S
  1868. (1) Scalar (1) Scalar
  1869. (2) String
  1870. The parameter sequence definitions are identical to the previously
  1871. define igeneric_function, with the exception of the inclusion of the
  1872. return type - which can only be either a scalar T or a string S.
  1873. (6) function_compositor
  1874. The function compositor is a factory that allows one to define and
  1875. construct a function using ExprTk syntax. The functions are limited to
  1876. returning a single scalar value and consuming up to six parameters as
  1877. input.
  1878. All composited functions are registered with a symbol table, allowing
  1879. them to call other functions that have been registered with the symbol
  1880. table instance. Furthermore the functions can be recursive in nature
  1881. due to the inherent function prototype forwarding that occurs during
  1882. construction. The following example defines, by using two different
  1883. methods, composited functions and implicitly registering the functions
  1884. with the denoted symbol table.
  1885. typedef exprtk::symbol_table<T> symbol_table_t;
  1886. typedef exprtk::function_compositor<T> compositor_t;
  1887. typedef typename compositor_t::function function_t;
  1888. symbol_table_t symbol_table;
  1889. compositor_t compositor(symbol_table);
  1890. // define function koo0(v1,v2) { ... }
  1891. compositor
  1892. .add(
  1893. function_t(
  1894. "koo0",
  1895. " 1 + cos(v1 * v2) / 3;",
  1896. "v1","v2"));
  1897. // define function koo1(x,y,z) { ... }
  1898. compositor
  1899. .add(function_t()
  1900. .name("koo1")
  1901. .var("x").var("y").var("z")
  1902. .expression("1 + cos(x * y) / z;"));
  1903. (6) Using Functions In Expressions
  1904. For the above denoted custom and composited functions to be used in an
  1905. expression, an instance of each function needs to be registered with a
  1906. symbol_table that has been associated with the expression instance.
  1907. The following demonstrates how all the pieces are put together:
  1908. typedef exprtk::symbol_table<double> symbol_table_t;
  1909. typedef exprtk::expression<double> expression_t;
  1910. typedef exprtk::parser<double> parser_t;
  1911. typedef exprtk::function_compositor<double> compositor_t;
  1912. typedef typename compositor_t::function function_t;
  1913. foo<double> f;
  1914. boo<double> b;
  1915. too<double> t;
  1916. toupper<double> tu;
  1917. symbol_table_t symbol_table;
  1918. compositor_t compositor(symbol_table);
  1919. symbol_table.add_function("foo",f);
  1920. symbol_table.add_function("boo",b);
  1921. symbol_table.add_function("too",t);
  1922. symbol_table.add_function("toupper",
  1923. tu,
  1924. symbol_table_t::e_ft_strfunc);
  1925. compositor
  1926. .add(function_t()
  1927. .name("koo")
  1928. .var("v1")
  1929. .var("v2")
  1930. .expression("1 + cos(v1 * v2) / 3;"));
  1931. expression_t expression;
  1932. expression.register_symbol_table(symbol_table);
  1933. std::string expression_str =
  1934. " if (foo(1,2,3) + boo(1) > boo(1/2,2/3,3/4,4/5)) "
  1935. " koo(3,4); "
  1936. " else "
  1937. " too(2 * v1 + v2 / 3, 'abcdef'[2:4], 3.3); "
  1938. " ";
  1939. parser_t parser;
  1940. parser.compile(expression_str,expression);
  1941. expression.value();
  1942. (7) Function Side-Effects
  1943. All function calls are assumed to have side-effects by default. This
  1944. assumption implicitly disables constant folding optimisations when all
  1945. parameters being passed to the function are deduced as being constants
  1946. at compile time.
  1947. If it is certain that the function being registered does not have any
  1948. side effects and can be correctly constant folded where appropriate,
  1949. then during the construction of the function the side-effect trait of
  1950. the function can be disabled.
  1951. template <typename T>
  1952. struct foo : public exprtk::ifunction<T>
  1953. {
  1954. foo() : exprtk::ifunction<T>(3)
  1955. {
  1956. exprtk::disable_has_side_effects(*this);
  1957. }
  1958. T operator()(const T& v1, const T& v2, const T& v3)
  1959. { ... }
  1960. };
  1961. (8) Zero Parameter Functions
  1962. When either an ifunction, ivararg_function or igeneric_function
  1963. derived type is defined with zero number of parameters, there are two
  1964. calling conventions within expressions that are allowed. For a
  1965. function named 'foo' with zero input parameters the calling styles are
  1966. as follows:
  1967. (1) x + sin(foo()- 2) / y
  1968. (2) x + sin(foo - 2) / y
  1969. By default the zero parameter trait is disabled. In order to enable
  1970. it, a process similar to that of enabling of the side effect trait is
  1971. carried out:
  1972. template <typename T>
  1973. struct foo : public exprtk::ivararg_function<T>
  1974. {
  1975. foo()
  1976. {
  1977. exprtk::enable_zero_parameters(*this);
  1978. }
  1979. inline T operator()(const std::vector<T>& arglist)
  1980. { ... }
  1981. };
  1982. Note: For the igeneric_function type, there also needs to be a 'Z'
  1983. parameter sequence defined in order for the zero parameter trait to
  1984. properly take effect otherwise a compilation error will occur.
  1985. (9) Free Functions
  1986. The ExprTk symbol table supports the registration of free functions
  1987. and lambdas (anonymous functors) for use in expressions. The basic
  1988. requirements are similar to those found in ifunction derived user
  1989. defined functions. This includes support for free functions using
  1990. anywhere from zero up to fifteen input parameters of scalar type, with
  1991. a return type that is also scalar. Furthermore such functions will by
  1992. default be assumed to have side-effects and hence will not participate
  1993. in constant folding optimisations.
  1994. In the following example, a two input parameter free function named
  1995. 'compute1', and a three input parameter lambda named 'compute2' will
  1996. be registered with the given symbol_table instance:
  1997. double compute1(double v0, double v1)
  1998. {
  1999. return 2.0 * v0 + v1 / 3.0;
  2000. }
  2001. .
  2002. .
  2003. .
  2004. typedef exprtk::symbol_table<double> symbol_table_t;
  2005. symbol_table_t symbol_table;
  2006. symbol_table.add_function("compute1", compute1);
  2007. symbol_table.add_function(
  2008. "compute2",
  2009. [](double v0, double v1, double v2) -> double
  2010. { return v0 / v1 + v2; });
  2011. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2012. [SECTION 16 - EXPRESSION DEPENDENTS]
  2013. Any expression that is not a literal (aka constant) will have
  2014. dependencies. The types of 'dependencies' an expression can have are
  2015. as follows:
  2016. (a) Variables
  2017. (b) Vectors
  2018. (c) Strings
  2019. (d) Functions
  2020. (e) Assignments
  2021. In the following example the denoted expression has its various
  2022. dependencies listed:
  2023. z := abs(x + sin(2 * pi / y))
  2024. (a) Variables: x, y, z and pi
  2025. (b) Functions: abs, sin
  2026. (c) Assignments: z
  2027. ExprTk allows for the derivation of expression dependencies via the
  2028. 'dependent_entity_collector' (DEC). When activated either through
  2029. 'compile_options' at the construction of the parser or through calls
  2030. to enabler methods just prior to compilation, the DEC will proceed to
  2031. collect any of the relevant types that are encountered during the
  2032. parsing phase. Once the compilation process has successfully
  2033. completed, the caller can then obtain a list of symbols and their
  2034. associated types from the DEC.
  2035. The kinds of questions one can ask regarding the dependent entities
  2036. within an expression are as follows:
  2037. * What user defined variables, vectors or strings are used?
  2038. * What functions or custom user functions are used?
  2039. * Which variables, vectors or strings have values assigned to them?
  2040. The following example demonstrates usage of the DEC in determining the
  2041. dependents of the given expression:
  2042. typedef typename parser_t::
  2043. dependent_entity_collector::symbol_t symbol_t;
  2044. std::string expression_string =
  2045. "z := abs(x + sin(2 * pi / y))";
  2046. T x,y,z;
  2047. parser_t parser;
  2048. symbol_table_t symbol_table;
  2049. symbol_table.add_variable("x",x);
  2050. symbol_table.add_variable("y",y);
  2051. symbol_table.add_variable("z",z);
  2052. expression_t expression;
  2053. expression.register_symbol_table(symbol_table);
  2054. //Collect only variable and function symbols
  2055. parser.dec().collect_variables() = true;
  2056. parser.dec().collect_functions() = true;
  2057. if (!parser.compile(expression_string,expression))
  2058. {
  2059. // error....
  2060. }
  2061. std::deque<symbol_t> symbol_list;
  2062. parser.dec().symbols(symbol_list);
  2063. for (std::size_t i = 0; i < symbol_list.size(); ++i)
  2064. {
  2065. symbol_t& symbol = symbol_list[i];
  2066. switch (symbol.second)
  2067. {
  2068. case parser_t::e_st_variable : ... break;
  2069. case parser_t::e_st_vector : ... break;
  2070. case parser_t::e_st_string : ... break;
  2071. case parser_t::e_st_function : ... break;
  2072. }
  2073. }
  2074. Note: The 'symbol_t' type is a std::pair comprising of the symbol name
  2075. (std::string) and the associated type of the symbol as denoted by the
  2076. cases in the switch statement.
  2077. Having particular symbols (variable or function) present in an
  2078. expression is one form of dependency. Another and just as interesting
  2079. and important type of dependency is that of assignments. Assignments
  2080. are the set of dependent symbols that 'may' have their values modified
  2081. within an expression. The following are example expressions and their
  2082. associated assignments:
  2083. Assignments Expression
  2084. (1) x x := y + z
  2085. (2) x, y x += y += z
  2086. (3) x, y, z x := y += sin(z := w + 2)
  2087. (4) w, z if (x > y, z := x + 2, w := 'A String')
  2088. (5) None x + y + z
  2089. Note: In expression 4, both variables 'w' and 'z' are denoted as being
  2090. assignments even though only one of them can ever be modified at the
  2091. time of evaluation. Furthermore the determination of which of the two
  2092. variables the modification will occur upon can only be known with
  2093. certainty at evaluation time and not beforehand, hence both are listed
  2094. as being candidates for assignment.
  2095. The following builds upon the previous example demonstrating the usage
  2096. of the DEC in determining the 'assignments' of the given expression:
  2097. //Collect assignments
  2098. parser.dec().collect_assignments() = true;
  2099. if (!parser.compile(expression_string,expression))
  2100. {
  2101. // error....
  2102. }
  2103. std::deque<symbol_t> symbol_list;
  2104. parser.dec().assignment_symbols(symbol_list);
  2105. for (std::size_t i = 0; i < symbol_list.size(); ++i)
  2106. {
  2107. symbol_t& symbol = symbol_list[i];
  2108. switch (symbol.second)
  2109. {
  2110. case parser_t::e_st_variable : ... break;
  2111. case parser_t::e_st_vector : ... break;
  2112. case parser_t::e_st_string : ... break;
  2113. }
  2114. }
  2115. Note: The assignments will only consist of variable types and as such
  2116. will not contain symbols denoting functions.
  2117. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2118. [SECTION 17 - HIERARCHIES OF SYMBOL TABLES]
  2119. Most situations will only require a single symbol_table instance to be
  2120. associated with a given expression instance.
  2121. However as an expression can have more than one symbol table instance
  2122. associated with itself, when building more complex systems that
  2123. utilise many expressions where each can in turn utilise one or more
  2124. variables from a large set of potential variables, functions or
  2125. constants, it becomes evident that grouping variables into layers of
  2126. symbol_tables will simplify and streamline the overall process.
  2127. A suggested hierarchy of symbol tables is as follows:
  2128. (a) Global constant value symbol table
  2129. (b) Global non side-effect functions symbol table
  2130. (c) Global variable symbol table
  2131. (d) Expression specific variable symbol table
  2132. (a) Global constant value symbol table
  2133. This symbol table will contain constant variables denoting immutable
  2134. values. These variables can be made available to all expressions, and
  2135. in turn expressions will assume the values themselves will never be
  2136. modified for the duration of the process run-time. Examples of such
  2137. variables are:
  2138. (1) pi or e
  2139. (2) speed_of_light
  2140. (3) avogadro_number
  2141. (4) num_cpus
  2142. (b) Global non side-effect functions symbol table
  2143. This symbol table will contain only user defined functions that will
  2144. not incur any side-effects that are visible to any of the expressions
  2145. that invoke them. These functions will be thread-safe or threading
  2146. invariant and will not maintain any form of state between invocations.
  2147. Examples of such functions are:
  2148. (1) calc_volume_of_sphere(r)
  2149. (2) distance(x0,y0,x1,y1)
  2150. (c) Global variable symbol table
  2151. This symbol table will contain variables that will be accessible to
  2152. all associated expressions and will not be specific or exclusive to
  2153. any one expression. This variant differs from (a) in that the values
  2154. of the variables can change (or be updated) between evaluations of
  2155. expressions - but through properly scheduled evaluations are
  2156. guaranteed to never change during the evaluation of any dependent
  2157. expressions. Furthermore it is assumed that these variables will be
  2158. used in a read-only context and that no expressions will attempt to
  2159. modify these variables via assignments or other means.
  2160. (1) price_of_stock_xyz
  2161. (2) outside_temperature or inside_temperature
  2162. (3) fuel_in_tank
  2163. (4) num_customers_in_store
  2164. (5) num_items_on_shelf
  2165. (d) Expression specific variable symbol table
  2166. This symbol_table is the most common form, and is used to store
  2167. variables that are specific and exclusive to a particular expression.
  2168. That is to say references to variables in this symbol_table will not
  2169. be part of another expression. Though it may be possible to have
  2170. expressions that contain the variables with the same name, in that
  2171. case those variables will be distinctly different. Which would mean if
  2172. a particular expression were to be compiled twice, each expression
  2173. would have its own unique symbol_table which in turn would have its
  2174. own instances of those variables. Examples of such variables could be:
  2175. (1) x or y
  2176. (2) customer_name
  2177. The following is a diagram depicting the possible version of the
  2178. denoted symbol table hierarchies. In the diagram there are two unique
  2179. expressions, each of which have a reference to the Global constant,
  2180. functions and variables symbol tables and an exclusive reference to a
  2181. local symbol table.
  2182. +-------------------------+ +-------------------------+
  2183. | Global Constants | | Global Functions |
  2184. | Symbol Table | | Symbol Table |
  2185. +----o--o-----------------+ +--------------------o----+
  2186. | | |
  2187. | | +-------+
  2188. | +------------------->----------------------------+ |
  2189. | +----------------------------+ | |
  2190. | | Global Variables | | |
  2191. | +------o Symbol Table o-----+ | V
  2192. | | +----------------------------+ | | |
  2193. | | | | |
  2194. | | +----------------+ +----------------+ | | |
  2195. | | | Symbol Table 0 | | Symbol Table 1 | | V |
  2196. | | +--o-------------+ +--o-------------+ | | |
  2197. | | | | | | |
  2198. | | | | | | |
  2199. +--V--V----V---------+ +-V---------------V--+ | |
  2200. | Expression 0 | | Expression 1 |<--+--+
  2201. | '2 * sin(x) - y' | | 'k + abs(x - y)' |
  2202. +--------------------+ +--------------------+
  2203. Bringing all of the above together, in the following example the
  2204. hierarchy of symbol tables are instantiated and initialised. An
  2205. expression that makes use of various elements of each symbol table is
  2206. then compiled and later on evaluated:
  2207. typedef exprtk::symbol_table<double> symbol_table_t;
  2208. typedef exprtk::expression<double> expression_t;
  2209. // Setup global constants symbol table
  2210. symbol_table_t glbl_const_symbol_table;
  2211. glbl_const_symbtab.add_constants(); // pi, epsilon and inf
  2212. glbl_const_symbtab.add_constant("speed_of_light",299e6);
  2213. glbl_const_symbtab.add_constant("avogadro_number",6e23);
  2214. // Setup global function symbol table
  2215. symbol_table_t glbl_funcs_symbol_table;
  2216. glbl_func_symbtab.add_function('distance',distance);
  2217. glbl_func_symbtab.add_function('calc_spherevol',calc_sphrvol);
  2218. ......
  2219. // Setup global variable symbol table
  2220. symbol_table_t glbl_variable_symbol_table;
  2221. glbl_variable_symbtab.add_variable('temp_outside',thermo.outside);
  2222. glbl_variable_symbtab.add_variable('temp_inside' ,thermo.inside );
  2223. glbl_variable_symbtab.add_variable('num_cstmrs',store.num_cstmrs);
  2224. ......
  2225. double x,y,z;
  2226. // Setup expression specific symbol table
  2227. symbol_table_t symbol_table;
  2228. symbol_table.add_variable('x',x);
  2229. symbol_table.add_variable('y',y);
  2230. symbol_table.add_variable('z',z);
  2231. expression_t expression;
  2232. // Register the various symbol tables
  2233. expression
  2234. .register_symbol_table(symbol_table);
  2235. expression
  2236. .register_symbol_table(glbl_funcs_symbol_table);
  2237. expression
  2238. .register_symbol_table(glbl_const_symbol_table);
  2239. expression
  2240. .register_symbol_table(glbl_variable_symbol_table);
  2241. std::string expression_str =
  2242. "abs(temp_inside - temp_outside) + 2 * speed_of_light / x";
  2243. parser_t parser;
  2244. parser.compile(expression_str,expression);
  2245. ......
  2246. while (keep_evaluating)
  2247. {
  2248. ....
  2249. T result = expression.value();
  2250. ....
  2251. }
  2252. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2253. [SECTION 18 - UNKNOWN UNKNOWNS]
  2254. In this section we will discuss the process of handling expressions
  2255. with a mix of known and unknown variables. Initially a discussion into
  2256. the types of expressions that exist will be provided, then a series of
  2257. possible solutions will be presented for each scenario.
  2258. When parsing an expression, there may be situations where one is not
  2259. fully aware of what if any variables will be used prior to the
  2260. expression being compiled.
  2261. This can become problematic, as in the default scenario it is assumed
  2262. the symbol_table that is registered with the expression instance will
  2263. already possess the externally available variables, functions and
  2264. constants needed during the compilation of the expression.
  2265. In the event there are symbols in the expression that can't be mapped
  2266. to either a reserved word, or located in the associated
  2267. symbol_table(s), an "Undefined symbol" error will be raised and the
  2268. compilation process will fail.
  2269. The numerous scenarios that can occur when compiling an expression
  2270. with ExprTk generally fall into one of the following three categories:
  2271. (a) No external variables
  2272. (b) Predetermined set of external variables
  2273. (c) Unknown set of variables
  2274. (a) No external variables
  2275. These are expressions that contain no external variables but may
  2276. contain local variables. As local variables cannot be accessed
  2277. externally from the expression, it is assumed that such expressions
  2278. will not have a need for a symbol_table and furthermore expressions
  2279. which don't make use of functions that have side-effects will be
  2280. evaluated completely at compile time resulting in a constant return
  2281. value. The following are examples of such expressions:
  2282. (1) 1 + 2
  2283. (2) var x := 3; 2 * x - 3
  2284. (3) var x := 3; var y := abs(x - 8); x - y / 7
  2285. (b) Predetermined set of external variables
  2286. These are expressions that are comprised of externally available
  2287. variables and functions and will only compile successfully if the
  2288. symbols that correspond to the variables and functions are already
  2289. defined in their associated symbol_table(s). This is by far the most
  2290. common scenario when using ExprTk.
  2291. As an example, one may have three external variables: x, y and z which
  2292. have been registered with the associated symbol_table, and will then
  2293. need to compile and evaluate expressions comprised of any subset of
  2294. these three variables. The following are a few examples of such
  2295. expressions:
  2296. (1) 1 + x
  2297. (2) x / y
  2298. (3) 2 * x * y / z
  2299. In this scenario one can use the 'dependent_entity_collector'
  2300. component as described in [Section 16] to further determine which of
  2301. the registered variables were actually used in the given expression.
  2302. As an example once the set of utilised variables are known, any
  2303. further 'attention' can be restricted to only those variables when
  2304. evaluating the expression. This can be quite useful when dealing with
  2305. expressions that can draw from a set of hundreds or even thousands of
  2306. variables.
  2307. (c) Unknown set of variables
  2308. These are expressions that are comprised of symbols other than the
  2309. standard ExprTk reserved words or what has been registered with their
  2310. associated symbol_table, and will normally fail compilation due to the
  2311. associated symbol_table not having a reference to them. As such this
  2312. scenario can be seen as a combination of scenario B, where one may
  2313. have a symbol_table with registered variables, but would also like to
  2314. handle the situation of variables that aren't present in said
  2315. symbol_table.
  2316. When dealing with expressions of category (c), one must perform all of
  2317. the following:
  2318. (1) Determine the variables used in the expression
  2319. (2) Populate a symbol_table(s) with the entities from (1)
  2320. (3) Compile the expression
  2321. (4) Provide a means by which the entities from (1) can be modified
  2322. Depending on the nature of processing, steps (1) and (2) can be done
  2323. either independently of each other or combined into one. The following
  2324. example will initially look at solving the problem of unknown
  2325. variables with the latter method using the 'unknown_symbol_resolver'
  2326. component.
  2327. typedef exprtk::symbol_table<T> symbol_table_t;
  2328. typedef exprtk::expression<T> expression_t;
  2329. typedef exprtk::parser<T> parser_t;
  2330. symbol_table_t unknown_var_symbol_table;
  2331. symbol_table_t symbol_table;
  2332. symbol_table.add_variable("x",x);
  2333. symbol_table.add_variable("y",y);
  2334. expression_t expression;
  2335. expression.register_symbol_table(unknown_var_symbol_table);
  2336. expression.register_symbol_table(symbol_table);
  2337. parser_t parser;
  2338. parser.enable_unknown_symbol_resolver();
  2339. std::string expression_str = "x + abs(y / 3k) * z + 2";
  2340. parser.compile(expression_str,expression);
  2341. In the example above, the symbols 'k' and 'z' will be treated as
  2342. unknown symbols. The parser in the example is set to handle unknown
  2343. symbols using the built-in default unknown_symbol_resolver (USR). The
  2344. default USR will automatically resolve any unknown symbols as a
  2345. variable (scalar type). The new variables will be added to the primary
  2346. symbol_table, which in this case is the 'unknown_var_symbol_table'
  2347. instance. Once the compilation has completed successfully, the
  2348. variables that were resolved during compilation can be accessed from
  2349. the primary symbol_table using the 'get_variable_list' and
  2350. 'variable_ref' methods and then if needed can be modified accordingly
  2351. after which the expression itself can be evaluated.
  2352. std::vector<std::string> variable_list;
  2353. unknown_var_symbol_table.get_variable_list(variable_list);
  2354. for (auto& var_name : variable_list)
  2355. {
  2356. T& v = unknown_var_symbol_table.variable_ref(var_name);
  2357. v = ...;
  2358. }
  2359. ...
  2360. expression.value();
  2361. Note: As previously mentioned the default USR will automatically
  2362. assume any unknown symbol to be a valid scalar variable, and will then
  2363. proceed to add said symbol as a variable to the primary symbol_table
  2364. of the associated expression during the compilation process. However a
  2365. problem that may arise, is that expressions that are parsed with the
  2366. USR enabled, but contain 'typos' or otherwise syntactic errors may
  2367. inadvertently compile successfully due to the simplistic nature of the
  2368. default USR. The following are some example expressions:
  2369. (1) 1 + abz(x + 1)
  2370. (2) sine(y / 2) - coz(3x)
  2371. The two expressions above contain misspelt symbols (abz, sine, coz)
  2372. which if implied multiplications and default USR are enabled during
  2373. compilation will result in them being assumed to be valid 'variables',
  2374. which obviously is not the intended outcome by the user. A possible
  2375. solution to this problem is for one to implement their own specific
  2376. USR that will perform a user defined business logic in determining if
  2377. an encountered unknown symbol should be treated as a variable or if it
  2378. should raise a compilation error. The following example demonstrates a
  2379. simple user defined USR:
  2380. typedef exprtk::symbol_table<T> symbol_table_t;
  2381. typedef exprtk::expression<T> expression_t;
  2382. typedef exprtk::parser<T> parser_t;
  2383. template <typename T>
  2384. struct my_usr : public parser_t::unknown_symbol_resolver
  2385. {
  2386. typedef typename parser_t::unknown_symbol_resolver usr_t;
  2387. bool process(const std::string& unknown_symbol,
  2388. typename usr_t::usr_symbol_type& st,
  2389. T& default_value,
  2390. std::string& error_message)
  2391. {
  2392. if (0 != unknown_symbol.find("var_"))
  2393. {
  2394. error_message = "Invalid symbol: " + unknown_symbol;
  2395. return false;
  2396. }
  2397. st = usr_t::e_usr_variable_type;
  2398. default_value = T(123.123);
  2399. return true;
  2400. }
  2401. };
  2402. ...
  2403. symbol_table_t unknown_var_symbol_table;
  2404. symbol_table_t symbol_table;
  2405. symbol_table.add_variable("x",x);
  2406. symbol_table.add_variable("y",y);
  2407. expression_t expression;
  2408. expression.register_symbol_table(unknown_var_symbol_table);
  2409. expression.register_symbol_table(symbol_table);
  2410. my_usr<T> musr;
  2411. parser_t parser;
  2412. parser.enable_unknown_symbol_resolver(&musr);
  2413. std::string expression_str = "var_x + abs(var_y - 3) * var_z";
  2414. parser.compile(expression_str,expression);
  2415. In the example above, a user specified USR is defined, and is
  2416. registered with the parser enabling the USR functionality. Then when
  2417. an unknown symbol is encountered during the compilation process, the
  2418. USR's process method will be invoked. The USR in the example will only
  2419. 'accept' unknown symbols that have a prefix of 'var_' as being valid
  2420. variables, all other unknown symbols will result in a compilation
  2421. error being raised.
  2422. In the example above the callback of the USR that is invoked during
  2423. the unknown symbol resolution process only allows for scalar variables
  2424. to be defined and resolved - as that is the simplest and most common
  2425. form.
  2426. There is also an extended version of the callback that can be
  2427. overridden that will allow for more control and choice over the type
  2428. of symbol being resolved. The following is an example definition of
  2429. said extended callback:
  2430. template <typename T>
  2431. struct my_usr : public parser_t::unknown_symbol_resolver
  2432. {
  2433. typedef typename parser_t::unknown_symbol_resolver usr_t;
  2434. my_usr()
  2435. : usr_t(usr_t::e_usrmode_extended)
  2436. {}
  2437. virtual bool process(const std::string& unknown_symbol,
  2438. symbol_table_t& symbol_table,
  2439. std::string& error_message)
  2440. {
  2441. bool result = false;
  2442. if (0 == unknown_symbol.find("var_"))
  2443. {
  2444. // Default value of zero
  2445. result = symbol_table.create_variable(unknown_symbol,0);
  2446. if (!result)
  2447. {
  2448. error_message = "Failed to create variable...";
  2449. }
  2450. }
  2451. else if (0 == unknown_symbol.find("str_"))
  2452. {
  2453. // Default value of empty string
  2454. result = symbol_table.create_stringvar(unknown_symbol,"");
  2455. if (!result)
  2456. {
  2457. error_message = "Failed to create string variable...";
  2458. }
  2459. }
  2460. else
  2461. error_message = "Indeterminable symbol type.";
  2462. return result;
  2463. }
  2464. };
  2465. In the example above, the USR callback when invoked will pass the
  2466. primary symbol table associated with the expression being parsed. The
  2467. symbol resolution business logic can then determine under what
  2468. conditions a symbol will be resolved including its type (scalar,
  2469. string, vector etc) and default value. When the callback successfully
  2470. returns the symbol parsing and resolution process will again be
  2471. executed by the parser. The idea here is that given the primary symbol
  2472. table will now have the previously detected unknown symbol registered,
  2473. it will be correctly resolved and the general parsing processing can
  2474. then resume as per normal.
  2475. Note: In order to have the USR's extended mode callback be invoked It
  2476. is necessary to pass the e_usrmode_extended enum value during the
  2477. constructor of the user defined USR.
  2478. Note: The primary symbol table for an expression is the first symbol
  2479. table to be registered with that instance of the expression.
  2480. Note: For a successful symbol resolution using the normal USR all of
  2481. the following are required:
  2482. (1) Only if successful shall the process method return TRUE
  2483. (2) The default_value parameter will have been set
  2484. (3) The error_message parameter will be empty
  2485. (4) usr_symbol_type input parameter field will be set to either:
  2486. (*) e_usr_variable_type
  2487. (*) e_usr_constant_type
  2488. Note: For a successful symbol resolution using the extended USR all of
  2489. the following are required:
  2490. (1) Only if successful shall the process method return TRUE
  2491. (2) symbol_table parameter will have had the newly resolved
  2492. variable or string added to it
  2493. (3) error_message parameter will be empty
  2494. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2495. [SECTION 19 - ENABLING & DISABLING FEATURES]
  2496. The parser can be configured via its settings instance to either allow
  2497. or disallow certain features that are available within the ExprTk
  2498. grammar. The features fall into one of the following six categories:
  2499. (1) Base Functions
  2500. (2) Control Flow Structures
  2501. (3) Logical Operators
  2502. (4) Arithmetic Operators
  2503. (5) Inequality Operators
  2504. (6) Assignment Operators
  2505. (1) Base Functions
  2506. The list of available base functions is as follows:
  2507. abs, acos, acosh, asin, asinh, atan, atanh, atan2, avg, ceil,
  2508. clamp, cos, cosh, cot, csc, equal, erf, erfc, exp, expm1,
  2509. floor, frac, hypot, iclamp, like, log, log10, log2, logn,
  2510. log1p, mand, max, min, mod, mor, mul, ncdf, pow, root, round,
  2511. roundn, sec, sgn, sin, sinc, sinh, sqrt, sum, swap, tan, tanh,
  2512. trunc, not_equal, inrange, deg2grad, deg2rad, rad2deg, grad2deg
  2513. The above mentioned base functions can be either enabled or disabled
  2514. 'all' at once, as is demonstrated below:
  2515. parser_t parser;
  2516. expression_t expression;
  2517. parser.settings().disable_all_base_functions();
  2518. parser
  2519. .compile("2 * abs(2 - 3)",expression); // compilation failure
  2520. parser.settings().enable_all_base_functions();
  2521. parser
  2522. .compile("2 * abs(2 - 3)",expression); // compilation success
  2523. One can also enable or disable specific base functions. The following
  2524. example demonstrates the disabling of the trigonometric functions
  2525. 'sin' and 'cos':
  2526. parser_t parser;
  2527. expression_t expression;
  2528. parser.settings()
  2529. .disable_base_function(settings_t::e_bf_sin)
  2530. .disable_base_function(settings_t::e_bf_cos);
  2531. parser
  2532. .compile("(sin(x) / cos(x)) == tan(x)",expression); // failure
  2533. parser.settings()
  2534. .enable_base_function(settings_t::e_bf_sin)
  2535. .enable_base_function(settings_t::e_bf_cos);
  2536. parser
  2537. .compile("(sin(x) / cos(x)) == tan(x)",expression); // success
  2538. (2) Control Flow Structures
  2539. The list of available control flow structures is as follows:
  2540. (a) If or If-Else
  2541. (b) Switch statement
  2542. (c) For Loop
  2543. (d) While Loop
  2544. (e) Repeat Loop
  2545. The above mentioned control flow structures can be either enabled
  2546. or disabled 'all' at once, as is demonstrated below:
  2547. parser_t parser;
  2548. expression_t expression;
  2549. std::string program =
  2550. " var x := 0; "
  2551. " for (var i := 0; i < 10; i += 1) "
  2552. " { "
  2553. " x += i; "
  2554. " } ";
  2555. parser.settings().disable_all_control_structures();
  2556. parser
  2557. .compile(program,expression); // compilation failure
  2558. parser.settings().enable_all_control_structures();
  2559. parser
  2560. .compile(program,expression); // compilation success
  2561. One can also enable or disable specific control flow structures. The
  2562. following example demonstrates the disabling of the for-loop control
  2563. flow structure:
  2564. parser_t parser;
  2565. expression_t expression;
  2566. std::string program =
  2567. " var x := 0; "
  2568. " for (var i := 0; i < 10; i += 1) "
  2569. " { "
  2570. " x += i; "
  2571. " } ";
  2572. parser.settings()
  2573. .disable_control_structure(settings_t::e_ctrl_for_loop);
  2574. parser
  2575. .compile(program,expression); // failure
  2576. parser.settings()
  2577. .enable_control_structure(settings_t::e_ctrl_for_loop);
  2578. parser
  2579. .compile(program,expression); // success
  2580. (3) Logical Operators
  2581. The list of available logical operators is as follows:
  2582. and, nand, nor, not, or, xnor, xor, &, |
  2583. The above mentioned logical operators can be either enabled or
  2584. disabled 'all' at once, as is demonstrated below:
  2585. parser_t parser;
  2586. expression_t expression;
  2587. parser.settings().disable_all_logic_ops();
  2588. parser
  2589. .compile("1 or not(0 and 1)",expression); // compilation failure
  2590. parser.settings().enable_all_logic_ops();
  2591. parser
  2592. .compile("1 or not(0 and 1)",expression); // compilation success
  2593. One can also enable or disable specific logical operators. The
  2594. following example demonstrates the disabling of the 'and' logical
  2595. operator:
  2596. parser_t parser;
  2597. expression_t expression;
  2598. parser.settings()
  2599. .disable_logic_operation(settings_t::e_logic_and);
  2600. parser
  2601. .compile("1 or not(0 and 1)",expression); // failure
  2602. parser.settings()
  2603. .enable_logic_operation(settings_t::e_logic_and);
  2604. parser
  2605. .compile("1 or not(0 and 1)",expression); // success
  2606. (4) Arithmetic Operators
  2607. The list of available arithmetic operators is as follows:
  2608. +, -, *, /, %, ^
  2609. The above mentioned arithmetic operators can be either enabled or
  2610. disabled 'all' at once, as is demonstrated below:
  2611. parser_t parser;
  2612. expression_t expression;
  2613. parser.settings().disable_all_arithmetic_ops();
  2614. parser
  2615. .compile("1 + 2 / 3",expression); // compilation failure
  2616. parser.settings().enable_all_arithmetic_ops();
  2617. parser
  2618. .compile("1 + 2 / 3",expression); // compilation success
  2619. One can also enable or disable specific arithmetic operators. The
  2620. following example demonstrates the disabling of the addition '+'
  2621. arithmetic operator:
  2622. parser_t parser;
  2623. expression_t expression;
  2624. parser.settings()
  2625. .disable_arithmetic_operation(settings_t::e_arith_add);
  2626. parser
  2627. .compile("1 + 2 / 3",expression); // failure
  2628. parser.settings()
  2629. .enable_arithmetic_operation(settings_t::e_arith_add);
  2630. parser
  2631. .compile("1 + 2 / 3",expression); // success
  2632. (5) Inequality Operators
  2633. The list of available inequality operators is as follows:
  2634. <, <=, >, >=, ==, =, != <>
  2635. The above mentioned inequality operators can be either enabled or
  2636. disabled 'all' at once, as is demonstrated below:
  2637. parser_t parser;
  2638. expression_t expression;
  2639. parser.settings().disable_all_inequality_ops();
  2640. parser
  2641. .compile("1 < 3",expression); // compilation failure
  2642. parser.settings().enable_all_inequality_ops();
  2643. parser
  2644. .compile("1 < 3",expression); // compilation success
  2645. One can also enable or disable specific inequality operators. The
  2646. following example demonstrates the disabling of the less-than '<'
  2647. inequality operator:
  2648. parser_t parser;
  2649. expression_t expression;
  2650. parser.settings()
  2651. .disable_inequality_operation(settings_t::e_ineq_lt);
  2652. parser
  2653. .compile("1 < 3",expression); // failure
  2654. parser.settings()
  2655. .enable_inequality_operation(settings_t::e_ineq_lt);
  2656. parser
  2657. .compile("1 < 3",expression); // success
  2658. (6) Assignment Operators
  2659. The list of available assignment operators is as follows:
  2660. :=, +=, -=, *=, /=, %=
  2661. The above mentioned assignment operators can be either enabled or
  2662. disabled 'all' at once, as is demonstrated below:
  2663. parser_t parser;
  2664. expression_t expression;
  2665. symbol_table_t symbol_table;
  2666. T x = T(0);
  2667. symbol_table.add_variable("x",x);
  2668. expression.register_symbol_table(symbol_table);
  2669. parser.settings().disable_all_assignment_ops();
  2670. parser
  2671. .compile("x := 3",expression); // compilation failure
  2672. parser.settings().enable_all_assignment_ops();
  2673. parser
  2674. .compile("x := 3",expression); // compilation success
  2675. One can also enable or disable specific assignment operators. The
  2676. following example demonstrates the disabling of the '+=' addition
  2677. assignment operator:
  2678. parser_t parser;
  2679. expression_t expression;
  2680. symbol_table_t symbol_table;
  2681. T x = T(0);
  2682. symbol_table.add_variable("x",x);
  2683. expression.register_symbol_table(symbol_table);
  2684. parser.settings()
  2685. .disable_assignment_operation(settings_t::e_assign_addass);
  2686. parser
  2687. .compile("x += 3",expression); // failure
  2688. parser.settings()
  2689. .enable_assignment_operation(settings_t::e_assign_addass);
  2690. parser
  2691. .compile("x += 3",expression); // success
  2692. Note: In the event of a base function being disabled, one can redefine
  2693. the base function using the standard custom function definition
  2694. process. In the following example the 'sin' function is disabled then
  2695. redefined as a function taking degree input.
  2696. template <typename T>
  2697. struct sine_deg : public exprtk::ifunction<T>
  2698. {
  2699. sine_deg() : exprtk::ifunction<T>(1) {}
  2700. inline T operator()(const T& v)
  2701. {
  2702. const T pi = exprtk::details::numeric::constant::pi;
  2703. return std::sin((v * T(pi)) / T(180));
  2704. }
  2705. };
  2706. ...
  2707. typedef exprtk::symbol_table<T> symbol_table_t;
  2708. typedef exprtk::expression<T> expression_t;
  2709. typedef exprtk::parser<T> parser_t;
  2710. typedef typename parser_t::settings_store settings_t;
  2711. sine_deg<T> sine;
  2712. symbol_table.add_reserved_function("sin",sine);
  2713. expression_t expression;
  2714. expression.register_symbol_table(symbol_table);
  2715. parser_t parser;
  2716. parser.settings()
  2717. .disable_base_function(settings_t::e_bf_sin);
  2718. parser.compile("1 + sin(30)",expression);
  2719. In the example above, the custom 'sin' function is registered with the
  2720. symbol_table using the method 'add_reserved_function'. This is done so
  2721. as to bypass the checks for reserved words that are carried out on the
  2722. provided symbol names when calling the standard 'add_function' method.
  2723. Normally if a user specified symbol name conflicts with any of the
  2724. ExprTk reserved words, the add_function call will fail.
  2725. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2726. [SECTION 20 - EXPRESSION RETURN VALUES]
  2727. ExprTk expressions can return immediately from any point by utilizing
  2728. the return call. Furthermore the return call can be used to transfer
  2729. out multiple return values from within the expression.
  2730. If an expression evaluation exits using a return point, the result of
  2731. the call to the 'value' method will be NaN, and it's expected that the
  2732. return values will be available from the results_context.
  2733. In the following example there are three return points in the
  2734. expression. If neither of the return points are hit, then the
  2735. expression will return normally.
  2736. std::string expression_string =
  2737. " if (x < y) "
  2738. " return [x + 1,'return-call 1']; "
  2739. " else if (x > y) "
  2740. " return [y / 2, y + 1, 'return-call 2']; "
  2741. " else if (equal(x,y)) "
  2742. " x + y; "
  2743. " return [x, y, x + y, x - y, 'return-call 3'] ";
  2744. typedef exprtk::symbol_table<double> symbol_table_t;
  2745. typedef exprtk::expression<double> expression_t;
  2746. typedef exprtk::parser<double> parser_t;
  2747. symbol_table_t symbol_table;
  2748. expression_t expression;
  2749. parser_t parser;
  2750. double x = 0;
  2751. double y = 0;
  2752. symbol_table.add_variable("x",x);
  2753. symbol_table.add_variable("y",y);
  2754. expression.register_symbol_table(symbol_table);
  2755. parser.compile(expression_string,expression);
  2756. T result = expression.value();
  2757. if (expression.results().count())
  2758. {
  2759. typedef exprtk::results_context<T> results_context_t;
  2760. typedef typename results_context_t::type_store_t type_t;
  2761. typedef typename type_t::scalar_view scalar_t;
  2762. typedef typename type_t::vector_view vector_t;
  2763. typedef typename type_t::string_view string_t;
  2764. const results_context_t& results = expression.results();
  2765. for (std::size_t i = 0; i < results.count(); ++i)
  2766. {
  2767. type_t t = results[i];
  2768. switch (t.type)
  2769. {
  2770. case type_t::e_scalar : ...
  2771. break;
  2772. case type_t::e_vector : ...
  2773. break;
  2774. case type_t::e_string : ...
  2775. break;
  2776. default : continue;
  2777. }
  2778. }
  2779. Note: Processing of the return results is similar to that of the
  2780. generic function call parameters.
  2781. It is however recommended that if there is to be only a single flow of
  2782. execution through the expression, that the simpler approach of
  2783. registering external variables of appropriate type be used.
  2784. This method simply requires the variables that are to hold the various
  2785. results that are to be computed within the expression to be registered
  2786. with an associated symbol_table instance. Then within the expression
  2787. itself to have the result variables be assigned the appropriate
  2788. values.
  2789. typedef exprtk::symbol_table<double> symbol_table_t;
  2790. typedef exprtk::expression<double> expression_t;
  2791. typedef exprtk::parser<double> parser_t;
  2792. std::string expression_string =
  2793. " var x := 123.456; "
  2794. " var s := 'ijk'; "
  2795. " result0 := x + 78.90; "
  2796. " result1 := s + '123' ";
  2797. double result0;
  2798. std::string result1;
  2799. symbol_table_t symbol_table;
  2800. symbol_table.add_variable ("result0",result0);
  2801. symbol_table.add_stringvar("result1",result1);
  2802. expression_t expression;
  2803. expression.register_symbol_table(symbol_table);
  2804. parser_t parser;
  2805. parser.compile(expression_string,expression);
  2806. expression.value();
  2807. printf("Result0: %15.5f\n", result0 );
  2808. printf("Result1: %s\n" , result1.c_str());
  2809. In the example above, the expression will compute two results. As such
  2810. two result variables are defined to hold the values named result0 and
  2811. result1 respectively. The first is of scalar type (double), the second
  2812. is of string type. Once the expression has been evaluated, the two
  2813. variables will have been updated with the new result values, and can
  2814. then be further utilised from within the calling program.
  2815. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2816. [SECTION 21 - COMPILATION ERRORS]
  2817. When attempting to compile a malformed or otherwise erroneous ExprTk
  2818. expression, the compilation process will result in an error, as is
  2819. indicated by the 'compile' method returning a false value. A
  2820. diagnostic indicating the first error encountered and its cause can be
  2821. obtained by invoking the 'error' method, as is demonstrated in the
  2822. following example:
  2823. if (!parser.compile(expression_string,expression))
  2824. {
  2825. printf("Error: %s\n", parser.error().c_str());
  2826. return false;
  2827. }
  2828. Any error(s) resulting from a failed compilation will be stored in the
  2829. parser instance until the next time a compilation is performed. Before
  2830. then errors can be enumerated in the order they occurred by invoking
  2831. the 'get_error' method which itself will return a 'parser_error' type.
  2832. A parser_error object will contain an error diagnostic, an error mode
  2833. (or class), and the character position of the error in the expression
  2834. string. The following example demonstrates the enumeration of error(s)
  2835. in the event of a failed compilation.
  2836. if (!parser.compile(expression_string,expression))
  2837. {
  2838. for (std::size_t i = 0; i < parser.error_count(); ++i)
  2839. {
  2840. typedef exprtk::parser_error::type error_t;
  2841. error_t error = parser.get_error(i);
  2842. printf("Error[%02d] Position: %02d Type: [%14s] Msg: %s\n",
  2843. i,
  2844. error.token.position,
  2845. exprtk::parser_error::to_str(error.mode).c_str(),
  2846. error.diagnostic.c_str());
  2847. }
  2848. return false;
  2849. }
  2850. Assuming the following expression '2 + (3 / log(1 + x))' which uses a
  2851. variable named 'x' that has not been registered with the appropriate
  2852. symbol_table instance and is not a locally defined variable, once
  2853. compiled the above denoted post compilation error handling code shall
  2854. produce the following output:
  2855. Error: ERR184 - Undefined symbol: 'x'
  2856. Error[00] Pos:17 Type:[Syntax] Msg: ERR184 - Undefined symbol: 'x'
  2857. For expressions comprised of multiple lines, the error position
  2858. provided in the parser_error object can be converted into a pair of
  2859. line and column numbers by invoking the 'update_error' function as is
  2860. demonstrated by the following example:
  2861. if (!parser.compile(program_str,expression))
  2862. {
  2863. for (std::size_t i = 0; i < parser.error_count(); ++i)
  2864. {
  2865. typedef exprtk::parser_error::type error_t;
  2866. error_t error = parser.get_error(i);
  2867. exprtk::parser_error::update_error(error,program_str);
  2868. printf("Error[%02d] at line: %d column: %d\n",
  2869. i,
  2870. error.line_no,
  2871. error.column_no);
  2872. }
  2873. return false;
  2874. }
  2875. Note: There are five distinct error modes in ExprTk which denote the
  2876. class of an error. These classes are as follows:
  2877. (a) Syntax
  2878. (b) Token
  2879. (c) Numeric
  2880. (d) Symbol Table
  2881. (e) Lexer
  2882. (a) Syntax Errors
  2883. These are errors related to invalid syntax found within the denoted
  2884. expression. Examples are invalid sequences of operators and variables,
  2885. incorrect number of parameters to functions, invalid conditional or
  2886. loop structures and invalid use of keywords.
  2887. eg: 'for := sin(x,y,z) + 2 * equal > until[2 - x,3]'
  2888. (b) Token Errors
  2889. Errors in this class relate to token level errors detected by one or
  2890. more of the following checkers:
  2891. (1) Bracket Checker
  2892. (2) Numeric Checker
  2893. (3) Sequence Checker
  2894. (c) Numeric Errors
  2895. This class of error is related to conversion of numeric values from
  2896. their string form to the underlying numerical type (float, double
  2897. etc).
  2898. (d) Symbol Table Errors
  2899. This is the class of errors related to failures when interacting with
  2900. the registered symbol_table instance. Errors such as not being able to
  2901. find, within the symbol_table, symbols representing variables or
  2902. functions, to being unable to create new variables in the symbol_table
  2903. via the 'unknown symbol resolver' mechanism.
  2904. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2905. [SECTION 22 - RUNTIME LIBRARY PACKAGES]
  2906. ExprTk contains a set of simple extensions, that provide
  2907. functionalities beyond basic numerical calculations. Currently the
  2908. available packages are:
  2909. +---+--------------------+-----------------------------------+
  2910. | # | Package Name | Namespace/Type |
  2911. +---+--------------------+-----------------------------------+
  2912. | 1 | Basic I/O | exprtk::rtl::io::package<T> |
  2913. | 2 | File I/O | exprtk::rtl::io::file::package<T> |
  2914. | 3 | Vector Operations | exprtk::rtl::vecops::package<T> |
  2915. +---+--------------------+-----------------------------------+
  2916. In order to make the features of a specific package available within
  2917. an expression, an instance of the package must be added to the
  2918. expression's associated symbol table. In the following example, the
  2919. file I/O package is made available for the given expression:
  2920. typedef exprtk::symbol_table<T> symbol_table_t;
  2921. typedef exprtk::expression<T> expression_t;
  2922. typedef exprtk::parser<T> parser_t;
  2923. exprtk::rtl::io::file::package<T> fileio_package;
  2924. std::string expression_string =
  2925. " var file_name := 'file.txt'; "
  2926. " var stream := null; "
  2927. " "
  2928. " stream := open(file_name,'w'); "
  2929. " "
  2930. " write(stream,'Hello world....\n'); "
  2931. " "
  2932. " close(stream); "
  2933. " ";
  2934. symbol_table_t symbol_table;
  2935. symbol_table.add_package(fileio_package);
  2936. expression_t expression;
  2937. expression.register_symbol_table(symbol_table);
  2938. parser_t parser;
  2939. parser.compile(expression_string,expression);
  2940. expression.value();
  2941. (1) Basic I/O functions:
  2942. (a) print
  2943. (b) println
  2944. (2) File I/O functions:
  2945. (a) open (b) close
  2946. (c) write (d) read
  2947. (e) getline (f) eof
  2948. (3) Vector Operations functions:
  2949. (a) all_true (b) all_false
  2950. (c) any_true (d) any_false
  2951. (e) count (f) copy
  2952. (g) rotate-left (h) rotate-right
  2953. (i) shift-left (j) shift-right
  2954. (k) sort (l) nth_element
  2955. (m) iota (n) sumk
  2956. (o) axpy (p) axpby
  2957. (q) axpyz (r) axpbyz
  2958. (s) axpbz (t) dot
  2959. (u) dotk
  2960. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2961. [SECTION 23 - HELPERS & UTILS]
  2962. The ExprTk library provides a series of usage simplifications via
  2963. helper routines that combine various processes into a single 'function
  2964. call' making certain actions easier to carry out though not
  2965. necessarily in the most efficient way possible. A list of the routines
  2966. are as follows:
  2967. (a) collect_variables
  2968. (b) collect_functions
  2969. (c) compute
  2970. (d) integrate
  2971. (e) derivative
  2972. (f) second_derivative
  2973. (g) third_derivative
  2974. (a) collect_variables
  2975. This function will collect all the variable symbols in a given string
  2976. representation of an expression and return them in an STL compatible
  2977. sequence data structure (eg: std::vector, dequeue etc) specialised
  2978. upon a std::string type. If an error occurs during the parsing of the
  2979. expression then the return value of the function will be false,
  2980. otherwise it will be true. An example use of the given routine is as
  2981. follows:
  2982. std::string expression = "x + abs(y / z)";
  2983. std::vector<std::string> variable_list;
  2984. if (exprtk::collect_variables(expression, variable_list))
  2985. {
  2986. for (const auto& var : variable_list)
  2987. {
  2988. ...
  2989. }
  2990. }
  2991. else
  2992. printf("An error occurred.");
  2993. (b) collect_functions
  2994. This function will collect all the function symbols in a given string
  2995. representation of an expression and return them in an STL compatible
  2996. sequence data structure (eg: std::vector, dequeue etc) specialised
  2997. upon a std::string type. If an error occurs during the parsing of the
  2998. expression then the return value of the function will be false,
  2999. otherwise it will be true. An example use of the given routine is as
  3000. follows:
  3001. std::string expression = "x + abs(y / cos(1 + z))";
  3002. std::deque<std::string> function_list;
  3003. if (exprtk::collect_functions(expression, function_list))
  3004. {
  3005. for (const auto& func : function_list)
  3006. {
  3007. ...
  3008. }
  3009. }
  3010. else
  3011. printf("An error occurred.");
  3012. Note: When either the 'collect_variables' or 'collect_functions' free
  3013. functions return true - that does not necessarily indicate the
  3014. expression itself is valid. It is still possible that when compiled
  3015. the expression may have certain 'type' related errors - though it is
  3016. highly likely that no semantic errors will occur if either return
  3017. true.
  3018. Note: The default interface provided for both the collect_variables
  3019. and collect_functions free_functions, assumes that expressions will
  3020. only be utilising the ExprTk reserved functions (eg: abs, cos, min
  3021. etc). When user defined functions are to be used in an expression, a
  3022. symbol_table instance containing said functions can be passed to
  3023. either routine, and will be incorporated during the compilation and
  3024. Dependent Entity Collection processes. In the following example, a
  3025. user defined free function named 'foo' is registered with a
  3026. symbol_table. Finally the symbol_table instance and associated
  3027. expression string are passed to the exprtk::collect_functions routine.
  3028. template <typename T>
  3029. T foo(T v)
  3030. {
  3031. return std::abs(v + T(2)) / T(3);
  3032. }
  3033. ......
  3034. exprtk::symbol_table<T> sym_tab;
  3035. symbol_table.add_function("foo",foo);
  3036. std::string expression = "x + foo(y / cos(1 + z))";
  3037. std::deque<std::string> function_list;
  3038. if (exprtk::collect_functions(expression, sym_tab, function_list))
  3039. {
  3040. for (const auto& func : function_list)
  3041. {
  3042. ...
  3043. }
  3044. }
  3045. else
  3046. printf("An error occurred.");
  3047. (c) compute
  3048. This free function will compute the value of an expression from its
  3049. string form. If an invalid expression is passed, the result of the
  3050. function will be false indicating an error, otherwise the return value
  3051. will be true indicating success. The compute function has three
  3052. overloads, the definitions of which are:
  3053. (1) No variables
  3054. (2) One variable called x
  3055. (3) Two variables called x and y
  3056. (3) Three variables called x, y and z
  3057. Example uses of each of the three overloads for the compute routine
  3058. are as follows:
  3059. T result = T(0);
  3060. // No variables overload
  3061. std::string no_vars = "abs(1 - (3 / pi)) * 5";
  3062. if (!exprtk::compute(no_vars,result))
  3063. printf("Failed to compute: %s",no_vars.c_str());
  3064. else
  3065. printf("Result: %15.5f\n",result);
  3066. // One variable 'x' overload
  3067. T x = 123.456;
  3068. std::string one_var = "abs(x - (3 / pi)) * 5";
  3069. if (!exprtk::compute(one_var, x, result))
  3070. printf("Failed to compute: %s",one_var.c_str());
  3071. else
  3072. printf("Result: %15.5f\n",result);
  3073. // Two variables 'x' and 'y' overload
  3074. T y = 789.012;
  3075. std::string two_var = "abs(x - (y / pi)) * 5";
  3076. if (!exprtk::compute(two_var, x, y, result))
  3077. printf("Failed to compute: %s",two_var.c_str());
  3078. else
  3079. printf("Result: %15.5f\n",result);
  3080. // Three variables 'x', 'y' and 'z' overload
  3081. T z = 345.678;
  3082. std::string three_var = "abs(x - (y / pi)) * z";
  3083. if (!exprtk::compute(three_var, x, y, z, result))
  3084. printf("Failed to compute: %s",three_var.c_str());
  3085. else
  3086. printf("Result: %15.5f\n",result);
  3087. (d) integrate
  3088. This free function will attempt to perform a numerical integration of
  3089. a single variable compiled expression over a specified range and step
  3090. size. The numerical integration is based on the three point form of
  3091. Simpson's rule. The integrate function has two overloads, where the
  3092. variable of integration can either be passed as a reference or as a
  3093. name in string form. Example usage of the function is as follows:
  3094. typedef exprtk::parser<T> parser_t;
  3095. typedef exprtk::expression<T> expression_t;
  3096. typedef exprtk::symbol_table<T> symbol_table_t;
  3097. std::string expression_string = "sqrt(1 - (x^2))";
  3098. T x = T(0);
  3099. symbol_table_t symbol_table;
  3100. symbol_table.add_variable("x",x);
  3101. expression_t expression;
  3102. expression.register_symbol_table(symbol_table);
  3103. parser_t parser;
  3104. parser.compile(expression_string,expression);
  3105. ....
  3106. // Integrate in domain [-1,1] using a reference to x variable
  3107. T area1 = exprtk::integrate(expression, x, T(-1), T(1));
  3108. // Integrate in domain [-1,1] using name of x variable
  3109. T area2 = exprtk::integrate(expression, "x", T(-1), T(1));
  3110. (e) derivative
  3111. This free function will attempt to perform a numerical differentiation
  3112. of a single variable compiled expression at a given point for a given
  3113. epsilon, using a variant of Newton's difference quotient called the
  3114. five-point stencil method. The derivative function has two overloads,
  3115. where the variable of differentiation can either be passed as a
  3116. reference or as a name in string form. Example usage of the derivative
  3117. function is as follows:
  3118. typedef exprtk::parser<T> parser_t;
  3119. typedef exprtk::expression<T> expression_t;
  3120. typedef exprtk::symbol_table<T> symbol_table_t;
  3121. std::string expression_string = "sqrt(1 - (x^2))";
  3122. T x = T(0);
  3123. symbol_table_t symbol_table;
  3124. symbol_table.add_variable("x",x);
  3125. expression_t expression;
  3126. expression.register_symbol_table(symbol_table);
  3127. parser_t parser;
  3128. parser.compile(expression_string,expression);
  3129. ....
  3130. // Differentiate expression at value of x = 12.3 using a reference
  3131. // to the x variable
  3132. x = T(12.3);
  3133. T derivative1 = exprtk::derivative(expression, x);
  3134. // Differentiate expression where value x = 45.6 using name
  3135. // of the x variable
  3136. x = T(45.6);
  3137. T derivative2 = exprtk::derivative(expression, "x");
  3138. (f) second_derivative
  3139. This free function will attempt to perform a numerical second
  3140. derivative of a single variable compiled expression at a given point
  3141. for a given epsilon, using a variant of Newton's difference quotient
  3142. method. The second_derivative function has two overloads, where the
  3143. variable of differentiation can either be passed as a reference or as
  3144. a name in string form. Example usage of the second_derivative function
  3145. is as follows:
  3146. typedef exprtk::parser<T> parser_t;
  3147. typedef exprtk::expression<T> expression_t;
  3148. typedef exprtk::symbol_table<T> symbol_table_t;
  3149. std::string expression_string = "sqrt(1 - (x^2))";
  3150. T x = T(0);
  3151. symbol_table_t symbol_table;
  3152. symbol_table.add_variable("x",x);
  3153. expression_t expression;
  3154. expression.register_symbol_table(symbol_table);
  3155. parser_t parser;
  3156. parser.compile(expression_string,expression);
  3157. ....
  3158. // Second derivative of expression where value of x = 12.3 using a
  3159. // reference to x variable
  3160. x = T(12.3);
  3161. T derivative1 = exprtk::second_derivative(expression,x);
  3162. // Second derivative of expression where value of x = 45.6 using
  3163. // name of x variable
  3164. x = T(45.6);
  3165. T derivative2 = exprtk::second_derivative(expression, "x");
  3166. (g) third_derivative
  3167. This free function will attempt to perform a numerical third
  3168. derivative of a single variable compiled expression at a given point
  3169. for a given epsilon, using a variant of Newton's difference quotient
  3170. method. The third_derivative function has two overloads, where the
  3171. variable of differentiation can either be passed as a reference or as
  3172. a name in string form. Example usage of the third_derivative function
  3173. is as follows:
  3174. typedef exprtk::parser<T> parser_t;
  3175. typedef exprtk::expression<T> expression_t;
  3176. typedef exprtk::symbol_table<T> symbol_table_t;
  3177. std::string expression_string = "sqrt(1 - (x^2))";
  3178. T x = T(0);
  3179. symbol_table_t symbol_table;
  3180. symbol_table.add_variable("x",x);
  3181. expression_t expression;
  3182. expression.register_symbol_table(symbol_table);
  3183. parser_t parser;
  3184. parser.compile(expression_string,expression);
  3185. ....
  3186. // Third derivative of expression where value of x = 12.3 using a
  3187. // reference to the x variable
  3188. x = T(12.3);
  3189. T derivative1 = exprtk::third_derivative(expression, x);
  3190. // Third derivative of expression where value of x = 45.6 using
  3191. // name of the x variable
  3192. x = T(45.6);
  3193. T derivative2 = exprtk::third_derivative(expression, "x");
  3194. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3195. [SECTION 24 - BENCHMARKING]
  3196. As part of the ExprTk package there is an expression benchmark utility
  3197. named 'exprtk_benchmark'. The utility attempts to determine expression
  3198. evaluation speed (or rate of evaluations - evals per second), by
  3199. evaluating each expression numerous times and mutating the underlying
  3200. variables of the expression between each evaluation. The utility
  3201. assumes any valid ExprTk expression (containing conditionals, loops
  3202. etc), however it will only make use of a predefined set of scalar
  3203. variables, namely: a, b, c, x, y, z and w. That being said expressions
  3204. themselves can contain any number of local variables, vectors or
  3205. strings. There are two modes of operation:
  3206. (1) Default
  3207. (2) User Specified Expressions
  3208. (1) Default
  3209. The default mode is enabled simply by executing the exprtk_benchmark
  3210. binary with no command line parameters. In this mode a predefined set
  3211. of expressions will be evaluated in three phases:
  3212. (a) ExprTk evaluation
  3213. (b) Native evaluation
  3214. (c) ExprTk parse
  3215. In the first two phases (a and b) a list of predefined (hard-coded)
  3216. expressions will be evaluated using both ExprTk and native mode
  3217. implementations. This is done so as to compare evaluation times
  3218. between ExprTk and native implementations. The set of expressions used
  3219. are as follows:
  3220. (01) (y + x)
  3221. (02) 2 * (y + x)
  3222. (03) (2 * y + 2 * x)
  3223. (04) ((1.23 * x^2) / y) - 123.123
  3224. (05) (y + x / y) * (x - y / x)
  3225. (06) x / ((x + y) + (x - y)) / y
  3226. (07) 1 - ((x * y) + (y / x)) - 3
  3227. (08) (5.5 + x) + (2 * x - 2 / 3 * y) * (x / 3 + y / 4) + (y + 7.7)
  3228. (09) 1.1x^1 + 2.2y^2 - 3.3x^3 + 4.4y^15 - 5.5x^23 + 6.6y^55
  3229. (10) sin(2 * x) + cos(pi / y)
  3230. (11) 1 - sin(2 * x) + cos(pi / y)
  3231. (12) sqrt(111.111 - sin(2 * x) + cos(pi / y) / 333.333)
  3232. (13) (x^2 / sin(2 * pi / y)) - x / 2
  3233. (14) x + (cos(y - sin(2 / x * pi)) - sin(x - cos(2 * y / pi))) - y
  3234. (15) clamp(-1.0, sin(2 * pi * x) + cos(y / 2 * pi), +1.0)
  3235. (16) max(3.33, min(sqrt(1 - sin(2 * x) + cos(pi / y) / 3), 1.11))
  3236. (17) if((y + (x * 2.2)) <= (x + y + 1.1), x - y, x*y) + 2 * pi / x
  3237. The third and final phase (c), is used to determine average
  3238. compilation rates (compiles per second) for expressions of varying
  3239. complexity. Each expression is compiled 100K times and the average for
  3240. each expression is output.
  3241. (2) User Specified Expressions
  3242. In this mode two parameters are passed to the utility via the command
  3243. line:
  3244. (a) A name of a text file containing one expression per line
  3245. (b) An integer representing the number of evaluations per expression
  3246. An example execution of the benchmark utility in this mode is as
  3247. follows:
  3248. ./exprtk_benchmark my_expressions.txt 1000000
  3249. The above invocation will load the expressions from the file
  3250. 'my_expressions.txt' and will then proceed to evaluate each expression
  3251. one million times, varying the above mentioned variables (x, y, z
  3252. etc.) between each evaluation, and at the end of each expression round
  3253. a print out of running times, result of a single evaluation and total
  3254. sum of results is provided as demonstrated below:
  3255. Expression 1 of 7 4.770 ns 47700 ns ( 9370368.0) '((((x+y)+z)))'
  3256. Expression 2 of 7 4.750 ns 47500 ns ( 1123455.9) '((((x+y)-z)))'
  3257. Expression 3 of 7 4.766 ns 47659 ns (21635410.7) '((((x+y)*z)))'
  3258. Expression 4 of 7 5.662 ns 56619 ns ( 1272454.9) '((((x+y)/z)))'
  3259. Expression 5 of 7 4.950 ns 49500 ns ( 4123455.9) '((((x-y)+z)))'
  3260. Expression 6 of 7 7.581 ns 75810 ns (-4123455.9) '((((x-y)-z)))'
  3261. Expression 7 of 7 4.801 ns 48010 ns ( 0.0) '((((x-y)*z)))'
  3262. The benchmark utility can be very useful when investigating evaluation
  3263. efficiency issues with ExprTk or simply during the prototyping of
  3264. expressions. As an example, lets take the following expression:
  3265. 1 / sqrt(2x) * e^(3y)
  3266. Lets say we would like to determine which sub-part of the expression
  3267. takes the most time to evaluate and perhaps attempt to rework the
  3268. expression based on the results. In order to do this we will create a
  3269. text file called 'test.txt' and then proceed to make some educated
  3270. guesses about how to break the expression up into its more
  3271. 'interesting' sub-parts which we will then add as one expression per
  3272. line to the file. An example breakdown may be as follows:
  3273. 1 / sqrt(2x) * e^(3y)
  3274. 1 / sqrt(2x)
  3275. e^(3y)
  3276. The benchmark with the given file, where each expression will be
  3277. evaluated 100K times can be executed as follows:
  3278. ./exprtk_benchmark test.txt 100000
  3279. Expr 1 of 3 90.340 ns 9034000 ns (296417859.3) '1/sqrt(2x)*e^(3y)'
  3280. Expr 2 of 3 11.100 ns 1109999 ns ( 44267.3) '1/sqrt(2x)'
  3281. Expr 3 of 3 77.830 ns 7783000 ns (615985286.6) 'e^(3y)'
  3282. [*] Number Of Evals: 300000
  3283. [*] Total Time: 0.018sec
  3284. [*] Total Single Eval Time: 0.000ms
  3285. From the results above we conclude that the third expression (e^(3y))
  3286. consumes the largest amount of time. The variable 'e', as used in both
  3287. the benchmark and in the expression, is an approximation of the
  3288. transcendental mathematical constant e (2.71828182845904...) hence the
  3289. sub-expression should perhaps be modified to use the generally more
  3290. efficient built-in 'exp' function.
  3291. ./exprtk_benchmark test.txt 1000000
  3292. Expr 1 of 5 86.563 ns 8656300ns (296417859.6) '1/sqrt(2x)*e^(3y)'
  3293. Expr 2 of 5 40.506 ns 4050600ns (296417859.6) '1/sqrt(2x)*exp(3y)'
  3294. Expr 3 of 5 14.248 ns 1424799ns ( 44267.2) '1/sqrt(2x)'
  3295. Expr 4 of 5 88.840 ns 8884000ns (615985286.9) 'e^(3y)'
  3296. Expr 5 of 5 29.267 ns 2926699ns (615985286.9) 'exp(3y)'
  3297. [*] Number Of Evals: 5000000
  3298. [*] Total Time: 0.260sec
  3299. [*] Total Single Eval Time: 0.000ms
  3300. The above output demonstrates the results from making the previously
  3301. mentioned modification to the expression. As can be seen the new form
  3302. of the expression using the 'exp' function reduces the evaluation time
  3303. by over 50%, in other words increases the evaluation rate by two fold.
  3304. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3305. [SECTION 25 - EXPRTK NOTES]
  3306. The following is a list of facts and suggestions one may want to take
  3307. into account when using ExprTk:
  3308. (00) Precision and performance of expression evaluations are the
  3309. dominant principles of the ExprTk library.
  3310. (01) ExprTk uses a rudimentary imperative programming model with
  3311. syntax based on languages such as Pascal and C. Furthermore
  3312. ExprTk is an LL(2) type grammar and is processed using a
  3313. recursive descent parsing algorithm.
  3314. (02) Supported types are float, double, long double and MPFR/GMP.
  3315. (03) Standard mathematical operator precedence is applied (BEDMAS).
  3316. (04) Results of expressions that are deemed as being 'valid' are to
  3317. exist within the set of Real numbers. All other results will be
  3318. of the value: Not-A-Number (NaN).
  3319. (05) Supported user defined types are numeric and string
  3320. variables, numeric vectors and functions.
  3321. (06) All reserved words, keywords, variable, vector, string and
  3322. function names are case-insensitive.
  3323. (07) Variable, vector, string variable and function names must begin
  3324. with a letter (A-Z or a-z), then can be comprised of any
  3325. combination of letters, digits, underscores and dots. (eg: x,
  3326. var1 or power_func99, person.age, item.size.0)
  3327. (08) Expression lengths and sub-expression lists are limited only by
  3328. storage capacity.
  3329. (09) The life-time of objects registered with or created from a
  3330. specific symbol-table must span at least the life-time of the
  3331. compiled expressions which utilise objects, such as variables,
  3332. of that symbol-table, otherwise the result will be undefined
  3333. behavior.
  3334. (10) Equal and Nequal are normalised-epsilon equality routines,
  3335. which use epsilons of 0.0000000001 and 0.000001 for double and
  3336. float types respectively.
  3337. (11) All trigonometric functions assume radian input unless stated
  3338. otherwise.
  3339. (12) Expressions may contain white-space characters such as space,
  3340. tabs, new-lines, control-feed et al.
  3341. ('\n', '\r', '\t', '\b', '\v', '\f')
  3342. (13) Strings may be comprised of any combination of letters, digits
  3343. special characters including (~!@#$%^&*()[]|=+ ,./?<>;:"`~_) or
  3344. hexadecimal escaped sequences (eg: \0x30) and must be enclosed
  3345. with single-quotes.
  3346. eg: 'Frankly my dear, \0x49 do n0t give a damn!'
  3347. (14) User defined normal functions can have up to 20 parameters,
  3348. where as user defined generic-functions and vararg-functions
  3349. can have an unlimited number of parameters.
  3350. (15) The inbuilt polynomial functions can be at most of degree 12.
  3351. (16) Where appropriate constant folding optimisations may be applied.
  3352. (eg: The expression '2 + (3 - (x / y))' becomes '5 - (x / y)')
  3353. (17) If the strength reduction compilation option has been enabled,
  3354. then where applicable strength reduction optimisations may be
  3355. applied.
  3356. (18) String processing capabilities are available by default. To
  3357. turn them off, the following needs to be defined at compile
  3358. time: exprtk_disable_string_capabilities
  3359. (19) Composited functions can call themselves or any other functions
  3360. that have been defined prior to their own definition.
  3361. (20) Recursive calls made from within composited functions will have
  3362. a stack size bound by the stack of the executing architecture.
  3363. (21) User defined functions by default are assumed to have side
  3364. effects. As such an "all constant parameter" invocation of such
  3365. functions wont result in constant folding. If the function has
  3366. no side effects then that can be noted during the constructor
  3367. of the ifunction allowing it to be constant folded where
  3368. appropriate.
  3369. (22) The entity relationship between symbol_table and an expression
  3370. is many-to-many. However the intended 'typical' use-case where
  3371. possible, is to have a single symbol table manage the variable
  3372. and function requirements of multiple expressions.
  3373. (23) The common use-case for an expression is to have it compiled
  3374. only ONCE and then subsequently have it evaluated multiple
  3375. times. An extremely inefficient and suboptimal approach would
  3376. be to recompile an expression from its string form every time
  3377. it requires evaluating.
  3378. (24) It is strongly recommended that the return value of method
  3379. invocations from the parser and symbol_table types be taken
  3380. into account. Specifically the 'compile' method of the parser
  3381. and the 'add_xxx' set of methods of the symbol_table as they
  3382. denote either the success or failure state of the invoked call.
  3383. Continued processing from a failed state without having first
  3384. rectified the underlying issue will in turn result in further
  3385. failures and undefined behaviours.
  3386. (25) The following are examples of compliant floating point value
  3387. representations:
  3388. (1) 12345 (5) -123.456
  3389. (2) +123.456e+12 (6) 123.456E-12
  3390. (3) +012.045e+07 (7) .1234
  3391. (4) 123.456f (8) -321.654E+3L
  3392. (26) Expressions may contain any of the following comment styles:
  3393. (1) // .... \n
  3394. (2) # .... \n
  3395. (3) /* .... */
  3396. (27) The 'null' value type is a special non-zero type that
  3397. incorporates specific semantics when undergoing operations with
  3398. the standard numeric type. The following is a list of type and
  3399. boolean results associated with the use of 'null':
  3400. (1) null +,-,*,/,% x --> x
  3401. (2) x +,-,*,/,% null --> x
  3402. (3) null +,-,*,/,% null --> null
  3403. (4) null == null --> true
  3404. (5) null == x --> true
  3405. (6) x == null --> true
  3406. (7) x != null --> false
  3407. (8) null != null --> false
  3408. (9) null != x --> false
  3409. (28) The following is a list of reserved words and symbols used by
  3410. ExprTk. Attempting to add a variable or custom function to a
  3411. symbol table using any of the reserved words will result in a
  3412. failure.
  3413. abs, acos, acosh, and, asin, asinh, atan, atan2, atanh, avg,
  3414. break, case, ceil, clamp, continue, cosh, cos, cot, csc,
  3415. default, deg2grad, deg2rad, else, equal, erfc, erf, exp,
  3416. expm1, false, floor, for, frac, grad2deg, hypot, iclamp, if,
  3417. ilike, in, inrange, in, like, log, log10, log1p, log2, logn,
  3418. mand, max, min, mod, mor, mul, nand, ncdf, nor, not,
  3419. not_equal, not, null, or, pow, rad2deg, repeat, return,
  3420. root, roundn, round, sec, sgn, shl, shr, sinc, sinh, sin,
  3421. sqrt, sum, swap, switch, tanh, tan, true, trunc, until, var,
  3422. while, xnor, xor, xor
  3423. (29) Every valid ExprTk statement is a "value returning" expression.
  3424. Unlike some languages that limit the types of expressions that
  3425. can be performed in certain situations, in ExprTk any valid
  3426. expression can be used in any "value consuming" context. eg:
  3427. var y := 3;
  3428. for (var x := switch
  3429. {
  3430. case 1 : 7;
  3431. case 2 : -1 + ~{var x{};};
  3432. default : y > 2 ? 3 : 4;
  3433. };
  3434. x != while (y > 0) { y -= 1; };
  3435. x -= {
  3436. if (min(x,y) < 2 * max(x,y))
  3437. x + 2;
  3438. else
  3439. x + y - 3;
  3440. }
  3441. )
  3442. {
  3443. (x + y) / (x - y);
  3444. }
  3445. (30) It is recommended when prototyping expressions that the ExprTk
  3446. REPL be utilised, as it supports all the features available in
  3447. the library, including complete error analysis, benchmarking
  3448. and dependency dumps etc which allows for rapid
  3449. coding/prototyping and debug cycles without the hassle of
  3450. having to recompile test programs with expressions that have
  3451. been hard-coded. It's also a good source of truth for how the
  3452. library's various features can be applied.
  3453. (31) For performance considerations, one should assume the actions
  3454. of expression, symbol table and parser instance instantiation
  3455. and destruction, and the expression compilation process itself
  3456. to be of high latency. Hence none of them should be part of any
  3457. performance critical code paths, and should instead occur
  3458. entirely either before or after such code paths.
  3459. (32) Deep copying an expression instance for the purposes of
  3460. persisting to disk or otherwise transmitting elsewhere with the
  3461. intent to 'resurrect' the expression instance later on is not
  3462. possible due to the reasons described in the final note of
  3463. Section 10. The recommendation is to instead simply persist the
  3464. string form of the expression and compile the expression at
  3465. run-time on the target.
  3466. (33) Before jumping in and using ExprTk, do take the time to peruse
  3467. the documentation and all of the examples, both in the main and
  3468. the extras distributions. Having an informed general view of
  3469. what can and can't be done, and how something should be done
  3470. with ExprTk, will likely result in a far more productive and
  3471. enjoyable programming experience.
  3472. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3473. [SECTION 26 - SIMPLE EXPRTK EXAMPLE]
  3474. The following is a simple yet complete example demonstrating typical
  3475. usage of the ExprTk Library. The example instantiates a symbol table
  3476. object, adding to it three variables named x, y and z, and a custom
  3477. user defined function, that accepts only two parameters, named myfunc.
  3478. The example then proceeds to instantiate an expression object and
  3479. register to it the symbol table instance.
  3480. A parser is then instantiated, and the string representation of the
  3481. expression and the expression object are passed to the parser's
  3482. compile method for compilation. If an error occurred during
  3483. compilation, the compile method will return false, leading to a series
  3484. of error diagnostics being printed to stdout. Otherwise the newly
  3485. compiled expression is evaluated by invoking the expression object's
  3486. value method, and subsequently printing the result of the computation
  3487. to stdout.
  3488. --- snip ---
  3489. #include <cstdio>
  3490. #include <string>
  3491. #include "exprtk.hpp"
  3492. template <typename T>
  3493. struct myfunc : public exprtk::ifunction<T>
  3494. {
  3495. myfunc() : exprtk::ifunction<T>(2) {}
  3496. T operator()(const T& v1, const T& v2)
  3497. {
  3498. return T(1) + (v1 * v2) / T(3);
  3499. }
  3500. };
  3501. int main()
  3502. {
  3503. typedef exprtk::symbol_table<double> symbol_table_t;
  3504. typedef exprtk::expression<double> expression_t;
  3505. typedef exprtk::parser<double> parser_t;
  3506. typedef exprtk::parser_error::type error_t;
  3507. std::string expression_str =
  3508. "z := 2 myfunc([4 + sin(x / pi)^3],y ^ 2)";
  3509. double x = 1.1;
  3510. double y = 2.2;
  3511. double z = 3.3;
  3512. myfunc<double> mf;
  3513. symbol_table_t symbol_table;
  3514. symbol_table.add_constants();
  3515. symbol_table.add_variable("x",x);
  3516. symbol_table.add_variable("y",y);
  3517. symbol_table.add_variable("z",z);
  3518. symbol_table.add_function("myfunc",mf);
  3519. expression_t expression;
  3520. expression.register_symbol_table(symbol_table);
  3521. parser_t parser;
  3522. if (!parser.compile(expression_str,expression))
  3523. {
  3524. // A compilation error has occurred. Attempt to
  3525. // print all errors to stdout.
  3526. printf("Error: %s\tExpression: %s\n",
  3527. parser.error().c_str(),
  3528. expression_str.c_str());
  3529. for (std::size_t i = 0; i < parser.error_count(); ++i)
  3530. {
  3531. // Include the specific nature of each error
  3532. // and its position in the expression string.
  3533. error_t error = parser.get_error(i);
  3534. printf("Error: %02d Position: %02d "
  3535. "Type: [%s] "
  3536. "Message: %s "
  3537. "Expression: %s\n",
  3538. static_cast<int>(i),
  3539. static_cast<int>(error.token.position),
  3540. exprtk::parser_error::to_str(error.mode).c_str(),
  3541. error.diagnostic.c_str(),
  3542. expression_str.c_str());
  3543. }
  3544. return 1;
  3545. }
  3546. // Evaluate the expression and obtain its result.
  3547. double result = expression.value();
  3548. printf("Result: %10.5f\n",result);
  3549. return 0;
  3550. }
  3551. --- snip ---
  3552. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3553. [SECTION 27 - BUILD OPTIONS]
  3554. When building ExprTk there are a number of defines that will enable or
  3555. disable certain features and capabilities. The defines can either be
  3556. part of a compiler command line switch or scoped around the include to
  3557. the ExprTk header. The defines are as follows:
  3558. (01) exprtk_enable_debugging
  3559. (02) exprtk_disable_comments
  3560. (03) exprtk_disable_break_continue
  3561. (04) exprtk_disable_sc_andor
  3562. (05) exprtk_disable_return_statement
  3563. (06) exprtk_disable_enhanced_features
  3564. (07) exprtk_disable_string_capabilities
  3565. (08) exprtk_disable_superscalar_unroll
  3566. (09) exprtk_disable_rtl_io_file
  3567. (10) exprtk_disable_rtl_vecops
  3568. (11) exprtk_disable_caseinsensitivity
  3569. (01) exprtk_enable_debugging
  3570. This define will enable printing of debug information to stdout during
  3571. the compilation process.
  3572. (02) exprtk_disable_comments
  3573. This define will disable the ability for expressions to have comments.
  3574. Expressions that have comments when parsed with a build that has this
  3575. option, will result in a compilation failure.
  3576. (03) exprtk_disable_break_continue
  3577. This define will disable the loop-wise 'break' and 'continue'
  3578. capabilities. Any expression that contains those keywords will result
  3579. in a compilation failure.
  3580. (04) exprtk_disable_sc_andor
  3581. This define will disable the short-circuit '&' (and) and '|' (or)
  3582. operators
  3583. (05) exprtk_disable_return_statement
  3584. This define will disable use of return statements within expressions.
  3585. (06) exprtk_disable_enhanced_features
  3586. This define will disable all enhanced features such as strength
  3587. reduction and special function optimisations and expression specific
  3588. type instantiations. This feature will reduce compilation times and
  3589. binary sizes but will also result in massive performance degradation
  3590. of expression evaluations.
  3591. (07) exprtk_disable_string_capabilities
  3592. This define will disable all string processing capabilities. Any
  3593. expression that contains a string or string related syntax will result
  3594. in a compilation failure.
  3595. (08) exprtk_disable_superscalar_unroll
  3596. This define will set the loop unroll batch size to 4 operations per
  3597. loop instead of the default 8 operations. This define is used in
  3598. operations that involve vectors and aggregations over vectors. When
  3599. targeting non-superscalar architectures, it may be recommended to
  3600. build using this particular option if efficiency of evaluations is of
  3601. concern.
  3602. (09) exprtk_disable_rtl_io_file
  3603. This define will disable the file I/O RTL package features. When
  3604. present, any attempts to register the file I/O package with a given
  3605. symbol table will fail causing a compilation error.
  3606. (10) exprtk_disable_rtl_vecops
  3607. This define will disable the extended vector operations RTL package
  3608. features. When present, any attempts to register the vector operations
  3609. package with a given symbol table will fail causing a compilation
  3610. error.
  3611. (11) exprtk_disable_caseinsensitivity
  3612. This define will disable case-insensitivity when matching variables
  3613. and functions. Furthermore all reserved and keywords will only be
  3614. acknowledged when in all lower-case.
  3615. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3616. [SECTION 28 - FILES]
  3617. The source distribution of ExprTk is comprised of the following set of
  3618. files:
  3619. (00) Makefile
  3620. (01) readme.txt
  3621. (02) exprtk.hpp
  3622. (03) exprtk_test.cpp
  3623. (04) exprtk_benchmark.cpp
  3624. (05) exprtk_simple_example_01.cpp
  3625. (06) exprtk_simple_example_02.cpp
  3626. (07) exprtk_simple_example_03.cpp
  3627. (08) exprtk_simple_example_04.cpp
  3628. (09) exprtk_simple_example_05.cpp
  3629. (10) exprtk_simple_example_06.cpp
  3630. (11) exprtk_simple_example_07.cpp
  3631. (12) exprtk_simple_example_08.cpp
  3632. (13) exprtk_simple_example_09.cpp
  3633. (14) exprtk_simple_example_10.cpp
  3634. (15) exprtk_simple_example_11.cpp
  3635. (16) exprtk_simple_example_12.cpp
  3636. (17) exprtk_simple_example_13.cpp
  3637. (18) exprtk_simple_example_14.cpp
  3638. (19) exprtk_simple_example_15.cpp
  3639. (20) exprtk_simple_example_16.cpp
  3640. (21) exprtk_simple_example_17.cpp
  3641. (22) exprtk_simple_example_18.cpp
  3642. (23) exprtk_simple_example_19.cpp
  3643. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3644. [SECTION 29 - LANGUAGE STRUCTURE]
  3645. The following are the various language structures available within
  3646. ExprTk and their structural representations.
  3647. (00) If Statement
  3648. (01) Else Statement
  3649. (02) Ternary Statement
  3650. (03) While Loop
  3651. (04) Repeat Until Loop
  3652. (05) For Loop
  3653. (06) Switch Statement
  3654. (07) Multi Subexpression Statement
  3655. (08) Multi Case-Consequent Statement
  3656. (09) Variable Definition Statement
  3657. (10) Vector Definition Statement
  3658. (11) String Definition Statement
  3659. (12) Range Statement
  3660. (13) Return Statement
  3661. (00) - If Statement
  3662. +-------------------------------------------------------------+
  3663. | |
  3664. | [if] ---> [(] ---> [condition] -+-> [,] -+ |
  3665. | | | |
  3666. | +---------------<---------------+ | |
  3667. | | | |
  3668. | | +------------------<------------------+ |
  3669. | | | |
  3670. | | +--> [consequent] ---> [,] ---> [alternative] ---> [)] |
  3671. | | |
  3672. | +--> [)] --+-> [{] ---> [expression*] ---> [}] --+ |
  3673. | | | |
  3674. | | +---------<----------+ |
  3675. | +----<-----+ | |
  3676. | | v |
  3677. | +--> [consequent] --> [;] -{*}-> [else-statement] |
  3678. | |
  3679. +-------------------------------------------------------------+
  3680. (01) - Else Statement
  3681. +-------------------------------------------------------------+
  3682. | |
  3683. | [else] -+-> [alternative] ---> [;] |
  3684. | | |
  3685. | +--> [{] ---> [expression*] ---> [}] |
  3686. | | |
  3687. | +--> [if-statement] |
  3688. | |
  3689. +-------------------------------------------------------------+
  3690. (02) - Ternary Statement
  3691. +-------------------------------------------------------------+
  3692. | |
  3693. | [condition] ---> [?] ---> [consequent] ---> [:] --+ |
  3694. | | |
  3695. | +------------------------<------------------------+ |
  3696. | | |
  3697. | +--> [alternative] --> [;] |
  3698. | |
  3699. +-------------------------------------------------------------+
  3700. (03) - While Loop
  3701. +-------------------------------------------------------------+
  3702. | |
  3703. | [while] ---> [(] ---> [condition] ---> [)] ---+ |
  3704. | | |
  3705. | +----------------------<----------------------+ |
  3706. | | |
  3707. | +--> [{] ---> [expression*] ---> [}] |
  3708. | |
  3709. +-------------------------------------------------------------+
  3710. (04) - Repeat Until Loop
  3711. +-------------------------------------------------------------+
  3712. | |
  3713. | [repeat] ---> [expression*] ---+ |
  3714. | | |
  3715. | +--------------<---------------+ |
  3716. | | |
  3717. | +--> [until] ---> [(] ---> [condition] --->[)] |
  3718. | |
  3719. +-------------------------------------------------------------+
  3720. (05) - For Loop
  3721. +-------------------------------------------------------------+
  3722. | |
  3723. | [for] ---> [(] -+-> [initialise expression] --+--+ |
  3724. | | | | |
  3725. | +------------->---------------+ v |
  3726. | | |
  3727. | +-----------------------<------------------------+ |
  3728. | | |
  3729. | +--> [;] -+-> [condition] -+-> [;] ---+ |
  3730. | | | | |
  3731. | +------->--------+ v |
  3732. | | |
  3733. | +------------------<---------+--------+ |
  3734. | | | |
  3735. | +--> [increment expression] -+-> [)] --+ |
  3736. | | |
  3737. | +------------------<-------------------+ |
  3738. | | |
  3739. | +--> [{] ---> [expression*] ---> [}] |
  3740. | |
  3741. +-------------------------------------------------------------+
  3742. (06) - Switch Statement
  3743. +-------------------------------------------------------------+
  3744. | |
  3745. | [switch] ---> [{] ---+ |
  3746. | | |
  3747. | +---------<----------+-----------<-----------+ |
  3748. | | | |
  3749. | +--> [case] ---> [condition] ---> [:] ---+ | |
  3750. | | | |
  3751. | +-------------------<--------------------+ | |
  3752. | | | |
  3753. | +--> [consequent] ---> [;] --------->--------+ |
  3754. | | | |
  3755. | | | |
  3756. | +--> [default] ---> [consequent] ---> [;] ---+ |
  3757. | | | |
  3758. | +---------------------<----------------------+ |
  3759. | | |
  3760. | +--> [}] |
  3761. | |
  3762. +-------------------------------------------------------------+
  3763. (07) - Multi Subexpression Statement
  3764. +-------------------------------------------------------------+
  3765. | |
  3766. | +--------------<---------------+ |
  3767. | | | |
  3768. | [~] ---> [{\(] -+-> [expression] -+-> [;\,] ---+ |
  3769. | | |
  3770. | +----------------<----------------+ |
  3771. | | |
  3772. | +--> [}\)] |
  3773. | |
  3774. +-------------------------------------------------------------+
  3775. (08) - Multi Case-Consequent Statement
  3776. +-------------------------------------------------------------+
  3777. | |
  3778. | [[*]] ---> [{] ---+ |
  3779. | | |
  3780. | +--------<--------+--------------<----------+ |
  3781. | | | |
  3782. | +--> [case] ---> [condition] ---> [:] ---+ | |
  3783. | | | |
  3784. | +-------------------<--------------------+ | |
  3785. | | | |
  3786. | +--> [consequent] ---> [;] ---+------>------+ |
  3787. | | |
  3788. | +--> [}] |
  3789. | |
  3790. +-------------------------------------------------------------+
  3791. (09) - Variable Definition Statement
  3792. +-------------------------------------------------------------+
  3793. | |
  3794. | [var] ---> [symbol] -+-> [:=] -+-> [expression] -+-> [;] |
  3795. | | | | |
  3796. | | +-----> [{}] -->--+ |
  3797. | | | |
  3798. | +------------->-------------+ |
  3799. | |
  3800. +-------------------------------------------------------------+
  3801. (10) - Vector Definition Statement
  3802. +-------------------------------------------------------------+
  3803. | |
  3804. | [var] ---> [symbol] ---> [[] ---> [constant] ---> []] --+ |
  3805. | | |
  3806. | +---------------------------<---------------------------+ |
  3807. | | |
  3808. | | +--------->---------+ |
  3809. | | | | |
  3810. | +--> [:=] ---> [{] -+-+-> [expression] -+-> [}] ---> [;] |
  3811. | | | |
  3812. | +--<--- [,] <-----+ |
  3813. | |
  3814. +-------------------------------------------------------------+
  3815. (11) - String Definition Statement
  3816. +-------------------------------------------------------------+
  3817. | |
  3818. | [var] --> [symbol] --> [:=] --> [str-expression] ---> [;] |
  3819. | |
  3820. +-------------------------------------------------------------+
  3821. (12) - Range Statement
  3822. +-------------------------------------------------------------+
  3823. | |
  3824. | +-------->--------+ |
  3825. | | | |
  3826. | [[] -+-> [expression] -+-> [:] -+-> [expression] -+--> []] |
  3827. | | | |
  3828. | +-------->--------+ |
  3829. | |
  3830. +-------------------------------------------------------------+
  3831. (13) - Return Statement
  3832. +-------------------------------------------------------------+
  3833. | |
  3834. | [return] ---> [[] -+-> [expression] -+-> []] ---> [;] |
  3835. | | | |
  3836. | +--<--- [,] <-----+ |
  3837. | |
  3838. +-------------------------------------------------------------+