admin 管理员组

文章数量: 887021


2024年2月19日发(作者:染膏颜色代码记忆口诀1到8)

}void hello::on_btnTest_clicked(){ test1(); test2();}void test2(){ if (!Py_isInitialize()) return; import_array() PyRun_simpleString("import sys"); QString setSysPath = QString("('%1')").arg(QCoreApplication::applicationDirPath()); PyRun_SimpleString(tring().c_str()); PyObject* pModule = PyImport_ImportModule("test2"); if (!pModule) { qDebug("Can not open python file!"); return;

} PyObject * pFunc = PyObject_GetAttrString(pModule, "test2Function"); if (!pFunc) { qDebug("Get Function failed!"); return; }

PyObject* listUP = PyList_New(3); double arrUP[] = {1.1,2,3}; for (int i = 0; i < 3; i++) { PyList_SetItem(listUP, i, Py_BuildValue("d", arrUP[i])); }

PyObject* listLISAN1 = PyList_New(5); for (int i = 0; i < 5; i++) { PyList_SetItem(listLISAN1, i, Py_BuildValue("i", i+1); } PyObject* listLISAN2 = PyList_New(7); for (int i = 0; i < 7; i++) { PyList_SetItem(listLISAN2, i, Py_BuildValue("i", i+1); } PyObject* listLISAN = PyList_New(0); PyList_Append(listLISAN, listLISAN1); PyList_Append(listLISAN, listLISAN2);

PyObject* DOWNEST = PyFloat_FromDouble(-1.0e+100);

PyObject* args = PyTuple_New(3); PyTuple_SetItem(args, 0, listUP); PyTuple_SetItem(args, 1, listLISAN); PyTuple_SetItem(args, 2, DOWNEST);

PyObject* res = PyEval_CallObject(pFunc, args);

PyObject* poMinMubiaos = PyTuple_GetItem(res, 0); PyObject* poFanggans = PyTuple_GetItem(res, 1); PyObject* poMubiaos = PyTuple_GetItem(res, 2);

{ qDebug("MinMubiaos = %frn", PyFloat_AsDouble(poMinMubiaos)); } {

{ //cout << "poFanggans type: " << poFanggans->ob_type->tp_name << endl << flush; // type: list cout << "Fangans=[rn" << flush; for (int i = 0; i < PyList_Size(poFanggans); i++) { PyObject* childList = PyList_GetItem(poFanggans, i); cout << "[" << flush; for (int j = 0; j < PyList_Size(childList): j++) { if (j != 0) cout << ", " << flush; cout << PyFloat_AsDouble(PyList_GetItem(childList, j)) << flush; } cout << "], rn" << flush; } cout << "] rn" << flush; } { cout << "poMinMubiaos size = " << PyArray_Size(poMinMubiaos) << endl; PyArrayObject* pyArr = reinterpret_cast(poMinMubiaos); int nd = pyArr->nd; // 维度 if (nd == 1) { int row = pyArr->dimensions[0]; assert(row == PyArray_Size(poMinMubiaos)); cout << "poMinMubiaos=[" << flush; for (int i = 0; i < row; i++) { if (i != 0) cout << " " flush; cout << *((double*)(pyArr->data + i * pyArr->strides[0])) << flush; } cout << "]rn" << flush; } }}void test1(){ Py_SetPythonHome("c:Python27"); import_array() PyRun_simpleString("import sys"); QString setSysPath = QString("('%1')").arg(QCoreApplication::applicationDirPath()); PyRun_SimpleString(tring().c_str()); PyObject* pModule = PyImport_ImportModule("test1"); if (!pModule) { qDebug("Can not open python file!"); return;

} PyObject * pFunc = PyObject_GetAttrString(pModule, "test1Function"); if (!pFunc) { qDebug("Get Function failed!"); return; }// data: [[1 2 3], [1 2 3]] const int SIZE1 = 2; const int SIZE2 = 3; const int ND = 2; npy_intp dims[ND] = {SIZE1, SIZE2}; double (*c_arr)[SIZE2] = new double[SIZE1][SIZE2]; for (int i = 0; i < SIZE1; i++) for (int j = 0; j < SIZE2; j++) c_arr[i][j] = i*SIZE1+j;

pyArrayObject *pyArr; pyArr = reinterpret_cast( PyArray_SimpleNewFromData(ND, dims, NPY_LONGDOUBLE, reinterpret_cast(c_arr))); PyObject *args = PyTuple_New(1); PyTuple_SetItem(args, 0, reinterpret_cast(pyArr)); PyEval_CallObject(pFunc, args); delete[] c_arr;}


本文标签: 颜色代码 记忆 染膏 作者 口诀