How to solve the math operation of templates' arguments in SWIG? - swig

I want to convert some C++ code to Java code in SWIG. In some C++ templates, there are some math operation about templates' parameters. Such as:
template<size_t nbits, size_t es>
value<1 + 2 * (nbits - es)> fma(const posit<nbits, es>& a, const posit<nbits, es>& b, const posit<nbits, es>& c);
The parameters, nbits and es, are used to calculate the parameters of template class value<...>.
When I want to use %template(...) to instantiate the template function, fma, like this:
%template(fma_32_2) sw::unum::fma<32,2>;
However, it fails and display:
error: 'nbits' was not declared in this scope
SwigValueWrapper< sw::unum::value< 1+2*(nbits-es) > > result;
^~~~~
error: 'es' was not declared in this scope
SwigValueWrapper< sw::unum::value< 1+2*(nbits-es) > > result;
^~
error: template argument 1 is invalid
SwigValueWrapper< sw::unum::value< 1+2*(nbits-es) > > result;
^
error: template argument 1 is invalid
SwigValueWrapper< sw::unum::value< 1+2*(nbits-es) > > result;
So, is there any ways to fix it?

Related

Retrieving A Function From A WebhookScript Global Variable

In WebhookScript, I can store a function in a variable with:
sub = function(a, b) {
return a - b
}
I'd like to store a function in a Global Variable so that I can use it in multiple Custom Actions. But if I've saved the above function as $sub$ then
sub2 = var('$sub$')
subX = sub(1,2)
causes an error:
Trying to invoke a non-function 'string' # line...
And
function subX(a,b){
var('$sub$')
}
when sub only contains return a - b, doesn't work either.
Obviously I need to convert the string to a function but I'm not sure whether that's possible.
I know this is a bit of an obscure language but if anyone knows how this can be done in similar languages like JavaScript and PHP, I'm happy to test out any guesses...
The solution here is to remove the function section and just enter the script, which inherits the execution scope so if my global variable $script$ is:
return 'hello ' + a
Then I can execute the function with:
a = 'world'
value = exec(var('$script$'))
echo(value)
(credit to Webhook.Site's support team for explaining this)

How to pass an object as argument to an anonymous function in MATLAB?

I'm working on a MATLAB app that programatically creates anonymous functions to evaluate any native MATLAB function and pass it a list of variables as argument. In the example below, 'formula' contains a string with the function and arguments to be evaluated (e.g., "sum( var1, var2 )" ). The formulas sometimes contain function calls nested within function calls, so the code below would be used recursively until obtaining the final result:
Func2 = str2func( sprintf( '#(%s) %s', strjoin( varNames, ',' ), formula ) );
This evaluates fine for native MATLAB functions. But there's a particular case of a function (named Func1) I made myself that not only needs the list of variables but also an object as argument, like this:
function output = Func1( anObject, varNames )
% do some stuff with the object and the vars
end
For this particular function, I've tried doing this:
Func2 = str2func( sprintf( '#(%s,%s) %s', "objectToPassToFunc1", strjoin( varNames, ',' ), "Func1(objectToPass,""" + strjoin( varNames, '","' ) +""")" ) )
...which doesn't throw an error, but Func1 doesn't receive the objectToPassToFunc1, instead it gets values from one of the variables in varNames. And I don't know why.
So how can I correctly pass the object to Func1????
Matlab doesn't care about the type of arguments you pass to a function. As a matter of fact, the input could be scalar, vector, matrix, and even an object of a class. See the following example.
classdef ClassA
methods
function print(~)
disp('method print() is called.');
end
end
end
This class has only one method. Now, let us define an anonymous function func which accepts one input.
func = #(arg) arg.print;
Notice that we explicitly assume that the input is an object of ClassA. If you pass another type of data to this function, Matlab will throw an error. To test the code,
obj = ClassA;
func = #(arg) arg.print;
func(obj)
To avoid the error, you may need to check the type of the input before using it. For example,
function [] = func(arg)
% check if arg is an object of ClassA
if isa(arg,'ClassA')
arg.print;
end
end
Now you can pass different types for the input without getting an error.

Result of a function with parameters passed by name

Consider the following pseudocode snippet:
int c = 2;
int bar(int a)
{
c = c + 2;
return a * 2;
}
int foo(void)
{
return(bar(c + 1));
}
I'm asked to determine what the return value of foo(); will be, assuming that the language used passes all parameters by name.
My reasoning is that, since parameters are passed by name, c+1 won't be evaluated when bar(c+1) is called, but only when the first instance of the formal parameter a is encountered in bar, that is in the return a*2 line, after bar has modified the global variable c, so, since c+1 has to be evaluated in the caller's environment, that is in foo's environment and foo has only the global c in its scope it will be evaluated as 4+1, giving a fine return value of 10.
My doubt is whether this should be 6 instead, if I blindly apply a syntactical substitution rule, as passing by name requires the fifth line should be interpreted as return c+1*2, instead of return (c+1)*2, so what is the correct approach here?
For reference I'm using the definition of passing by name provided in section 7.1.2 of Programming Languages:Principles and Paradgigms by Gabbrielli and Martini

error: can't perform indexing operations for <unknown type> type when sourcing

With this code, saved as test.m
function test()
x = 1;
endfunction
I get the following error message when sourcing it via source(test.m) in the GUI:
>> clear
>> source (test.m)
x = 1
error: can't perform indexing operations for <unknown type> type
error: evaluating argument list element number 1
>>
Calling the function test via >> test works fine, but I'd like to know what I'm doing wrong here.
Progress:
calling just test, in the right dir seems to do it, but then what are we sourcing for?
If you run
source(test.m)
the interpreter tries to evaluate the "." subscript on the variable "test" (which is a function in your case) and then call source with the result.
What you want is to call the function source with the string "test.m" so you have to use quotes:
source ("test.m")
or don't use () in which case all arguments are passed as strings:
source test.m

Passing expression as a parameter in Call by reference

All,
When we are passing an expression as a parameter, how does the evaluation occur? Here is a small example. This is just a pseudocode kind of example:
f (x,y)
{
y = y+1;
x = x+y;
}
main()
{
a = 2; b = 2;
f(a+b, a)
print a;
}
When accessing variable x in f, does it access the address of the temp variable which contains the result of a+b or will it access the individual addresses of a and b and then evaluate the value of a+b
Please help.
Regards,
darkie15
Somewhat language dependent, but in C++
f(a+b, a)
evaluates a + b and and pushes the result of evaluation onto the stack and then passes references to this value to f(). This will only work if the first parameter is of f() is s const reference, as temporary objects like the result of a + b can only be bound to const references.
In C or C++, as long as x and y are not pointers (in which case the expression is not useful anyway), they are both evaluated before the function call and the VALUE of the result is pushed on the stack. There are no references involved, at all.
All parameters in C and C++ are always passed by value. If a reference type (eg int*, int&) is passed to the function, the VALUE of the reference is passed. While the referenced object may be changed by accessing eg *x within the function, the value of the reference still cannot be changed, because C and C++ parameters are always always always passed by value only.
EDIT: an exception in C and C++ is the case in which some overloaded operator is defined like the following:
T* operator+ (L lhs, R rhs) {return new T(lhs, rhs);}
and x is an L, and y is an R. In this case, the value of the T* generated by the function is pushed on the stack as a parameter. Don't write code like that, it confuses other programmers =D.