Why do you have recursive functions?
Note that numbers themselves are recursive - most practical
applications that "need recursive functions" can be redefined to
instead "need functions with numeric domains".
Put differently: a specification which specifies *how* something gets
done will often be miserable to implement and have miserable
performance. A specification which instead specified *what* needs to
get done will tend to be more pleasant to work with.
Translating between them tends to be a "mere matter of mathematics"
(which, ok, is sometimes intractable).
But impractical problems can be a sign that you should avoid J and
instead work with the language the problems were designed for.
That said, I'd just code it up in the simplest most obvious way and
then take a look at what the results were like to decide if I wanted
to do anything different.
That said, in the case of:
F(t,i,j,k) is a minimum of
{C(i,j,k)+ sum of all g(t-1, i',j',k) + sum of all h(t-1, i, j", k') }
and
{F(t,i-1,j,k-1)+1},
where C -- user defined cost function,
i' from 0 to i-1, j' and j" from zero to j-1, k' from 0 to k-j.
That's an incomplete specification.
It's not just that you've not defined C, g and h, but you've also not
specified what F is like when you have zeros for either of the even
numbered arguments. (Also, you seem to have an implicit constraint
that k be no smaller than j -- but at least that's sort of stated.)
I also think it's a mistake to declare j' and j'' as being independent
- I don't see any valid reason for that. At least, not yet.
In other words, a quick and dirty implementation might look like this:
F=:3 :0
't i j k'=. y
I=. i.i assert. i>:0
J=. i.j assert. j>:0
K=. i.1+k-j assert. k>:j
G=.+/,g&> (t-1),each ({I;J),each k
H=.+/,h&> (t-1),each i,each {J;K
(1+F t;(i-1);j;k-1) <. (C i;j;k)+G+H
)
And that will always fail. Do you see why?
Anyways... the general rule is: first make sure you are computing the
right result - that you are solving the right problem. It's not worth
bothering to trying to do anything else until you've gotten past that
point.
Once you have a sample solution (and that includes having reasonably
sized example data and corresponding example results) it's often worth
spending some time trying to understand what's happening so you can do
it better.
Thanks,
--
Raul
--
Raul
On Sun, Apr 5, 2015 at 5:34 AM, Sergeif <[email protected]> wrote:
> I'm not sure. May be I should use memoization instead. I have a bunch of
> recursive functions f,g,h,... defined like this:
>
> F(t,i,j,k) is a minimum of
> {C(i,j,k)+ sum of all g(t-1, i',j',k) + sum of all h(t-1, i, j", k') }
> and
> {F(t,i-1,j,k-1)+1},
> where C -- user defined cost function,
> i' from 0 to i-1, j' and j" from zero to j-1, k' from 0 to k-j.
>
> I'm intrested in a value of F(T,N,M,K) for some T,N,M,K ≤ 1000. A common
> pattern for tasks like this would be very helpful for me.
> 04 апр. 2015 г. 23:50 пользователь "Henry Rich" <[email protected]>
> написал:
>
>> It's unusual in J to need the full odometer array. Are you sure that your
>> problem requires it?
>>
>> Henry rich
>>
>> On 4/4/2015 5:48 PM, Sergeif wrote:
>>
>>> Thanks! It's 10x faster than my code but 10x slower than odometer
>>> function.
>>> Anyway I lernt the catalogue verb from this.
>>>
>>> On Sat, Apr 4, 2015 at 11:21 PM, R.E. Boss <[email protected]> wrote:
>>>
>>>
>>>> >,{<@i."0[2 3 4
>>>> 0 0 0
>>>> 0 0 1
>>>> 0 0 2
>>>> 0 0 3
>>>> 0 1 0
>>>> 0 1 1
>>>> 0 1 2
>>>> 0 1 3
>>>> 0 2 0
>>>> 0 2 1
>>>> 0 2 2
>>>> 0 2 3
>>>> 1 0 0
>>>> 1 0 1
>>>> 1 0 2
>>>> 1 0 3
>>>> 1 1 0
>>>> 1 1 1
>>>> 1 1 2
>>>> 1 1 3
>>>> 1 2 0
>>>> 1 2 1
>>>> 1 2 2
>>>> 1 2 3
>>>>
>>>> R.E. Boss
>>>>
>>>>
>>>> -----Original Message-----
>>>>> From: [email protected] [mailto:programming-
>>>>> [email protected]] On Behalf Of Nollaig MacKenzie
>>>>> Sent: zaterdag 4 april 2015 23:16
>>>>> To: [email protected]
>>>>> Subject: Re: [Jprogramming] indexing a table
>>>>>
>>>>> Is something like this what you have in mind?
>>>>>
>>>>> m=. i. 2 3 4
>>>>> indmak m
>>>>> 0 0 0
>>>>> 0 0 1
>>>>> 0 0 2
>>>>> 0 0 3
>>>>>
>>>>> 0 1 0
>>>>> 0 1 1
>>>>> 0 1 2
>>>>> 0 1 3
>>>>>
>>>>> 0 2 0
>>>>> 0 2 1
>>>>> 0 2 2
>>>>> 0 2 3
>>>>>
>>>>>
>>>>> 1 0 0
>>>>> 1 0 1
>>>>> 1 0 2
>>>>> 1 0 3
>>>>>
>>>>> 1 1 0
>>>>> 1 1 1
>>>>> 1 1 2
>>>>> 1 1 3
>>>>>
>>>>> 1 2 0
>>>>> 1 2 1
>>>>> 1 2 2
>>>>> 1 2 3
>>>>> indmak
>>>>> (#: i.)@$
>>>>>
>>>>>
>>>>> On 2015.04.04 23:09:58, you,
>>>>> the extraordinary Sergeif, spake thus:
>>>>>
>>>>> Hi.
>>>>>>
>>>>>> How can someone create list of indexes of 3d table (N x M x K)?
>>>>>>
>>>>>> I have written this simple code:
>>>>>>
>>>>>> ind3d =: 3 : 0
>>>>>> 'n m k' =. y
>>>>>> p0 =. k&|
>>>>>> p1 =. (m&|)@:<.@:(%&k)
>>>>>> p2 =. <.@:(%&(m*k))
>>>>>> (p2 , p1 , p0)"0 (i. (n*m*k))
>>>>>> )
>>>>>>
>>>>>> but it's very very slow. Does any tacit solution for this problem
>>>>>>
>>>>> exist?
>>>>
>>>>>
>>>>>> Another question is how to fill the table with values depending on
>>>>>>
>>>>> indexes
>>>>
>>>>> of cell? For example, F[i,j,k] = (i * j) - (i * k) + (j * k).
>>>>>> ----------------------------------------------------------------------
>>>>>> For information about J forums see
>>>>>>
>>>>> http://www.jsoftware.com/forums.htm
>>>>>
>>>>> --
>>>>> Nollaig MacKenzie
>>>>> http://www.yorku.ca/nollaig
>>>>> ----------------------------------------------------------------------
>>>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>>>
>>>> ----------------------------------------------------------------------
>>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>>
>>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>>
>>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm