Example #1
0
def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0):  # pylint: disable=missing-docstring
    dtype = dtypes.as_dtype(dtype) if dtype else np_utils.result_type(
        start, stop, float(num), np_array_ops.zeros((), dtype))
    computation_dtype = np.promote_types(dtype.as_numpy_dtype, np.float32)
    start = np_array_ops.asarray(start, dtype=computation_dtype)
    stop = np_array_ops.asarray(stop, dtype=computation_dtype)
    # follow the numpy geomspace convention for negative and complex endpoints
    start_sign = 1 - np_array_ops.sign(np_array_ops.real(start))
    stop_sign = 1 - np_array_ops.sign(np_array_ops.real(stop))
    signflip = 1 - start_sign * stop_sign // 2
    res = signflip * logspace(log10(signflip * start),
                              log10(signflip * stop),
                              num,
                              endpoint=endpoint,
                              base=10.0,
                              dtype=computation_dtype,
                              axis=0)
    if axis != 0:
        res = np_array_ops.moveaxis(res, 0, axis)
    return math_ops.cast(res, dtype)
Example #2
0
  def testSign(self):
    state = np.random.RandomState(0)
    test_types = [np.float16, np.float32, np.float64, np.int32, np.int64,
                  np.complex64, np.complex128]
    test_shapes = [(), (1,), (2, 3, 4), (2, 3, 0, 4)]

    for dtype in test_types:
      for shape in test_shapes:
        if np.issubdtype(dtype, np.complex):
          arr = (np.asarray(state.randn(*shape) * 100, dtype=dtype) +
                 1j * np.asarray(state.randn(*shape) * 100, dtype=dtype))
        else:
          arr = np.asarray(state.randn(*shape) * 100, dtype=dtype)
        self.match(np_array_ops.sign(arr), np.sign(arr))