|
| 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 | + |
| 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 :) |
0 commit comments