Post Reply 
HP_PRIME PYTHON SYNTAX: HP_PPY
11-12-2017, 03:59 PM (This post was last modified: 05-09-2021 10:23 PM by compsystems.)
Post: #1
HP_PRIME PYTHON SYNTAX: HP_PPY
cas + pixels cmds

Example with standard CAS notation.
PHP Code:
#cas
fract1X_Y_Nmax ):=
begin
        
// Mandelbrot fractal, not using symmetry.
    
local xyzcjwhres1;
    
//freeze;
    
:= 2.7/X_;
    
:= -1.87/Y_;
    
res1 := []; 
    
Y_ := Y_-1;
    for 
y from 0 to Y_ do
        
:= -2.1 i*(h*y+0.935);
        for 
x from 0 to X_-do
            
:= 0;
            for 
j from 0 to Nmax-do
                if 
abs:= z^2+)>2 then break; end;
            
end;
            
res1.appendpixon_pxy126*j+2079 )); 
            
:= c+w;
        
end;
    
end;
    
wait(0); 
    return 
"Done"//return res1; 
end;
#end 
fract1( 320, 240, 10 ); [enter]
[Image: xcas_and_gui_image00.png]

As function xcas compatibility
PHP Code:
#cas
function fract2X_Y_Nmax )
        
// Mandelbrot fractal, not using symmetry.
    
local xyzcjwhres1;
    
:= 2.7/X_;
    
:= -1.87/Y_;
    
res1 := []; 
    
Y_ := Y_-1;
    for 
y from 0 to Y_ do
        
:= -2.1 i*(h*y+0.935);
        for 
x from 0 to X_-do
            
:= 0;
            for 
j from 0 to Nmax-do
                if 
abs:= z^2+)>2 then break; end;
            
end;
            
res1.appendpixon_pxy126*j+2079 )); // Xcas pixon_p -> pixon
            
:= c+w;
        
end;
    
end;
    
wait(0); // Xcas: comment on this line
    
return "Done" ;
end;
#end 
fract2( 320, 240, 10 ); [enter]
deltalist(time(fract2(320,240,10))); // only for Xcas to determine the execution time

using symmetry
PHP Code:
#cas
function fract2aX_Y_Nmax )  
        
// Mandelbrot fractal, using symmetry.      
    
local xyzcjwhres1res2;
    
//freeze;
    
:= 2.7/X_;
    
:= -1.87/Y_;
    
res1:=makelist(-ceilingX_*Y_/)-1);
    
res2 := res1
    
Y_ := Y_-1;
    for 
y from 0 to Y_/do
        
:= -2.1 i*(h*y+0.935);
        for 
x from 0 to X_-do
            
:= 0;
            for 
j from 0 to Nmax-do
                if 
abs:= z^2+)>2 then break; end;
            
end;
            
res1.appendpixon_pxy126*j+2079 )); 
            
res2.appendpixon_pxY_-y126*j+2079 ));
            
:= c+w;
        
end;
    
end;
    
wait(0); 
    return 
"Done"//    return res1,res2;
end;
#end 


With Python syntax.
PHP Code:
#cas
def fract3X_Y_Nmax ):
    
local xyzc
    
for x in rangeX_ ):
        for 
y in rangeY_ ):
            
0
            c 
2.7*X_-2.1 i*( -1.87*y/Y_ .935)
            for 
j in rangeNmax ):
                
z*c
                
if abs) > 2:  # abs(z = z*z+c)>2:
                    
break
            
pixon_pxy255*20*256 )
    
wait(0)
    return 
"Done"
#end 
fract3( 320, 240, 10 ); [enter]
[Image: xcas_and_gui_image01.png]

XCAS only
PHP Code:
function fract2X_Y_Nmax )
    
local xyzcjwhres1;
    
:= 2.7/X_;
    
:= -1.87/Y_;
    
res1 := []; 
    
Y_ := Y_-1;
    for 
y from 0 to Y_ do
        
