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 7 of 12 Topic 2726 of 3037
Post > Topic >>

Re: Data structures for cplmsi bijections

by Neelakantan Krishnaswami <neelk@[EMAIL PROTECTED] > Jan 13, 2008 at 10:06 PM

In article 
<<0ca99e02-7b97-46e5-a56e-346d6b070de1@[EMAIL PROTECTED]
>>,
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.

So your cplmsi function has a start position, an initial value, and a
sequence of linear functions over some intervals. We can take that 
definition and just use it as a datatype, and a small amount of algebra
will tell you how to invert it.

As an aside, this code isn't quite as efficient as it could be, since
you could store the pieces as a binary tree of intervals and
interpolate the value. But it's good enough for Usenet. :)

type piece = {slope: float; 
              run: float}

type fn = {init: float;   (* Initial y-value *)
           start: float;  (* Starting x-value *)
           pieces: piece list}

let invert_piece p = {slope = 1.0 /. p.slope; 
                      run = p.run *. p.slope}

let invert fn = {init = fn.start; 
                 start = fn.init;
		 pieces = List.map invert_piece fn.pieces}

let eval fn x =
  let rec loop pieces xleft total =
    match pieces with
    | [] -> failwith "Argument after end of range"
    | p :: ps when xleft <= p.run -> total +. (p.slope *. xleft)
    | p :: ps -> loop ps (xleft -. p.run) (total +. (p.slope *. p.run))
  in
  if x >= fn.start then 
    loop fn.pieces (x -. fn.start) fn.init
  else
    failwith "Argument before start range"
	

-- 
Neel R. Krishnaswami
neelk@[EMAIL PROTECTED]

 




 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 Fri Oct 10 13:52:29 CDT 2008.