# Construct a function whose definition depends on the values of its arguments

I have to evaluate the components of a 6x6 matrix $S$. This matrix depends upon the semi-axes of an ellipsoid $a1$, $a2$ and $a3$.

For various specific cases (sphere, prolate spheroid, oblate spheroid, cylinder) I have the close-form solution for $S$.

However, in the general ellipsodal case S is calculated numerically.

Let's say that in:

• Subsection_1: I give S for the case of sphere
• Subsection_2: I give S for the case of prolate spheroid
• Subsection_3: I give S for the case of oblate spheroid
• Subsection_4: I give S for the case of cylinder
• Subsection_5: I give for the case of a general ellipsoid

What I want is for the correct form of the function S[a1, a2, a3] to be called depending to the values of $a1$, $a2$, $a3$.

If, for instance, $a1=1$, $a2=1$, $a3=1000$, then Subsection_4 (and only this) will be executed and for the rest of the Section S will have the corresponding value for the cylinder case.

How can I accomplish this?

• Work out the conditions that should determine the choice of subsection. The choice of method to accomplish your goal will depend on the exact form of your conditions. In general, you could consider making multiple definitions of the same function S[a1, a2, a3] using pattern conditions. When you call S, the arguments will be compared to the patterns you imposed, and only the functional form that matches the pattern will be used. May 28 '15 at 16:48
• Also, what does $S$ represent? May 28 '15 at 16:54
• Thanks for the reply and edits. May 28 '15 at 16:58
• S represents the components of Eshelby tensor (related to a ellipsoidal inclusion) May 28 '15 at 16:59

On second thought, you could also use a Which statement to direct your function's flow. Consider the following sample function:

Clear[S]

S[a1_, a2_, a3_] := Module[
{sortedargs},

sortedargs = SortBy[-# &][{a1, a2, a3}];
Print[sortedargs];

Which[
(* sphere *)
a1 == a2 == a3,
Print["It's a sphere\n"];
Print["Its volume is ", Volume@Ball[{0, 0, 0}, a1]];
Return["from sphere"],

(* oblate spheroid *)
sortedargs[] == sortedargs[],
Print["It's a prolate spheroid"];
Print["Its major semi-axis is ", sortedargs[] ];
Return["from prolate spheroid"],

(* prolate spheroid *)
sortedargs[] == sortedargs[],
Print["It's an oblate spheroid"];
Print["Its minor semi-axis is ", sortedargs[] ];
Return["from oblate spheroid"],

(* generic ellipsoid spheroid *)
True,
Print["It's a generic ellipsoid"];
Print["Its semi-axes are ", sortedargs ];
Return["from generic ellipsoid"]
]
]


Its behavior depends on the magnitudes of the three arguments:

S[100, 100, 100] S[10, 100, 10] S[10, 20, 20] S[1, 2, 3] • Personally, I'd switch from a Which[]/Switch[]/Piecewise[]/If[] to multiple definitions if the code is sufficiently complicated for each case that trying to put them all in one definition renders the function a lot less maintainable. May 28 '15 at 21:13
• @Guesswhoitis. I had started going down that road, but since the choice tree here is pretty simple, and the OP's calculations straightforward, so I went for the quick and dirty route. dimitris, let us know if this adapts to your problem, and we'll try to go from there... May 28 '15 at 21:29
• Thanks for the replies. @ MarcoB I will see if your approach adapts to my problem. Otherwise I will try to proceed as Guess who it is. If there is problem I will return here:-)! May 29 '15 at 8:03
• Btw, I didn't notice. Marco your approach does not cover the case of cylinder. Say for a1=1, a2, a3=500 I want to avoid the lengthy numerical calculation of the S-matrix components (which it is not symmetric). I had a closed-form solution for elliptic cylinder that I want to apply. How should I modify your code? May 29 '15 at 8:07
• @dimitris In your example of a cylinder above, you said that a1=1, a3=500; what values of a2 would the function receive in that case? May 29 '15 at 13:51

Thanks to the aid of MarcoB I found the solution.

The function has to evaluate the components of Eshelby Tensor according to the values of ellipsoidal inclusion (do not bother about the physical meaning).

Due to the lengthy approach, I uploaded the notebook in the following link

Eshelby