flinalg.py 1.7 KB

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