Question

I have been simultaneously reading "Concrete Mathematics" by Graham, Knuth and Patashnik and "Concrete Math Companion" by Iverson. The second chapter is about summations, and Iverson introduces the following code to demonstrate the "repertoire method" from GKP.

CPA=: (@Ei) %. ^/~@Ei

where Ei is defined earlier

Ei=: i.@>:

So the full expression is

(@i.@>:) %. ^/~@Ei

Here is a quote to give better context:

In a manner analogous to the repertoire method of GKP, we will find a non-recursive equivalent to a recursively defined function by finding a polynomial fit to a few of its values. To this end we will use the adverb:

CPA=: (@Ei) %. ^/~@Ei

so defined that "f CPA n" yields the coefficients of a polynomial approximation of order n to the function f.

Here is the syntax error statement:

|syntax error
|   CPA=:(    @i.@>:)%.^/~@(i.@>:)

I only recently started playing with J, so thus far I have just been using Iverson's notes as a way to solidify the concepts from GKP. Thanks S.O.

Was it helpful?

Solution

Since J501, trains of adverb-verb-verb are no longer permitted. Hence, the only way to write CPA is, as bob noted, explicitly:

CPA =: 1 :'u@Ei %. ^/~@Ei'

In the same book, a bit above, Iverson introduces another adverb S =: (+/@:)(@Ei)"0 which works fine.

Let's define each part of CPA and S:

cpa1 =: @Ei
cpa2 =: %.
cpa3 =: ^/~@Ei
s1 =: +/@:
s2 =: @Ei

so

list {~ class ;:'cpa1 cpa2 cpa3'
┌──────┬────┬────┐
│adverb│verb│verb│
└──────┴────┴────┘
list {~ class ;:'s1 s2'
┌──────┬──────┐
│adverb│adverb│
└──────┴──────┘

we see that CPA is a train of adverb-verb-verb, which is not permitted in J501 and above, but S is an adverb-adverb hook, which is permitted in J501 and above.

defs:

list =: ;:'noun adverb conjuction verb'

class =: 4!:0

OTHER TIPS

You could try defining it explicitly

CPA=: 1 : '(u@Ei) %. ^/~@Ei'

At least that doesn't return a syntax error in J602. Whether it does what you expect will be up to you to determine as I do not have either text at my disposal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top