Python: Mandelbrot explorer 0.9 now save and resume update Jan 23 /2022
06-09-2021, 08:31 PM (This post was last modified: 01-23-2022 11:01 PM by Guenter Schink.)
Post: #1
 Guenter Schink Senior Member Posts: 476 Joined: Dec 2013
Python: Mandelbrot explorer 0.9 now save and resume update Jan 23 /2022
edit Jan22/2022
Major changes:

1. Solid Guess
3. Time out
4. Auto Save / Resume
5. using "TEXTOUT_P() from HOME without to much hassle
/edit

Next exploration on Python!

Please keep in mind, that I'm a real beginner with Python and that I have no great experiences with other program languages other than a little bit of basic and pascal 30 years ago. And some RPN and RPL of course :-). The objective is to explore some specifics of Python on the Prime. The Mandelbrot set is a welcome test bed.

After my first attempts with Python on the Prime I have now completed something I'd like to share with you. It's a Mandelbrot explorer. Here https://en.wikipedia.org/wiki/Mandelbrot_set is a summary of what this set is.

This program let's you dig really deep into the fascination of the Mandelbrot set at a very amazing speed for a calculator.

My program starts with the well known picture of the Mandelbrot set. Upon completion of the first drawing it will immediately show an introductory help screen, telling you about the keys to be pressed.

When going back to the Mandelbrot display you might be disappointed a little bit by the less than perfect resolution. But this of course has a reason. The calculations for each pixel may take an awful lot of time. Therefore I decided to at first plot in "coarse" mode (a square of 4x4 pixels instead of one pixel at a time). This enables you to go rather quick (for a calculator) through the different zooms back and forth. Once you decide to have a better resolution, press [Plot] and there you go and you'll immediately recognize how much more time the fine draw needs over the coarse draw.

You zoom in with just a touch on the screen. Your hit will determine the center of the new picture enlarged by a zoom factor of initially 4. The backspace (Del) key brings you back the previous pictures up to the first one. A little square in the top left corner will tell you the current process was finished. (Beep is missing )

The second row X^Y, SIN,COS,TAN,LN,LOG lets you switch between different color schemes, these are: black and white; 8, 27, 64, 125 and 216 colors of RGB

These different color schemes are really useful. While the higher color numbers show overall nicer pictures, those with only 27 or less provide you with a 3D appearance when zoomed in at specific places and iterations set to higher values. Selecting larger iterations will get you there.

[Plot] as mentioned before, the fine draw

[Num] shows the values of the current screen: Iiterations, magnification, zoom factor, co-ordinates, colors, time needed for the creation of the display.

The set starts at the co-ordinates (-2, 1) for the real part and (1.2,-1.2) for the imaginary part. Initial number of iterations(depth) is 20. The depth will increase by 10 each time you push [+]; [-] consequently will decrease the depth until a minimum of 10. [x] and [÷] will increase/decrease the iterations by 50.

[x²]/[EEX] increase/decrease the zoom factor. Best done when you had switched to the information screen [Num]. There you'll immediately see, what you've selected.

[Help] shows the introductory screen again, with the information on the keys.

[Home] takes you back to the initial screen.

[Esc] finally lets you exit. There you have the choice to restart or switch to the Home (or Cas) screen.

If you are interested in examining my program, for Windows users I'd strongly recommend "notepad++". With the "notepad++" feature of collapsing you have a great view on the simple structure which is otherwise hidden by the lines of code. And "notepad++" is a great help in tracking the various levels of indentation, that's so important with Python.

Tribute to Geoff whose Mandelbrot Program was fundamental to get the components that I had already created before in a reasonable order. I even applied a lot of the names he used, because they are so nicely descriptive.

Unpack the.zip, copy the directory"MandelExpl.hpappdir" to (most probably) "C:\Users\<some user>\Documents\HP Connectivity Kit\Content\Programs" and from there let the Ck send it to your Prime.

Please let me know of any quirks you might encounter. I'd really love to get some comments and perhaps suggestions.

Enjoy, Günter

Discussion, hints on Python specifics are very welcome

Attached File(s)
06-09-2021, 09:33 PM
Post: #2
 matalog Member Posts: 206 Joined: May 2021
