Python numpy.copysign() Examples

The following are 30 code examples of numpy.copysign(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: utils.py    From VIP with MIT License 6 votes vote down vote up
def idl_round(x):
    """
    Round to the *nearest* integer, half-away-from-zero.

    Parameters
    ----------
    x : array-like
        Number or array to be rounded

    Returns
    -------
    r_rounded : array-like
        note that the returned values are floats

    Notes
    -----
    IDL ``ROUND`` rounds to the *nearest* integer (commercial rounding),
    unlike numpy's round/rint, which round to the nearest *even*
    value (half-to-even, financial rounding) as defined in IEEE-754
    standard.

    """
    return np.trunc(x + np.copysign(0.5, x)) 
Example #2
Source File: transaction.py    From qstrader with MIT License 6 votes vote down vote up
def __init__(
        self,
        asset,
        quantity,
        dt,
        price,
        order_id,
        commission=0.0
    ):
        self.asset = asset
        self.quantity = quantity
        self.direction = np.copysign(1, self.quantity)
        self.dt = dt
        self.price = price
        self.order_id = order_id
        self.commission = commission 
Example #3
Source File: pymm_convert.py    From geoist with MIT License 6 votes vote down vote up
def to_geodetic(z_coord, hypot_xy):
        """ Get geodetic coordinates calculated by the Ferrarri's solution. """
        ee4 = WGS84_EPS2**2
        pa2 = (hypot_xy / WGS84_A)**2
        zt = (1.0 - WGS84_EPS2) * (z_coord / WGS84_A)**2
        rh = (pa2 + zt - ee4)/6.0
        ss = (0.25*ee4) * zt * pa2
        rh3 = rh**3
        tmp = rh3 + ss + sqrt(ss*(ss+2.0*rh3))
        tt = copysign(fabs(tmp)**(1.0/3.0), tmp)
        uu = rh + tt + rh**2 / tt
        vv = sqrt(uu**2 + ee4*zt)
        ww = (0.5*WGS84_EPS2) * (uu + vv - zt)/vv
        kp = 1.0 + WGS84_EPS2*(sqrt(uu + vv + ww**2) + ww)/(uu + vv)
        zkp = kp * z_coord
        return (
            RAD2DEG*arctan2(zkp, hypot_xy),
            hypot(hypot_xy, zkp)*(1.0/kp - 1.0 + WGS84_EPS2)/WGS84_EPS2
        ) 
Example #4
Source File: angle_utilities.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def hms_to_hours(h, m, s=None):
    """
    Convert hour, minute, second to a float hour value.
    """

    check_hms_ranges(h, m, s)

    # determine sign
    sign = np.copysign(1.0, h)

    try:
        h = np.floor(np.abs(h))
        if s is None:
            m = np.abs(m)
            s = 0
        else:
            m = np.floor(np.abs(m))
            s = np.abs(s)
    except ValueError:
        raise ValueError(format_exception(
            "{func}: HMS values ({1[0]},{2[1]},{3[2]}) could not be "
            "converted to numbers.", h, m, s))

    return sign * (h + m / 60. + s / 3600.) 
Example #5
Source File: angle_utilities.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def dms_to_degrees(d, m, s=None):
    """
    Convert degrees, arcminute, arcsecond to a float degrees value.
    """

    _check_minute_range(m)
    _check_second_range(s)

    # determine sign
    sign = np.copysign(1.0, d)

    try:
        d = np.floor(np.abs(d))
        if s is None:
            m = np.abs(m)
            s = 0
        else:
            m = np.floor(np.abs(m))
            s = np.abs(s)
    except ValueError:
        raise ValueError(format_exception(
            "{func}: dms values ({1[0]},{2[1]},{3[2]}) could not be "
            "converted to numbers.", d, m, s))

    return sign * (d + m / 60. + s / 3600.) 
Example #6
Source File: test_op_div.py    From hope with GNU General Public License v3.0 6 votes vote down vote up
def test_cross_div(dtypea, dtypeb, dtypec):
    if dtypea == np.int8 and dtypeb == np.int8:
        pytest.skip("Different behaviour in c++ and python for int8 / int8".format(dtypea, dtypeb))
    def fkt(a, b, c):
        c[:] = a / b
    hfkt = hope.jit(fkt)
    (ao, ah), (bo, bh), (co, ch) = random(dtypea, [10]), random(dtypeb, [10]), random(dtypec, [10])
    ao, ah, bo, bh = ao.astype(np.float64), ah.astype(np.float64), bo.astype(np.float64), bh.astype(np.float64)
    ao, ah = np.copysign(np.power(np.abs(ao), 1. / 4.), ao).astype(dtypea), np.copysign(np.power(np.abs(ah), 1. / 4.), ah).astype(dtypea)
    bo, bh = np.copysign(np.power(np.abs(bo), 1. / 4.), bo).astype(dtypeb), np.copysign(np.power(np.abs(bh), 1. / 4.), bh).astype(dtypeb)
    if np.count_nonzero(bo == 0) > 0: bo[bo == 0] += 1
    if np.count_nonzero(bh == 0) > 0: bh[bh == 0] += 1
    fkt(ao, bo, co),  hfkt(ah, bh, ch)
    assert check(co, ch)
    fkt(ao, bo, co),  hfkt(ah, bh, ch)
    assert check(co, ch) 
Example #7
Source File: triggers.py    From corrscope with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def custom_line(
        self, name: str, data: np.ndarray, offset: bool, invert: bool = True
    ):
        """
        :param offset:
        - True, for untriggered wave data:
            - line will be shifted and triggered (by offset_viewport()).
        - False, for triggered data and buffers:
            - line is immune to offset_viewport().

        :param invert:
        - True, for wave (data and buffers):
            - If wave data is inverted (edge_direction = -1),
              data will be plotted inverted.
        - False, for buffers and autocorrelated wave data:
            - Data is plotted as-is.
        """
        if self._renderer is None:
            return
        data = data / abs_max(data, 0.01) / 2
        if invert:
            data *= np.copysign(1, self._wave.amplification)
        self._renderer.update_custom_line(
            name, self._wave_idx, self._stride, data, offset=offset
        ) 
Example #8
Source File: test_umath.py    From keras-lambda with MIT License 5 votes vote down vote up
def test_copysign():
    assert_(np.copysign(1, -1) == -1)
    with np.errstate(divide="ignore"):
        assert_(1 / np.copysign(0, -1) < 0)
        assert_(1 / np.copysign(0, 1) > 0)
    assert_(np.signbit(np.copysign(np.nan, -1)))
    assert_(not np.signbit(np.copysign(np.nan, 1))) 
Example #9
Source File: test_op_pow.py    From hope with GNU General Public License v3.0 5 votes vote down vote up
def test_binary_pow(dtype, shape):
    def fkt(a, c):
        c[:] = a ** 2
    hfkt = hope.jit(fkt)
    (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
    ao, ah = np.copysign(np.sqrt(np.abs(ao)), ao).astype(dtype), np.copysign(np.sqrt(np.abs(ah)), ah).astype(dtype)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch) 
Example #10
Source File: test_umath.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_copysign():
    assert_(np.copysign(1, -1) == -1)
    with np.errstate(divide="ignore"):
        assert_(1 / np.copysign(0, -1) < 0)
        assert_(1 / np.copysign(0, 1) > 0)
    assert_(np.signbit(np.copysign(np.nan, -1)))
    assert_(not np.signbit(np.copysign(np.nan, 1))) 
Example #11
Source File: test_umath.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def test_copysign():
    assert_(np.copysign(1, -1) == -1)
    with np.errstate(divide="ignore"):
        assert_(1 / np.copysign(0, -1) < 0)
        assert_(1 / np.copysign(0, 1) > 0)
    assert_(np.signbit(np.copysign(np.nan, -1)))
    assert_(not np.signbit(np.copysign(np.nan, 1))) 
Example #12
Source File: test_tir_intrin.py    From incubator-tvm with Apache License 2.0 5 votes vote down vote up
def test_binary_intrin():
    test_funcs = [
        (tvm.tir.atan2, lambda x1, x2 : np.arctan2(x1, x2)),
        (tvm.tir.nextafter, lambda x1, x2 : np.nextafter(x1, x2)),
        (tvm.tir.copysign, lambda x1, x2 : np.copysign(x1, x2)),
        (tvm.tir.hypot, lambda x1, x2 : np.hypot(x1, x2)),
    ]
    def run_test(tvm_intrin, np_func):
        m = te.var("m",)
        A = te.placeholder((m,), name='A')
        B = te.placeholder((m,), name='B')
        C = te.compute((m,), lambda *i: tvm_intrin(A(*i), B(*i)), name='C')
        s = te.create_schedule(C.op)
        f = tvm.build(s, [A, B, C], "llvm")
        ctx = tvm.cpu(0)
        n = 10
        a = tvm.nd.array(np.random.uniform(0, 1, size=n).astype(A.dtype), ctx)
        b = tvm.nd.array(np.random.uniform(0, 1, size=n).astype(B.dtype), ctx)
        c = tvm.nd.array( \
            np.random.uniform(size=n).astype(A.dtype), ctx)
        f(a, b, c)
        tvm.testing.assert_allclose(
            c.asnumpy(), np_func(a.asnumpy(), b.asnumpy()), atol=1e-5, rtol=1e-5)

    for func in test_funcs:
        run_test(*func) 
Example #13
Source File: test_quantity_ufuncs.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_copysign_scalar(self):
        assert np.copysign(3 * u.m, 1.) == 3. * u.m
        assert np.copysign(3 * u.m, 1. * u.s) == 3. * u.m
        assert np.copysign(3 * u.m, -1.) == -3. * u.m
        assert np.copysign(3 * u.m, -1. * u.s) == -3. * u.m 
Example #14
Source File: test_umath.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_copysign():
    assert_(np.copysign(1, -1) == -1)
    with np.errstate(divide="ignore"):
        assert_(1 / np.copysign(0, -1) < 0)
        assert_(1 / np.copysign(0, 1) > 0)
    assert_(np.signbit(np.copysign(np.nan, -1)))
    assert_(not np.signbit(np.copysign(np.nan, 1))) 
Example #15
Source File: position.py    From qstrader with MIT License 5 votes vote down vote up
def direction(self):
        """
        Returns an integer value representing the direction.

        Returns
        -------
        `int`
            1 - Long, 0 - No direction, -1 - Short.
        """
        if self.net_quantity == 0:
            return 0
        else:
            return np.copysign(1, self.net_quantity) 
Example #16
Source File: test_umath.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_copysign():
    assert_(np.copysign(1, -1) == -1)
    with np.errstate(divide="ignore"):
        assert_(1 / np.copysign(0, -1) < 0)
        assert_(1 / np.copysign(0, 1) > 0)
    assert_(np.signbit(np.copysign(np.nan, -1)))
    assert_(not np.signbit(np.copysign(np.nan, 1))) 
Example #17
Source File: order.py    From qstrader with MIT License 5 votes vote down vote up
def __init__(
        self,
        dt,
        asset,
        quantity,
        commission=0.0,
        order_id=None
    ):
        self.created_dt = dt
        self.cur_dt = dt
        self.asset = asset
        self.quantity = quantity
        self.commission = commission
        self.direction = np.copysign(1, self.quantity)
        self.order_id = self._set_or_generate_order_id(order_id) 
Example #18
Source File: test_umath.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_copysign():
    assert_(np.copysign(1, -1) == -1)
    with np.errstate(divide="ignore"):
        assert_(1 / np.copysign(0, -1) < 0)
        assert_(1 / np.copysign(0, 1) > 0)
    assert_(np.signbit(np.copysign(np.nan, -1)))
    assert_(not np.signbit(np.copysign(np.nan, 1))) 
Example #19
Source File: angle_utilities.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def degrees_to_dms(d):
    """
    Convert a floating-point degree value into a ``(degree, arcminute,
    arcsecond)`` tuple.
    """
    sign = np.copysign(1.0, d)

    (df, d) = np.modf(np.abs(d))  # (degree fraction, degree)
    (mf, m) = np.modf(df * 60.)  # (minute fraction, minute)
    s = mf * 60.

    return np.floor(sign * d), sign * np.floor(m), sign * s 
Example #20
Source File: angle_utilities.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def hours_to_hms(h):
    """
    Convert an floating-point hour value into an ``(hour, minute,
    second)`` tuple.
    """

    sign = np.copysign(1.0, h)

    (hf, h) = np.modf(np.abs(h))  # (degree fraction, degree)
    (mf, m) = np.modf(hf * 60.0)  # (minute fraction, minute)
    s = mf * 60.0

    return (np.floor(sign * h), sign * np.floor(m), sign * s) 
Example #21
Source File: test_op_mult.py    From hope with GNU General Public License v3.0 5 votes vote down vote up
def test_binary_mult(dtype, shape):
    def fkt(a, b, c):
        c[:] = a * b
    hfkt = hope.jit(fkt)
    (ao, ah), (bo, bh), (co, ch) = random(dtype, shape), random(dtype, shape), random(dtype, shape)
    ao = np.copysign(np.sqrt(np.abs(ao.astype(np.float64))).astype(dtype), ao).astype(dtype)
    ah = np.copysign(np.sqrt(np.abs(ah.astype(np.float64))).astype(dtype), ah).astype(dtype)
    bo = np.copysign(np.sqrt(np.abs(bo.astype(np.float64))).astype(dtype), bo).astype(dtype)
    bh = np.copysign(np.sqrt(np.abs(bh.astype(np.float64))).astype(dtype), bh).astype(dtype)
    ro, rh = fkt(ao, bo, co),  hfkt(ah, bh, ch)
    assert check(co, ch)
    ro, rh = fkt(ao, bo, co),  hfkt(ah, bh, ch)
    assert check(co, ch) 
Example #22
Source File: test_simulated_broker.py    From qstrader with MIT License 5 votes vote down vote up
def __init__(self, asset, quantity, order_id=None):
        self.asset = asset
        self.quantity = quantity
        self.order_id = 1 if order_id is None else order_id
        self.direction = np.copysign(1, self.quantity) 
Example #23
Source File: test_op_pow.py    From hope with GNU General Public License v3.0 5 votes vote down vote up
def test_augmented_pow(dtype, shape):
    def fkt(a, c): 
        c[:] **= a
    hfkt = hope.jit(fkt)
    (ao, ah), (co, ch) = random(np.uint8, shape), random(dtype, shape)
    if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1
    if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1
    if np.count_nonzero(co == 0) > 0: co[co == 0] += 1
    if np.count_nonzero(ch == 0) > 0: ch[ch == 0] += 1
    co, ch = np.copysign(np.sqrt(np.abs(co)), co).astype(dtype), np.copysign(np.sqrt(np.abs(ch)), ch).astype(dtype)
    ao, ah = np.power(np.abs(ao).astype(np.float64), 1. / co.astype(np.float64)).astype(dtype), np.power(np.abs(ah).astype(np.float64), 1. / ch.astype(np.float64)).astype(dtype)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)

