├── LICENSE ├── README.md └── eval_expr.py /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eval_expr 2 | A TI-Nspire CX II python library to evaluate arbitrary TI-Basic expressions. 3 | 4 | ## Context 5 | TI provides useful functions in their [ti_system module](https://education.ti.com/html/webhelp/EG_TINspire/EN/Subsystems/EG_Python/Content/m_menumap/mm_tisystem.HTML). 6 | Among those, there is `eval_function("name",value)` which "evaluates a predefined OS function at the specified value". 7 | 8 | That's good but not good enough to evaluate any function that takes more than a single argument, for instance... And it seems to be restricted to numerical stuff only, too. 9 | How are we supposed to do fancy math stuff from Python? :( 10 | 11 | In fact, people have started asking this question on TI-Planet for instance, where [someone wanted](https://tiplanet.org/forum/viewtopic.php?f=116&t=24279#p256279) to have access to specfunc for Laplace transforms. That's when I started to dig in and see if there was any way to do more than just `eval_function`... 12 | 13 | 14 | ## How does this library work? 15 | Well, it turns out that the `ti_system` module also has some internal/lower-level functions exposed (but not listed in the menus), like `writeST` and `readST` (which I guess is used by other higher-level functions like `store_value` and `recall_value`), which interact with the *Symbol Table* which is basically where variables are stored and shared. 16 | 17 | Interestingly, `recall_value` seems to be able to evaluate the input and return the output, with much less restrictions! 18 | 19 | So, the library leverages that, with some pre- and post-processing to work around some quirks, but it seems to work relatively well. 20 | 21 | 22 | ## Installation 23 | 1. Download the .tns file from the [latest release page](https://github.com/TI-Planet/eval_expr/releases/latest). 24 | 2. Transfer it to your calculator, in the **PyLib** folder. 25 | 3. Enjoy! 26 | 27 | 28 | ## Usage 29 | Just import the module and use the functions it provides: `eval_expr` and `call_func`. 30 | 31 | If you just need the `eval_expr` function, you can just do this: `from eval_expr import eval_expr`. 32 | 33 | *Aliases (`caseval`, `eval_native`) to `eval_expr` are provided for convenience, for compatibility purposes, if you import the whole module.* 34 | 35 | 36 | ## Documentation 37 | ### `eval_expr(expr, trypyeval=False)`: 38 | This is the main function of the library. Pass a TI-Basic expression *(you'll probably want to make that a string)* in the first argument and it will try to evaluate it and return the result as a native Python type, otherwise a string. 39 | 40 | If you pass `True` as the 2nd argument (optional, it's `False` by default), it will try to actually evaluate the output in Python. This can be useful for numerical results. 41 | For instance, on an exact-math Nspire (CX II-T) or a CAS model, `eval_expr("sqrt(90)")` would give you `'3*sqrt(10)'`. But `eval_expr("sqrt(90)", True)` returns `9.486832980505138`. 42 | 43 | Notes: complex numbers uses the `i` math symbol (looks like `𝒊`) but in Python it's just the letter `j`. Substitution from Basic to Python is handled automatically, just like for other symbols (`√`, `π`, `𝒆`). 44 | 45 | 46 | ### `call_func(funcname, *pyargs)`: 47 | This builds on top of `eval_expr` in order to provide a more powerful `eval_function` that the `ti_system` module offers. 48 | 49 | This is a *variadic* function, meaning you can pass any number of arguments you want, for instance `call_func("log", 2, 3.0)` which will give `0.63093`. 50 | 51 | The function returns `None` if the output is the same as the input, meaning it couldn't be evaluated. This allows you to easily handle this case in your scripts. 52 | 53 | 54 | ## Caveats 55 | * TI-Basic lists (`{...}`) aren't automatically converted to/from Python lists `[...]`. In a next version? 56 | * No automatic substitution is done from Python to Basic. In a next version? 57 | 58 | If you find a bug, a weird behavior, or if you want to submit feedback or give ideas in general, please [open an issue here](https://github.com/TI-Planet/eval_expr/issues/new) :) 59 | 60 | 61 | ## Examples 62 | Here's a screenshot with a few expressions: *(note that `@i` is a way on the Nspire software to quickly get the complex i symbol)* 63 | 64 | ![Screenshot](https://i.imgur.com/zNZnSf6m.png) 65 | 66 | 67 | ## In the future... 68 | Well, I've contacted TI to see if they could add this kind of feature natively so that we don't need to resort to weird tricks, and they've replied that they're analyzing my feedback, so *there's hope* for a future update :) 69 | -------------------------------------------------------------------------------- /eval_expr.py: -------------------------------------------------------------------------------- 1 | # eval_expr TI-Nspire CX II python library 2 | # Latest version and documentation: https://github.com/TI-Planet/eval_expr 3 | # Author: Adrien "Adriweb" Bertrand 4 | # See tiplanet.org for more cool stuff :) 5 | # License: Unlicense / Public Domain / Do whatever you want 6 | 7 | # Useful for eval 8 | from math import sqrt, pi, e 9 | 10 | # Internal helper functions.... 11 | def _return_number_if_possible(s): 12 | try: 13 | f = float(s) 14 | return int(f) if int(f) == f else f 15 | except ValueError: 16 | return s 17 | 18 | def _return_evaled_if_possible(thing): 19 | try: 20 | return eval("("+str(thing)+")") 21 | except: 22 | return thing 23 | 24 | def _cleanstr(res): 25 | res = res[1:-1] # to remove the quotes 26 | res = res.replace("*\uf02f", "j") # complex i 27 | res = res.replace("\uf02f", "j") # complex i 28 | res = res.replace("\u221a", "sqrt") 29 | res = res.replace("\u03c0", "pi") 30 | res = res.replace("\uf03f", "e") 31 | res = _return_number_if_possible(res) # auto type... 32 | return res 33 | 34 | # Public functions 35 | def eval_expr(expr, trypyeval=False): 36 | from ti_system import writeST, readST 37 | writeST("tmppy_", 'strsub(string('+str(expr)+'),"/","$%$")') # eval and store 38 | res = readST("tmppy_") # retrieve stored value 39 | res = res.replace("$%$", "/") # magic replacement 40 | res = _cleanstr(res) 41 | if trypyeval == True: 42 | res = _return_evaled_if_possible(res) 43 | return res 44 | 45 | def call_func(funcname, *pyargs): 46 | fargs = ','.join(map(str, pyargs)) 47 | expr = funcname + '(' + fargs + ')' 48 | res = eval_expr(expr) 49 | return res if res != expr else None 50 | 51 | # Aliases for compat with other stuff 52 | caseval = eval_expr 53 | eval_native = eval_expr 54 | 55 | --------------------------------------------------------------------------------