RE: Python: Mandelbrot on fire, the explorer
That's a great explorer. I wonder how deep it will be able to go.

Thanks for sharing it.

One time I ran it and the cable connecting to it moved slightly and disconnected from USB while the initial screen was drawing, and it froze the calculator completely.
06-09-2021, 09:33 PM (This post was last modified: 06-10-2021 01:05 AM by Liamtoh Resu.)
Post: #3
 Liamtoh Resu Member Posts: 116 Joined: May 2021
RE: Python: Mandelbrot on fire, the explorer
Thanks for the program. :)

useage: All the functions worked ok on the real prime. I have not tested it on the emulator.

edit: some revised text has been added since the first new reply.

notepad ++ : I created a 27k pdf file of MandelExpl.py via notepad ++ and pdf creator.
It contains the line numbers and color and index highlighting.
I can attach it to a reply if you want it to be done.

(The following uses the windows file manager and the CK. A dual-pane file manager
such as the open source Double Commander could be used to do the manual
copy of the directory from the zip file.)

file management: using the hp prime connectivity kit.
The content folder can be used to store a copy of the zip file.
The zip file can be copied from an arbritary folder to there.
Drag the zip file to the Programs or App Library.
If everything was connected the MandelExpl icon will
appear as one of the apps.
(please feel free to edit or use the file management process)
06-10-2021, 09:06 PM
Post: #4
 Guenter Schink Senior Member Posts: 476 Joined: Dec 2013
RE: Python: Mandelbrot on fire, the explorer
Thank you User,

Günter
07-11-2021, 03:45 PM
Post: #5
 Guenter Schink Senior Member Posts: 476 Joined: Dec 2013
RE: Python: Mandelbrot on fire, the explorer
Mandelbrot explorer update 2021-07-11 Updated in post#1

After some nice days of vacation, I returned to my Python testbed, the Mandelbrot Explorer. The basic description in post #1 still applies. But I've done some changes for a hopefully much nicer appearance. And I'll ask the moderators to move this thread to the Prime Software Library.

When you start the program, you'll recognize that the screen is quickly filled with blocs which broadly resemble the Mandelbrot set. But then the drawing is going on - on the left hand side a small progress bar is visible that tells you that the program is active still, and will finish shortly

A small explanation, how it's done and where you might adjust things with ease.

The drawing is done in different stages.

CoarseDraw: this procedure draws blocs of arbitrary size. Here I choose 10 and 4 pixels high and wide to quickly show something that looks already similar to the Mandelbrot set, these values can be changed at will.

The last bloc is 2 pixels high and wide, this value must not be changed, because the procedure further on is dependent on this value.

Now we have the screen filled with blocs of 2x2 pixels each of the color of the top, left pixel. That is on each even line number (lines y start with 0) in each even column the pixel (columns x start with 0) has got the correct color. The other three pixels in the 2x2 square may be right or not.

Next step I call dithering (not quite correct, but ...) : Take the lines where y is odd, start with the column where x is odd and set each second pixel to its correct color. After that we have half of all the pixels set to the correct color and many of the others also, rather randomly.

By this procedure we avoide to do the time consuming calculation more than once for each pixel. The first two paintings of blocs (10 and 4) are negligible time-wise.

FineDraw: Last step is to get the right color for the remaining pixels. Start at the first line (y=0) set x=1 (x=0 already has the correct color) calculate the correct color for each second pixel. That is the pixels with x=odd get calculated and colored. Then in the next line (y=1) we do the same with the pixels where x=even. We continue, so that in lines where y=even all pixels where x=odd, and in lines where y=odd all pixels where x=even get calculated and colored correctly.

Why all this fuss? The Mandelbrot set does an awful lot of calculations. I start now with the maximum Iterations set to a depth of 100. While the calculator is working you have a fair view on to what is developing. By omitting the last FineDraw , the required time is only almost half of the time required for the full procedure. Yet it already shows a reasonable (in my view) appearance where you might like to go deeper and deeper, before you decide to press [Plot] for the perfect picture. The limit for magnification is ~2^13

Line 16: set CompleteAllPasses=False to True, specifically for the Virtual Prime for convenience.
Line 60: for size in (10, 4, 2) don't change "2" but you may play with the other figures, even add some e.g.(20, 15, 10, 8, 6, 2)
Line 190: change the initial boundaries for the set
line 191: change the initial values for the depth(Iterations), mag(which color scheme), initial ZoomFactor

