Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Functional > Re: Data struct...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 12 Topic 2726 of 3038
Post > Topic >>

Re: Data structures for cplmsi bijections

by Vesa Karvonen <vesa.karvonen@[EMAIL PROTECTED] > Jan 14, 2008 at 01:58 AM

Vesa Karvonen <vesa.karvonen@[EMAIL PROTECTED]
> wrote:
> David B. Benson <dbenson@[EMAIL PROTECTED]
> wrote:
> > Consider a continuous, piecewise linear, monotonically
> > strictly increasing (cplmsi) function on a real closed interval.
> > Clearly this determines a bijection.

> > The task is to determine a data structure for the cplmsi
> > function so that both functiion values and inverse function
> > values can be 'quickly' determined, using only functional
> > language constructs.  No imperative features allowed.

> Assuming that the function is constructed once and evaluated many many
> times, I'd probably just use an ordered (immutable) vector of {x, y}
> coordinates and either binary search, interpolation search [1], or a
> functional adaptation of the "hunt" algorithm (intended for repeated
> evaluation) described in [2].  The same ordered vector works for both
the
> forward and inverse functions.

Below is a simple version in SML using just a basic binary search.  It
should be given a sorted vector of at least 2 points.  This mainly just
demonstrates that a single vector works for both the forward and inverse
functions.

-Vesa Karvonen

fun cplmsi points = let
   val lo = 0
   val hi = Vector.length points - 1

   fun sub i = Vector.sub (points, i)

   fun eval getX getY x = let
      fun binary lo hi =
          if hi - lo < 2
          then lo
          else case lo + Int.quot (hi - lo, 2) (* Avoid overflow! *)
                of mid => if x < getX (sub mid)
                          then binary lo mid
                          else binary mid hi

      val i = if getX (sub lo) <= x andalso x <= getX (sub hi)
              then binary lo hi
              else raise Domain (* x is NaN or not in the domain! *)

      val p0 = sub i
      val p1 = sub (i+1)

      val dx = getX p1 - getX p0
      val dy = getY p1 - getY p0
   in
      getY p0 + dy / dx * (x - getX p0)
   end
in
   if Vector.length points < 2 then raise Fail "less than 2 points" else
()
 ; ignore (Vector.foldl
           (fn (n, p) =>
               if #x n <= #x p orelse #y n <= #y p
               then raise Fail "not strictly increasing"
               else n)
           {x = Real.negInf, y = Real.negInf}
           points)
 ; {forward = eval #x #y, inverse = eval #y #x}
end
 




 12 Posts in Topic:
Data structures for cplmsi bijections
"David B. Benson&quo  2008-01-12 16:55:25 
Re: Data structures for cplmsi bijections
Barb Knox <see@[EMAIL   2008-01-13 14:48:48 
Re: Data structures for cplmsi bijections
"David B. Benson&quo  2008-01-13 10:44:07 
Re: Data structures for cplmsi bijections
jmv16@[EMAIL PROTECTED]   2008-01-13 20:58:33 
Re: Data structures for cplmsi bijections
Joachim Durchholz <jo@  2008-01-14 02:15:04 
Re: Data structures for cplmsi bijections
Jon Harrop <usenet@[EM  2008-01-13 21:37:53 
Re: Data structures for cplmsi bijections
Neelakantan Krishnaswami   2008-01-13 22:06:12 
Re: Data structures for cplmsi bijections
"David B. Benson&quo  2008-01-13 15:35:28 
Re: Data structures for cplmsi bijections
Vesa Karvonen <vesa.ka  2008-01-13 23:57:38 
Re: Data structures for cplmsi bijections
Vesa Karvonen <vesa.ka  2008-01-14 01:58:13 
Re: Data structures for cplmsi bijections
"David B. Benson&quo  2008-01-13 16:20:17 
Re: Data structures for cplmsi bijections
"David B. Benson&quo  2008-01-14 12:26:03 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 11:12:43 CDT 2008.