Guest User

Untitled

a guest
Jan 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. def as_tensor_space(pspace, axis=None):
  2. """Convert a `ProductSpace` of `TensorSpace`'s to a tensor space.
  3.  
  4. Parameters
  5. ----------
  6. pspace : `ProductSpace`
  7. Power space (with arbitrary shape) whose base is a `TensorSpace`.
  8. axis : int or sequence of int, optional
  9. Indices at which the powers should be inserted as new axes.
  10. For the default ``None``, the power axes are added to the left.
  11.  
  12. Examples
  13. --------
  14. >>> pspace = odl.rn(3) ** 2
  15. >>> as_tensor_space(pspace)
  16. rn((2, 3))
  17. >>> as_tensor_space(pspace, axis=1)
  18. rn((3, 2))
  19. >>> pspace = odl.rn(4) ** (2, 3)
  20. >>> as_tensor_space(pspace)
  21. rn((2, 3, 4))
  22. >>> as_tensor_space(pspace, axis=(0, 2))
  23. rn((2, 4, 3))
  24. """
  25. assert isinstance(pspace, odl.ProductSpace) and pspace.is_power_space
  26. power_shape = pspace.shape
  27. power_ndim = len(pspace.shape)
  28. base = pspace[(0,) * power_ndim]
  29. assert isinstance(base, odl.space.base_tensors.TensorSpace)
  30. if axis is None:
  31. axis = list(range(power_ndim))
  32. elif np.isscalar(axis):
  33. axis = [axis]
  34. assert len(axis) == power_ndim
  35. newshape = []
  36. i = j = 0
  37. for nd in range(power_ndim + base.ndim):
  38. if nd in axis:
  39. newshape.append(power_shape[i])
  40. i += 1
  41. else:
  42. newshape.append(base.shape[j])
  43. j += 1
  44.  
  45. # TODO: This disregards weighting completely, needs fix!
  46. return type(base)(newshape, dtype=base.dtype)
  47.  
  48.  
  49. def as_power_space(tspace, axis=None):
  50. """Convert a `TensorSpace` to a `ProductSpace` smaller tensor spaces.
  51.  
  52. Parameters
  53. ----------
  54. tspace : `TensorSpace`
  55. Tensor space with ``ndim >= 1`` that should be converted to a
  56. `ProductSpace`.
  57. axis : int or sequence of int, optional
  58. Indices of the axes that should be turned into powers.
  59. For the default ``None``, the first axis is taken.
  60.  
  61. Examples
  62. --------
  63. >>> tspace = odl.rn((2, 3))
  64. >>> as_power_space(tspace)
  65. ProductSpace(rn(3), 2)
  66. >>> as_power_space(tspace, axis=1)
  67. ProductSpace(rn(2), 3)
  68. >>> tspace = odl.rn((2, 3, 4))
  69. >>> as_power_space(tspace)
  70. ProductSpace(rn(3, 4), 2)
  71. >>> as_power_space(tspace, axis=(0, 2))
  72. ProductSpace(ProductSpace(rn(3), 4), 2)
  73. """
  74. assert isinstance(tspace, odl.space.base_tensors.TensorSpace)
  75. assert tspace.ndim >= 1
  76. if axis is None:
  77. axis = [0]
  78. elif np.isscalar(axis):
  79. axis = [axis]
  80. else:
  81. axis = list(axis)
  82.  
  83. remaining_axes = [i for i in range(tspace.ndim) if i not in axis]
  84. removed_shape = [n for i, n in enumerate(tspace.shape) if i in axis]
  85. return tspace.byaxis[remaining_axes] ** removed_shape
Add Comment
Please, Sign In to add comment