It is really unbelievable how fast this Python implementation is on the Prime.

Kudos to the "Stone Soup Group" for FRACTINT and for H.-O-Peitgen and P.H.Richter for the book "The Beauty of Fractals".

Feed back is very welcome

Günter
08-11-2021, 06:15 PM
Post: #6
 jte Member Posts: 165 Joined: Feb 2014
RE: Python: Mandelbrot on fire, the explorer update 11.07.2021
Nice!

Ever since the 39gII days, I've been thinking of adding an Iterated Function app as a built-in app. It's great for other coders to be able to look at your code - a nice example! I found your code pretty easy to follow (I was wondering why the left edge had yellow and red along it).

When I was thinking of this sort of app, I was thinking of finishing up by hitting all 3 pixels from each 2x2 block remaining after the finest "coarse" phase finished. (And, with the Prime, allow computation to continue into subpixels.)
08-14-2021, 07:36 PM (This post was last modified: 08-14-2021 07:48 PM by Guenter Schink.)
Post: #7
 Guenter Schink Senior Member Posts: 476 Joined: Dec 2013
RE: Python: Mandelbrot explorer now interactive update 14.08.2021
Next update! Version 0.8 (Link in post #1) 14.08.2021

Couldn't help but had to play around once more.

In principle, the usage hasn't changed Look at the first post), except that the program is more interactive.

At any time while the drawing is developing you can touch the screen to dive in deeper, or push <Del> to go back to the last magnification. Also the various buttons for color selection or depth increase/decrease may be pushed at any time with immediate effect.

When you choose to increase or decrease the zoom-factor, this will take effect at the next zooming in.

As a teaser I have attached 4 samples. Sample1 and Sample2 exactly show the same area, just different colors. Do you recognize how much more a 3D- effect seems to be in 1 compared to 2?

Sample3 shows a completely different area.

While the drawing develops, a progress bar at the left shows you in which line the action is. And during drawing the bottom line shows you the number of colors selected, the depth (number of iterations) the zoom factor and the actual zoom, see sample4. The progress bar and bottom line are barely visible when you run the virtual PRIME, but they help you to see that something is happening still when it takes its time on the real calc.

I'd like to emphasize once again that the various passes of drawing simply serve the goal to make something reasonable visible as early as possible. Thus you can move in deeper and deeper (and back) without having to wait until the drawing is completed. Consequently the <Plot> key has no function anymore.

I hope someone enjoys this little gimmick

Günter

1.     2.     3.     4.
08-14-2021, 07:46 PM
Post: #8
 Guenter Schink Senior Member Posts: 476 Joined: Dec 2013
RE: Python: Mandelbrot explorer now interactive update 14.08.2021
(08-11-2021 06:15 PM)jte Wrote:  Nice!

Ever since the 39gII days, I've been thinking of adding an Iterated Function app as a built-in app. It's great for other coders to be able to look at your code - a nice example! I found your code pretty easy to follow (I was wondering why the left edge had yellow and red along it).

When I was thinking of this sort of app, I was thinking of finishing up by hitting all 3 pixels from each 2x2 block remaining after the finest "coarse" phase finished. (And, with the Prime, allow computation to continue into subpixels.)

Thanks for your comments. But I must admit that I'm just scratching the surface of Python. Something much more sophisticated is "Oulan's" PI41 emulator of the HP-41.

As you liked my little effort I guess you'll like the update even more.

Günter
08-15-2021, 06:10 AM
Post: #9
 jte Member Posts: 165 Joined: Feb 2014
RE: Python: Mandelbrot explorer now interactive update 14.08.2021
Nice update

Last time around, I was having fun with it on a virtual Windows calc. Tonight I updated one of my G1 calcs to 2.1.14588 and tried out Mandelbrot Explorer ver. 0.5.

Of course, I first wanted to try out the newest version of Mandelbrot Explorer, but I didn't have success transferring the app to the calc using the Connectivity Kit (after failing with the transfer to a physical G1 calc I tried with a virtual Windows calc, but same: no luck transferring!). I persevered & found success: I copied and pasted the Python source code from notepad into a virtual calc.

Nice & fun polishing, drawing out over the progress bar at the end! Gives the user a real sense of satisfaction.

The improved responsiveness to impatient users is good too — it pacifies those of us hammering away at the keys while the machine is simply trying to make a nice plot. If I'm admitting to impatience, I should also admit that I do try to dismiss the useful Help screen that comes up after the first plot by tapping on it.
08-15-2021, 11:36 AM
Post: #10
 Guenter Schink Senior Member Posts: 476 Joined: Dec 2013
RE: Python: Mandelbrot explorer now interactive update 15.08.2021
(08-15-2021 06:10 AM)jte Wrote:  Nice update

Last time around, I was having fun with it on a virtual Windows calc. Tonight I updated one of my G1 calcs to 2.1.14588 and tried out Mandelbrot Explorer ver. 0.5.

Of course, I first wanted to try out the newest version of Mandelbrot Explorer, but I didn't have success transferring the app to the calc using the Connectivity Kit (after failing with the transfer to a physical G1 calc I tried with a virtual Windows calc, but same: no luck transferring!). I persevered & found success: I copied and pasted the Python source code from notepad into a virtual calc.
Arghh... my fault. Instead of zipping the directory of
"MandelExpl.hpappdir" I only zipped the contents of this directory and that of course is not understood by the connectivity kit.

I've updated the zip-file. You can either download it again, or simply put the files from the zip in a directory named "MandelExpl.hpappdir" and send this directory to the Prime. Sorry for any inconvenience
Quote:Nice & fun polishing, drawing out over the progress bar at the end! Gives the user a real sense of satisfaction.

The improved responsiveness to impatient users is good too — it pacifies those of us hammering away at the keys while the machine is simply trying to make a nice plot. If I'm admitting to impatience, I should also admit that I do try to dismiss the useful Help screen that comes up after the first plot by tapping on it.
I listen to my customers

simply change the function "def wait()"
Code:
def wait():     ClearKeyboardBuffer();keyCode=keyboard()     ClearMouseBuffer();mm=mouse()     while keyCode==0 and mm==((),()):         keyCode=keyboard();mm=mouse()     ClearKeyboardBuffer();ClearMouseBuffer()
Now any key or touch will leave the help or info screen.

Günter
08-15-2021, 06:44 PM
Post: #11
 jte Member Posts: 165 Joined: Feb 2014
RE: Python: Mandelbrot explorer now interactive update 15.08.2021
(08-15-2021 11:36 AM)Guenter Schink Wrote:
(08-15-2021 06:10 AM)jte Wrote:
Nice update

I didn't have success transferring the app to the calc using the Connectivity Kit
Arghh... my fault. Instead of zipping the directory of
"MandelExpl.hpappdir" I only zipped the contents of this directory and that of course is not understood by the connectivity kit.

I've updated the zip-file.

That did the trick! I've now got Mandelbrot Explorer ver. 0.8 on two of my G1 calcs.

Quote:
Sorry for any inconvenience

No problem at all! I've had fun playing around with the new version this morning.

One interesting thing I noticed is that, when doing more complex plots, I have to intentionally hold down my finger until the display stops updating (and then release it) in order to get a zoom. From looking at the Python source, I'm thinking this is due to checking for touch events once per line (and that quick taps may entirely happen in the middle of rendering one row). This may well be an acceptable efficiency trade-off (to checking more often; I didn't try changing the code). The presence of "ClearMouseBuffer()" made me think perhaps it would take a couple of rows for just-the-right mouse event to show up, but that doesn't seem to be the case.

