Message Boards Message Boards

1
|
8617 Views
|
2 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Help understanding basic Mathematica function styles

I came across a code with a different format that I am accustomed to used for Function and its format. The original post is here and also in here

SphericalBesselI[0, 0] := SphericalBesselI[0, 0] = 1;
SphericalBesselI[l_, z_] := Sqrt[\[Pi]/(2 z)] BesselI[l + 1/2, z];
(*expansion coefficients for the Henyey-Greenstein phase function*)
\
\[Sigma][l_][g_] := g^l
(*diagonal matrix elements*)

h[l_][g_, \[Omega]_] := 
  h[l][g, \[Omega]] = (2 l + 1) (1 - \[Omega] \[Sigma][l][g]);

FIRST LINE--- What is the purpose to use the build in function as the function name and then equal to one?This is confusing as I thought the function shall start with small cap and the equal to the right if further confusing.

FIFTH LINE: What is the meaning of having 2 square brackets in the function definition?\[Sigma][l_][g_] := g^l

POSTED BY: Jose Calderon
2 Replies

SphericalBesselI[0, 0] := SphericalBesselI[0, 0] = 1; is probably a mistake. It has no purpose. But you may want to look up memoization.

f[x_][y_] := ... has no special meaning compared to f[x_, y_] := .... It simply allows for more convenient notation in some cases. You can e.g. write g /@ f[5] /@ list instead of g /@ (f[5, #]&) /@ list. The latter is cumbersome and you must remember the parentheses.

Since version 10, many builtins have such operator forms, see Map, Select, etc. and their typical use in Query/Dataset.

POSTED BY: Szabolcs Horvát

To add to @Szabolcs Horvát nice explanation, representation of f[x_, y_] as f[x_][y_] is called currying. There is example in docs that mentions it: Use Rules Iteratively.

POSTED BY: Vitaliy Kaurov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract