Post Reply 
Prime Python 101
02-23-2022, 08:42 AM
Post: #1
Prime Python 101
After connecting my prime to the connKit the firmware update was done, so I have python on the prime again. The wait(-1) thing was really annoying for me the last time I used the calculator because some functions I wrote in the past did not run anymore which is why I did not play with python till now. Decision to start over from scratch with the prime was made during the weekend and so...yes python.


Can someone give advice on how I am supposed to write functions or programs using python? Of course I have found the python app.
Inside the app I can switch to symb-view and see a main.py window which is where I suppoose I could write a program. If I switch from symb to numView, prime crashes and reboots. Reliably. :-(


Question: do I code under PythonApp-Symb view or can I code in the traditional Program Catalog?

Question: Where do PythonPrograms show up? Toolbox/user or somewhere else? Can/Should I kind of Export python functions?

Question: Python seems to be almost a hidden feature. I am surprised that I do not find information comparable to HPPL. Is there any structured tutorial out there?


I have a Prime G1, Prime Android App (not in use at all, makes no sense on a touch device I think) and have ordered a Prime G2. In case python might behave differently on different platforms.

Thank you and have a nice day
Find all posts by this user
Quote this message in a reply
02-23-2022, 09:54 AM
Post: #2
RE: Prime Python 101
(02-23-2022 08:42 AM)hape Wrote:  After connecting my prime to the connKit the firmware update was done, so I have python on the prime again. The wait(-1) thing was really annoying for me the last time I used the calculator because some functions I wrote in the past did not run anymore which is why I did not play with python till now. Decision to start over from scratch with the prime was made during the weekend and so...yes python.


Can someone give advice on how I am supposed to write functions or programs using python? Of course I have found the python app.
Inside the app I can switch to symb-view and see a main.py window which is where I suppoose I could write a program. If I switch from symb to numView, prime crashes and reboots. Reliably. :-(


Question: do I code under PythonApp-Symb view or can I code in the traditional Program Catalog?

Question: Where do PythonPrograms show up? Toolbox/user or somewhere else? Can/Should I kind of Export python functions?

Question: Python seems to be almost a hidden feature. I am surprised that I do not find information comparable to HPPL. Is there any structured tutorial out there?


I have a Prime G1, Prime Android App (not in use at all, makes no sense on a touch device I think) and have ordered a Prime G2. In case python might behave differently on different platforms.

Thank you and have a nice day

What I do is to edit my Python program right in the regular program editor by putting the Python program in an HPPL wrapper like this:
Code:
#PYTHON name
from math import *
from hpprime import *
t = eval("ticks")
loops = 30000
for i in range(loops):
    r0 = 10
    while True:
        x = r0
        x += 1
        x -= 4.567E-4
        x += 70
        x -= 69
        x *= 7
        x /= 11
        r0 -= 1
        if r0 <= 0:
            break
    x = log(x)
    x = sin(x)
    x = sqrt(x)
    x = sqrt(x)
print(x)
t = (eval("ticks")-t)/1000
print("Loops:", loops)
print("Time: {0:.3f} seconds".format(t))    
print("Index: {0:.2f}".format(34/t*loops))
#end
EXPORT calcperf()
BEGIN
  PYTHON(name);
END;

As you can see, you add

Code:
#PYTHON name

as the first line, then

Code:
#end
EXPORT calcperf()
BEGIN
  PYTHON(name);
END;

after the Python program. Change "calcperf" to whatever name you want to call your own routine.
In this example, calcperf will show up in the list of HPPL programs and can be run from there.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
02-23-2022, 12:43 PM
Post: #3
RE: Prime Python 101
That sounds good.

If I wanted to call cas for symbolic computation I think the "from cas import *" will do the trick.
Sadly there is no help available for the function so maybe someone can help?
Say I want to calculate '(x-2)*(x+3)':

Using the console i.e. numeric view inside the app I try a naive approach

>from cas import *
>caseval(CAS.simplify("(x-2)*(x+3)"))

unfortunatly does not work.

- CAS.simplify is automatically generated when I use Simplify from the toolbox

- I noticed that the xtphin-key uses capital X so I type x using the keyboard.

Any ideas? What is the difference between xcas-command and caseval-command?

thanks a lot
Find all posts by this user
Quote this message in a reply
02-23-2022, 02:25 PM
Post: #4
RE: Prime Python 101
Using Python please have at look at the threads in this subforum, using search.

An example: https://www.hpmuseum.org/forum/thread-18027.html

another one with examples by Guenter Schink: https://www.hpmuseum.org/forum/thread-17...#pid152765
Find all posts by this user
Quote this message in a reply
02-23-2022, 05:06 PM
Post: #5
RE: Prime Python 101
(02-23-2022 12:43 PM)hape Wrote:  Using the console i.e. numeric view inside the app I try a naive approach

>from cas import *
>caseval(CAS.simplify("(x-2)*(x+3)"))

unfortunatly does not work.

Try:

>from cas import *
>caseval("simplify((x-2)*(x+3))")

instead.

-road
Find all posts by this user
Quote this message in a reply
02-24-2022, 05:48 AM
Post: #6
RE: Prime Python 101
I see.
Did a lot of reading of the built-in help system and some things started to make more sense. Thanks.
Interesstingly there is no help file for the commands under cas tab.

Thomas, could not find anything about the caseval/xcas thing.
Find all posts by this user
Quote this message in a reply
02-24-2022, 08:34 AM (This post was last modified: 02-24-2022 08:35 AM by Thomas_Sch.)
Post: #7
RE: Prime Python 101
(02-24-2022 05:48 AM)hape Wrote:  I see.
Did a lot of reading of the built-in help system and some things started to make more sense. Thanks.
Interesstingly there is no help file for the commands under cas tab.

Thomas, could not find anything about the caseval/xcas thing.
Sorry, but you asked also for help and infos about Prime Python, not only relating the "caseval thing". Please use also the build in help about python libraries.
Good success!
Find all posts by this user
Quote this message in a reply
02-24-2022, 02:28 PM (This post was last modified: 02-24-2022 07:23 PM by roadrunner.)
Post: #8
RE: Prime Python 101
I don't know the what the difference between xcas and caseval is, but

caseval("simplify((x-2)*(x+3))")
xcas("simplify((x-2)*(x+3))")
eval_expr("simplify((x-2)*(x+3))")

all return the same thing: 'x^2+x-6'

edit: I did find this: https://www.hpmuseum.org/forum/thread-16...#pid147018

-road
Find all posts by this user
Quote this message in a reply
02-25-2022, 08:20 PM
Post: #9
RE: Prime Python 101
caseval, xcas and eval_expr are synonyms (reason behind synonyms is compatibility).
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)