| 1 | /* |
| 2 | * Copyright © 2005-2011 Ondra Kamenik |
| 3 | * Copyright © 2019-2023 Dynare Team |
| 4 | * |
| 5 | * This file is part of Dynare. |
| 6 | * |
| 7 | * Dynare is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * Dynare is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with Dynare. If not, see <https://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "dynmex.h" |
| 22 | #include "mex.h" |
| 23 | |
| 24 | #include "GeneralSylvester.hh" |
| 25 | #include "SylvException.hh" |
| 26 | #include "int_power.hh" |
| 27 | |
| 28 | void |
| 29 | gen_sylv_solve(int order, int n, int m, int zero_cols, const ConstVector& A, const ConstVector& B, |
| 30 | const ConstVector& C, Vector& X) |
| 31 | { |
| 32 | GeneralSylvester s(order, n, m, zero_cols, A, B, C, X, false); |
| 33 | s.solve(); |
| 34 | } |
| 35 | |
| 36 | mxArray* |
| 37 | gen_sylv_solve_and_check(int order, int n, int m, int zero_cols, const ConstVector& A, |
| 38 | const ConstVector& B, const ConstVector& C, const ConstVector& D, |
| 39 | Vector& X) |
| 40 | { |
| 41 | GeneralSylvester s(order, n, m, zero_cols, A, B, C, X, true); |
| 42 | s.solve(); |
| 43 | s.check(D); |
| 44 | return s.getParams().createStructArray(); |
| 45 | } |
| 46 | |
| 47 | extern "C" |
| 48 | { |
| 49 | void |
| 50 | mexFunction(int nlhs, mxArray* plhs[], int nhrs, const mxArray* prhs[]) |
| 51 | { |
| 52 | if (nhrs != 5 || nlhs > 2 || nlhs < 1) |
| 53 | mexErrMsgTxt("Gensylv: Must have exactly 5 input args and either 1 or 2 output args."); |
| 54 | |
| 55 | if (!(mxIsScalar(prhs[0]) && mxIsNumeric(prhs[0]))) |
| 56 | mexErrMsgTxt("First argument should be a numeric scalar"); |
| 57 | |
| 58 | auto order = static_cast<int>(mxGetScalar(prhs[0])); |
| 59 | const mxArray* const A = prhs[1]; |
| 60 | const mxArray* const B = prhs[2]; |
| 61 | const mxArray* const C = prhs[3]; |
| 62 | const mxArray* const D = prhs[4]; |
| 63 | |
| 64 | if (!mxIsDouble(A) || mxIsComplex(A) || mxIsSparse(A)) |
| 65 | mexErrMsgTxt("Matrix A must be a real dense matrix."); |
| 66 | if (!mxIsDouble(B) || mxIsComplex(B) || mxIsSparse(B)) |
| 67 | mexErrMsgTxt("Matrix B must be a real dense matrix."); |
| 68 | if (!mxIsDouble(C) || mxIsComplex(C) || mxIsSparse(C)) |
| 69 | mexErrMsgTxt("Matrix C must be a real dense matrix."); |
| 70 | if (!mxIsDouble(D) || mxIsComplex(D) || mxIsSparse(D)) |
| 71 | mexErrMsgTxt("Matrix D must be a real dense matrix."); |
| 72 | |
| 73 | const mwSize* const Adims = mxGetDimensions(A); |
| 74 | const mwSize* const Bdims = mxGetDimensions(B); |
| 75 | const mwSize* const Cdims = mxGetDimensions(C); |
| 76 | const mwSize* const Ddims = mxGetDimensions(D); |
| 77 | |
| 78 | if (Adims[0] != Adims[1]) |
| 79 | mexErrMsgTxt("Matrix A must be a square matrix."); |
| 80 | if (Adims[0] != Bdims[0]) |
| 81 | mexErrMsgTxt("Matrix A and matrix B must have the same number of rows."); |
| 82 | if (Adims[0] != Ddims[0]) |
| 83 | mexErrMsgTxt("Matrix A and matrix B must have the same number of rows."); |
| 84 | if (Cdims[0] != Cdims[1]) |
| 85 | mexErrMsgTxt("Matrix C must be square."); |
| 86 | if (Bdims[0] < Bdims[1]) |
| 87 | mexErrMsgTxt("Matrix B must not have more columns than rows."); |
| 88 | if (Ddims[1] != static_cast<mwSize>(power(Cdims[0], order))) |
| 89 | mexErrMsgTxt("Matrix D has wrong number of columns."); |
| 90 | |
| 91 | auto n = static_cast<int>(Adims[0]); |
| 92 | auto m = static_cast<int>(Cdims[0]); |
| 93 | auto zero_cols = static_cast<int>(Bdims[0] - Bdims[1]); |
| 94 | mxArray* X = mxCreateDoubleMatrix(Ddims[0], Ddims[1], mxREAL); |
| 95 | // copy D to X |
| 96 | ConstVector Avec {A}, Bvec {B}, Cvec {C}, Dvec {D}; |
| 97 | Vector Xvec {X}; |
| 98 | Xvec = Dvec; |
| 99 | // solve (or solve and check) |
| 100 | try |
| 101 | { |
| 102 | if (nlhs == 1) |
| 103 | gen_sylv_solve(order, n, m, zero_cols, Avec, Bvec, Cvec, Xvec); |
| 104 | else if (nlhs == 2) |
| 105 | plhs[1] = gen_sylv_solve_and_check(order, n, m, zero_cols, Avec, Bvec, Cvec, Dvec, Xvec); |
| 106 | } |
| 107 | catch (const SylvException& e) |
| 108 | { |
| 109 | mexErrMsgTxt(e.getMessage().c_str()); |
| 110 | } |
| 111 | plhs[0] = X; |
| 112 | } |
| 113 | }; |