π
<-
Chat plein-écran
[^]

Release v1.0 module python eval_expr

TI's micropython + modules

Release v1.0 module python eval_expr

Message non lude Adriweb » 17 Oct 2020, 16:36

Suite à ce topic... j'ai développé une petite lib Python pour Nspire CX II.

Je viens de faire une premiere release: https://github.com/TI-Planet/eval_expr avec documentation, examples etc. :)

Je reprends ca ici pour le fun:

eval_expr
A TI-Nspire CX II python library to evaluate arbitrary TI-Basic expressions.

Context
TI provides useful functions in their ti_system module.
Among those, there is eval_function("name",value) which "evaluates a predefined OS function at the specified value".

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.
How are we supposed to do fancy math stuff from Python? :(

In fact, people have started asking this question on TI-Planet for instance, where someone wanted 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...


How does this library work?
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.

Interestingly, recall_value seems to be able to evaluate the input and return the output, with much less restrictions!

So, the library leverages that, with some pre- and post-processing to work around some quirks, but it seems to work relatively well.


Installation
  1. Download the .tns file from the latest release page.
  2. Transfer it to your calculator, in the PyLib folder.
  3. Enjoy!

Usage
Just import the module and use the functions it provides: eval_expr and call_func.

If you just need the eval_expr function, you can just do this: from eval_expr import eval_expr.

Aliases (caseval, eval_native) to eval_expr are provided for convenience, for compatibility purposes, if you import the whole module.


Documentation
  • eval_expr(expr, trypyeval=False) :
    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.
    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.
    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.
    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 the symbols (, π, 𝒆).
  • call_func(funcname, *pyargs):
    This builds on top of eval_expr in order to provide a more powerful eval_function that the ti_system module offers.
    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.
    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.


Caveats
  • TI-Basic lists ({...}) aren't automatically converted to/from Python lists [...]. In a next version?
  • No automatic substitution is done from Python to Basic. In a next version?

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 :)


Examples
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)

Image


In the future...
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 :)
Image

MyCalcs: Help the community's calculator documentations by filling out your calculators info!
MyCalcs: Aidez la communauté à documenter les calculatrices en donnant des infos sur vos calculatrices !
Inspired-Lua.org: All about TI-Nspire Lua programming (tutorials, wiki/docs...)
Avatar de l’utilisateur
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Prochain niv.: 80.2%
 
Messages: 14616
Images: 1218
Inscription: 01 Juin 2007, 00:00
Localisation: France
Genre: Homme
Calculatrice(s):
MyCalcs profile
Twitter/X: adriweb
GitHub: adriweb

Re: Release v1.0 module python eval_expr

Message non lude Ti64CLi++ » 17 Oct 2020, 16:43

Super travail :bj:
Même si TI intègre cette fonctionnalité nativement, c'est toujours pratique de connaitre ces fonctions non listées comme writeST/readST
Image
Avatar de l’utilisateur
Ti64CLi++Modo
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Prochain niv.: 32.3%
 
Messages: 3441
Images: 75
Inscription: 04 Juil 2014, 14:40
Localisation: Clermont-Ferrand 63
Genre: Homme
Calculatrice(s):
MyCalcs profile
Classe: ENS Rennes
GitHub: Ti64CLi


Retourner vers Python

Qui est en ligne

Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 1 invité

-
Rechercher
-
Social TI-Planet
-
Sujets à la une
Comparaisons des meilleurs prix pour acheter sa calculatrice !
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
Phi NumWorks jailbreak
123
-
Faire un don / Premium
Pour plus de concours, de lots, de tests, nous aider à payer le serveur et les domaines...
Faire un don
Découvrez les avantages d'un compte donateur !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partenaires et pub
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
1299 utilisateurs:
>1269 invités
>24 membres
>6 robots
Record simultané (sur 6 mois):
6892 utilisateurs (le 07/06/2017)
-
Autres sites intéressants
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)