evmax.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* evmax.c CCMATH mathematics library source code.
  2. *
  3. * Copyright (C) 2000 Daniel A. Atkinson All rights reserved.
  4. * This code may be redistributed under the terms of the GNU library
  5. * public license (LGPL). ( See the lgpl.license file for details.)
  6. * ------------------------------------------------------------------------
  7. */
  8. #include <stdlib.h>
  9. #include "ccmath.h"
  10. double evmax(double *a, double *u, int n)
  11. {
  12. double *p, *q, *qm, *r, *s, *t;
  13. double ev, evm, c, h;
  14. int kc;
  15. q = (double *)calloc(n, sizeof(double));
  16. qm = q + n;
  17. *(qm - 1) = 1.;
  18. ev = 0.;
  19. for (kc = 0; kc < 200; ++kc) {
  20. h = c = 0.;
  21. evm = ev;
  22. for (p = u, r = a, s = q; s < qm;) {
  23. *p = 0.;
  24. for (t = q; t < qm;)
  25. *p += *r++ * *t++;
  26. c += *p * *p;
  27. h += *p++ * *s++;
  28. }
  29. ev = c / h;
  30. c = sqrt(c);
  31. for (p = u, s = q; s < qm;) {
  32. *p /= c;
  33. *s++ = *p++;
  34. }
  35. if (((c = ev - evm) < 0. ? -c : c) < 1.e-16 * (ev < 0. ? -ev : ev)) {
  36. free(q);
  37. return ev;
  38. }
  39. }
  40. free(q);
  41. for (kc = 0; kc < n;)
  42. u[kc++] = 0.;
  43. return 0.;
  44. }