:= -2.1+i*(h*y+0.935);
        for 
x from 0 to X_-do
            
:= 0;
            for 
j from 0 to Nmax-do
                if 
abs:= z^2+)>2 then break; end;
            
end;
            
res1.appendpixonxy126*j+2079 )); // hpprime pixon -> pixon_p
            
:= c+w;
        
end;
    
end;
    return 
res1;
end 
deltalist(time(fract2(320,240,10))); [enter]

XCAS only
PHP Code:
function fract2aX_Y_Nmax )  
    
// Mandelbrot fractal, using symmetry.      
    
local xyzcjwhres1res2;
    
:= 2.7/X_;
    
:= -1.87/Y_;
    
res1:=makelist(-ceilingX_*Y_/)-1);
    
res2 := res1
    
Y_ := Y_-1;
    for 
y from 0 to Y_/do
        
:= -2.1 i*(h*y+0.935);
        for 
x from 0 to X_-do
            
:= 0;
            for 
j from 0 to Nmax-do
                if 
abs:= z^2+)>2 then break; end;
            
end;
            
res1.appendpixonxy126*j+2079 )); 
            
res2.appendpixonxY_-y126*j+2079 ));
            
:= c+w;
        
end;
    
end;
    return 
res1,res2;
end 
Find all posts by this user
Quote this message in a reply
11-12-2017, 04:42 PM
Post: #2
RE: HP_PRIME PYTHON: HP_PPY
(11-12-2017 03:59 PM)compsystems Wrote:  



We need an updated primePAD. (external program editor)


.

So where can we get Python for Prime?

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
11-12-2017, 05:58 PM
Post: #3
RE: HP_PRIME PYTHON: HP_PPY
I think the language is in the new beta and it's limited....I don't know if the docs exist yet.
I'd also like to know is it compiled like HPPL or what? i.e. Is it as fast as HPPL?
Find all posts by this user
Quote this message in a reply
11-12-2017, 06:08 PM
Post: #4
RE: HP_PRIME PYTHON: HP_PPY
Speed test only with FOR loop and exponent


Attached File(s)
.hpprgm  CAS-Python-HPPPL.hpprgm (Size: 2.05 KB / Downloads: 112)

Viga C | TD | FB
Visit this user's website Find all posts by this user
Quote this message in a reply
11-12-2017, 06:12 PM
Post: #5
RE: HP_PRIME PYTHON: HP_PPY
I just tried that on the virtual prime on my PC and it says the Python is faster than HPPL is faster than CAS. I know it's a limited test, but it's still encouraging....
Find all posts by this user
Quote this message in a reply
11-12-2017, 06:19 PM
Post: #6
RE: HP_PRIME PYTHON: HP_PPY
The times of CAS and "Python" should be similar, with respect to the tests per loop it is inconsistent when varies the variable a=10 000, 50 000, 100 000, 500 000

Viga C | TD | FB
Visit this user's website Find all posts by this user
Quote this message in a reply
11-12-2017, 06:21 PM
Post: #7
RE: HP_PRIME PYTHON: HP_PPY
I take that back. I noticed that the test ran for under a second and I remembered that you really need to run for a few seconds to get an accurate evaluation, so I increased the iteration count by 100, and get:

CAS = 21.5 s
Python = 18.5 s
HPPL = 14.5 s

...which is more in line with what I'd expect....i.e. HPPL is faster than python.
Find all posts by this user
Quote this message in a reply
11-12-2017, 06:26 PM
Post: #8
RE: HP_PRIME PYTHON: HP_PPY
I still believe that CAS = "Python" xD

