mjdgges.c 4.2 KB
Newer Older
sebastien's avatar
sebastien committed
1
/*
2
 * Copyright (C) 2006-2011 Dynare Team
sebastien's avatar
sebastien committed
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
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include <dynmex.h>
#include <dynlapack.h>

double criterium;

lapack_int
my_criteria(const double *alphar, const double *alphai, const double *beta)
{
  return ((*alphar * *alphar + *alphai * *alphai) < criterium * *beta * *beta);
}

void
mjdgges(double *a, double *b, double *z, double *n, double *sdim, double *eval_r, double *eval_i, double *info)
{
  lapack_int i_n, i_info, i_sdim, one, lwork;
  double *alphar, *alphai, *beta, *work, *par, *pai, *pb, *per, *pei;
  double *junk;
  lapack_int *bwork;

  one = 1;
  i_n = (lapack_int)*n;
  alphar = mxCalloc(i_n, sizeof(double));
  alphai = mxCalloc(i_n, sizeof(double));
  beta = mxCalloc(i_n, sizeof(double));
  lwork = 16*i_n+16;
  work = mxCalloc(lwork, sizeof(double));
  bwork = mxCalloc(i_n, sizeof(lapack_int));
  /* made necessary by bug in Lapack */
  junk = mxCalloc(i_n*i_n, sizeof(double));

  dgges("N", "V", "S", my_criteria, &i_n, a, &i_n, b, &i_n, &i_sdim, alphar, alphai, beta, junk, &i_n, z, &i_n, work, &lwork, bwork, &i_info);

  *sdim = i_sdim;
  *info = i_info;

  par = alphar;
  pai = alphai;
  pb = beta;
  pei = eval_i;
  for (per = eval_r; per <= &eval_r[i_n-1]; ++per)
    {
63
64
65
66
67
68
69
70
71
      if ((fabs(*par) > 1e-6) || (fabs(*pb) > 1e-6)) 
	*per = *par / *pb;
      else
	{
	  /* the ratio is too close to 0/0;
	     returns specific error number only if no other error */
	  if (i_info == 0)
	    *info = -30;
	}
72
      if (*pai == 0.0 && *pb == 0.0)
73
        *pei = 0.0;
74
      else
75
        *pei = *pai / *pb;
sebastien's avatar
sebastien committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
      ++par;
      ++pai;
      ++pb;
      ++pei;
    }
}

/* MATLAB interface */
void
mexFunction(int nlhs, mxArray *plhs[],
            int nrhs, const mxArray *prhs[])

{
  unsigned int m1, n1, m2, n2;
  double *s, *t, *z, *sdim, *eval_r, *eval_i, *info, *a, *b;
  double n;

  /* Check for proper number of arguments */

95
96
  if (nrhs < 2 || nrhs > 3 || nlhs == 0 || nlhs > 7)
    DYN_MEX_FUNC_ERR_MSG_TXT("MJDGGES: takes 2 or 3 input arguments and between 1 and 7 output arguments.");
sebastien's avatar
sebastien committed
97
98
99
100
101
102
103
104
105
106

  /* Check that A and B are real matrices of the same dimension.*/

  m1 = mxGetM(prhs[0]);
  n1 = mxGetN(prhs[0]);
  m2 = mxGetM(prhs[1]);
  n2 = mxGetN(prhs[1]);
  if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0])
      || !mxIsDouble(prhs[1]) || mxIsComplex(prhs[1])
      || (m1 != n1) || (m2 != n1) || (m2 != n2))
107
    DYN_MEX_FUNC_ERR_MSG_TXT("MJDGGES requires two square real matrices of the same dimension.");
sebastien's avatar
sebastien committed
108
109
110
111

  /* Create a matrix for the return argument */
  plhs[1] = mxCreateDoubleMatrix(n1, n1, mxREAL);
  plhs[2] = mxCreateDoubleMatrix(n1, n1, mxREAL);
112
113
114
115
  plhs[3] = mxCreateDoubleMatrix(n1, n1, mxREAL);
  plhs[4] = mxCreateDoubleMatrix(1, 1, mxREAL);
  plhs[5] = mxCreateDoubleMatrix(n1, 1, mxCOMPLEX);
  plhs[6] = mxCreateDoubleMatrix(1, 1, mxREAL);
sebastien's avatar
sebastien committed
116
117

  /* Assign pointers to the various parameters */
118
119
120
121
122
123
124
  s = mxGetPr(plhs[1]);
  t = mxGetPr(plhs[2]);
  z = mxGetPr(plhs[3]);
  sdim = mxGetPr(plhs[4]);
  eval_r = mxGetPr(plhs[5]);
  eval_i = mxGetPi(plhs[5]);
  info = mxGetPr(plhs[6]);
sebastien's avatar
sebastien committed
125
126
127
128
129

  a = mxGetPr(prhs[0]);
  b = mxGetPr(prhs[1]);

  /* set criterium for stable eigenvalues */
130
  if (nrhs == 3 && mxGetM(prhs[2]) > 0)
sebastien's avatar
sebastien committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    {
      criterium = *mxGetPr(prhs[2]);
    }
  else
    {
      criterium = 1+1e-6;
    }

  /* keep a and b intact */
  memcpy(s, a, sizeof(double)*n1*n1);
  memcpy(t, b, sizeof(double)*n1*n1);

  n = n1;

  /* Do the actual computations in a subroutine */
  mjdgges(s, t, z, &n, sdim, eval_r, eval_i, info);

148
  plhs[0] = mxCreateDoubleScalar(0);
sebastien's avatar
sebastien committed
149
150
151
152
153
154
}

/*
  07/30/03 MJ added user set criterium for stable eigenvalues
  corrected error messages in mexfunction()
*/