Amesos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
amesos_klu_scale.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /* === KLU_scale ============================================================ */
3 /* ========================================================================== */
4 
5 /* Scale a matrix and check to see if it is valid. Can be called by the user.
6  * This is called by KLU_factor and KLU_refactor. Returns TRUE if the input
7  * matrix is valid, FALSE otherwise. If the W input argument is non-NULL,
8  * then the input matrix is checked for duplicate entries.
9  *
10  * scaling methods:
11  * <0: no scaling, do not compute Rs, and do not check input matrix.
12  * 0: no scaling
13  * 1: the scale factor for row i is sum (abs (A (i,:)))
14  * 2 or more: the scale factor for row i is max (abs (A (i,:)))
15  */
16 
17 #include "amesos_klu_internal.h"
18 
19 Int KLU_scale /* return TRUE if successful, FALSE otherwise */
20 (
21  /* inputs, not modified */
22  Int scale, /* 0: none, 1: sum, 2: max */
23  Int n,
24  Int Ap [ ], /* size n+1, column pointers */
25  Int Ai [ ], /* size nz, row indices */
26  double Ax [ ],
27  /* outputs, not defined on input */
28  double Rs [ ], /* size n, can be NULL if scale <= 0 */
29  /* workspace, not defined on input or output */
30  Int W [ ], /* size n, can be NULL */
31  /* --------------- */
32  KLU_common *Common
33 )
34 {
35  double a ;
36  Entry *Az ;
37  Int row, col, p, pend, check_duplicates ;
38 
39  /* ---------------------------------------------------------------------- */
40  /* check inputs */
41  /* ---------------------------------------------------------------------- */
42 
43  if (Common == NULL)
44  {
45  return (FALSE) ;
46  }
47  Common->status = KLU_OK ;
48 
49  if (scale < 0)
50  {
51  /* return without checking anything and without computing the
52  * scale factors */
53  return (TRUE) ;
54  }
55 
56  Az = (Entry *) Ax ;
57 
58  if (n <= 0 || Ap == NULL || Ai == NULL || Az == NULL ||
59  (scale > 0 && Rs == NULL))
60  {
61  /* Ap, Ai, Ax and Rs must be present, and n must be > 0 */
62  Common->status = KLU_INVALID ;
63  return (FALSE) ;
64  }
65  if (Ap [0] != 0 || Ap [n] < 0)
66  {
67  /* nz = Ap [n] must be >= 0 and Ap [0] must equal zero */
68  Common->status = KLU_INVALID ;
69  return (FALSE) ;
70  }
71  for (col = 0 ; col < n ; col++)
72  {
73  if (Ap [col] > Ap [col+1])
74  {
75  /* column pointers must be non-decreasing */
76  Common->status = KLU_INVALID ;
77  return (FALSE) ;
78  }
79  }
80 
81  /* ---------------------------------------------------------------------- */
82  /* scale */
83  /* ---------------------------------------------------------------------- */
84 
85  if (scale > 0)
86  {
87  /* initialize row sum or row max */
88  for (row = 0 ; row < n ; row++)
89  {
90  Rs [row] = 0 ;
91  }
92  }
93 
94  /* check for duplicates only if W is present */
95  check_duplicates = (W != (Int *) NULL) ;
96  if (check_duplicates)
97  {
98  for (row = 0 ; row < n ; row++)
99  {
100  W [row] = EMPTY ;
101  }
102  }
103 
104  for (col = 0 ; col < n ; col++)
105  {
106  pend = Ap [col+1] ;
107  for (p = Ap [col] ; p < pend ; p++)
108  {
109  row = Ai [p] ;
110  if (row < 0 || row >= n)
111  {
112  /* row index out of range, or duplicate entry */
113  Common->status = KLU_INVALID ;
114  return (FALSE) ;
115  }
116  if (check_duplicates)
117  {
118  if (W [row] == col)
119  {
120  /* duplicate entry */
121  Common->status = KLU_INVALID ;
122  return (FALSE) ;
123  }
124  /* flag row i as appearing in column col */
125  W [row] = col ;
126  }
127  /* a = ABS (Az [p]) ;*/
128  ABS (a, Az [p]) ;
129  if (scale == 1)
130  {
131  /* accumulate the abs. row sum */
132  Rs [row] += a ;
133  }
134  else if (scale > 1)
135  {
136  /* find the max abs. value in the row */
137  Rs [row] = MAX (Rs [row], a) ;
138  }
139  }
140  }
141 
142  if (scale > 0)
143  {
144  /* do not scale empty rows */
145  for (row = 0 ; row < n ; row++)
146  {
147  /* matrix is singular */
148  PRINTF (("Rs [%d] = %g\n", row, Rs [row])) ;
149 
150  if (Rs [row] == 0.0)
151  {
152  PRINTF (("Row %d of A is all zero\n", row)) ;
153  Rs [row] = 1.0 ;
154  }
155  }
156  }
157 
158  return (TRUE) ;
159 }
#define KLU_INVALID
#define EMPTY
#define Int
#define FALSE
#define MAX(a, b)
#define NULL
#define KLU_common
Int KLU_scale(Int scale, Int n, Int Ap[], Int Ai[], double Ax[], double Rs[], Int W[], KLU_common *Common)
#define Entry
#define PRINTF(params)
int n
#define TRUE
#define ABS(s, a)
#define KLU_OK