I am using alglib::lsfitcreatef() in C++ to do nonlinear least-square fitting. I copied sample code from the website, here. The main difference is that I am using a vector of data that gets computed by my application. I converted the data to an alglib::real_2d_array by following the example at this link. Visual Studio is stating that no overloaded function of this type matches the arguments list. Here is the code I wrote, which also converts one input to a real_1d_array:
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "interpolation.h"
#include <integration.h>
#include <alglib-cpp/src/ap.h>
double epsx = 0.000001;
alglib_impl::ae_int_t maxits = 0;
alglib_impl::lsfitstate state;
alglib_impl::lsfitreport rep;
const double diffstep = 0.0001;
//convert to proper parameters
const alglib::real_2d_array x_data = convert_to_alglib_2d_array(x);
const alglib::real_1d_array y_data = convert_to_alglib_1d_array(y_sub_array);
alglib::real_1d_array parameter_data = convert_to_alglib_1d_array(p0);
//
// Fitting without weights
//
alglib::lsfitcreatef(x_data, y_data, parameter_data, diffstep, state); //error here
lsfitsetcond(state, epsx, maxits); //this defines the stopping criteria
alglib::lsfitfit(state, lorentzian); //actual fit function
lsfitresults(state, parameter_data, rep); //middle parameter is the output parameter
alglib::real_2d_array SpectrumData::convert_to_alglib_2d_array(std::vector<int> input)
{
alglib::real_2d_array xy;
xy.setlength(input.size(), 1); //# rows first, # columns second, just for this method
for (int i = 0; i < input.size(); i++)
{
//xy(columns, rows)
xy(0, i) = input[i];
}
//alglib::real_2d_array* output = &xy;
return xy;
}
alglib::real_1d_array SpectrumData::convert_to_alglib_1d_array(std::vector<long> input)
{
std::vector<double> contents(input.begin(), input.end());
alglib::real_1d_array y;
y.setlength(contents.size());
for (int i = 0; i < y.length(); i++)
{
y[i] = contents[i];
}
//alglib::real_1d_array* output = &y;
return y;
}
Actual error: "no instance of overloaded function alglib::lsfitcreatef matches the argument list."
Details on my setup and what I have tried:
- I downloaded the source, and included this "Additional Include Directory" in the project: C:\3d-party-software\alglib-cpp\src. The src folder has all the .h and .cpp files you'd get from the official download
- I kept looking up code samples on the forum and the site examples, they still look the same as what I linked earlier
- I Googled this to see if anyone else had the same problem, got no answers
- Used autocomplete to be sure I was using the desired version of the 2 overloads of the function
- Check the actual source code in "interpolation.h": void lsfitcreatef(const real_2d_array &x, const real_1d_array &y, const real_1d_array &c, const double diffstep, lsfitstate &state, const xparams _xparams = alglib::xdefault);