June, sorry this is late in reply but I am catching up on old emails. The
problem you describe below with heating oil price/scales is analogous to the
tax rate calculation based on TaxBands and TaxRates.
This has been covered in APL teaching materials (eg A Working Introduction
to APL written by Ken Iverson in the early 1980's), and is also described
very clearly in Henry Rich's book "J for C Programmers" (Chapter 9,
"Continuing to Write in J" - see the verb <renderuntocaesar>).
I needed this function recently to utilise new tax rates in Australia and
derived the following directly from Henry's book:
NB. ================== Tax Calculation Script - Start ===============
ArrayMaker =: ". ;. _2
tax2006 =: ArrayMaker 0 : 0
0 0
6000 0.15
25000 0.30
75000 0.40
150000 0.45
)
tax =: 4 : 0
NB. x is taxYYYY containing tax scales as above (eg Tax2005, Tax2006)
NB. y is list of salaries for which tax is to be calculated
TaxBands =. 0{"1 x
TaxRates =. 1{"1 x
parts =. 0 >. (y <."0 1 }.TaxBands,_)-"1 TaxBands
tax =. parts +/ . * TaxRates
)
NB. ================== Tax Calculation Script - End =================
Hence:
s ,. tax2006 tax s=: 80000 100000 150000
80000 19850
100000 27850
150000 47850
For your example, we create and use oil rates instead of tax rates:
[ OilRates =: 3 2$ 0 0.90 20 0.80 40 0.75
0 0.9
20 0.8
40 0.75
OilRates tax 35 NB. To reproduce your problem ...
30
Finally, to answer your question on outer product and rank: As the above
example shows, yes the rank operator encapsulates outer product function:
+"0 1~ i.5
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
+/~i.5
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
In the same way, the definition of the <tax> verb uses the rank operator for
both the floor (<.) and - operations to provide the outer product and
reshape that Roy Sykes derived in his paper. I think this tax example
demonstrates the power and versatility of the rank concept quite nicely.
Your last question: Do APL's have a rank operator also ? Not all.
'Function Rank' was proposed by Ken Iverson while at I.P.Sharp Associates,
(cf Rationalised APL I think) but then Arthur Whitney (of K/Kdb) proposed
the generalised 'Rank Operator' in the early 1980's as a more general
implementation of several existing APL concepts (including the Axis
Operator). It was implemented in some APL's (mostly Sharp variants) and
Arthur implemented it in his own languages in the mid to late 1980's. It
also appeared in J through Arthur's very close association with Ken, Eugene,
Roger and Eric (thank heavens !).
.../Regards Rob
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of June Kim
Sent: Monday, 8 May 2006 2:54 AM
To: Programming forum
Subject: [Jprogramming] APL thinking
Hello,
I read Roy A. Sykes, Jr.'s Whizbang! article in APL Quote Quad vol 27,
no 2. The title is APL thinking. I found it quite instructive even
though I know close to nothing about APL syntax.
I have a few questions on it.
The problem that the article solves through is computing the total
cost of some heating oil using the following rate structure:
First 20 gallons ... 90c per gallon
Next 40 gallons ... 80c per gallon
Remainder ... 75c per gallon
For example,
c=: 20 40
p=: 0.9 0.8 0.75
q=: 35
and then the total cost is 30
I have translated the first apl solution into J : +/p* 0>.(c,q)<. q - 0,+/\c
The article continues to expand q as a vector.
Firstly, it replaces +/p* with "inner product". I suppose that is, p (+/
.*)~
And then it replaces (c,q) part with something more complexed. It
gives two possibilities: ((($q),$c)$c),q or >(<c), each q
In the former possibility, I just replaced the original rho function
with J's shape($), and it doesn't work. I had to figure out
((($q),$c)$c),"1 0 q
Am I doing it right? I suppose APL's concatenation quite different from J's.
Now the article continues the task and replaces "q -" part with outer
product, and I think it could be translated into J as "q -/"
I wonder now. Is it necessary to do all this stuff in J as well?
Wouldn't simply applying rank adverb to verbs work? Doesn't APL have
rank operator? Any advantages of doing this replacement(inner, outer,
and etc) to get a differnt rank verb in J?
Finally, the article looks toward making a verb and handling
arguments. In order to avoid taking several arguments boxed into one,
it makes a new concept, price schedule.
PriceSched=:0 20 40 80 ,: 0.9 0.8 0.75 0.7
And a verb named OILPRICE is dyadic with PriceSched as x arg and
Gallons as y arg.
I might instead do,
+/ p * c bucket q
Overall, the article is very nice for learning problem solving mind in
APL(or J).
June
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm