printing the function name in fortran 90 - fortran90

I wrote a code that finds the root of a function whose name is provided among the arguments, I think I took it from Numerical Recipes. Something like
double precision function rtsafe(x_init, x1, x2, xacc, func, dfunc)
where func and dfunc are two functions' names.
Of course I use rtsafe with different function func and dfunc.
I would like to print the name of the called functions func and dfunc when I am inside rtsafe, because when there is an error in rtsafe I would like to know which function I was using. Something like
write(,)"my func = ", func
(?)
Does anybody know how to do that?

You could add an optional argument in your functions that returns the name of the function:
FUNCTION f(x, fname) RESULT (fx)
IMPLICIT NONE
REAL :: x, fx
CHARACTER(LEN=*), OPTIONAL :: fname
CHARACTER(LEN=*), PARAMETER :: myfname='somename'
IF (present(fname)) fname=myfname
fx = x ! or whatever else
END FUNCTION f
In the first call to your function in rtsafe you get the name of the function for later printing in case of an error.
Did not test this but it should work more or less like this, and it the only way I can think of to do this in Fortran.

Maybe you can work up some manual solution (pass the name of the function, then print it with "OK" ... or something like that), but printing the names of the functions/subroutines (reflecting) is not possible.

Related

Inheritance with hump in love2D

I'm trying to do OOP using the hump library in Lua for a game coded in löve 2D. Everything is working fine. However, when I try to play with my code the way bellow, a message error tells me that "self" is a nill value. Can someone tell me what I did wrong please?
Item=Class{
init=function(x,y,size)
self.x=x
self.y=y
self.size=size
self.dx=dx
self.dy=dy
self.dx2=dx2
self.dy2=dy2
end;
update=function(dt)
self.dx=self.dx+self.dx2
self.x=self.x+self.dx*dt
self.dy=self.dy+self.dy2
self.y=self.y+self.dy*dt
end;
coliide=function(ball)
return math.sqrt((self.x-ball.x)^2+(self.y-ball.y)^2)<self.size
end;
reset=function()
self.x=love.graphics.getWidth()/2
self.y=love.graphics.getHeight()/2
self.dy=0
self.dx=0
self.dy2=0
self.dx2=0
end
}
Thank you and regards
In the given snippet
Item = Class{}
Item.init=function(x,y,size)
self.x = x
end
self is nil because you did not define it.
In order to do what you want you have to define the function like that:
Item.init = function(self, x, y, size)
self.x = x
end
and call it like that
Item.init(Item, x, y, size)
Then self equals Item and you may index it without an error.
To make this a bit more convenient we can use something called Syntactic Sugar
Let's have a look into the Lua 5.3 Reference Manual:
3.4.10 - Function Calls
A call v:name(args) is syntactic sugar for v.name(v,args), except that
v is evaluated only once.
3.4.11 - Function Definitions
The colon syntax is used for defining methods, that is, functions that
have an implicit extra parameter self. Thus, the statement
function t.a.b.c:f (params) body end
is syntactic sugar for
t.a.b.c.f = function (self, params) body end
Using this knowledge we can simply write:
function Item:init(x,y,size)
self.x = x
end
and call it like so:
Item:init(x,y)
The implicit self argument is available to function when it was declared using colon syntax. E.g.:
Item=Class{}
function Item:init(x,y,size)
self.x = x
self.y = y
-- ...
end
Alternatively you could just add self argument explicitly in your existing code. Just make sure you're calling it with colon syntax.

Finding parameter names from anonymous function

I want to be able to find the parameter names of an anonymous function in Matlab.
I tried to see if there was any information about the parameter names in the functions() command, but to no avail.
Say I have an anonymous function f:
f = #(x, y) x^2 + y^2
I need to be able to find the parameter names 'x' and 'y' from this equation. Is there a built in method in Matlab which can do this? Or would I somehow have to parse the function to receive the parameter names?
The function field in the output of functions (or equivalently the output of func2str) gives the function definition as a string. You then use a regular expression to match each sequence of one or more non-), non-, characters that are between a #( or , and a , or ):
s = functions(f);
inputVarNames = regexp(s.function, '(?<=(,|#\())[^\)]+?(?=(,|\)))', 'match');

Difference between matlab function 'handle' and python function 'object'

It was suggested in this comment that there is a difference between how Matlab and Python pass around functions. From what I can tell by looking and using the two, there is no difference between the two, but maybe I'm missing something?
In Matlab, you would create a quick function handle like this:
fun = #(x) x.^2 + 1;
In Python, using a lambda function, you could create a similar function like this:
def fun(x):
return x^2
In both languages, it's possible to send the term 'fun' to another function as an argument - but the commenter I linked to insinuated that they are not the same and/or need to be used differently.
What am I missing?
The first comment seems to simply reiterate the idea that you can pass a MATLAB function handle as an argument (although the answer didn't state anything that would make me think otherwise). The second comment seemed to interpret this to mean that the first commenter thought that you couldn't do this in Python and responded to state that you can use either a lambda or pass the function directly.
Regardless, assuming that you use them correctly, a function handle in MATLAB is functionally equivalent to using either a lambda or function object as an input argument in Python.
In python, if you don't append the () to the end of the function, it doesn't execute the function and instead yields the function object which can then be passed to another function.
# Function which accepts a function as an input
def evalute(func, val)
# Execute the function that's passed in
return func(val)
# Standard function definition
def square_and_add(x):
return x**2 + 1
# Create a lambda function which does the same thing.
lambda_square_and_add = lambda x: x**2 + 1
# Now pass the function to another function directly
evaluate(square_and_add, 2)
# Or pass a lambda function to the other function
evaluate(lambda_square_and_add, 2)
In MATLAB, you have to use a function handle because MATLAB attempts to execute a function even if you omit the ().
function res = evaluate(func, val)
res = func(val)
end
function y = square_and_add(x)
y = x^2 + 1;
end
%// Will try to execute square_and_add with no inputs resulting in an error
evaluate(square_and_add)
%// Must use a function handle
evaluate(#square_and_add, 2)

Pass additional arguments to a built-in function

I'm pretty new on Julia and have a question that may appear simple. Say I have a function, which I will name test(x::Vector, arg1, arg2) where x is a vector of variables and the function has two arguments arg1 & arg2.
I would like to optimize (minimize) the function test with respect to the vector x. I can't figure out how to use the optimize function from the Optim package that accepts two arguments values. In R, one may do as following:
optim(initial guest, test, arg1=value1,arg2=value2)
Is there a similar way to declare argument value in Julia?
You can define another function that fixes the value of those arguments.
# Function to minimize
f(x::Vector, a, b) = (x[1] - a)^2 + (x[2] - b)^2
using Optim
g(x::Vector) = f(x, 3, 4)
optimize(g, [0.,0.])
You could also use an anonymous function (but it may be less efficient).
optimize(x -> f(x,3,4), [0.,0.])

Namespaces for functions and variables in Swift

If you run this code, the variable f seems to shadow the function f. Is there anyway to reach the function f?
func f (a:Int)->Int{
return a + 43
}
var f = {(a:Int) in a + 42}
var z = f(1)
println(z)
No.
In Swift, function declarations are simply shortcuts for what you did with that closure + variable thing. That is, function names are essentially constants and should always be viewed as such (you can even pass around the function name, without brackets, as a reference).
What you're doing is you're redeclaring the name f to the variable closure. It seems Swift has a compiler issue not complaining about this. However, this problem would never occur in good code, so it's not a real problem.
It can be a bit confusing, though.