Post Reply 
Recursive functions and Advanced Graphing
09-11-2018, 12:34 PM
Post: #1
Recursive functions and Advanced Graphing
I’m intrigued by the ability of the Prime to handle recursion in simple function definitions.. but also its limitations.

Note - I’m just using the Lite app at present, so have no access to programming.

Ay first sight, it appears that the Define function does not allow you to reference the function being defined. If I try to define fact(X)=when(X<=2,X,X*fact(X-1)), it gives a syntax error.

However, I discovered that if you first define fact as a non-recursive function, e.g. X*(X-1), and exit the Define screen, the calculator ‘learns’ that “fact” is a function; you can then go back in and edit the function to make the recursive call to itself.

Sure enough, fact(5) gives 120. (The calculator does struggle after roughly 35 iterations, presumably as the stack overflows, but that’s to be expected.)

Next, I plot fact(X)in the Graphing app. It’s a tad slower than plotting a ‘normal’ function, but very impressive!

I now go into Advanced Graphing, and try Z1: Y=fact(X)... and sadly get an ‘Invalid object’ error.

Does anyone know why this fails, and if there’s a way around it?

(Motivation: what I really want to do is define mandelbrot(N, Z, C)= when(N<2, |Z|<2, mandelbrot(N-1, Z^2+C),C), and plot mandelbrot(20, 0, X+Yi)=1 ! :-) )
Find all posts by this user
Quote this message in a reply
09-11-2018, 04:44 PM
Post: #2
RE: Recursive functions and Advanced Graphing
(09-11-2018 12:34 PM)Sven44 Wrote:  (Motivation: what I really want to do is define mandelbrot(N, Z, C)= when(N<2, |Z|<2, mandelbrot(N-1, Z^2+C),C), and plot mandelbrot(20, 0, X+Yi)=1 ! :-) )

Since it's tail recursive, can't you do it by iteration instead of by recursion?

— Ian Abbott
Find all posts by this user
Quote this message in a reply
09-11-2018, 04:53 PM
Post: #3
RE: Recursive functions and Advanced Graphing
(09-11-2018 04:44 PM)ijabbott Wrote:  Since it's tail recursive, can't you do it by iteration instead of by recursion?

That would be great - but is there a library function to perform iteration? Remember this is in the purely functional 'Define' environment.. I can't write a procedural program.

thanks
Find all posts by this user
Quote this message in a reply
09-12-2018, 04:30 AM
Post: #4
RE: Recursive functions and Advanced Graphing
The Advanced Graphing app is rather special in terms of which functions/equations it works for. I don't know what exactly the requirements are unfortunately, but I'm getting the same error for `RE((x+i*y)^3)=0` for example (the i appears to be the problem). I suspect that recursive functions might simply not be supported there.

I think there's some sophisticated algorithm at work in the Advanced Graphing app that performs some kind of analysis and/or calculus on the equation, though again I'm not sure what exactly that is. Maybe it's using gradients to find roots or something like that?
Find all posts by this user
Quote this message in a reply
09-12-2018, 05:25 AM
Post: #5
RE: Recursive functions and Advanced Graphing
Hello,

The advanced grapher uses its own "evaluator" which is mostly float based. This is why your function does not work...

I think that "when" is a CAS function, which does not help...
Try using IFTE which is a home function instead, it will, at minimum speed up things and allows for larger recurssion.

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
09-12-2018, 09:21 AM
Post: #6
RE: Recursive functions and Advanced Graphing
Thanks both for those answers.

I've switched to IFTE (wasn't familar with that), and that certainly seems to be an improvement in terms of just invoking the function from the home screen - as you say, handles larger numbers, and quite a bit faster in the simple graphing app too.

But still no luck in the advanced grapher. I think you're both right - it must be doing all kinds of clever optimisation, pre-compilation, interpolation or whatever else, and just can't handle recursive user-defined functions. (Even my factorial function, changed to using IFTE.) It does happily plot a simple user-defined function though, like Y=Squ(X), = X*X.

Oh well... it was probably asking a bit much!
Find all posts by this user
Quote this message in a reply
09-12-2018, 07:14 PM
Post: #7
RE: Recursive functions and Advanced Graphing
(09-12-2018 05:25 AM)cyrille de brébisson Wrote:  Hello,

The advanced grapher uses its own "evaluator" which is mostly float based. This is why your function does not work...

I think that "when" is a CAS function, which does not help...
Try using IFTE which is a home function instead, it will, at minimum speed up things and allows for larger recurssion.

Cyrille

Cyrille, "allows for larger recursion"
How about lifting the stupid 100 recursion of variable/function limit
It was okay in the days of the previous 39G/40G calculators
Now we have have a super calculatrice.
Limit, if any, could be ~ (2^31)-1
Find all posts by this user
Quote this message in a reply
09-13-2018, 06:17 AM
Post: #8
RE: Recursive functions and Advanced Graphing
Hello,

"How about lifting the stupid 100 recursion of variable/function limit"

Which restriction are you refering to here? It does not strike a bell that there is such restriction, at least in home.

We have restrictions on things like list (10K elements I think)... but not on variables/functions...

Functions do have a hard limit of 16 parameters, but this is due to memory structures, not because of an arbirary limit.

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
09-13-2018, 10:11 AM
Post: #9
RE: Recursive functions and Advanced Graphing
(09-13-2018 06:17 AM)cyrille de brébisson Wrote:  Hello,

"How about lifting the stupid 100 recursion of variable/function limit"

Which restriction are you refering to here? It does not strike a bell that there is such restriction, at least in home.

We have restrictions on things like list (10K elements I think)... but not on variables/functions...

Functions do have a hard limit of 16 parameters, but this is due to memory structures, not because of an arbirary limit.

Cyrille

CAS Settings
Page 2
The maximum of these is 100; it could be 2,147,483,647

Recursive Evaluation
Recursive Replacement
Recursive Function
---------------------------------------
Find all posts by this user
Quote this message in a reply
09-14-2018, 05:31 AM
Post: #10
RE: Recursive functions and Advanced Graphing
Hello,

Ho, that CAS setting...
I will see with Bernard why it is there...

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
09-14-2018, 08:44 AM
Post: #11
RE: Recursive functions and Advanced Graphing
(06-14-2014 06:43 PM)parisse Wrote:  I don't know any portable way to check for stack overflow in C. That's the reason for the max recursive eval parameter. Initially, you got a runtime error if the max number of recursive calls was reached, later I implemented a non-recursive evaluator (with dynamic allocation of the space required to make recursive calls), you don't get an error anymore, but it's slower, therefore the warning remains.

Perhaps on the G2, with 256Mb ram, the restriction could be relaxed?
Find all posts by this user
Quote this message in a reply
Post Reply 




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