Quote:
Quote:
I should also admit that I do try to dismiss the useful Help screen that comes up after the first plot by tapping on it.
I listen to my customers

simply change the function "def wait()"
Code:
def wait():     ClearKeyboardBuffer();keyCode=keyboard()     ClearMouseBuffer();mm=mouse()     while keyCode==0 and mm==((),()):         keyCode=keyboard();mm=mouse()     ClearKeyboardBuffer();ClearMouseBuffer()
Now any key or touch will leave the help or info screen.

Nice! Now I have ver 0.8PRO.
08-15-2021, 07:25 PM (This post was last modified: 08-15-2021 07:48 PM by Guenter Schink.)
Post: #12
 Guenter Schink Senior Member Posts: 476 Joined: Dec 2013
RE: Python: Mandelbrot explorer now interactive update 15.08.2021
(08-15-2021 06:44 PM)jte Wrote:  One interesting thing I noticed is that, when doing more complex plots, I have to intentionally hold down my finger until the display stops updating (and then release it) in order to get a zoom. From looking at the Python source, I'm thinking this is due to checking for touch events once per line (and that quick taps may entirely happen in the middle of rendering one row). This may well be an acceptable efficiency trade-off (to checking more often; I didn't try changing the code). The presence of "ClearMouseBuffer()" made me think perhaps it would take a couple of rows for just-the-right mouse event to show up, but that doesn't seem to be the case.
Yes, you're right. At first I used this program on a G2, where this problem only occasionally pops up. But on the G1 it's more discernible. Checking for touch or key at each pixel has a significant cost in time. Therefore I believe its a matter of getting accustomed to the need of a more determined touch. But it should suffice to let the finger for about a second (say "twenty one") on the screen, worked for me on the G1. It usually also works this way with the keys.

