I try to employ vegas routine (by G.P. Lepage) on Fortran90 to calculate multidimensional integral from function of 7 arguments and 1 parameter.
I took a code of this subroutine vegas in famous book Numerical Recipes in Fortran90. Here is declaration of the subroutine:
SUBROUTINE vegas(region,func,init,ncall,itmx,nprn,tgral,sd,chi2a)
The second argument is described as
user-supplied d-dimensional function func over a rectangular volume specified by region, a vector of length 2d consisting of d “lower left” coordinates of the region followed by d “upper right” coordinates.
So, here is my question: how can I pass to this function func one extra argument that is a parameter in my function. Accordingly to description of the function it has exactly as many arguments as there are integration variables (d = 7 in my case) while I need to pass one more argument to this function.
Solved.
Related
Documentation:
Argument-type declarations normally have no impact on performance:
regardless of what argument types (if any) are declared, Julia
compiles a specialized version of the function for the actual argument
types passed by the caller. For example, calling fib(1) will trigger
the compilation of specialized version of fib optimized specifically
for Int arguments, which is then re-used if fib(7) or fib(15) are
called.
The provided example:
fib(n::Integer) = n ≤ 2 ? one(n) : fib(n-1) + fib(n-2)
but then in the Performance Tips:
# This will not specialize:
function f_type(t) # or t::Type
x = ones(t, 10)
return sum(map(sin, x))
end
From my understanding of the performance tips, with or without type annotations fib(b::Integer) or fib(b) shouldn't specialize specifically for Int arguments.
It seems with or without the type annotation no specialization will happen, but why does the manual seem to indicate that it will specialize?
The example in the Performance Tips page is about the specific case where you pass the type itself as the argument to the function. As the beginning of that section says:
Julia avoids automatically specializing on argument type parameters in three specific cases: Type, Function, and Vararg
f_type in that example is a function that accepts a Type t. A call to that function will look like, for eg., f_type(Int). The section says that type specialization isn't done based on the Int type in this case, where the Int is passed as a value of the parameter itself (and then passed through to another function). (Specifically, the Type isn't specialized into a specific parametrized type like Type{Int}, as it normally would be if the argument wasn't one of the above mentioned three cases.)
This doesn't apply to the fib method (or most methods we normally define). fib accepts a value whose type is (a subtype of) Integer, not the type itself. In such cases, a call like fib(1) or fib(UInt8(2)) does create type-specialized compiled versions of the function.
In layman words, Julia will always specialize on function arguments either with or without annotation, except in three cases. One of them is when the arguments are passed to another function, since specializing twice will slow compilation down, and it will finally be specialized at the inner function call, so no performance hit occurs.
So, fib(n:Int) is equivalent to fib(n) and both will use a specialized compiled version of the function for the Int64 type unless n is already of a different type than Int64, it will specialize for that type.
This question already has answers here:
Passing external function of multiple variables as a function of one variable in Fortran
(2 answers)
How to pass a function with multiple arguments to a subroutine that expects a function with only one argument?
(1 answer)
Function with more arguments and integration
(1 answer)
Fortran minimization of a function with additional arguments
(2 answers)
Closed 4 years ago.
I would like to write a Fortran function that takes another function as input. I know that I can do this via an interface. From now on I will call these two different functions the 'function' and the 'interfaced function'.
Imagine that my function only varies the first argument of the interfaced function. For example, it might be an integration routine that is integrating in x, which is the first argument of any interfaced function that I would pass to it.
I would like to be able to pass an interfaced function with any number of arguments to this first function, and somehow be able to set fixed constant values for any other arguments that the interfaced function requires. A minimal example of the type of thing I want is below:
PROGRAM test
IMPLICIT NONE
!This works
WRITE(*,*) add(g)
!This does not work
WRITE(*,*) add(h(:,0.))
CONTAINS
REAL FUNCTION add(f)
IMPLICIT NONE
INTERFACE
REAL FUNCTION f(x)
REAL, INTENT(IN) :: x
END FUNCTION f
END INTERFACE
add=f(1.)+f(2.)
END FUNCTION add
REAL FUNCTION g(x)
IMPLICIT NONE
REAL, INTENT(IN) :: x
g=x**2
END FUNCTION g
REAL FUNCTION h(x,y)
IMPLICIT NONE
REAL, INTENT(IN) :: x, y
h=x**2+y**2
END FUNCTION h
END PROGRAM test
The above code will not compile with the second add call in the main program and complains of a syntax error in the argument list. However, it works fine without this line.
Is there any way I can adjust my code so that I can call my add function with the input h function as well as with the input g function?
Note that the actual application I have in mind for this is relatively complex, and involves 'interfaced functions' that would need all sorts of different types in their argument list.
I was under the impression that the main differences between subroutines and functions in Fortran was that functions returned values, while subroutines change some or all of the values passed as arguments. But then I learned that you could modify variables passed in to functions as arguments too. I'm confused, and can't find a good reference for the differences between them.
So, what are the differences between the two constructs, and when and why should one be preferred over the other?
Whether to use one or another is more or less a matter of programming style. You are allowed to write the arguments of both functions and subroutines as intent(in), intent(inout) or intent(out).
My personal style is however to only use the intent(in) arguments for functions, which is also a requirement for pure functions. An exception to this rule can be made when en error code intent(out) argument is necessary.
There is a subtle trap hidden in functions which return different results for the same input argument value. Consider a hypothetical function returning a random number
real function rnd()
end function
calling it once
x = rnd()
is completely OK. Calling it multiple times in a single expression
x = (rnd() + rnd()) / 2
can result in the function being called only once. Fortran language rules allow such behaviour. Therefore, the standard Fortran procedure for getting random numbers random_number() is a subroutine (and because all intrinsic functions are pure).
Where ever you cannot use a function, use a subroutine.
Any function can by converted to a subroutine by moving the result variable to a dummy argument with intent(out). The opposite process may be more problematic.
I am reading through a book for homework, and I understand that using #' is treating the variable as a function instead of a variable. But I am a little hazy on FUNCALL. I understand that lisp makes object out of variables, so is the function name just a 'pointer' (may be a bad word, but hopefully you get what I mean), in which case you use #' to invoke it, or is funcall the only way to invoke them? ex.
(defun plot (fn min max step)
(loop for i from min to max by step do
(loop repeat (funcall fn i) do (format t "*"))
(format t "~%")))
couldn't I just do:
(defun plot (fn min max step)
(loop for i from min to max by step do
(loop repeat #'(fn i) do (format t "*"))
(format t "~%")))
I guess my confusion lies in what exactly is in the function names. When I read the book, it said that the variable's value is what will be the function object.
#'function-name is (function function-name). Nothing is called, evaluating either results in the function associated with function-name (the object representing the function). funcall is used to call functions.
See funcall and function in the HyperSpec.
Sample session using both:
CL-USER> (defun square (x) (* x x))
SQUARE
CL-USER> #'square
#<FUNCTION SQUARE>
CL-USER> (function square)
#<FUNCTION SQUARE>
CL-USER> (funcall #'square 3)
9
CL-USER> (funcall 'square 3)
9
The second invocation of funcall works because it also accepts a symbol as function designator (see the link for funcall above for details).
The #' and funcall notations are needed in Common Lisp because this language is a so-called "Lisp-2" where a given symbol can have two separate and unrelated main "meanings" normally listed as
When used as first element of a form it means a function
When used in any other place it means a variable
These are approximate explanations, as you will see in the following example that "first element of a form" and "any other place" are not correct definitions.
Consider for example:
the above code prints 144... it may seem surprising at first but the reason is that the same name square is used with two different meanings: the function that given an argument returns the result of multiplying the argument by itself and the local variable square with value 12.
The first and third uses of the name square the meaning is the function named square and I've painted the name with red color. The second and fourth uses instead are about a variable named square and are painted in blue instead.
How can Common Lisp decide which is which? the point is the position... after defun it's clearly in this case a function name, like it's a function name in the first part of (square square). Likewise as first element of a list inside a let form it's clearly a variable name and it's also a variable name in the second part of (square square).
This looks pretty psychotic... doesn't it? Well there is indeed some splitting in the Lisp community about if this dual meaning is going to make things simpler or more complex and it's one of the main differences between Common Lisp and Scheme.
Without getting into the details I'll just say that this apparently crazy choice has been made to make Lisp macros more useful, providing enough hygiene to make them working nicely without the added complexity and the removed expressiveness power of full hygienic macros. For sure it's a complication that makes it harder to explain the language to whoever is learning it (and that's why Scheme is considered a better (simpler) language for teaching) but many expert lispers think that it's a good choice that makes the Lisp language a better tool for real problems.
Also in human languages the context plays an important role anyway and there it's not a serious problem for humans that sometimes the very same word can be used with different meanings (e.g. as a noun or as a verb like "California is the state I live in" or "State your opinion").
Even in a Lisp-2 you need however to use functions as values, for example passing them as parameters or storing them into a data structure, or you need to use values as functions, for example calling a function that has been received as parameter (your plot case) or that has been stored somewhere. This is where #' and funcall come into play...
#'foo is indeed just a shortcut for (function foo), exactly like 'x is a shortcut for (quote x). This "function" thing is a special form that given a name (foo in this case) returns the associated function as a value, that you can store in variables or pass around:
(defvar *fn* #'square)
in the above code for example the variable *fn* is going to receive the function defined before. A function value can be manipulated as any other value like a string or a number.
funcall is the opposite, allowing to call a function not using its name but by using a value...
(print (funcall *fn* 12))
the above code will display 144... because the function that was stored in the variable *fn* now is being called passing 12 as argument.
If you know the "C" programming language an analogy is considering (let ((p #'square))...) like taking the address of the function square (as with { int (*p)(int) = □ ...}) and instead (funcall p 12) is like calling a function using a pointer (as with (*p)(12) that "C" allows to be abbreviated to p(12)).
The admittely confusing part in Common Lisp is that you can have both a function named square and a variable named square in the same scope and the variable will not hide the function. funcall and function are two tools you can use when you need to use the value of a variable as a function or when you want a function as a value, respectively.
This question already has answers here:
What's the difference between an argument and a parameter?
(38 answers)
Closed 10 years ago.
Is there a difference between a "parameter" and an "argument", or are they simply synonyms?
Argument is often used in the sense of actual argument vs. formal parameter.
The formal parameter is what is given in the function declaration/definition/prototype, while the actual argument is what is passed when calling the function — an instance of a formal parameter, if you will.
That being said, they are often used interchangeably, their exact use depending on different programming languages and their communities. For example, I have also heard actual parameter etc.
So here, x and y would be formal parameters:
int foo(int x, int y) {
...
}
Whereas here, in the function call, 5 and z are the actual arguments:
foo(5, z);
Generally, the parameters are what are used inside the function and the arguments are the values passed when the function is called. (Unless you take the opposite view — Wikipedia mentions alternative conventions when discussing parameters and arguments).
double sqrt(double x)
{
...
return x;
}
void other(void)
{
double two = sqrt(2.0);
}
Under my thesis, x is the parameter to sqrt() and 2.0 is the argument.
The terms are often used at least somewhat interchangeably.
They are often used interchangeably in text, but in most standards the distinction is that an argument is an expression passed to a function, where a parameter is a reference declared in a function declaration.
Arguments and parameters are different
in that parameters are used to different values in the program and
The arguments are passed the same value
in the program so they are used in c++.
But no difference in c. It is the same for arguments and parameters in c.