def test_multiply(): a = afnumpy.random.random((2, 3)) b = numpy.array(a) fassert(afnumpy.multiply(a, a), numpy.multiply(b, b)) a = afnumpy.array(2) ao = afnumpy.array(0) b = numpy.array(a) bo = numpy.array(0) fassert(afnumpy.multiply(a, a), numpy.multiply(b, b)) fassert(afnumpy.multiply(a, a, out=ao), numpy.multiply(b, b, out=bo)) fassert(ao, bo)
def test_multiply(): a = afnumpy.random.random((2,3)) b = numpy.array(a) fassert(afnumpy.multiply(a,a), numpy.multiply(b,b)) a = afnumpy.array(2) ao = afnumpy.array(0) b = numpy.array(a) bo = numpy.array(0) fassert(afnumpy.multiply(a,a), numpy.multiply(b,b)) fassert(afnumpy.multiply(a,a, out=ao), numpy.multiply(b,b, out = bo)) fassert(ao, bo)
def __imul__(self, other): afnumpy.multiply(self, pu.raw(other), out=self) self._eval() return self
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None): if axis is not None: axisa, axisb, axisc = (axis,) * 3 a = asarray(a) b = asarray(b) # Move working axis to the end of the shape a = rollaxis(a, axisa, a.ndim) b = rollaxis(b, axisb, b.ndim) msg = ("incompatible dimensions for cross product\n" "(dimension must be 2 or 3)") if a.shape[-1] not in (2, 3) or b.shape[-1] not in (2, 3): raise ValueError(msg) # Create the output array shape = broadcast(a[..., 0], b[..., 0]).shape if a.shape[-1] == 3 or b.shape[-1] == 3: shape += (3,) dtype = afnumpy.promote_types(a.dtype, b.dtype) cp = afnumpy.empty(shape, dtype) # create local aliases for readability a0 = a[..., 0] a1 = a[..., 1] if a.shape[-1] == 3: a2 = a[..., 2] b0 = b[..., 0] b1 = b[..., 1] if b.shape[-1] == 3: b2 = b[..., 2] if cp.ndim != 0 and cp.shape[-1] == 3: cp0 = cp[..., 0] cp1 = cp[..., 1] cp2 = cp[..., 2] if a.shape[-1] == 2: if b.shape[-1] == 2: # a0 * b1 - a1 * b0 afnumpy.multiply(a0, b1, out=cp) cp -= a1 * b0 if cp.ndim == 0: return cp else: # This works because we are moving the last axis return rollaxis(cp, -1, axisc) else: # cp0 = a1 * b2 - 0 (a2 = 0) # cp1 = 0 - a0 * b2 (a2 = 0) # cp2 = a0 * b1 - a1 * b0 afnumpy.multiply(a1, b2, out=cp0) afnumpy.multiply(a0, b2, out=cp1) negative(cp1, out=cp1) afnumpy.multiply(a0, b1, out=cp2) cp2 -= a1 * b0 elif a.shape[-1] == 3: if b.shape[-1] == 3: # cp0 = a1 * b2 - a2 * b1 # cp1 = a2 * b0 - a0 * b2 # cp2 = a0 * b1 - a1 * b0 afnumpy.multiply(a1, b2, out=cp0) tmp = afnumpy.array(a2 * b1) cp0 -= tmp afnumpy.multiply(a2, b0, out=cp1) afnumpy.multiply(a0, b2, out=tmp) cp1 -= tmp afnumpy.multiply(a0, b1, out=cp2) afnumpy.multiply(a1, b0, out=tmp) cp2 -= tmp else: # cp0 = 0 - a2 * b1 (b2 = 0) # cp1 = a2 * b0 - 0 (b2 = 0) # cp2 = a0 * b1 - a1 * b0 afnumpy.multiply(a2, b1, out=cp0) negative(cp0, out=cp0) afnumpy.multiply(a2, b0, out=cp1) afnumpy.multiply(a0, b1, out=cp2) cp2 -= a1 * b0 if cp.ndim == 1: return cp else: # This works because we are moving the last axis return rollaxis(cp, -1, axisc)
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None): if axis is not None: axisa, axisb, axisc = (axis, ) * 3 a = asarray(a) b = asarray(b) # Move working axis to the end of the shape a = rollaxis(a, axisa, a.ndim) b = rollaxis(b, axisb, b.ndim) msg = ("incompatible dimensions for cross product\n" "(dimension must be 2 or 3)") if a.shape[-1] not in (2, 3) or b.shape[-1] not in (2, 3): raise ValueError(msg) # Create the output array shape = broadcast(a[..., 0], b[..., 0]).shape if a.shape[-1] == 3 or b.shape[-1] == 3: shape += (3, ) dtype = afnumpy.promote_types(a.dtype, b.dtype) cp = afnumpy.empty(shape, dtype) # create local aliases for readability a0 = a[..., 0] a1 = a[..., 1] if a.shape[-1] == 3: a2 = a[..., 2] b0 = b[..., 0] b1 = b[..., 1] if b.shape[-1] == 3: b2 = b[..., 2] if cp.ndim != 0 and cp.shape[-1] == 3: cp0 = cp[..., 0] cp1 = cp[..., 1] cp2 = cp[..., 2] if a.shape[-1] == 2: if b.shape[-1] == 2: # a0 * b1 - a1 * b0 afnumpy.multiply(a0, b1, out=cp) cp -= a1 * b0 if cp.ndim == 0: return cp else: # This works because we are moving the last axis return rollaxis(cp, -1, axisc) else: # cp0 = a1 * b2 - 0 (a2 = 0) # cp1 = 0 - a0 * b2 (a2 = 0) # cp2 = a0 * b1 - a1 * b0 afnumpy.multiply(a1, b2, out=cp0) afnumpy.multiply(a0, b2, out=cp1) negative(cp1, out=cp1) afnumpy.multiply(a0, b1, out=cp2) cp2 -= a1 * b0 elif a.shape[-1] == 3: if b.shape[-1] == 3: # cp0 = a1 * b2 - a2 * b1 # cp1 = a2 * b0 - a0 * b2 # cp2 = a0 * b1 - a1 * b0 afnumpy.multiply(a1, b2, out=cp0) tmp = afnumpy.array(a2 * b1) cp0 -= tmp afnumpy.multiply(a2, b0, out=cp1) afnumpy.multiply(a0, b2, out=tmp) cp1 -= tmp afnumpy.multiply(a0, b1, out=cp2) afnumpy.multiply(a1, b0, out=tmp) cp2 -= tmp else: # cp0 = 0 - a2 * b1 (b2 = 0) # cp1 = a2 * b0 - 0 (b2 = 0) # cp2 = a0 * b1 - a1 * b0 afnumpy.multiply(a2, b1, out=cp0) negative(cp0, out=cp0) afnumpy.multiply(a2, b0, out=cp1) afnumpy.multiply(a0, b1, out=cp2) cp2 -= a1 * b0 if cp.ndim == 1: return cp else: # This works because we are moving the last axis return rollaxis(cp, -1, axisc)