edit: Just tested the G1 to have the touch and key checked at each round of "x" in the first and second pass only. That has a lesser draw back in time. The initial screen only needs 4 seconds more, ~33.5 instead of ~29.5, well that could be acceptable. And is immediately more responsive. If you wish to give it a try, simply adjust the respective indentations. /edit

Unfortunately the G1 is much slower, ~3-times, than the G2. But the Virtual Prime is an alternative for excessive explorations.

Günter
01-06-2022, 06:16 PM
Post: #13
 TomC Member Posts: 283 Joined: Dec 2013
RE: Python: Mandelbrot explorer now interactive update 15.08.2021
Hello Guenter:

Happy New Year!!!

Nice work. I've long played with Mandelbrot code - from the days of having an Atari 800 or a DOS PC run all night!!!, and this app is great.

I am stumbling a bit with using the Prime (I've been distracted by other machines and life!!!). The whole connectivity kit, App structure and Python still require some exercise on my part!!!

It would be interesting to compare the Python performance with 'native' HPPPL performance.

Regards,
TomC

(08-15-2021 07:25 PM)Guenter Schink Wrote:
(08-15-2021 06:44 PM)jte Wrote:  One interesting thing I noticed is that, when doing more complex plots, I have to intentionally hold down my finger until the display stops updating (and then release it) in order to get a zoom. From looking at the Python source, I'm thinking this is due to checking for touch events once per line (and that quick taps may entirely happen in the middle of rendering one row). This may well be an acceptable efficiency trade-off (to checking more often; I didn't try changing the code). The presence of "ClearMouseBuffer()" made me think perhaps it would take a couple of rows for just-the-right mouse event to show up, but that doesn't seem to be the case.
Yes, you're right. At first I used this program on a G2, where this problem only occasionally pops up. But on the G1 it's more discernible. Checking for touch or key at each pixel has a significant cost in time. Therefore I believe its a matter of getting accustomed to the need of a more determined touch. But it should suffice to let the finger for about a second (say "twenty one") on the screen, worked for me on the G1. It usually also works this way with the keys.

edit: Just tested the G1 to have the touch and key checked at each round of "x" in the first and second pass only. That has a lesser draw back in time. The initial screen only needs 4 seconds more, ~33.5 instead of ~29.5, well that could be acceptable. And is immediately more responsive. If you wish to give it a try, simply adjust the respective indentations. /edit

Unfortunately the G1 is much slower, ~3-times, than the G2. But the Virtual Prime is an alternative for excessive explorations.

Günter
01-08-2022, 06:55 PM
Post: #14
 Guenter Schink Senior Member Posts: 476 Joined: Dec 2013
RE: Python: Mandelbrot explorer now interactive update 15.08.2021
(01-06-2022 06:16 PM)TomC Wrote:  Hello Guenter:

Happy New Year!!!

Nice work. I've long played with Mandelbrot code - from the days of having an Atari 800 or a DOS PC run all night!!!, and this app is great.

