       Finite Functions

• To: mathgroup at yoda.physics.unc.edu
• Subject: Finite Functions
• From: roger at isy.liu.se (Roger Germundsson)
• Date: Thu, 26 Sep 91 10:38:28 +0200

```Does anyone out there know of a good way of handling
finite functions in Mathematica?

One obvious way would be to use:

f[x1] = y1; f[x2] = y2; ...

Then you would have make f a global variable (!!!)
This approach is implemented in BuildFunction[] below.
Another obvious choice would be to use lists as follows

{{x1, y1}, {x2, y2}, ... }

and then search this list. This is implemented in
Lookup[] below. However this will run at least one
order of magnitude slower than the first approach.

What I think is really needed is something like Maples
table construct which build a list representation, but
packs a hashing function along with that (This is probably
the way Mathematica has implemented the approach
f[x1] = y1, ... ). This would enable you to send such
objects as arguments to functions and also receive those.

See below for implementation and experimental data:

// Roger Germundsson (roger at isy.liu.se)

(*
These functions assume that fun looks like:
{ { x1, f[x1] }, ... ,{ xn, f[xn] } }
*)

Lookup[ fun_List, key_ ] :=
Block[{pos},
(* Assume only one match .. since function *)
pos = Flatten[ Position[ fun, {key,_}] ];
Last[ Last[ fun[[ pos ]] ] ]
]

BuildFunction[ fun_List, fname_ ] :=
Block[{i, f},
f = fun;
Do[	(
fname[ f[[1,1]] ] = f[[1,2]];
f = Rest[f];
),
{i,Length[fun]}
];
fname
]

(* Experimental setup *)

flist = Table[ {i,Random[Integer,{0,10}]}, {i,1,100}];

TFun[k_] :=
Timing[
(
BuildFunction[flist,F];
Do[ F[ Random[ Integer, {1,100}] ], {k} ];
)
][]/Second

TLook[k_] :=
Timing[
Do[ Lookup[flist, Random[Integer,{1,100}] ], {k} ];
][]/Second

(* Experimental data *)

(*

In:= <<FiniteFunction.m

In:= tfun = Table[ TFun[l], {l,1000,10000,1000} ];

In:= tlook = Table[ TLook[l], {l,1000,10000,1000} ];

In:= pfun = ListPlot[ tfun, DisplayFunction -> Identity ];

In:= plook = ListPlot[ tlook, DisplayFunction -> Identity ];

In:= <<Terminal.m
-- Terminal graphics initialized --

In:= Show[ {pfun, plook}, DisplayFunction :> \$DisplayFunction ]

#                                                                      #
#
70##
#                                                               #
#
##                                                        #
60 #
#                                                 #
50##
#
#                                          #
##
40 #                                   #
#
##                           #
30 #
#
##                    #
20 #
#             #
#
##      #
10 #                                                               #      #
#             #      #      #       #      #      #      #             #
############################################################################
2             4              6             8            10

Out= -Graphics-

In:= tfun

Out= {0.666667, 1.06667, 1.45, 1.98333, 2.43333, 2.81667, 3.2,

>    3.76667, 4.06667, 4.43333}

In:= tlook

Out= {7.7, 15.1833, 22.2833, 29.7833, 37.15, 44.55, 52.95,

>    60.4667, 67.4833, 74.9167}

*)

```

• Prev by Date: Funny Limits
• Next by Date: Re: Funny Limits
• Previous by thread: Re: Funny Limits
• Next by thread: Student MS-DOS version of Mathematica