SINFONI Pipeline Reference Manual  2.6.0
sinfo_balance.c
1 /*
2  * This file is part of the ESO SINFONI Pipeline
3  * Copyright (C) 2004,2005 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18  */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include "sinfo_solve_poly_root.h"
23 
24 
25 #define RADIX 2
26 #define RADIX2 (RADIX*RADIX)
27 
42 void
43 sinfo_balance_companion_matrix (double *m, size_t nc)
44 {
45  int not_converged = 1;
46 
47  double row_norm = 0;
48  double col_norm = 0;
49 
50  while (not_converged)
51  {
52  size_t i, j;
53  double g, f, s;
54 
55  not_converged = 0;
56 
57  for (i = 0; i < nc; i++)
58  {
59  /* column norm, excluding the diagonal */
60 
61  if (i != nc - 1)
62  {
63  col_norm = fabs (MAT (m, i + 1, i, nc));
64  }
65  else
66  {
67  col_norm = 0;
68 
69  for (j = 0; j < nc - 1; j++)
70  {
71  col_norm += fabs (MAT (m, j, nc - 1, nc));
72  }
73  }
74 
75  /* row norm, excluding the diagonal */
76 
77  if (i == 0)
78  {
79  row_norm = fabs (MAT (m, 0, nc - 1, nc));
80  }
81  else if (i == nc - 1)
82  {
83  row_norm = fabs (MAT (m, i, i - 1, nc));
84  }
85  else
86  {
87  row_norm = (fabs (MAT (m, i, i - 1, nc))
88  + fabs (MAT (m, i, nc - 1, nc)));
89  }
90 
91  if (col_norm == 0 || row_norm == 0)
92  {
93  continue;
94  }
95 
96  g = row_norm / RADIX;
97  f = 1;
98  s = col_norm + row_norm;
99 
100  while (col_norm < g)
101  {
102  f *= RADIX;
103  col_norm *= RADIX2;
104  }
105 
106  g = row_norm * RADIX;
107 
108  while (col_norm > g)
109  {
110  f /= RADIX;
111  col_norm /= RADIX2;
112  }
113 
114  if ((row_norm + col_norm) < 0.95 * s * f)
115  {
116  not_converged = 1;
117 
118  g = 1 / f;
119 
120  if (i == 0)
121  {
122  MAT (m, 0, nc - 1, nc) *= g;
123  }
124  else
125  {
126  MAT (m, i, i - 1, nc) *= g;
127  MAT (m, i, nc - 1, nc) *= g;
128  }
129 
130  if (i == nc - 1)
131  {
132  for (j = 0; j < nc; j++)
133  {
134  MAT (m, j, i, nc) *= f;
135  }
136  }
137  else
138  {
139  MAT (m, i + 1, i, nc) *= f;
140  }
141  }
142  }
143  }
144 }