Amesos Package Browser (Single Doxygen Collection)
Development
src
SuiteSparse
KLU
Source
amesos_klu_l_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
/* This file should make the long int version of KLU */
18
#define DLONG 1
19
20
#include "
amesos_klu_internal.h
"
21
22
Int
KLU_scale
/* return TRUE if successful, FALSE otherwise */
23
(
24
/* inputs, not modified */
25
Int
scale
,
/* 0: none, 1: sum, 2: max */
26
Int
n
,
27
Int
Ap [ ],
/* size n+1, column pointers */
28
Int
Ai [ ],
/* size nz, row indices */
29
double
Ax [ ],
30
/* outputs, not defined on input */
31
double
Rs [ ],
/* size n, can be NULL if scale <= 0 */
32
/* workspace, not defined on input or output */
33
Int
W [ ],
/* size n, can be NULL */
34
/* --------------- */
35
KLU_common
*
Common
36
)
37
{
38
double
a ;
39
Entry
*Az ;
40
Int
row, col, p, pend, check_duplicates ;
41
42
/* ---------------------------------------------------------------------- */
43
/* check inputs */
44
/* ---------------------------------------------------------------------- */
45
46
if
(
Common
==
NULL
)
47
{
48
return
(
FALSE
) ;
49
}
50
Common
->status =
KLU_OK
;
51
52
if
(
scale
< 0)
53
{
54
/* return without checking anything and without computing the
55
* scale factors */
56
return
(
TRUE
) ;
57
}
58
59
Az = (
Entry
*) Ax ;
60
61
if
(
n
<= 0 || Ap ==
NULL
|| Ai ==
NULL
|| Az ==
NULL
||
62
(
scale
> 0 && Rs ==
NULL
))
63
{
64
/* Ap, Ai, Ax and Rs must be present, and n must be > 0 */
65
Common
->status =
KLU_INVALID
;
66
return
(
FALSE
) ;
67
}
68
if
(Ap [0] != 0 || Ap [
n
] < 0)
69
{
70
/* nz = Ap [n] must be >= 0 and Ap [0] must equal zero */
71
Common
->status =
KLU_INVALID
;
72
return
(
FALSE
) ;
73
}
74
for
(col = 0 ; col <
n
; col++)
75
{
76
if
(Ap [col] > Ap [col+1])
77
{
78
/* column pointers must be non-decreasing */
79
Common
->status =
KLU_INVALID
;
80
return
(
FALSE
) ;
81
}
82
}
83
84
/* ---------------------------------------------------------------------- */
85
/* scale */
86
/* ---------------------------------------------------------------------- */
87
88
if
(
scale
> 0)
89
{
90
/* initialize row sum or row max */
91
for
(row = 0 ; row <
n
; row++)
92
{
93
Rs [row] = 0 ;
94
}
95
}
96
97
/* check for duplicates only if W is present */
98
check_duplicates = (W != (
Int
*)
NULL
) ;
99
if
(check_duplicates)
100
{
101
for
(row = 0 ; row <
n
; row++)
102
{
103
W [row] =
EMPTY
;
104
}
105
}
106
107
for
(col = 0 ; col <
n
; col++)
108
{
109
pend = Ap [col+1] ;
110
for
(p = Ap [col] ; p < pend ; p++)
111
{
112
row = Ai [p] ;
113
if
(row < 0 || row >=
n
)
114
{
115
/* row index out of range, or duplicate entry */
116
Common
->status =
KLU_INVALID
;
117
return
(
FALSE
) ;
118
}
119
if
(check_duplicates)
120
{
121
if
(W [row] == col)
122
{
123
/* duplicate entry */
124
Common
->status =
KLU_INVALID
;
125
return
(
FALSE
) ;
126
}
127
/* flag row i as appearing in column col */
128
W [row] = col ;
129
}
130
/* a = ABS (Az [p]) ;*/
131
ABS
(a, Az [p]) ;
132
if
(
scale
== 1)
133
{
134
/* accumulate the abs. row sum */
135
Rs [row] += a ;
136
}
137
else
if
(
scale
> 1)
138
{
139
/* find the max abs. value in the row */
140
Rs [row] =
MAX
(Rs [row], a) ;
141
}
142
}
143
}
144
145
if
(
scale
> 0)
146
{
147
/* do not scale empty rows */
148
for
(row = 0 ; row <
n
; row++)
149
{
150
/* matrix is singular */
151
PRINTF
((
"Rs [%d] = %g\n"
, row, Rs [row])) ;
152
153
if
(Rs [row] == 0.0)
154
{
155
PRINTF
((
"Row %d of A is all zero\n"
, row)) ;
156
Rs [row] = 1.0 ;
157
}
158
}
159
}
160
161
return
(
TRUE
) ;
162
}
Common
KLU_INVALID
#define KLU_INVALID
Definition:
amesos_klu_decl.h:134
EMPTY
#define EMPTY
Definition:
amesos_amd_internal.h:144
Int
#define Int
Definition:
amesos_amd_internal.h:190
KLU_scale
Int KLU_scale(Int scale, Int n, Int Ap [], Int Ai [], double Ax [], double Rs [], Int W [], KLU_common *Common)
Definition:
amesos_klu_l_scale.c:23
FALSE
#define FALSE
Definition:
amesos_amd_internal.h:141
amesos_klu_internal.h
MAX
#define MAX(a, b)
Definition:
amesos_amd_internal.h:125
NULL
#define NULL
Definition:
amesos_amd_internal.h:153
klu_l_common_struct::scale
UF_long scale
Definition:
amesos_klu_decl.h:219
KLU_common
#define KLU_common
Definition:
amesos_klu_version.h:182
Entry
#define Entry
Definition:
amesos_klu_version.h:254
PRINTF
#define PRINTF(params)
Definition:
amesos_amd_internal.h:216
n
int n
TRUE
#define TRUE
Definition:
amesos_amd_internal.h:140
ABS
#define ABS(s, a)
Definition:
amesos_klu_version.h:279
KLU_OK
#define KLU_OK
Definition:
amesos_klu_decl.h:131
Generated by
1.8.14