1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #pragma once
#ifndef LIBYX_LINEAR_SYSTEM_H #define LIBYX_LINEAR_SYSTEM_H
#include "MatlabSparseMatrix.h"
namespace LibYX { class CLinearSystem { public: CLinearSystem() {} CLinearSystem(double* pMatrixA, int nRow, int nColumn, double* pColumnB); CLinearSystem(MatlabSparseInfor* pSI, int nNum, int nRow, int nColumn, double* pColumnB); ~CLinearSystem() {}
public: void GetResult(double* pX); void GetResultSparse(double* pX);
private: double* m_pMatrixA; double* m_pColumnB; int m_nRow; int m_nColumn;
private: MatlabSparseInfor* m_pSI; int m_nNum; }; };
#endif
#include "stdafx.h" #include "LinearSystem.h" #include <cassert> #include "engine.h" #pragma comment(lib, "libeng.lib") #pragma comment(lib, "libmx.lib") #pragma comment(lib, "libmat.lib")
namespace LibYX { CLinearSystem::CLinearSystem(double* pMatrixA, int nRow, int nColumn, double* pColumnB) : m_pMatrixA(pMatrixA), m_nRow(nRow), m_nColumn(nColumn), m_pColumnB(pColumnB) {
}
CLinearSystem::CLinearSystem(MatlabSparseInfor* pSI, int nNum, int nRow, int nColumn, double* pColumnB) : m_pSI(pSI), m_nNum(nNum), m_nRow(nRow), m_nColumn(nColumn), m_pColumnB(pColumnB) {
}
void CLinearSystem::GetResult(double* pX) { assert(pX != NULL);
Engine *ep = NULL; if (!(ep = engOpen(NULL))) { fprintf(stderr, "\nCan't start MATLAB engine\n"); return; }
mxArray* matrixA = mxCreateDoubleMatrix(m_nRow, m_nColumn, mxREAL); double* pA = mxGetPr(matrixA); for (int j = 0; j < m_nColumn; ++j) { for (int i = 0; i < m_nRow; ++i) { *pA++ = *(m_pMatrixA + i * m_nColumn + j); } }
mxArray* columnB = mxCreateDoubleMatrix(m_nRow, 1, mxREAL); memcpy(mxGetPr(columnB), m_pColumnB, sizeof(double) * m_nRow * 1);
engPutVariable(ep, "A", matrixA); engPutVariable(ep, "B", columnB); engEvalString(ep, "X = A\\B;");
mxArray* rowX = engGetVariable(ep, "X"); memcpy(pX, mxGetPr(rowX), sizeof(double) * m_nColumn);
mxDestroyArray(matrixA); mxDestroyArray(columnB); mxDestroyArray(rowX); engClose(ep); }
void CLinearSystem::GetResultSparse(double* pX) { Engine *ep = NULL; if (!(ep = engOpen(NULL))) { fprintf(stderr, "\nCan't start MATLAB engine\n"); return; }
mxArray* sm = mxCreateSparse(m_nRow, m_nColumn, m_nNum, mxREAL); double* pr = mxGetPr(sm); mwIndex* ir = mxGetIr(sm); mwIndex* jc = mxGetJc(sm);
int k = 0; int nLastC = -1; for (int i = 0; i < m_nNum; ++i) { pr[i] = m_pSI[i].v; ir[i] = m_pSI[i].r;
if (m_pSI[i].c != nLastC) { jc[k] = i; nLastC = m_pSI[i].c; ++k; } } jc[k] = m_nNum;
mxArray* columnB = mxCreateSparse(m_nRow, 1, m_nRow, mxREAL); pr = mxGetPr(columnB); ir = mxGetIr(columnB); jc = mxGetJc(columnB);
jc[0] = 0; for (int i = 0; i < m_nRow; ++i) { pr[i] = m_pColumnB[i]; ir[i] = i; } jc[1] = m_nRow;
engPutVariable(ep, "sm", sm); engPutVariable(ep, "columnB", columnB); engEvalString(ep, "X = sm\\columnB;");
mxArray* rowX = engGetVariable(ep, "X"); memcpy(pX, mxGetPr(rowX), sizeof(double) * m_nColumn);
mxDestroyArray(rowX); mxDestroyArray(columnB); mxDestroyArray(sm); engClose(ep); } };
|