Viga C | TD | FB
Visit this user's website Find all posts by this user
Quote this message in a reply
11-12-2017, 07:35 PM (This post was last modified: 11-12-2017 07:39 PM by parisse.)
Post: #9
RE: HP_PRIME PYTHON: HP_PPY
It's not Python, it's Python syntax compatibility inside CAS. I wrote a pre-parser that translates Python-like indentation to CAS blocs: def/for/while/if/else -> function/for/while/if/else
There are differences with true Python: only basic Python syntax is supported (no class or generators for example), string delimiters must be " (' keeps the same meaning that in normal CAS programs), there is no module import, but you have access to all CAS commands. It is recommended to declare local variables, the current implementation of the pre-parser will leave undeclared variables as global. List indexes follow HP convention on the Prime (start at 1). The programming editor of the Prime does not support this syntax, therefore it's not easy to program on the calc with Python syntax.
The interpreter behind is the CAS interpreter, therefore it should run at the same speed, and this is in general slower than the Home interpreter, there is a price to pay for more types (exact types, symbolic expressions, but also functions that can be passed as arguments to functions). There are a few optimizations in the CAS interpreter in this beta version, they will work if the variables used in the loops are local variables (and loops are a little faster if they contain only 1 instruction), another reason to declare local variables with the local keyword (something that you can not do in Python).
Find all posts by this user
Quote this message in a reply
11-12-2017, 11:27 PM
Post: #10
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
To be blunt, I would not recommend using this at this time. It will work, but the tools and editors are not set up to support it.

TW

Although I work for HP, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
11-13-2017, 12:10 AM
Post: #11
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
Good evening dear parisse I write to you from my country Peru,
as much respect and I already ask for my apologies if uncomfortable with my comment I would like to say what
following:
   1. I find it unnecessary to put that basic Python syntax in HPPPL for me is to waste time should worry about other issues as I will mention in the lines below.
  2._ They should worry about releasing a way to protect the programs in HPPPL, I mentioned this topic because where I live according to conversations with my friends (by the way they are good programmers), they do not want to share their programs or publish for the theme of the piracy, then I think you should worry about that issue, because if they do not lose Hp Prime users, because others still prefer 50G and we know it is obsolete, but they prefer it because they have many programs that HP PRIME.
  3._Well, it's just an opinion that the HP PRIME team is losing time doing unnecessary things because HPPPL is enough.



Buenas noches estimado parisse le escribo desde mi país Perú,
como mucho respeto y ya pido mis disculpas si incómodo con mi comentario quisiera decir lo
siguiente:
1._Me parece innecesario que pongan esa sintaxis básica de Python en HPPPL para mi es perder tiempo deben preocuparse en otros temas como voy mencionar en líneas abajo.
2._ Deben preocuparse en liberar una forma de proteger los programas en HPPPL, le mencionó este tema porque por donde vivo según conversaciones con mis amigos (por cierto que son buenos programadores), no quieren compartir sus programas o publicar por el tema de la piratería, entonces pienso deben preocuparse en ese tema, ya que si no lo hacen van perder usuarios de Hp Prime , porque otros siguen prefiriendo 50G y sabemos es obsoleto, pero lo prefieren porque tienen muchos programas que HP PRIME.
3._Bueno es solo una opinión recalco pierden tiempo el equipo de HP PRIME haciendo cosas innecesarias porque HPPPL es suficiente.
Find all posts by this user
Quote this message in a reply
11-13-2017, 08:55 AM
Post: #12
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
To make it clear, the Python syntax compatibility is not something HP has decided to do (and that explains that there is no UI support for that), it is a feature that I have decided to implement for Giac/Xcas because there is an important change in the highschool curriculum in France, and it seems that a lot of teachers will switch to Python. They will be more comfortable to use Xcas if they can program with the same control structures. Now, I also believe it could be a real plus once integrated in the HP programming editor UI. In the mean time, you can program in Python with your favorite UI on your PC, check that it works with Xcas for Firefox and then transfer to the calc.
Find all posts by this user
Quote this message in a reply
11-13-2017, 12:09 PM
Post: #13
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
most of python things are due beginners trying things out. It does not matter the language if one wants to do something serious.

As one learns python one can learn HPPL or whathever. Moreover without the possibility to integrate the common python libraries, having only the syntax is just cosmetics. Therefore I fully understand the approach of Mr. parisse to cover school curriculums.

But this is misunderstood by those that don't produce actual code.

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
11-13-2017, 04:16 PM
Post: #14
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
You did not copy the code correctly. You will get the translation if you do a copy/paste in a recent version of Xcas (the translation of the pre-parser is displayed so that syntax error are meaningfull).
Find all posts by this user
Quote this message in a reply
11-13-2017, 05:01 PM
Post: #15
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
(11-13-2017 12:09 PM)pier4r Wrote:  most of python things are due beginners trying things out. It does not matter the language if one wants to do something serious.

As one learns python one can learn HPPL or whathever. Moreover without the possibility to integrate the common python libraries, having only the syntax is just cosmetics. Therefore I fully understand the approach of Mr. parisse to cover school curriculums.

But this is misunderstood by those that don't produce actual code.

I don't see the point in just having some of the syntax the same (then again, I don't go to school in France!) I'd love a real, full Python implementation for the Prime but just including some of the syntax elements doesn't do anything for me.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
11-13-2017, 05:46 PM
Post: #16
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
You just need a small subset of Python to run French highschool programs, then it's easy to provide full integration in the calculator, I don't think you can have a full Python that is interoperable with the calculator without very very large changes. And I'm going to add an optionnal feature that I think is useful in an educational context and is not available in Python: declare variable type, and check at sto.
Find all posts by this user
Quote this message in a reply
11-13-2017, 07:32 PM
Post: #17
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
(11-13-2017 05:46 PM)parisse Wrote:  You just need a small subset of Python to run French highschool programs, then it's easy to provide full integration in the calculator, I don't think you can have a full Python that is interoperable with the calculator without very very large changes.

A few people are working on replacement firmware for the Prime (non-HP, of course). I'd gladly give up HPPL, CAS or both for a full Python interpreter.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
11-13-2017, 09:07 PM
Post: #18
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
(11-13-2017 07:32 PM)toml_12953 Wrote:  course). I'd gladly give up HPPL, CAS or both for a full Python interpreter.

I assume you mean "full, standard python" not tiny python or any of the more restricted ones. Would you accept no having C bindings? Would you give up any possibility to interact with the HP math routines and all CAS functions as part of that trade?

TW

Although I work for HP, the views and opinions I post here are my own.
Find all posts by this user
Quote this message in a reply
11-13-2017, 09:08 PM
Post: #19
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
(11-13-2017 07:32 PM)toml_12953 Wrote:  
(11-13-2017 05:46 PM)parisse Wrote:  You just need a small subset of Python to run French highschool programs, then it's easy to provide full integration in the calculator, I don't think you can have a full Python that is interoperable with the calculator without very very large changes.

A few people are working on replacement firmware for the Prime (non-HP, of course). I'd gladly give up HPPL, CAS or both for a full Python interpreter.
Very nice if they succeed, I always respect the work of others. But I doubt a calculator is the best device to program in Python, it's slow and it's certainly hard to port some popular Python modules. I believe it's better to have integrated software and that basic Python-like syntax is sufficient on a calculator.
Find all posts by this user
Quote this message in a reply
11-13-2017, 09:36 PM
Post: #20
RE: HP_PRIME PYTHON SYNTAX: HP_PPY
(11-13-2017 09:08 PM)parisse Wrote:  
(11-13-2017 07:32 PM)toml_12953 Wrote:  A few people are working on replacement firmware for the Prime (non-HP, of course). I'd gladly give up HPPL, CAS or both for a full Python interpreter.
Very nice if they succeed, I always respect the work of others. But I doubt a calculator is the best device to program in Python, it's slow and it's certainly hard to port some popular Python modules. I believe it's better to have integrated software and that basic Python-like syntax is sufficient on a calculator.

Even if not Python then I'd like some other standardized language that's available across platforms. Languages such as C or Pascal. If there's room in the ROM, HPPL could easily be extended to Pascal. It's so close now.

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
Post Reply 




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