Skip to main content

Function that counts the number of arguments of other functions


I have a newbie question: is it possible to write a function that counts the arguments (total and optionals) of a given function? Possibly it should be able to work with built-in and custom functions as well.


For example, if I define


f1[x_Integer] := x + 1;

f2[x_Integer, y_Integer: 1] := x + y;
g[x_Real, y_] := x - y;

I would like to have


countArgs[f1]
{1,0}
countArgs[f2]
{2,1}
countArgs[g]
{2,0}


and also, for example,


countArgs[Sin]
{1,0}

thank you.


@celtschk


Well, I didn't even know the use of UpValues, but basically what I am asking is the number of inputs the function needs, I don't care what the function does with those inputs. In your examples I would say



  • {Infinity,0} for foo? It's more a question than an answer, sorry, but I had not thought aboute these unusual cases.


  • this is nasty, I didn't think of a case like that either, I would say {3,{2}}, the {2} meaning exactly 2, to avoid bar[1,2], which is not legal.

  • {2,0} but just because you wrote f[g,g], so it's practically a guess, I don't know what are UpValues and if they go against the spirit of my question by messing with the function. I hope I was clear.

  • the last one I would say {3,0} as they were flattened.


Thank you, I start seeing that my question is not so obvious because there are too many complicated definitions for functions to take into account.


For now I understood that is possible with built-in functions with


SyntaxInformation[f]

(thank you Heike) but that mybe is a little too much asking for a general custom function.



Answer




Here is my attempt. The function below will work on functions with default args and options, as well as those having multiple definitions. I made the following assumptions:



  • Only DownValues - based definitions are considered

  • Default arguments, if present, are always to the right of mandatory arguments.

  • Options, if present, are always to the right of all other arguments, and are declared either by OptionsPattern[] or opts:OptionsPattern[] pattern.


Here is the code:


ClearAll[countArgs];
SetAttributes[countArgs, {HoldAll, Listable}];
countArgs[f_Symbol] :=

With[{dv = DownValues[f]}, countArgs[dv]];

countArgs[Verbatim[HoldPattern][HoldPattern[f_Symbol[args___]]] :> _] :=
countArgs[f[args]];

countArgs[
f_[Except[_Optional | _OptionsPattern |
Verbatim[Pattern][_, _OptionsPattern]], rest___]] :=
{1, 0, 0} + countArgs[f[rest]];


countArgs[ f_[o__Optional, rest___]] :=
{0, Length[HoldComplete[o]], 0} + countArgs[f[rest]];

countArgs[f_[_OptionsPattern | Verbatim[Pattern][_, _OptionsPattern]]] :=
{0, 0, 1};

countArgs[f_[]] := {0, 0, 0};

This function represents a mini-parser for the function's declarations. It returns a list of 3-element sublists, of the length equal to a number of DownValues. In each sublist, the first number is a number of normal arguments, the second one is a number of default arguments, and the last one (which can only be 0 or 1), tells us whether or not there are options declared.


Some examples:



ClearAll[f1, f2, f3, f4, g]
f1[x_Integer] := x + 1;
f2[x_Integer, y_Integer: 1] := x + y;
f3[x_, y_, z_: 1, q_: 2, opts : OptionsPattern[]] := x + y + z + q;
f4[x_, y_: 1] := x + y;
f4[x_, y_, z_] := x + y + z;
g[x_Real, y_] := x - y;

Now applying our function:


countArgs /@ {f1, f2, f3, f4, g}



{{{1, 0, 0}}, {{1, 1, 0}}, {{2, 2, 1}}, {{1, 1, 0}, {3, 0, 0}},{{2, 0, 0}}}

Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Adding a thick curve to a regionplot

Suppose we have the following simple RegionPlot: f[x_] := 1 - x^2 g[x_] := 1 - 0.5 x^2 RegionPlot[{y < f[x], f[x] < y < g[x], y > g[x]}, {x, 0, 2}, {y, 0, 2}] Now I'm trying to change the curve defined by $y=g[x]$ into a thick black curve, while leaving all other boundaries in the plot unchanged. I've tried adding the region $y=g[x]$ and playing with the plotstyle, which didn't work, and I've tried BoundaryStyle, which changed all the boundaries in the plot. Now I'm kinda out of ideas... Any help would be appreciated! Answer With f[x_] := 1 - x^2 g[x_] := 1 - 0.5 x^2 You can use Epilog to add the thick line: RegionPlot[{y < f[x], f[x] < y < g[x], y > g[x]}, {x, 0, 2}, {y, 0, 2}, PlotPoints -> 50, Epilog -> (Plot[g[x], {x, 0, 2}, PlotStyle -> {Black, Thick}][[1]]), PlotStyle -> {Directive[Yellow, Opacity[0.4]], Directive[Pink, Opacity[0.4]],