# Writing a function with a domain that is a finite range of integers [closed]

I want to write a conditional function defined for $$n=[-5:5]$$. Explicitly,

$$\qquad Z_n=A e^{ikn}+Be^{-ikn}$$ for $$n\leq1$$ and $$Z_n=T e^{ikn}$$ for $$n\geq2$$.

I have written it but it is not very neat and hard to use elsewhere. Can it be written in a better way so could be easy to use in another code? I wrote it as

n = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
nn = {-5, -4, -3, -2, -1, 0, 1};
mm = {2, 3, 4, 5};
a = A*E^(I*k*nn) + B*E^(-I*k*nn) %for n<=1
b = T*E^(I*k*mm)                 %for n>=2

• Have a look at Piecewise. Commented Feb 22, 2019 at 11:15
• Have a look too at Mathematica's basic documentation on how to write functions. A lot of folk here will look at your 'code' and think that's not how to write Mathematica functions, in fact it's so far from being the way I balk at explaining how wrong it is. Commented Feb 22, 2019 at 11:30
• @b.gatessucks Thanks. Something like this? n = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}; pw = Piecewise[{{A*E^(I*k*n) + B*E^(-I*k*n), n <= 1}, {T*E^(I*k*n), n >= 2}}]
– AtoZ
Commented Feb 22, 2019 at 11:43
• As suggested by @HighPerformanceMark, please read DefiningFunctions). Commented Feb 22, 2019 at 11:52
• How about this? pw /@ Range[-5, 5] Commented Feb 22, 2019 at 13:41

If k, aa, bb, and tt (best not to use capital letters to avoid collisions with Mathematica-defined terms) are currently just unassigned symbols, or if they are permanent constants, you can memoize them.

Table[zz[n] = aa E^(I k n) + bb E^(-I k n), {n, -5, 1}];
Table[zz[n] = tt E^(I k n), {n, 2, 5}];


Printing them out...

Table[{zz[n]}, {n, -5, 5}] // MatrixForm


$$\left( \begin{array}{c} e^{-5 i k} \text{aa}+\text{bb} e^{5 i k} \\ e^{-4 i k} \text{aa}+\text{bb} e^{4 i k} \\ e^{-3 i k} \text{aa}+\text{bb} e^{3 i k} \\ e^{-2 i k} \text{aa}+\text{bb} e^{2 i k} \\ e^{-i k} \text{aa}+\text{bb} e^{i k} \\ \text{aa}+\text{bb} \\ e^{i k} \text{aa}+\text{bb} e^{-i k} \\ e^{2 i k} \text{tt} \\ e^{3 i k} \text{tt} \\ e^{4 i k} \text{tt} \\ e^{5 i k} \text{tt} \\ \end{array} \right)$$

• Y Thank you very much!..
– AtoZ
Commented Feb 23, 2019 at 8:34

If you just want the function to return unevaluated for values of n that do not meet the conditions:

ClearAll[Z];

Z[n_Integer?(-5 <= # <= 1 &)] :=
A*Exp[I*k*n] + B*Exp[-I*k*n];
Z[n_Integer?(2 <= # <= 5 &)] := T*Exp[I*k*n];


For example,

Z /@ {-6, -3., -3, 3, 3., 6}

(* {Z[-6], Z[-3.], A E^(-3 I k) + B E^(3 I k), E^(3 I k) T, Z[3.], Z[6]} *)


If you want the function to return unevaluated and an error message for values of n that do not meet the conditions:

ClearAll[Z]

Z::argni =
"The argument 1 is not an integer in the closed interval {-5, 5}.";

Z[n_] /; If[TrueQ[Element[n, Integers] && -5 <= n <= 5],
True, Message[Z::argni, n]; False] :=
Piecewise[{{A*Exp[I*k*n] + B*Exp[-I*k*n], -5 <= n <= 1}},
T*Exp[I*k*n]];


For example,

Z /@ {-6, -3., -3, 3, 3., 6}


(* {Z[-6], Z[-3.], A E^(-3 I k) + B E^(3 I k), E^(3 I k) T, Z[3.], Z[6]} *)


If you want the function to evaluate to zero for values of n that do not meet the conditions:

ClearAll[Z];

Z[n_] := Piecewise[{
{A*Exp[I*k*n] + B*Exp[-I*k*n], IntegerQ[n] && -5 <= n <= 1},
{T*Exp[I*k*n],
IntegerQ[n] && 2 <= n <= 5}}];


For example,

Z /@ {-6, -3., -3, 3, 3., 6}

(* {0, 0, A E^(-3 I k) + B E^(3 I k), E^(3 I k) T, 0, 0} *)

• Thank you !.....
– AtoZ
Commented Feb 23, 2019 at 8:33

Assuming that A, B and T are given constants and the k is an integer parameter, you could write

With[{A = 1, B = 2, T = 3},
z[k_Integer][n_Integer /; MemberQ[Range[-5, 5], n]] :=
Piecewise[{{A E^(I k n) + B E^(-I k n), n <= 1}, {T E^(I k n), n >= 2}}]]


I have given arbitrary values to A, B and T because you didn't define them; you should substitute your actual value for these parameters.

Now let's test z over your domain for a few values of k.

Table[z[k] /@ Range[-5, 5], {k, 3}]

{{E^(-5 I) + 2 E^(5 I), E^(-4 I) + 2 E^(4 I), E^(-3 I) + 2 E^(3 I), E^(-2 I) + 2 E^(2 I),
E^-I + 2 E^I, 3, 2 E^-I + E^I, 3 E^(2 I), 3 E^(3 I), 3 E^(4 I), 3 E^(5 I)},
{E^(-10 I) + 2 E^(10 I), E^(-8 I) + 2 E^(8 I), E^(-6 I) + 2 E^(6 I), E^(-4 I) + 2 E^(4 I),
E^(-2 I) + 2 E^(2 I), 3, 2 E^(-2 I) + E^(2 I), 3 E^(4 I), 3 E^(6 I), 3 E^(8 I), 3 E^(10 I)},
{E^(-15 I) + 2 E^(15 I), E^(-12 I) + 2 E^(12 I), E^(-9 I) + 2 E^(9 I), E^(-6 I) + 2 E^(6 I),
E^(-3 I) + 2 E^(3 I), 3, 2 E^(-3 I) + E^(3 I), 3 E^(6 I), 3 E^(9 I), 3 E^(12 I), 3 E^(15 I)}}


But z won't accept integers outside the interval {-5, 5}>

z[2][11]


z[2][11]

### Update

This is my 2nd thought on implementing z. It should have been my first thought given that the integer range comprising the domain is a simple sequence and not a disjoint collection of integers.

With[{A = 1, B = 2, T = 3},
z[k_Integer][n_Integer /; -5 <= n <= 5] :=
Piecewise[{{A E^(I k n) + B E^(-I k n), n <= 1}, {T E^(I k n), n >= 2}}]]


This is more concise, more succinct and more efficient than 1st thought on the matter.

• Many Thanks! ......
– AtoZ
Commented Feb 23, 2019 at 8:34