I am newbie in Julia language, and the tutorial is not very deep yet and I didn't understand what is the best way to pass a parameter list of a function. My function looks like this:
function dxdt(x)
return a*x**2 + b*x - c
end
where x is the variable (2D array) and a,c, and d are parameters. As I understand it is not recommended to work with global variables in Julia. So what is the right way to do it?
What you want to do really is passing an instance of a data structure (composite data type) to your function. to do this, first design your data type:
and implement
dxtd
function:then some where in your code you make an instance of MyType like this:
you can update
myinstance
and at the end when
myinstance
get ready fordxxt
To me it sounds like you're looking for anonymous functions. For example:
this is a thing:
or the compact definition:
see also argument passing in functions in the docs.
The idiomatic solution would be to create a type to hold the parameters and use multiple dispatch to call the correct version of the function.
Here's what I might do
Sometimes if a type has many fields, I define a helper function
_unpack
(or whatever you want to name it) that looks like this:And then I could change the implementation of
dxdt
to beYou may use the power of functional language (function as a first-class object and closures):