Mathematica discrete functions

Hello,

What I want to be able to do with Mathematica is define a discrete function with a discrete domain and range that I create and designate. For example:

$\displaystyle A = \{a, b, c, d\}$

$\displaystyle B = \{1, 2, 9, \pi\}$

$\displaystyle f: A \rightarrow B$

$\displaystyle f(x) = \begin{cases} 1 &\text{ if }x = a\\2 &\text{ if }x = c\\ \pi &\text{ if }x = b\\ 9 &\text{ if }x = d \end{cases} $

So that when I type f[a] in Mathematica, it spits out 1, f[b] spits out $\displaystyle \pi$, etc.

I have plowed through the DC trying to find out how to do something like this, and although I discovered a lot of neat things you can do involving functions with Mathematica, I could not find how to do something as simple as this.

Thanks for any help!

James

Re: Mathematica discrete functions

You are talking about piecewise defined functions - so use Code:

`Piecewise[definition,condition]`

.

Re: Mathematica discrete functions

In[1]:= f[x_]:=Which[x===a,1,x===b,Pi,x===c,2,x===d,9];f[d]

Out[2]= 9

In[3]:= A={a,b,c,d};B={1,Pi,2,9};f[x_]:=x/.Thread[Rule[A,B]];f[d]

Out[5]= 9