Resample a step function defined by (tp, vp) into intervals t. Args: t: tensor with shape (..., n+1), the endpoints to resample into. tp: tensor with shape (..., m+1), the endpoints of the step function being resampled. vp: tensor with shape (..., m), the values of the step func
(t, tp, vp, use_avg=False)
| 340 | |
| 341 | |
| 342 | def resample(t, tp, vp, use_avg=False): |
| 343 | """Resample a step function defined by (tp, vp) into intervals t. |
| 344 | |
| 345 | Args: |
| 346 | t: tensor with shape (..., n+1), the endpoints to resample into. |
| 347 | tp: tensor with shape (..., m+1), the endpoints of the step function being |
| 348 | resampled. |
| 349 | vp: tensor with shape (..., m), the values of the step function being |
| 350 | resampled. |
| 351 | use_avg: bool, if False, return the sum of the step function for each |
| 352 | interval in `t`. If True, return the average, weighted by the width of |
| 353 | each interval in `t`. |
| 354 | eps: float, a small value to prevent division by zero when use_avg=True. |
| 355 | |
| 356 | Returns: |
| 357 | v: tensor with shape (..., n), the values of the resampled step function. |
| 358 | """ |
| 359 | eps = torch.finfo(t.dtype).eps |
| 360 | # eps = 1e-3 |
| 361 | |
| 362 | if use_avg: |
| 363 | wp = torch.diff(tp, dim=-1) |
| 364 | v_numer = resample(t, tp, vp * wp, use_avg=False) |
| 365 | v_denom = resample(t, tp, wp, use_avg=False) |
| 366 | v = v_numer / v_denom.clamp_min(eps) |
| 367 | return v |
| 368 | |
| 369 | acc = torch.cumsum(vp, dim=-1) |
| 370 | acc0 = torch.cat([torch.zeros(acc.shape[:-1] + (1,), device=acc.device), acc], dim=-1) |
| 371 | acc0_resampled = math.sorted_interp(t, tp, acc0) # TODO |
| 372 | v = torch.diff(acc0_resampled, dim=-1) |
| 373 | return v |
| 374 | |
| 375 | |
| 376 | def resample_np(t, tp, vp, use_avg=False): |
nothing calls this directly
no outgoing calls
no test coverage detected