I have a function called f for example.
function f(x1,x2,x3)
x1+x2*x3
end
How can get the number of variable of this function? For this example it should return 3.
I have a function called f for example.
function f(x1,x2,x3)
x1+x2*x3
end
How can get the number of variable of this function? For this example it should return 3.
You can do something like this:
julia> function f(args...)
return length(args)
end
f (generic function with 1 method)
julia> f(1,2,3)
3
What are you trying to accomplish? The analogue of nargin
in Matlab in Julia is to define multiple methods (or optional/keyword arguments).
No. How to use the āaccomplishā? I tried
accomplish(f)
But it seems not works. By the way, the ānarginā in Matlab does works.
Heās asking what you are trying to do. There is no accomplish
function.
In Matlab nargin
is an important function that you need to use all the time to figure out what inputs you have. In Julia you donāt need this at all.
Edit: I meant to reply to @gangchern
Matlab:
function ret = f(x1,x2)
if nargin == 1
ret = x1;
elseif nargin == 2
ret = x1+x2;
end
end
and then:
>> f(2)
ans =
2
>> f(2,2)
ans =
4
In Julia:
julia> f(x1) = x1
f (generic function with 1 method)
julia> f(x1,x2) = x1+x2
f (generic function with 2 methods)
julia> f(2)
2
julia> f(2,2)
4
Thereās also optioinal argument and keyword argument combining with dispatch usally cover most of the cases I need to deal with variadic functions.
And if all else failed, you can still declare the function as variadic which @mbaz mentioned in the very first reply alreadyā¦
And if you want to figure out how many argument a function can accept from outside the function, then no thatās not a property of a function and you generally should not rely on that.
Unless itās for debugging, in which case you can just iterate through the methods
.
I donāt think you can do that in Matlab either.
Thanks,
This code is useful for me. Perhaps Julia do not have inherit method to get the number of variables of a function. As other people replied, it is not necessary for Julia.
To add to the excellent answers above: strictly speaking functions (eg f
) donāt have an argument list, methods (eg f(::Any, ::Any, ::Any)
do. The distinction between the two is crucial to using Julia efficiently.
Also, unless you are debugging or doing something very advanced, trying to recover this meta-information is usually a sign that the code is not well-designed for Julia.
Thank
Hi @gangchern!
I also had this question, and @Tamas_Pappās answer put two and two together for me, haha.
Thanks for asking it.
Also, you should mark his answer as the solution so future enquirers will find it easier.
Just for completeness, there is a way to access the number of arguments of a particular Method
:
f = (x,y) -> x + y
first(methods(f)).nargs - 1
This is used nicely in the LinearMaps.jl
package that certainly qualifies as āsomething very advancedā as @Tamas_Papp saidā¦
I donāt understand why the -1
is needed but it worksā¦
But this is probably not what most Matlab users want when they search for nargin
Complementing even more, I would directly translate the given Matlab example
to
julia> function f(x1, x2 = nothing)
if x2 === nothing
return x1
else
return x1 + x2
end
end
f (generic function with 2 methods)
julia> f(2)
2
julia> f(2, 2)
4
This might be a typo, but: Matlab = Matrix Laboratory.
It was a typo. Corrected.
Again, you really donāt want to do this. This looks like an XY problem, applying the Matlab solution to Julia, where there are more idiomatic approaches, eg
for a vararg like f(args...)
, you just want length(args)
,
for optional arguments, just do that, like @Henrique_Becker shows above,
for the Matlab-style switch nargin
, you want to define separate methods,
Matlabās nargin(f)
really does not make sense in Julia because of multiple dispatch,
in the remaining 0.1% cases if you are writing code introspection tools, you know what you are doing and need the internals.
Somehow one has to reach that point where āyou knowā, and searching forums like this can be helpful. The manual itself is very brief about what methods
can doā¦
I think you misunderstand the context: most users never need to learn about internals and introspection of the methods table, so they donāt have to reach that point. In fact, using methods
in regular code is an anti-pattern; itās for interactive exploration.
If one really, really needs to know these details, they are mostly in the devdocs. Putting them into the manual per se would be misleading.