Thanks Raul, I need to study your /\ solution more when I get home.

My question wasn't really how to solve the RC task, but how to solve a family 
of related tasks using I.  .  In those cases handling
y < {. x (for x ascending, and analogously for descending) is an explicit 
requirement.  Also being able to determine which interval
(left or right) values of x are considered to fall into is an explicit 
requirement.  

Is I. inherently biased towards either the left or the right (for y which are 
either members of x or out-of-bounds), or is that just
the matter of selecting the appropriate grade for x (or, less desirably, 
appropriate reversals for arguments)?

-Dan

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Raul Miller
Sent: Tuesday, March 16, 2010 1:44 PM
To: Programming forum
Subject: Re: [Jprogramming] How to use dyad I.

On Tue, Mar 16, 2010 at 10:53 AM, Dan Bron <[email protected]> wrote:
> When using dyad I, how does one control how to handle the various edge 
> conditions gracefully (specifically out-of-bounds values
and
> values exactly between intervals)?
>
>        (1) y is greater or lesser than the head or tail of x

y I. x (with x in ascending order) gives the index of
the largest value in x which is not smaller than y.

So, properly speaking, y < {. x is not an edge condition
since the smallest value in x is not smaller than y
and 0 is an appropriate result.

when y > {: x we get an index which is the length
of x.  For this case, we can add an element to the
list we are selecting from.

> For example, how would you solve http://rosettacode.org/wiki/Price_Fraction 
> cleanly?

That problem specifies that the floating point value be in
the range 0..1.0.  It is not clear to me that any
explicit treatment of invalid values would be clean.

Anyways, for a quick solution, I would

'bound replace'=:|:(3 5 { 0 ". ]);._2]0 :0
>=  0.00  <  0.06  :=  0.10
>=  0.06  <  0.11  :=  0.18
>=  0.11  <  0.16  :=  0.26
>=  0.16  <  0.21  :=  0.32
>=  0.21  <  0.26  :=  0.38
>=  0.26  <  0.31  :=  0.44
>=  0.31  <  0.36  :=  0.50
>=  0.36  <  0.41  :=  0.54
>=  0.41  <  0.46  :=  0.58
>=  0.46  <  0.51  :=  0.62
>=  0.51  <  0.56  :=  0.66
>=  0.56  <  0.61  :=  0.70
>=  0.61  <  0.66  :=  0.74
>=  0.66  <  0.71  :=  0.78
>=  0.71  <  0.76  :=  0.82
>=  0.76  <  0.81  :=  0.86
>=  0.81  <  0.86  :=  0.90
>=  0.86  <  0.91  :=  0.94
>=  0.91  <  0.96  :=  0.98
>=  0.96  <  1.01  :=  1.00
)

rescale=: replace {~ bound I. ]

If I wanted to explicitly treat error cases,
I would add guard values at the beginning
and end of that table.

For a more compact definition, I might make use of:

   (-: /:~) 2-/\ replace
1
   (~.,:#/.~)2-/\ replace
_0.08 _0.06 _0.04 _0.02
    2     4    12     1

But if I had any expectation that the table would
receive arbitrary updates in the future, I would
not bother trying for a more compact definition.

FYI,

-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to