I am stumbling a bit with using the Prime (I've been distracted by other machines and life!!!). The whole connectivity kit, App structure and Python still require some exercise on my part!!!

It would be interesting to compare the Python performance with 'native' HPPPL performance.

Regards,
TomC
Hi Tom,
a PPL-implementation of Mandelbrot can be found here

The 8-Queen Benchmark: Program here and Results look for "Python"

The "Summation Benchmark": Program and Results again, look for "Python"

This should give you some indication how much faster Python is than PPL

Günter,
looking forward to hopefully a meeting this year
01-23-2022, 10:52 PM
Post: #15
 Guenter Schink Senior Member Posts: 476 Joined: Dec 2013
RE: Python: Mandelbrot explorer now interactive update 15.08.2021
Most of what's described in post#1 still applies.

While it's really fun to chew on my Mandelbrot Explorer, my major objective is to explore ways to almost seamlessly interface with the HOME environment. In this update special observation is dedicated to output of text in graphics.

major changes:

1. Solid Guess
3. Time out
4. Auto Save / Resume
5. using "TEXTOUT_P() from HOME without to much hassle

1. SOLID GUESS

Pass 1 calculates blocs of 4x4 pixels and assigns the color found for the top/left pixel to the entire bloc. then it checks its 3 right hand and lower neighbor blocs. Once they all have the same color this bloc is marked as completed. As the term "guess" indicates, this is not perfect but works pretty good specifically once you zoom in deeper and deeper. And it doesn't waist to much time in the "real" Mandelbrot set, The "blue sea".

Pass 1 and 2 fill those blocs which were not marked as completed. That could be done in one pass, but the "checker board" approach gives a nicer appearance.

But if you wish, there is a brute force approach with [Plot]. That takes its time specifically when large parts of the real Mandelbrot set are within the image. Try it yourself. Zoom in so that the entire screen is the "blue sea". The info screen should give an elapsed time of about 3.3 seconds( on a G2) press [Plot] and wait ... The info screen should give you an elapsed time of about 35 seconds. Quite a difference! Now try this with a depth of 300. Time t o get a coffee

2. BOTTOM LINE

The extended possibilities of text output led me to redesign the bottom line. Just for fun. Once the image is finished you still can invoke it with pushing [B] or ([Mem] if that's easier to recall)

3. TIME OUT

As far as I've seen Python doesn't have a time out. Thus if you forgot that "Mandelbrot Explorer" is still running you could end up with a calculator completely out of power. This is now prevented from by taking care of the run time. The help screen, the info screen, and the manually invoked bottom info automatically time out after ~60 seconds. The major loop adds 0.2 seconds during each round, once 240 seconds are reached the program terminates. This may lead to some lag when touching the screen or pushing a button. Simply be more determined.

4. Auto SAVE / RESUME

When the program finishes, either by time out or [Esc] the current Image with its parameters (color, depth, zoomstack) is saved. Upon restart you resume where you left the last run. (Doesn't work) if you leave by pressing [On]

5. TEXTOUT_P()

The textout() function of the module hpprime is rather limited compared to the TEXTOUT_P() function of HOME. This adds optional font (size) selection, a max_width parameter and background color. As I have explained elsewhere the interface is rather cumbersome. In order to make life easier I've created a few function to serve this objective:

Code:
 from hpprime import * def string(arg):                #create a string understood by HOME environment     return '"' + str(arg) +'"'      def RGB(r,g,b):                 #make RGB understood by HOME environment     return (r*256+g)*256+b      def strip(arg):                 #strip parantheses from a tuple converted to string     return str(arg)[1:-1] def TxtOut(t,x,y,f,c,*args):    #provide an easy to handle interface to TEXTOUT_P in HOME     if len(args)>2:             #Syntax: TxtOut(string,x,y,font,color[,max_width[,background color]]        raise(SyntaxError("too many arguments"))     if True in [isinstance(i,list) for i in t]:        raise(TypeError("can't convert 'list' object to str implicitly"))     parms=(int(x),int(y),int(f),int(c))     if len(args)>0:parms+=(int(args[0]),) #add max_width     if len(args)>1:parms+=(int(args[1]),) #add background color     return eval('TEXTOUT_P('+string(t)+','+strip(parms)+')')

This function doesn't implement a choice of the graphic. It always writes to G0, and it doesn't know anything about "2D" functions. But I think it is valuable and easy to use.