~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/linalg/blas.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Author: Pearu Peterson, March 2002
 
3
#
 
4
 
 
5
__all__ = ['get_blas_funcs']
 
6
 
 
7
import string
 
8
 
 
9
# The following ensures that possibly missing flavor (C or Fortran) is
 
10
# replaced with the available one. If none is available, exception
 
11
# is raised at the first attempt to use the resources.
 
12
 
 
13
import fblas
 
14
import cblas
 
15
if hasattr(cblas,'empty_module'):
 
16
    cblas = fblas
 
17
elif hasattr(fblas,'empty_module'):
 
18
    fblas = cblas
 
19
 
 
20
_type_conv = {'f':'s', 'd':'d', 'F':'c', 'D':'z'} # 'd' will be default for 'i',..
 
21
_inv_type_conv = {'s':'f','d':'d','c':'F','z':'D'}
 
22
 
 
23
def get_blas_funcs(names,arrays=(),debug=0):
 
24
    """Return available BLAS function objects with names.
 
25
    arrays are used to determine the optimal prefix of
 
26
    BLAS routines."""
 
27
    ordering = []
 
28
    for i in range(len(arrays)):
 
29
        t = arrays[i].typecode()
 
30
        if not _type_conv.has_key(t): t = 'd'
 
31
        ordering.append((t,i))
 
32
    if ordering:
 
33
        ordering.sort()
 
34
        required_prefix = _type_conv[ordering[0][0]]
 
35
    else:
 
36
        required_prefix = 'd'
 
37
    typecode = _inv_type_conv[required_prefix]
 
38
    # Default lookup:
 
39
    if ordering and fblas.has_column_major_storage(arrays[ordering[0][1]]):
 
40
        # prefer Fortran code for leading array with column major order
 
41
        m1,m2 = fblas,cblas
 
42
    else:
 
43
        # in all other cases, C code is preferred
 
44
        m1,m2 = cblas,fblas
 
45
    funcs = []
 
46
    for name in names:
 
47
        if name=='ger' and typecode in 'FD':
 
48
            name = 'gerc'
 
49
        func_name = required_prefix + name
 
50
        func = getattr(m1,func_name,None)
 
51
        if func is None:
 
52
            func = getattr(m2,func_name)
 
53
            func.module_name = string.split(m2.__name__,'.')[-1]
 
54
        else:
 
55
            func.module_name = string.split(m1.__name__,'.')[-1]
 
56
        func.prefix = required_prefix
 
57
        func.typecode = typecode
 
58
        funcs.append(func)
 
59
    return tuple(funcs)