# TODO: fix for np.ulonglong and uint64, std::power produce different results 
Example #24
Source File: test_return.py    From hope with GNU General Public License v3.0 5 votes vote down vote up
def test_return_arr_expr(dtype, shape):
    def fkt(a, c):
        return a ** -2 + a ** -4.0 + a ** -7
    hfkt = hope.jit(fkt)
    (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
    ao, ah = np.copysign(np.power(np.abs(ao), 1. / 8.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 8.), ah).astype(dtype)
    if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1
    if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch) 
Example #25
Source File: test_optimize.py    From hope with GNU General Public License v3.0 5 votes vote down vote up
def test_opt_basic_array(dtype, shape):
    def fkt(a, c):
        c[:] = (a + a) * a - 1 / a
    hope.config.optimize = True
    hfkt = hope.jit(fkt)
    (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
    ao, ah = np.copysign(np.power(np.abs(ao), 1. / 4.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 4.), ah).astype(dtype)
    if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1
    if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    hope.config.optimize = False 
Example #26
Source File: test_optimize.py    From hope with GNU General Public License v3.0 5 votes vote down vote up
def test_opt_neg_pow_scalar(dtype):
    def fkt(a, c):
        c[0] = a ** -2 + a ** -4.0 + a ** -7
    hope.config.optimize = True
    hfkt = hope.jit(fkt)
    (ao, ah), (co, ch) = random(dtype, []), random(dtype, [1])
    ao, ah = np.copysign(np.power(np.abs(ao), 1. / 8.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 8.), ah).astype(dtype)
    if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1
    if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1
    fkt(ao, co), hfkt(ah, ch)
    assert check(co, ch)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    hope.config.optimize = False 
Example #27
Source File: test_optimize.py    From hope with GNU General Public License v3.0 5 votes vote down vote up
def test_opt_neg_pow_array(dtype, shape):
    def fkt(a, c):
        c[:] = a ** -2 + a ** -4.0 + a ** -7
    hope.config.optimize = True
    hfkt = hope.jit(fkt)
    (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
    ao, ah = np.copysign(np.power(np.abs(ao), 1. / 8.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 8.), ah).astype(dtype)
    if np.count_nonzero(ao == 0) > 0: ao[ao == 0] += 1
    if np.count_nonzero(ah == 0) > 0: ah[ah == 0] += 1
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    hope.config.optimize = False 
Example #28
Source File: test_optimize.py    From hope with GNU General Public License v3.0 5 votes vote down vote up
def test_opt_pow_scalar(dtype):
    def fkt(a, c):
        c[0] = a ** 2 + a ** 4 + a ** 7.0
    hope.config.optimize = True
    hfkt = hope.jit(fkt)
    (ao, ah), (co, ch) = random(dtype, []), random(dtype, [1])
    ao, ah = np.copysign(np.power(np.abs(ao), 1. / 8.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 8.), ah).astype(dtype)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    hope.config.optimize = False 
Example #29
Source File: test_optimize.py    From hope with GNU General Public License v3.0 5 votes vote down vote up
def test_opt_pow_array(dtype, shape):
    def fkt(a, c):
        c[:] = a ** 2.0 + a ** 4 + a ** 7
    hope.config.optimize = True
    hfkt = hope.jit(fkt)
    (ao, ah), (co, ch) = random(dtype, shape), random(dtype, shape)
    ao, ah = np.copysign(np.power(np.abs(ao), 1. / 8.), ao).astype(dtype), np.copysign(np.power(np.abs(ah), 1. / 8.), ah).astype(dtype)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    fkt(ao, co),  hfkt(ah, ch)
    assert check(co, ch)
    hope.config.optimize = False 
Example #30
Source File: test_umath.py    From ImageFusion with MIT License 5 votes vote down vote up
def test_copysign():
    assert_(np.copysign(1, -1) == -1)
    with np.errstate(divide="ignore"):
        assert_(1 / np.copysign(0, -1) < 0)
        assert_(1 / np.copysign(0, 1) > 0)
    assert_(np.signbit(np.copysign(np.nan, -1)))
    assert_(not np.signbit(np.copysign(np.nan, 1)))