_flinalg_py.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #
  2. # Author: Pearu Peterson, March 2002
  3. #
  4. __all__ = ['get_flinalg_funcs']
  5. # The following ensures that possibly missing flavor (C or Fortran) is
  6. # replaced with the available one. If none is available, exception
  7. # is raised at the first attempt to use the resources.
  8. try:
  9. from . import _flinalg
  10. except ImportError:
  11. _flinalg = None
  12. # from numpy.distutils.misc_util import PostponedException
  13. # _flinalg = PostponedException()
  14. # print _flinalg.__doc__
  15. has_column_major_storage = lambda a:0
  16. def has_column_major_storage(arr):
  17. return arr.flags['FORTRAN']
  18. _type_conv = {'f':'s', 'd':'d', 'F':'c', 'D':'z'} # 'd' will be default for 'i',..
  19. def get_flinalg_funcs(names,arrays=(),debug=0):
  20. """Return optimal available _flinalg function objects with
  21. names. Arrays are used to determine optimal prefix."""
  22. ordering = []
  23. for i, ar in enumerate(arrays):
  24. t = ar.dtype.char
  25. if t not in _type_conv:
  26. t = 'd'
  27. ordering.append((t,i))
  28. if ordering:
  29. ordering.sort()
  30. required_prefix = _type_conv[ordering[0][0]]
  31. else:
  32. required_prefix = 'd'
  33. # Some routines may require special treatment.
  34. # Handle them here before the default lookup.
  35. # Default lookup:
  36. if ordering and has_column_major_storage(arrays[ordering[0][1]]):
  37. suffix1,suffix2 = '_c','_r'
  38. else:
  39. suffix1,suffix2 = '_r','_c'
  40. funcs = []
  41. for name in names:
  42. func_name = required_prefix + name
  43. func = getattr(_flinalg,func_name+suffix1,
  44. getattr(_flinalg,func_name+suffix2,None))
  45. funcs.append(func)
  46. return tuple(funcs)