Код: Выделить всё
np.linalg.lstsq(left_hand, right_hand, rcond=None)Мне нужно написать функцию в C ++, которая выполняет то же самое, что и эта программа Python. Мой первый подход к этому был использование OpenCV CV :: Reolpe
Код: Выделить всё
cv::solve(equationSystem, equationsRightSide, solutions, cv::DECOMP_SVD);Однако, хотя их результаты очень похожи большую часть времени, иногда они отличаются довольно много. Например, для системы с 3 переменными: < /p>
c ++ дает это решение:
* 0.0008230780686749673; 0,001646156137349933; 8.470329472543003E-20] < /p>
Python дает это решение:
*-8.76551484e-05 1.41121756E-03 8.62337999E-04] < /p>
Код: Выделить всё
[+8.23078069e-04; 1.64615614e-03; 8.47032947e-20 ] C++
| . 1.| . ||
[-8.76551484e-05 1.41121756e-03 8.62337999e-04 ] Py
Код: Выделить всё
Eigen::JacobiSVD svd(
ES_Eigen, Eigen::ComputeThinU | Eigen::ComputeThinV);
// In Python, if rcond = None, linalg.lstsq calculates the rcond value as the machine's precision
// times the greatest between the equations right side's columns and rows
double rcond = std::numeric_limits::epsilon() * std::max(equationsRightSide.rows, equationsRightSide.cols);
svd.setThreshold(rcond);
Eigen::VectorXd x = svd.solve(RS_Eigen);
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79751381/c-equivalent-of-pythons-numpy-linalg-lstsq[/url]
Мобильная версия