0

I have a function wrapper, which is as defined below: Here I pass in a set of coefficients (inputArr), and a Tuple containing 2 double arrays.

The expected output is how many times the "WminObjectivefunction" shall be called and also to return the "FuncValue" which is basically "WminObjectivefunction" evaluated at "inputArr". Every time I call "WrapFunction" the array "inputArr" passed shall be different.

public void WrapFunction(out int ncalls, out double FuncValue, double[] inputArr, Tuple<List<double>, List<double>> arguments)
{
    int calls = 0;//Number_of_FunctionEvaluations
    // MASTER class instance  
    Master prismpy = new Master();
    calls += 1;//Number_of_FunctionEvaluations_Increment
    ncalls = calls;//Return_Number_of_FunctionEvaluations
    FuncValue = prismpy.WminObjectivefunction(inputArr, arguments.Item1, arguments.Item2);//Return_FunctionValuation
}

Question1: I want to store the FuncValue as elements in an Array fsim, How can I command the WrapFunction to store the value as an array element denoted by an index. The following is what I tried, and the error is : Cannot implicitly convert 'void' to 'double'

// FSIM: put array in array of arrays
double[] fsim = new double[5];
int fcall;
fsim[0] = WrapFunction(out fcall, out fsim[0], _x0, args);

Question2: Is it necessary to call WrapFunction with all out arguments every time ? Is there a way if I just want to get either one of those two outputs?

2
  • 1
    Isn't ncalls always going to be 1? In which case the only useful information is the FuncValue and you could just return it instead of using an out parameter. Commented Feb 9, 2016 at 15:56
  • This is a huge code smell Commented Feb 9, 2016 at 16:05

1 Answer 1

1

Not sure what you mean, but I would guess like this:

public double WrapFunction(out int ncalls, out double FuncValue, double[] inputArr, Tuple<List<double>, List<double>> arguments)
{
    int calls = 0;//Number_of_FunctionEvaluations
    // MASTER class instance  
    Master prismpy = new Master();
    calls += 1;//Number_of_FunctionEvaluations_Increment
    ncalls = calls;//Return_Number_of_FunctionEvaluations
    FuncValue = prismpy.WminObjectivefunction(inputArr, arguments.Item1, arguments.Item2);//Return_FunctionValuation
    return FuncValue;
    // NOTE: the FuncValue parameter may be redundant -- RBarryYoung
}

Here's another version, that reflects some of the comments/discussion:

int calls = 0;//Number_of_FunctionEvaluations

public double WrapFunction(out int ncalls, double[] inputArr, Tuple<List<double>, List<double>> arguments)
{
    // MASTER class instance  
    Master prismpy = new Master();
    calls += 1;//Number_of_FunctionEvaluations_Increment
    ncalls = calls;//Return_Number_of_FunctionEvaluations
    return prismpy.WminObjectivefunction(inputArr, arguments.Item1, arguments.Item2);//Return_FunctionValuation
}
Sign up to request clarification or add additional context in comments.

5 Comments

You can remove the out double FuncValue in this case.
@juharr True, but the OP's reqs seem vague and like they are part of some larger context, so I opted not to change the interface. I will change it to set FuncValue also.
Ah yes, how can I validate how many times Wrap function is called ? In the present case, like @juharr said, its going to be 1 always.
@abhilashsukumari Then calls would need to be moved to a higher scope. If you want number of calls on a specific instance then move it to the class level. If you want it across all instances then you also need to make it static.
@abhilashsukumari OK, I will revert it and remove the FuncValue parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.