HP Forums
CAS Equations Internal Representation For Python - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: CAS Equations Internal Representation For Python (/thread-19071.html)



CAS Equations Internal Representation For Python - Ioncubekh - 11-03-2022 02:42 PM

I wanted full symbolic equations to be transported from stack to python code so that it can calculate & work with those; however, I found that symbolic expressions have to be modified before being used by python math() library

For instance this code retrieves CAS symbolic equations from a user list & display what it contains internally

Code:
EXPORT strpy()
BEGIN
PRINT;
strPY(a);
RETURN TERMINAL(1);
END;

#PYTHON strPY(a)
import sys
print(sys.argv[0])

Check CAS settings
Observations for python compatibility & manipulations:
~ CAS changes equations to some approximation? See screenshot
~ If you have alphabets in list [] CAS returns a list encapsulated in {}; if only numbers in list CAS returns []
~ { & } are to be replaced by [ & ] for python list conversions
~ remove extra space after ever ,
~ math operator like LN, SIN, ATAN... are to be made lower case
~ raised to power ^ to be replaced with **
~ there is no LN() in python math() replace it with log()
~ HP's alog10, log10 are converted to e^ & LN notations. Weird, though python has log10
~ sqrt sign √ will certainly be a challenge for python to understand, don't use it
~ Last example very important the minus sign. Hp provides a separate - sign for prefixing negative numerals perhaps it will not play well with python in the result?