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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
Copyright (C) 1996, 1997 John W. Eaton
This file is part of Octave.
Octave 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 2, or (at your option) any
later version.
Octave 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 Octave; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if defined (__GNUG__)
#pragma implementation
#endif
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "EIG.h"
#include "dColVector.h"
#include "f77-fcn.h"
#include "lo-error.h"
extern "C"
{
int F77_FCN (dgeev, DGEEV) (const char*, const char*, const int&,
double*, const int&, double*, double*,
double*, const int&, double*,
const int&, double*, const int&, int&,
long, long);
int F77_FCN (zgeev, ZGEEV) (const char*, const char*, const int&,
Complex*, const int&, Complex*,
Complex*, const int&, Complex*,
const int&, Complex*, const int&,
double*, int&, long, long);
int F77_FCN (dsyev, DSYEV) (const char*, const char*, const int&,
double*, const int&, double*, double*,
const int&, int&, long, long);
int F77_FCN (zheev, ZHEEV) (const char*, const char*, const int&,
Complex*, const int&, double*, Complex*,
const int&, double*, int&, long, long);
}
static bool
is_hermitian (const ComplexMatrix m)
{
int nr = m.rows ();
int nc = m.cols ();
if (nr > 0 && nr == nc)
{
for (int i = 0; i < nr; i++)
for (int j = i; j < nc; j++)
if (m.elem (i, j) != conj (m.elem (j, i)))
return false;
return true;
}
return false;
}
static bool
is_symmetric (const Matrix& m)
{
int nr = m.rows ();
int nc = m.cols ();
if (nr > 0 && nr == nc)
{
for (int i = 0; i < nr; i++)
for (int j = i+1; j < nc; j++)
if (m.elem (i, j) != m.elem (j, i))
return false;
return true;
}
return false;
}
int
EIG::init (const Matrix& a)
{
if (is_symmetric (a))
return symmetric_init (a);
int n = a.rows ();
if (n != a.cols ())
{
(*current_liboctave_error_handler) ("EIG requires square matrix");
return -1;
}
int info = 0;
Matrix atmp = a;
double *tmp_data = atmp.fortran_vec ();
Array<double> wr (n);
double *pwr = wr.fortran_vec ();
Array<double> wi (n);
double *pwi = wi.fortran_vec ();
Matrix vr (n, n);
double *pvr = vr.fortran_vec ();
// XXX FIXME XXX -- it might be possible to choose a better value of
// lwork that would result in more efficient computations.
int lwork = 8*n;
Array<double> work (lwork);
double *pwork = work.fortran_vec ();
double *dummy = 0;
int idummy = 1;
F77_XFCN (dgeev, DGEEV, ("N", "V", n, tmp_data, n, pwr, pwi, dummy,
idummy, pvr, n, pwork, lwork, info, 1L, 1L));
if (f77_exception_encountered || info < 0)
(*current_liboctave_error_handler) ("unrecoverable error in dgeev");
else
{
if (info > 0)
(*current_liboctave_error_handler) ("dgeev failed to converge");
else
{
lambda.resize (n);
v.resize (n, n);
for (int j = 0; j < n; j++)
{
if (wi.elem (j) == 0.0)
{
lambda.elem (j) = Complex (wr.elem (j));
for (int i = 0; i < n; i++)
v.elem (i, j) = vr.elem (i, j);
}
else
{
if (j+1 >= n)
{
(*current_liboctave_error_handler)
("EIG: internal error");
return -1;
}
lambda.elem(j) = Complex (wr.elem(j), wi.elem(j));
lambda.elem(j+1) = Complex (wr.elem(j+1), wi.elem(j+1));
for (int i = 0; i < n; i++)
{
double real_part = vr.elem (i, j);
double imag_part = vr.elem (i, j+1);
v.elem (i, j) = Complex (real_part, imag_part);
v.elem (i, j+1) = Complex (real_part, -imag_part);
}
j++;
}
}
}
}
return info;
}
int
EIG::symmetric_init (const Matrix& a)
{
int n = a.rows ();
if (n != a.cols ())
{
(*current_liboctave_error_handler) ("EIG requires square matrix");
return -1;
}
int info = 0;
Matrix atmp = a;
double *tmp_data = atmp.fortran_vec ();
Array<double> wr (n);
double *pwr = wr.fortran_vec ();
// XXX FIXME XXX -- it might be possible to choose a better value of
// lwork that would result in more efficient computations.
int lwork = 8*n;
Array<double> work (lwork);
double *pwork = work.fortran_vec ();
F77_XFCN (dsyev, DSYEV, ("V", "U", n, tmp_data, n, pwr, pwork,
lwork, info, 1L, 1L));
if (f77_exception_encountered || info < 0)
(*current_liboctave_error_handler) ("unrecoverable error in dsyev");
else
{
if (info > 0)
(*current_liboctave_error_handler) ("dsyev failed to converge");
else
{
lambda.resize (n);
for (int j = 0; j < n; j++)
lambda.elem (j) = Complex (wr.elem (j));
v = atmp;
}
}
return info;
}
int
EIG::init (const ComplexMatrix& a)
{
if (is_hermitian (a))
return hermitian_init (a);
int n = a.rows ();
if (n != a.cols ())
{
(*current_liboctave_error_handler) ("EIG requires square matrix");
return -1;
}
int info = 0;
ComplexMatrix atmp = a;
Complex *tmp_data = atmp.fortran_vec ();
ComplexColumnVector w (n);
Complex *pw = w.fortran_vec ();
ComplexMatrix vtmp (n, n);
Complex *pv = vtmp.fortran_vec ();
// XXX FIXME XXX -- it might be possible to choose a better value of
// lwork that would result in more efficient computations.
int lwork = 8*n;
Array<Complex> work (lwork);
Complex *pwork = work.fortran_vec ();
int lrwork = 2*n;
Array<double> rwork (lrwork);
double *prwork = rwork.fortran_vec ();
Complex *dummy = 0;
int idummy = 1;
F77_XFCN (zgeev, ZGEEV, ("N", "V", n, tmp_data, n, pw, dummy, idummy,
pv, n, pwork, lwork, prwork, info, 1L, 1L));
if (f77_exception_encountered || info < 0)
(*current_liboctave_error_handler) ("unrecoverable error in zgeev");
else if (info > 0)
(*current_liboctave_error_handler) ("zgeev failed to converge");
else
{
lambda = w;
v = vtmp;
}
return info;
}
int
EIG::hermitian_init (const ComplexMatrix& a)
{
int n = a.rows ();
if (n != a.cols ())
{
(*current_liboctave_error_handler) ("EIG requires square matrix");
return -1;
}
int info = 0;
ComplexMatrix atmp = a;
Complex *tmp_data = atmp.fortran_vec ();
ColumnVector w (n);
double *pw = w.fortran_vec ();
// XXX FIXME XXX -- it might be possible to choose a better value of
// lwork that would result in more efficient computations.
int lwork = 8*n;
Array<Complex> work (lwork);
Complex *pwork = work.fortran_vec ();
int lrwork = 3*n;
Array<double> rwork (lrwork);
double *prwork = rwork.fortran_vec ();
F77_XFCN (zheev, ZHEEV, ("V", "U", n, tmp_data, n, pw, pwork,
lwork, prwork, info, 1L, 1L));
if (f77_exception_encountered || info < 0)
(*current_liboctave_error_handler) ("unrecoverable error in zheev");
else if (info > 0)
(*current_liboctave_error_handler) ("zheev failed to converge");
else
{
lambda = w;
v = atmp;
}
return info;
}
/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/
|