13

Is there a way to loop through all the Functions (Elementary and Special functions) that exist in Mathematica?

I want to construct a table of some identities and maybe I can discover something surprising if I plug every function that there is into my formula.

I.e. I want to do something like:

for function in Mathematica
print function[x]^2

(Note this is not Mathematica syntax, but I hope you get the idea)

Thanks, Michał

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574

2 Answers2

12

Single-argument functions:

funclist = Select[("ArgumentsPattern" /. SyntaxInformation[#]) === {_} &][
  FromEntity /@ MathematicalFunctionData[]]; 

Grid[Prepend[Thread[{funclist, Through[funclist@2]}], 
     Item[#, Background -> LightBlue] & /@ {"f", "f @ 2"}], 
 Dividers -> All]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
8

I am sure there is a way to do this using MathematicalFunctionData, just to obtain list of functions. But I could not find it. I got lost in help pages of Entities , FromEntity[], ToEntity[], and so on.

Here is lazy person's way of doing it.

isFunction[name_String] := Module[{m},
   m = SyntaxInformation[Symbol[name]];
   If[Length[m] > 0, True, False]
   ];

n = 0;
pkg = "System`";
names = Names[pkg <> "*"];

tbl = First@Last@Reap@Do[

      Print["Proccesing entry ", k];

       If[isFunction[names[[k]]],
          n++;
          Sow[{n, pkg <> names[[k]]}]
       ]

      , {k, 1, Length[names]}
      ];

Which gives

{{1, "System`AASTriangle"}, 
 {2, "System`AbelianGroup"}, 
 {3, "System`Abort"}, 
 {4, "System`AbortKernels"}, 
 {5, "System`AbortProtect"}, 
 {6, "System`AbortScheduledTask"}, {7, 
 .....
 {2300, "System`ListLogLinearPlot"}, 
 {2301, "System`ListLogLogPlot"}, 
 {2302, "System`ListLogPlot"}, 
 {2303, "System`ListPicker"}, 
 {2304, "System`ListPickerBox"}, 
 {2305, "System`ListPlay"}, 
 {2306, "System`ListPlot"}, 
 {2307, "System`ListPlot3D"}, 
 {2308, "System`ListPointPlot3D"}, 
 {2309, "System`ListPolarPlot"}, 
 {2310, "System`ListQ"}, 
 {2311, "System`ListSliceContourPlot3D"}, 
 {2312, "System`ListSliceDensityPlot3D"}, 
 {2313, "System`ListSliceVectorPlot3D"}, 
 {2314, "System`ListStepPlot"}, 
 {2315, "System`ListStreamDensityPlot"}, 
 {2316, "System`ListStreamPlot"}, 
 .....
 {4431, "System`$DefaultFrontEnd"}, 
 {4432,  "System`$DisplayFunction"}, 
 {4433, "System`$FormatType"}, 
 {4434,  "System`$FrontEndSession"}, 
 {4435, "System`$SoundDisplayFunction"}}

If when running the above code, you get pop-up windows asking you to login to Wolfram Cloud, just close it. I got such screen many times. I have no idea why it is asking me to login to Wolfram cloud.

Do not worry that they are strings in this list. You could always convert string to Mathematica expression using ToExpression to use the functions.

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • This is great, thank you! – Michał Kuczynski May 10 '20 at 06:03
  • 5
    @MichałKuczynski you are welcome. But you should wait to accept, sometime better answer for your needs might come along. I think you might want to accept Kglr answer instead of mine as it gives you single argument functions which is what you wanted for your tests and it is better answer than mine also. Mine was a hack/workaround since I did not know how to use MathematicalFunctionData – Nasser May 10 '20 at 06:21