7 jan 2009

Base

In responding Time intervals, I was contemplating the following.
Dyad #. is defined in Base as

(x#.y) -: +/(*/\.}.x,1)*y

Here is assumed that the items of }.x give the ratio's between the units of the items of y. The result is given in the unit {:y . An example will clarify.
Suppose we have results of time periods, measured in weeks, days, hours, minutes and seconds.

   x=.0 7 24 60 60    NB. weeks days  hours  minutes  seconds
                      NB.      7    24     60       60
                      NB. notice that  }.x denotes the ratio between these time units
   [y=.5 5$6{.1
1  0  0  0  0   NB. one week
0  1  0  0  0   NB. one day
0  0  1  0  0   NB. one hour
0  0  0  1  0   NB. one minute
0  0  0  0  1   NB. one second

   ,.x #. y   NB. with #. every time is expressed in seconds, which is the unit of {:y .
604800
 86400
  3600
    60
     1

In the use of #. , it is rather silly to be obliged to provide a number, {.x , which is not used at all or to get an error otherwise.
Apart from that, if the unit {:y is not the one you want, extra calculations has to be made, as can be seen in Time intervals(1).
For that reason I redefine base.

base=: 3 : 0 "1
#.y
:
(*/x #~ +/\1 e.~ x)%(0,x-.1)#.y
)

In base the left hand argument of #. is completed with a 1 on the place of the unit to be used. So, if in the example above, if the time has to be calculated in hours, which is the third unit, the left hand side argument is 7 24 1 60 60.

   [x=:((i.@>:@#)({.,1,}. )"0 1 ]) 7 24 60 60
1  7 24 60 60   NB. result in weeks
7  1 24 60 60   NB. result in days
7 24  1 60 60   NB. result in hours
7 24 60  1 60   NB. result in minutes
7 24 60 60  1   NB. result in seconds

   x base"1 _ y
           1             7           168       10080 604800   NB. one week in weeks, days, hours, minutes, seconds
  0.14285714             1            24        1440  86400   NB. one day in weeks, days, hours, minutes, seconds
 0.005952381   0.041666667             1          60   3600   NB. one hour in weeks, days, hours, minutes, seconds
9.9206349e_5 0.00069444444   0.016666667           1     60   NB. one minute in weeks, days, hours, minutes, seconds
1.6534392e_6  1.1574074e_5 0.00027777778 0.016666667      1   NB. one second in weeks, days, hours, minutes, seconds

RE Boss/J-blog/Base (last edited 2009-01-12 09:42:24 by RE Boss)