The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
Some Python Scripts to understand Variables Transfer
10-21-2022, 10:31 AM (This post was last modified: 10-27-2022 08:01 PM by Ioncubekh.)
Post: #1
Some Python Scripts to understand Variables Transfer
~~~ Transferring Python variables to / from PPL by Guenter Schink ~~~
https://www.hpmuseum.org/forum/thread-17833.html
https://www.hpmuseum.org/forum/thread-18...#pid160378

~~~ Using sys.argv[0] by drbarnack ~~~
https://www.hpmuseum.org/forum/thread-16...#pid148350

A script that produces x2 Matrix assigned data generated from python. Can be used in CAS for plotting multiple data in same graph like: plotlist(M1), plotlist(M2)
Code:

EXPORT ppl_mat(dt1, dt2)
BEGIN
PRINT;                        
// flushing HP's M1 & M2 matrix & inserting all data at once
M1:={}; M1:=dt1;
M2:={}; M2:=dt2;
END;
  
#PYTHON EXPORT ukmat()
#start

# for eval()
from hpprime import *
# for sin() cos() pi
from math import *
# for linspace range() doesnt allow noninteger step
from linalg import linspace

# Will return string {{..},{..},...,{..}}
def _2str(a,b):
   c=list(zip(a,b)) # unpacking syntax isn't supported =[*zip(a,b)]
   return str(c).replace('[','{').replace(']','}').replace('(','{').replace(')','}')
   
# some list comprehensions
uk_x=linspace(-3*pi,3*pi,100)
uk_y1=[cos(i) for i in uk_x]
uk_y2=[sin(i) for i in uk_x]
# Use custom function _2str to generate HP Matrix syntax
uk_y1=_2str(uk_x, uk_y1)
uk_y2=_2str(uk_x, uk_y2)

# PPL function will execute 
ttt=eval('ppl_mat('+uk_y1+','+uk_y2+')')  
print('DONE')

#end

Above can be modified to produce lists instead of Matrix; useful to study them in APPs related to Stats
Code:

EXPORT crtlst(dt1,dt2)
BEGIN
// Explanation in Py.PPL.Matrix.Plotlist.txt
PRINT;                        
L1:={}; L1:=dt1;
L2:={}; L2:=dt2;

END;
  
#PYTHON EXPORT ukvar()
#start

from hpprime import *
from math import *

uk_x=[i*2 for i in range(-10,10)]
uk_y=[cos(i) for i in uk_x]
uk_x=str(uk_x).replace('[','{').replace(']','}')
uk_y=str(uk_y).replace('[','{').replace(']','}')
ttt=eval('crtlst('+uk_x+','+uk_y+')')  
print('Done')

#end

A further implementation of python dict() that avoids excessive use of if else blocks. Perhaps a Python equivalent of PPL CHOOSE()

Code:

EXPORT passgrd(dt)
BEGIN 
  AVars("marks"):=dt;
  PYTHON(pygrd);                     
END;
  
#PYTHON EXPORT pygrd()
#start
from hpprime import *
# python dict() good alternative to CHOOSE()
grdlst = { 'A':range(90,100+1), 'B':range(80,90), 'C':range(70,80), 'D':range(60,70), 'F':range(0,60) }
marks=eval('AVars("marks")')
def _pygrd(arg1):
  for i in grdlst.keys():
    if arg1 in grdlst[i]:
      return i

grd=_pygrd(int(marks))

eval( 'AVars("grd"):='+'"'+grd+'"' )
eval( 'MSGBOX("Your Grade is: '+grd+'")' )

#end

HP Prime G1
Python via Android Termux...
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Some Python Scripts to understand Variables Transfer - Ioncubekh - 10-21-2022 10:31 AM



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