Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Equivalent of *args from Python in Julia?

Tags:

julia

Is there an equivalent command to *args from Python in Julia? I ask because I am trying to write a numerical integration function, the argument of which is a function that may depend on several constants. For example, if my Julia function was

function func(x,a,b)
return a*x + b
end

is there way that I could make an array

args = [a,b]

and call this function as

val = func(x,*args)?

like image 959
PF111 Avatar asked Nov 01 '25 19:11

PF111


1 Answers

This is quite easy in Julia. Putting ... after the last argument in a function definition makes it accept any number of arguments passed as a tuple:

function func(x,args...)
    result = zero(x*args[1])
    for (i,arg) in enumerate(args)
        result += arg * x^(length(args) - i) 
    end
    result
end

and you can call it either way:

args = [a, b]
val = func(x, args...)

# or
val = func(x, a, b)
like image 132
AboAmmar Avatar answered Nov 04 '25 07:11

AboAmmar