From: John Quincy on
Is there workaround for the no recursion in embedded matlab, I would really like to implement a recursive function but It won't let me. Does anyone have a workaround or even a reason why this constraint exists?
From: Walter Roberson on
John Quincy wrote:
> Is there workaround for the no recursion in embedded matlab, I would
> really like to implement a recursive function but It won't let me. Does
> anyone have a workaround or even a reason why this constraint exists?

Embedded matlab is, as best I understand, designed so that the calculations
all take predictable time and space, or at least provably bounded time and
space. These proven upper bounds are strictly necessary for real-time work: if
you have a function that can take an unpredicatable time or space, then you
cannot be sure of meeting your time targets or you cannot be sure that your
allocated stack or heap space is sufficient.

Recursive functions, in the general case, take unpredictable time and space.
You can convert any recursive algorithm to an iteritive algorithm (there is a
theorem about that that even outlines how to do it), but the cost of the
conversion is that the iterative algorithm might take an unbound amount of
heap space.

If you have a recursive algorithm whose time and space bounds are predictable
based upon its initial inputs, and you are able to prove that your overall
program has an upper bound on the time/space over all instances (e.g., because
you reject all problem sizes greater than a certain amount), then you should
probably convert the recursive algorithm to an interactive algorithm with a
fixed maximum stack size.
From: Michael Hosea on
"John Quincy" <q.john92(a)gmail.com> wrote in message
news:hr9qom$1vf$1(a)fred.mathworks.com...
> Is there workaround for the no recursion in embedded matlab, I would
> really like to implement a recursive function but It won't let me. Does
> anyone have a workaround or even a reason why this constraint exists?

The reason the constraint exists is complicated, but a big part of it is
that the Embedded MATLAB compiler uses a technique called "partial
evaluation", and before we supported variable-size arrays, the use case for
recursion wasn't very compelling. Nobody (or maybe I should say "hardly
anybody") wants their 5-line recursive function expanded to n or log2(n) or
whatever number of specialized, non-recursive C functions. In other words,
some development is required to enable the compiler to give you the code you
actually *want*. However, now that we have variable-sizing of arrays (since
R2009b) the case for recursion is much stronger than it used to be. I have
opened an enhancement request for this.
--
Mike


From: John Quincy on
"Michael Hosea" <Michael.Hosea(a)mathworks.com> wrote in message <hra2uo$8t3$1(a)fred.mathworks.com>...
> "John Quincy" <q.john92(a)gmail.com> wrote in message
> news:hr9qom$1vf$1(a)fred.mathworks.com...
> > Is there workaround for the no recursion in embedded matlab, I would
> > really like to implement a recursive function but It won't let me. Does
> > anyone have a workaround or even a reason why this constraint exists?
>
> The reason the constraint exists is complicated, but a big part of it is
> that the Embedded MATLAB compiler uses a technique called "partial
> evaluation", and before we supported variable-size arrays, the use case for
> recursion wasn't very compelling. Nobody (or maybe I should say "hardly
> anybody") wants their 5-line recursive function expanded to n or log2(n) or
> whatever number of specialized, non-recursive C functions. In other words,
> some development is required to enable the compiler to give you the code you
> actually *want*. However, now that we have variable-sizing of arrays (since
> R2009b) the case for recursion is much stronger than it used to be. I have
> opened an enhancement request for this.
> --
> Mike
>
Thank you for the detailed response Michael and Walter