def test14_rsqrt(m): x = m.Float(1, .25, 0.0625) ek.enable_grad(x) y = ek.rsqrt(x) ek.backward(y) assert ek.allclose(ek.detach(y), [1, 2, 4]) assert ek.allclose(ek.grad(x), [-.5, -4, -32])
def test15_test_avx512_approx(): Float = get_class('enoki.llvm.Float') x = ek.linspace(Float, 0, 10, 1000) o = ek.full(Float, 1, 1000) assert ek.allclose(ek.rsqrt(x), o / ek.sqrt(x), rtol=2e-7, atol=0) assert ek.allclose(ek.rcp(x), o / x, rtol=2e-7, atol=0)
def matrix_to_quat(m): if not _ek.is_matrix_v(m): raise Exception('Unsupported type!') name = _ek.detail.array_name('Quaternion', m.Type, [4], m.IsScalar) module = _modules.get(m.__module__) Quat4f = getattr(module, name) o = 1.0 t0 = o + m[0, 0] - m[1, 1] - m[2, 2] q0 = Quat4f(t0, m[1, 0] + m[0, 1], m[0, 2] + m[2, 0], m[2, 1] - m[1, 2]) t1 = o - m[0, 0] + m[1, 1] - m[2, 2] q1 = Quat4f(m[1, 0] + m[0, 1], t1, m[2, 1] + m[1, 2], m[0, 2] - m[2, 0]) t2 = o - m[0, 0] - m[1, 1] + m[2, 2] q2 = Quat4f(m[0, 2] + m[2, 0], m[2, 1] + m[1, 2], t2, m[1, 0] - m[0, 1]) t3 = o + m[0, 0] + m[1, 1] + m[2, 2] q3 = Quat4f(m[2, 1] - m[1, 2], m[0, 2] - m[2, 0], m[1, 0] - m[0, 1], t3) mask0 = m[0, 0] > m[1, 1] t01 = _ek.select(mask0, t0, t1) q01 = _ek.select(mask0, q0, q1) mask1 = m[0, 0] < -m[1, 1] t23 = _ek.select(mask1, t2, t3) q23 = _ek.select(mask1, q2, q3) mask2 = m[2, 2] < 0.0 t0123 = _ek.select(mask2, t01, t23) q0123 = _ek.select(mask2, q01, q23) return q0123 * (_ek.rsqrt(t0123) * 0.5)
def rsqrt_(a0): if not a0.IsFloat: raise Exception("rsqrt(): requires floating point operands!") if not a0.IsSpecial: ar, sr = _check1(a0) for i in range(sr): ar[i] = _ek.rsqrt(a0[i]) return ar else: return _ek.rcp(_ek.sqrt(a0))
def test08_misc(): for i in range(-5, 5): for j in range(-5, 5): a = ek.sqrt(C(i, j)) b = cmath.sqrt(complex(i, j)) assert ek.allclose(a, C(b)) assert ek.allclose(ek.conj(a), C(b.conjugate())) assert ek.allclose(ek.abs(a), C(abs(b))) if i != 0 and j != 0: a = ek.rsqrt(C(i, j)) b = C(1 / cmath.sqrt(complex(i, j))) assert ek.allclose(a, b)
def K(self, x, m_): return ek.rsqrt(1 - m_ * ek.sqr(ek.sin(x)))