I have a function and I want to call it in the main function. all of arguments is sharedArray and we have one variable that is type. when I want to run the program I got an error.
#everywhere type dty{T <: Real}
...... (some variable)
end
#everywhere function func2!(v::dty,
out::SharedArray,
out2::SharedArray)
........
end
function func1()
...
out = SharedArray{Float64,2}(n,m)
out2 = SharedArray{Float64,2}(n,m)
......
func2!(v , out, out2)
end
Error:
MethodError: no method matching func2!(::dty{Float64}, ::Array{Float64,2}, ::SharedArray{Float64,2}, ::SharedArray{Float64,2})
Closest candidates are:
func2!(::dty, ::SharedArray, ::SharedArray) at In[3]:64
You can just define
#everywhere function func2!(v,
out,
out2)
and it'll work. Those dispatch designations don't do anything for performance. If you want to restrict it to "things which are arrays", then you can do
#everywhere function func2!(v,
out::AbstractArray,
out2::AbstractArray)
Again, this doesn't hurt performance and is only for setting dispatches and throwing errors. What happened in your code is you had out::SharedArray, when then threw an error when out was not a SharedArray (your example doesn't show why it wasn't a SharedArray, but the error message is saying out was a Matrix)
Related
I am trying to understand why the following examples on using set comprehension give different results:
https://play.openpolicyagent.org/p/5x5mXmsyr0
https://play.openpolicyagent.org/p/IVQlTYcVpD
In the first example, rlt is evaluated to an empty set despite foo["c"] is undefined. I expect rlt to also be undefined.
In the second example, I removed the function but directly set rlt2 to the result of a set comprehension. This time it does return undefined.
Can someone explain the difference here?
I think what you see here is the type checker doing the best it can.
The error you get, is because at compile time, the type checker knows which keys exist.
For the function call, foo is a function argument,
myFunc(foo) = rlt {
rlt := {f | f := foo["c"]}
}
and the compiler cannot tell if foo["c"] exists or does not exist -- that depends on the actual call. You might define the function like that, but use it in other ways like
$ echo '{"c": 123}' | opa eval -I -d policy.rego 'data.play.myFunc(input)'
So it doesn't do any kind of deeper flow analysis.
Now rlt is not undefined because set (object, array) comprehensions are never undefined -- if their bodies are always undefined, the overall collection becomes an empty set (object, array).
In python, I can make a function that requires data to be passed in. For example, I can make a function like:
def functionName(x)
and can then use x within the function. When I try this in processing like:
void functionName(x)
it gives me the error message Error on "... VariableDeclaratorId".
Is there a way to pass in data in processing or java like in python?
You have to define the type of the parameter. For example, if the value you want to be passed in is a float, you would define your function as:
void funtionName(float x)
I'm having trouble with function declarations and scope in julia. I have a main function, let's call it mainfunc which accepts some arguments. Within this function, I would ultimately like to call a different function, say callfunc, which takes a function as an argument. This function I will call passfunc.
One further complication I have is that there is a final function which I define outside of the logic which depends on the arguments but still depends on the arguments in a different way. I can call this initfunc. This must be composed with some other function, depending on the arguments, to create passfunc.
Based on the arguments given to mainfunc, I will have different definitions of passfunc. Given the answer I got to a related question here, I initially tried to define my function logic in the following way, using anonymous functions which are apparently more efficient:
function mainfunc(args)
init_func = x -> funcA(x, args)
if args[1] == "foo"
anon_func = x -> func1(x, args)
elseif args[1] == "bar"
anon_func = x -> func2(x, args)
end
function passfunc(x)
return init_func(x) + anon_func(x)
end
# ... define other args...
callfunc(passfunc, other_args)
end
Defining my function in this way leads to errors in julia - apparently passfunc is an undefined variable when I run this code. Does the scope not allow the anonymous functions to be defined in the if statements? How else could I write code that achieves this?
I feel like a better understanding of functional programming principles would make the solution here obvious. Thank you in advance for any tips you can offer on improving this.
Also, I am running this with julia v0.7
in order to calculate the inverse function of f(x) I defined following function:
inv(fx):=exp▶list(solve(fx=y,x),x)
which output is:
inv(x^(2)) {piecewise(−√(y),y≥0),piecewise(√(y),y≥0)}
So that part works already, but how can I use this result as a callable function i(y)?
Thanks for your help
Outside of your program, you can turn the result into function i(y) with:
i(y):=piecewise(-√(y),y≥0,√(y),y≥0)
I do not have a CAS, so your results may differ, but, because the function can only return one value, it would only return (and display in the graph) the first value, in this case, -√(y). If you want to display on the graph or get the values of both, you would be better off creating two separate functions (-√(y), and √(y)). Hope this helps you "use the result as a callable function."
In Lua I've created a pretty printer for my tables/objects. However, when a function is displayed, it's shown as a pointer.
I've been reading up on Lua Introspection but when I introspect the function with debug.getinfo() it won't return the function's name. This seems to be due to a scoping issue but I'm not sure how to get around it.
What is the best way to get a function's name using its pointer? (I understand functions are first class citizens in Lua and that they can be created anonymously, that's fine)
when you create the functions, register them in a table, using them as keys.
local tableNames = {}
function registerFunction(f, name)
tableNames[f] = name
end
function getFunctionName(f)
return tableNames[f]
end
...
function foo()
..
end
registerFunction(foo, "foo")
...
getFunctionName(foo) -- will return "foo"
For some reason, it seems to work only with number parameters (that is, active functions on the stack).
The script
function a() return debug.getinfo(1,'n') end
function prettyinfo(info)
for k,v in pairs(info) do print(k,v) end
end
prettyinfo(a())
prints
name a
namewhat global
but if I change the last line to
prettyinfo(debug.getinfo(a, 'n'))
it gives me only an empty string:
namewhat