solve.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. /* solves the system ab=c using gauss reduction */
  11. #include <assert.h>
  12. #include <common/render.h>
  13. #include <math.h>
  14. #include <neatogen/neatoprocs.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <util/alloc.h>
  18. static void swap(double *x, double *y) {
  19. const double temp = *x;
  20. *x = *y;
  21. *y = temp;
  22. }
  23. void solve(double *a, double *b, double *c, size_t n) { // a[n][n],b[n],c[n]
  24. assert(n >= 2);
  25. const size_t nsq = n * n;
  26. double *asave = gv_calloc(nsq, sizeof(double));
  27. double *csave = gv_calloc(n, sizeof(double));
  28. for (size_t i = 0; i < n; i++)
  29. csave[i] = c[i];
  30. for (size_t i = 0; i < nsq; i++)
  31. asave[i] = a[i];
  32. /* eliminate ith unknown */
  33. const size_t nm = n - 1;
  34. for (size_t i = 0; i < nm; i++) {
  35. /* find largest pivot */
  36. double amax = 0.;
  37. size_t istar = 0;
  38. for (size_t ii = i; ii < n; ii++) {
  39. const double dum = fabs(a[ii * n + i]);
  40. if (dum < amax)
  41. continue;
  42. istar = ii;
  43. amax = dum;
  44. }
  45. /* return if pivot is too small */
  46. if (amax < 1.e-10)
  47. goto bad;
  48. /* switch rows */
  49. for (size_t j = i; j < n; j++) {
  50. const size_t t = istar * n + j;
  51. swap(&a[t], &a[i * n + j]);
  52. }
  53. swap(&c[istar], &c[i]);
  54. /*pivot */
  55. const size_t ip = i + 1;
  56. for (size_t ii = ip; ii < n; ii++) {
  57. const double pivot = a[ii * n + i] / a[i * n + i];
  58. c[ii] -= pivot * c[i];
  59. for (size_t j = 0; j < n; j++)
  60. a[ii * n + j] = a[ii * n + j] - pivot * a[i * n + j];
  61. }
  62. }
  63. /* return if last pivot is too small */
  64. if (fabs(a[n * n - 1]) < 1.e-10)
  65. goto bad;
  66. b[n - 1] = c[n - 1] / a[n * n - 1];
  67. /* back substitute */
  68. for (size_t k = 0; k < nm; k++) {
  69. const size_t m = n - k - 2;
  70. b[m] = c[m];
  71. const size_t mp = m + 1;
  72. for (size_t j = mp; j < n; j++)
  73. b[m] -= a[m * n + j] * b[j];
  74. b[m] /= a[m * n + m];
  75. }
  76. /* restore original a,c */
  77. for (size_t i = 0; i < n; i++)
  78. c[i] = csave[i];
  79. for (size_t i = 0; i < nsq; i++)
  80. a[i] = asave[i];
  81. free(asave);
  82. free(csave);
  83. return;
  84. bad:
  85. printf("ill-conditioned\n");
  86. free(asave);
  87. free(csave);
  88. return;
  89. }