mitsuba-renderer / drjit

Dr.Jit — A Just-In-Time-Compiler for Differentiable Rendering
BSD 3-Clause "New" or "Revised" License
593 stars 43 forks source link

[Potential Documentation] `==` performs a reduction, but `<=` is applied element-wise. #171

Closed lkskstlr closed 1 year ago

lkskstlr commented 1 year ago

I think the behavior of == being a reduction and <= being performed element-wise is at least to me unexpected. Especially in the context where <= is used for an interval because (idx<=0) & (idx>=0) and idx==0 are not the same thing, as the second expression evaluates to either True/False.

If you want, I could send a PR that either adapts the documentation of dr.eq or adds a section to the First steps in Python?

Code

import drjit as dr
from drjit.cuda import UInt, Float
import numpy as np

n = 2

rng = np.random.default_rng(0)
idx = dr.arange(UInt, 0, n)
arr = Float(rng.uniform(size=[n]))

print(r1 := dr.gather(Float, arr, idx, active=idx==0))
print(r2 := dr.gather(Float, arr, idx, active=dr.eq(idx, 0)))
print(r3 := dr.gather(Float, arr, idx, active=(idx<=0) & (idx>=0)))

assert not np.array_equal(r1, r2)
assert np.array_equal(r2, r3)

print([type(m) for m in [idx==0, dr.eq(idx, 0), (idx<=0) & (idx>=0)]])

Output

[0.0, 0.0]
[0.6369616985321045, 0.0]
[0.6369616985321045, 0.0]
[<class 'bool'>, <class 'drjit.cuda.Bool'>, <class 'drjit.cuda.Bool'>]
wjakob commented 1 year ago

Stay tuned: https://drjit.readthedocs.io/en/nanobind_v2/changelog.html