8000 cyma57 Done · PythonOptimizers/HSL.py@54ed2d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 54ed2d0

Browse files
committed
cyma57 Done
1 parent b8aeaef commit 54ed2d0

File tree

16 files changed

+255
-128
lines changed

16 files changed

+255
-128
lines changed

config/setup.cpy

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ if build_cysparse_ext:
251251
{% for element_type in type_list %}
252252
retval = os.getcwd()
253253
os.chdir('hsl/solvers/src')
254-
call(['cython', '-I', cysparse_rootdir[0], '_cyma57_cysparse_@index_type@_@element_type@'])
254+
call(['cython', '-I', cysparse_rootdir[0], '_cyma57_cysparse_@index_type@_@element_type@.pyx'])
255255
os.chdir(retval)
256256
cysparse_ext_params_@index_type@_@element_type@ = copy.deepcopy(ext_params)
257257
cysparse_ext_params_@index_type@_@element_type@['include_dirs'].extend(cysparse_rootdir)
@@ -262,8 +262,7 @@ if build_cysparse_ext:
262262
{% endfor %}
263263

264264

265-
packages_list = ['hsl', 'hsl.ordering', 'hsl.ordering.src',
266-
'hsl.scaling', 'hsl.scaling.src', 'hsl.solvers',
265+
packages_list = ['hsl', 'hsl.ordering', 'hsl.scaling', 'hsl.solvers',
267266
'hsl.solvers.src']
268267

269268

@@ -308,4 +307,4 @@ with open(path.join(here, 'DESCRIPTION.rst'), encoding='utf-8') as f:
308307

309308
config.make_config_py()
310309

311-
setup(zip_safe=False, **config.todict())
310+
setup(packages=packages_list, zip_safe=False, **config.todict())

config/setup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def files_exist(file_list):
245245
if build_cysparse_ext:
246246
retval = os.getcwd()
247247
os.chdir('hsl/solvers/src')
248-
call(['cython', '-I', cysparse_rootdir[0], '_cyma57_cysparse_INT32_FLOAT64'])
248+
call(['cython', '-I', cysparse_rootdir[0], '_cyma57_cysparse_INT32_FLOAT64.pyx'])
249249
os.chdir(retval)
250250
cysparse_ext_params_INT32_FLOAT64 = copy.deepcopy(ext_params)
251251
cysparse_ext_params_INT32_FLOAT64['include_dirs'].extend(cysparse_rootdir)
@@ -254,8 +254,7 @@ def files_exist(file_list):
254254
**cysparse_ext_params_INT32_FLOAT64)
255255

256256

