Scaling method for sparse matrices

The mc29 Module

hsl.scaling.mc29.MC29AD_scale(m, n, a_row, a_col, a_val, b=None)

Use MC29AD for scaling values of A (in place).

Matrix A to scale must be provided in coordinate format

Parameters:
m:number of rows of A
n:number of columns of A
a_row:row indices of non zero elements of A
a_col:column indices of non zero elements of A
a_val:values of non zeros elements of A (in/out)
b:right-hand side (optional)
Returns:

  • row_scale: row scaling factors
  • col_scale: column scaling factors

Return type:

(tuple)

hsl.scaling.mc29.unscale(m, n, row_scale, col_scale, a_row, a_col, a_val, b=None)

Unscale values of A and possibly right-hand side.

Unscaling is performed using user-provided row and column scaling factors.

Matrix A to scale must be provided in coordinate format

Parameters:
m:number of rows of A
n:number of columns of A
row_scale:row scaling factors
col_scale:column scaling factors
a_row:row indices of non zero elements of A
a_col:column indices of non zero elements of A
a_val:values of non zeros elements of A (in/out)
b:right-hand side (optional)

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Exemple from MC29 spec sheet: http://www.hsl.rl.ac.uk/specs/mc29.pdf

IN PLACE scaling.
"""

from hsl.scaling.mc29 import MC29AD_scale, unscale
import numpy as np

irow = np.array([3, 0, 3, 1, 2, 2], dtype=np.int32)
jcol = np.array([2, 0, 1, 1, 0, 2], dtype=np.int32)
values = np.array([16000, 100, 14000, 6, 900, 110000], dtype=np.float64)
print 'orignal values:'
print '%2s % 2s %6s' % ('i', 'j', 'val')
for i in range(0, len(values)):
    print '%2d % 2d %6.5e' % (irow[i], jcol[i], values[i])

# Obtain row and column scaling
row_scale, col_scale = MC29AD_scale(4, 3, irow, jcol, values)

print '\nrow scaling factors:'
print row_scale

print '\ncolumn scaling factors:'
print col_scale

print '\nscaled values:'
print '%2s % 2s %6s' % ('i', 'j', 'val')
for i in range(0, len(values)):
    print '%2d % 2d %6.5e' % (irow[i], jcol[i], values[i])


unscale(4, 3, row_scale, col_scale, irow, jcol, values)
print '\nunscaled values:'
print '%2s % 2s %6s' % ('i', 'j', 'val')
for i in range(0, len(values)):
    print '%2d % 2d %6.5e' % (irow[i], jcol[i], values[i])

Output

orignal values:
 i  j    val
 3  2 1.60000e+04
 0  0 1.00000e+02
 3  1 1.40000e+04
 1  1 6.00000e+00
 2  0 9.00000e+02
 2  2 1.10000e+05

row scaling factors:
[  2.17526613e+00   5.55712076e+02   7.35847733e-02   3.90868730e-01]

column scaling factors:
[ 0.0083316   0.00023411  0.00014055]

scaled values:
 i  j    val
 3  2 8.78992e-01
 0  0 1.81235e+00
 3  1 1.28109e+00
 1  1 7.80587e-01
 2  0 5.51771e-01
 2  2 1.13767e+00

unscaled values:
 i  j    val
 3  2 1.60000e+04
 0  0 1.00000e+02
 3  1 1.40000e+04
 1  1 6.00000e+00
 2  0 9.00000e+02
 2  2 1.10000e+05