# Mathematica discrete functions

• Apr 29th 2012, 08:33 PM
james121515
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
• Apr 29th 2012, 09:59 PM
MathoMan
Re: Mathematica discrete functions
You are talking about piecewise defined functions - so use
Code:

Piecewise[definition,condition]
.
• Apr 30th 2012, 07:18 AM
BillSimpson
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