257-
packages_list = ['hsl', 'hsl.ordering', 'hsl.ordering.src',
258-
'hsl.scaling', 'hsl.scaling.src', 'hsl.solvers',
257+
packages_list = ['hsl', 'hsl.ordering', 'hsl.scaling', 'hsl.solvers',
259258
'hsl.solvers.src']
260259

261260

@@ -300,4 +299,4 @@ def files_exist(file_list):
300299

301300
config.make_config_py()
302301

303-
setup(zip_safe=False, **config.todict())
302+
setup(packages=packages_list, zip_safe=False, **config.todict())

doc/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
3232
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest']
3333
extensions += ['sphinx.ext.todo']
34+
extensions += ['sphinx.ext.napoleon']
3435
extensions += ['sphinx.ext.inheritance_diagram']
3536
extensions += ['ipython_console_highlighting']
3637
extensions += ['mathjax']

doc/source/scaling.rst

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ The :mod:`mc29` Module
1212

1313
.. _mc29-section:
1414

15+
.. automodule:: hsl.scaling.mc29
16+
17+
.. autofunction:: MC29AD_scale
18+
19+
.. autofunction:: unscale
20+
1521
Example
16-
-------
22+
=======
1723

1824
.. literalinclude:: ../../examples/demo_mc29.py
1925
:linenos:
@@ -23,14 +29,34 @@ Output
2329
.. code-block:: python
2430
2531
orignal values:
26-
[ 1.60000000e+04 1.00000000e+02 1.40000000e+04 6.00000000e+00
27-
9.00000000e+02 1.10000000e+05]
32+
i j val
33+
3 2 1.60000e+04
34+
0 0 1.00000e+02
35+
3 1 1.40000e+04
36+
1 1 6.00000e+00
37+
2 0 9.00000e+02
38+
2 2 1.10000e+05
2839
2940
row scaling factors:
3041
[ 2.17526613e+00 5.55712076e+02 7.35847733e-02 3.90868730e-01]
3142
3243
column scaling factors:
3344
[ 0.0083316 0.00023411 0.00014055]
3445
35-
scaled values
36-
[ 0.87899243 1.81234539 1.28108793 0.78058654 0.55177121 1.13766623]
46+
scaled values:
47+
i j val
48+
3 2 8.78992e-01
49+
0 0 1.81235e+00
50+
3 1 1.28109e+00
51+
1 1 7.80587e-01
52+
2 0 5.51771e-01
53+
2 2 1.13767e+00
54+
55+
unscaled values:
56+
i j val
57+
3 2 1.60000e+04
58+
0 0 1.00000e+02
59+
3 1 1.40000e+04
60+
1 1 6.00000e+00
61+
2 0 9.00000e+02
62+
2 2 1.10000e+05

doc/source/solvers.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ The :mod:`pyma27` Module
1717
:undoc-members:
1818

1919

20-
The :mod:`pyma57` Module
20+
The :mod:`ma57` Module
2121
========================
2222

23-
.. automodule:: hsl.solvers.pyma57
24-
25-
.. autoclass:: PyMa57Solver
26-
:show-inheritance:
27-
:members:
28-
:inherited-members:
29-
:undoc-members:
23+
Cython module no doc ...
3024

3125

3226

3327
Example
3428
=======
3529

30+
.. literalinclude:: ../../examples/demo_cyma57_numpy.py
31+
:linenos:
32+
33+
.. literalinclude:: ../../examples/demo_cyma57_cysparse.py
34+
:linenos:
35+
36+
TO BE CHANGED
3637
.. literalinclude:: ../../examples/demo_sils.py
3738
:linenos:

examples/demo_cyma57_cysparse.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,29 @@
1919

2020
context = CySparseMA57Solver_INT32_FLOAT64(A.nrow, A.ncol, A.nnz)
2121
context.get_matrix_data(A)
22+
2223
context.analyze()
23-
context.factorize()
2424

25-
print 'Solve:'
26-
residual = np.zeros(n, 'd')
27-
x = context.solve(rhs, residual, False)
28-
print ' x:'
29-
print x
25+
context.factorize()
3026

3127
print 'Fetch_perm:'
3228
perm = context.fetch_perm()
3329
print ' perm:'
3430
print perm
3531

32+
33+
print 'Solve:'
34+
x, residual = context.solve(rhs, True)
35+
print ' x:'
36+
print x
37+
print ' residual:'
38+
print residual
39+
3640
print 'Refine:'
37-
(new_x, new_rhs) = context.refine(x, rhs, 5)
41+
(new_x, new_res) = context.refine(x, rhs, residual, 5)
3842
print ' cond: ', context.cond
3943
print ' new_x: '
4044
print new_x
41-
print x
42-
print ' new_rhs: '
43-
print new_rhs
44-
print rhs
45+
print ' new_res: '
46+
print new_res
4547

examples/demo_cyma57_numpy.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Exemple from MA57 spec sheet: http://www.hsl.rl.ac.uk/specs/ma57.pdf
3+
"""
4+
15
import numpy as np
26

37
from hsl.solvers.src._cyma57_numpy_INT32_FLOAT64 import NumpyMA57Solver_INT32_FLOAT64
@@ -21,23 +25,22 @@
2125
context.factorize()
2226

2327
print 'Solve:'
24-
residual = np.zeros(n, 'd')
25-
x = context.solve(rhs, residual, False)
28+
x, residual = context.solve(rhs, True)
29+
# x = context.solve(rhs, False)
2630
print ' x:'
2731
print x
32+
print ' residual:'
33+
print residual
2834

2935
print 'Fetch_perm:'
3036
perm = context.fetch_perm()
3137
print ' perm:'
3238
print perm
3339

3440
print 'Refine:'
35-
(new_x, new_rhs) = context.refine(x, rhs, 5)
36-
print ' cond: ', context.cond
41+
(new_x, new_res) = context.refine(x, rhs, residual, 5)
3742
print ' new_x: '
3843
print new_x
39-
print x
40-
print ' new_rhs: '
41-
print new_rhs
42-
print rhs
44+
print ' new_res: '
45+
print new_res
4346

examples/demo_mc29.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
1-
"Exemple from MC29 spec sheet: http://www.hsl.rl.ac.uk/specs/mc29.pdf"
1+
"""
2+
Exemple from MC29 spec sheet: http://www.hsl.rl.ac.uk/specs/mc29.pdf"
23
3-
from hsl.scaling._mc29 import mc29ad
4+
IN PLACE scaling.
5+
"""
6+
7+
from hsl.scaling.mc29 import MC29AD_scale, unscale
48
import numpy as np
59

6-
irow = np.array([3,0,3,1,2,2], dtype=np.int32)
7-
jcol = np.array([2,0,1,1,0,2], dtype=np.int32)
10+
irow = np.array([3, 0, 3, 1, 2, 2], dtype=np.int32)
11+
jcol = np.array([2, 0, 1, 1, 0, 2], dtype=np.int32)
812
values = np.array([16000, 100, 14000, 6, 900, 110000], dtype=np.float64)
913
print 'orignal values:'
10-
print values
14+
print '%2s % 2s %6s'%('i', 'j', 'val')
15+
for i in range(0, len(values)):
16+
print '%2d % 2d %6.5e'%(irow[i], jcol[i], values[i])
1117

1218
# Obtain row and column scaling
13-
row_scale, col_scale, ifail = mc29ad(4, 3, values, irow+1, jcol+1)
14-
15-
if ifail==-1:
16-
raise ValueError("m < 1 or n < 1")
17-
elif ifail==-2:
18-
raise ValueError("number of non zero elemnts < 1")
19-
20-
# row_scale and col_scale contain in fact the logarithms of the
21-
# scaling factors.
22-
row_scale = np.exp(row_scale)
23-
col_scale = np.exp(col_scale)
19+
row_scale, col_scale = MC29AD_scale(4, 3, irow, jcol, values)
2420

2521
print '\nrow scaling factors:'
2622
print row_scale
2723

2824
print '\ncolumn scaling factors:'
2925
print col_scale
3026

31-
# Apply row and column scaling to constraint matrix A.
32-
values *= row_scale[irow]
33-
values *= col_scale[jcol]
27+
print '\nscaled values:'
28+
print '%2s % 2s %6s'%('i', 'j', 'val')
29+
for i in range(0, len(values)):
30+
print '%2d % 2d %6.5e'%(irow[i], jcol[i], values[i])
31+
3432

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

hsl/scaling/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
from hsl.scaling._mc29 import mc29ad
2-

hsl/scaling/mc29.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Matrix scaling module.
2+
3+
from hsl.scaling._mc29 import mc29ad
4+
import numpy as np
5+
6+
7+
def MC29AD_scale(m, n, a_row, a_col, a_val, b=None):
8+
"""
9+
Use MC29AD for scaling values of A (in place).
10+
11+
Matrix A to scale must be provided in coordinate format
12+
13+
Args:
14+
m: number of rows of A
15+
n: number of columns of A
16+
a_row: row indices of non zero elements of A
17+
a_col: column indices of non zero elements of A
18+
a_val: values of non zeros elements of A (in/out)
19+
b: right-hand side (optional)
20+
21+
Returns:
22+
(tuple):
23+
* row_scale: row scaling factors
24+
* col_scale: column scaling factors
25+
"""
26+
# Obtain row and column scaling
27+
# We need to add one to row and column indices to comply to Fortran
28+
# format.
29+
row_scale, col_scale, ifail = mc29ad(m, n, a_val, a_row+1, a_col+1)
30+
31+
if ifail == -1:
32+
raise ValueError("Number of rows < 1 or number of columns < 1")
33+
elif ifail == -2:
34+
raise ValueError("Number of non zero elements < 1")
35+
36+
# row_scale and col_scale contain in fact the logarithms of the
37+
# scaling factors.
38+
row_scale = np.exp(row_scale)
39+
col_scale = np.exp(col_scale)
40+
41+
# Apply row and column scaling to constraint matrix A.
42+
a_val *= row_scale[a_row]
43+
a_val *= col_scale[a_col]
44+
45+
if b is not None:
46+
# Apply row scaling to right-hand side b.
47+
b *= row_scale
48+
49+
return (row_scale, col_scale)
50+
51+
52+
def unscale(m, n, row_scale, col_scale, a_row, a_col, a_val, b=None):
53+
"""
54+
Unscale values of A and possibly right-hand side using user-provided
55+
row and column scaling factors.
56+
57+
Matrix A to scale must be provided in coordinate format
58+
59+
Args:
60+
m: number of rows of A
61+
n: number of columns of A
62+
row_scale: row scaling factors
63+
col_scale: column scaling factors
64+
a_row: row indices of non zero elements of A
65+
a_col: column indices of non zero elements of A
66+
a_val: values of non zeros elements of A (in/out)
67+
b: right-hand side (optional)
68+
"""
69+
# Unscale values of matrix A.
70+
a_val /= row_scale[a_row]
71+
a_val /= col_scale[a_col]
72+
73+
if b is not None:
74+
# Unscale right-hand side b.
75+
b /= row_scale
76+
77+
return

0 commit comments

Comments
 (0)
0