MCPcopy Create free account
hub / github.com/SimVascular/SimVascular / Math_fit_least_squares

Function Math_fit_least_squares

Code/Source/PythonAPI/Math_PyModule.cxx:486–549  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

484");
485
486static PyObject *
487Math_fit_least_squares(PyObject *self, PyObject *args)
488{
489 auto api = PyUtilApiFunction("OOii", PyRunTimeErr, __func__);
490 int xOrder = 0;
491 int yOrder = 0;
492
493 PyObject *xtermsArg;
494 PyObject *ytermsArg;
495
496 if (!PyArg_ParseTuple(args, api.format, &xtermsArg, &ytermsArg, &xOrder,&yOrder)) {
497 return api.argsError();
498 }
499
500 int numberOfSamples = PyList_Size(xtermsArg);
501 int numberOfSamplesY = PyList_Size(ytermsArg);
502 if (numberOfSamples!= numberOfSamplesY) {
503 api.error("The number of x terms ("+std::to_string(numberOfSamples)+") != the number of y terms ("+std::to_string(numberOfSamplesY)+".");
504 return nullptr;
505 }
506
507 // Get an array of points from the xtermsArg list.
508 int dim = 3;
509 auto xt = GetPointsFromList(api, xtermsArg, dim, "x terms");
510 if (xt == nullptr) {
511 return nullptr;
512 }
513
514 // Get an array of points from the xtermsArg list.
515 auto yt = GetPointsFromList(api, ytermsArg, dim, "y terms");
516 if (yt == nullptr) {
517 return nullptr;
518 }
519
520 // Perform the least squares fit.
521 //
522 auto mathObj = cvMath();
523 double **mt = mathObj.createArray(xOrder,yOrder);
524 if (mathObj.fitLeastSquares(numberOfSamples,xt,xOrder,yt,yOrder,mt) == SV_ERROR) {
525 PyErr_SetString( PyRunTimeErr, "error in least squares fit" );
526 mathObj.deleteArray(xt,numberOfSamples,xOrder);
527 mathObj.deleteArray(yt,numberOfSamples,yOrder);
528 api.error("Error performing the least squares fit.");
529 return nullptr;
530 }
531
532 // create result string
533 char r[2048];
534 PyObject *pylist=PyList_New(xOrder);
535 for (int i = 0; i < xOrder; i++) {
536 r[0] = '\0';
537 for (int j = 0; j < yOrder; j++) {
538 sprintf(r,"%.6le ",mt[i][j]);
539 PyObject *rr=PyBytes_FromString(r);
540 PyList_SET_ITEM(pylist, i, rr);
541 }
542 }
543

Callers

nothing calls this directly

Calls 6

GetPointsFromListFunction · 0.85
argsErrorMethod · 0.80
errorMethod · 0.80
fitLeastSquaresMethod · 0.80
createArrayMethod · 0.45
deleteArrayMethod · 0.45

Tested by

no test coverage detected