File: bench_expm_multiply.py

package info (click to toggle)
python-scipy 0.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 52,228 kB
  • ctags: 63,719
  • sloc: python: 112,726; fortran: 88,685; cpp: 86,979; ansic: 85,860; makefile: 530; sh: 236
file content (80 lines) | stat: -rw-r--r-- 2,620 bytes parent folder | download
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""benchmarks for the scipy.sparse.linalg._expm_multiply module"""
from __future__ import division, print_function, absolute_import

import time

import numpy as np
from numpy.testing import (Tester, TestCase, assert_allclose, run_module_suite)

import scipy.linalg
from scipy.sparse.linalg import expm_multiply


def random_sparse(m, n, nnz_per_row):
    # Copied from the scipy.sparse benchmark.
    rows = np.arange(m).repeat(nnz_per_row)
    cols = np.random.random_integers(low=0, high=n-1, size=nnz_per_row*m)
    vals = np.random.random_sample(m*nnz_per_row)
    M = scipy.sparse.coo_matrix((vals,(rows,cols)), (m,n), dtype=float)
    return M.tocsr()


class BenchmarkExpmMultiply(TestCase):

    def _help_bench_expm_multiply(self, A, i, j):
        n = A.shape[0]
        print('converting the sparse matrix to a dense array...')
        tm_start = time.clock()
        A_dense = A.toarray()
        tm_end = time.clock()
        print(tm_end - tm_start, ' seconds')
        print()
        print('computing full expm of the dense array...')
        tm_start = time.clock()
        A_expm = scipy.linalg.expm(A_dense)
        full_expm_entry = A_expm[i, j]
        tm_end = time.clock()
        print('expm(A)[%d, %d]:' % (i, j), full_expm_entry)
        print(tm_end - tm_start, ' seconds')
        print()
        print('computing only column', j, 'of expm of the sparse matrix...')
        tm_start = time.clock()
        v = np.zeros(n, dtype=float)
        v[j] = 1
        A_expm_col_j = expm_multiply(A, v)
        expm_col_entry = A_expm_col_j[i]
        tm_end = time.clock()
        print('expm(A)[%d, %d]:' % (i, j), expm_col_entry)
        print(tm_end - tm_start, ' seconds')
        print()
        if np.allclose(full_expm_entry, expm_col_entry):
            print('The two methods give the same answer.')
        else:
            print('!!! The two methods give different answers. !!!')
        print()

    def bench_expm_multiply(self):
        np.random.seed(1234)
        n = 2000
        i = 100
        j = 200
        shape = (n, n)
        nnz_per_row = 25
        print()
        print('expm multiply benchmarking')
        print('--------------------------')
        print()
        print('sampling a random sparse matrix...')
        print('shape:', shape)
        print('nnz per row:', nnz_per_row)
        tm_start = time.clock()
        A = random_sparse(n, n, nnz_per_row)
        tm_end = time.clock()
        print(tm_end - tm_start, ' seconds')
        print()
        self._help_bench_expm_multiply(A, i, j)
        print()


if __name__ == '__main__':
    Tester().bench()