BrainPy icon indicating copy to clipboard operation
BrainPy copied to clipboard

Brainpy array does not support "where" argument in division

Open CloudyDory opened this issue 2 years ago • 1 comments

  • [x] Check for duplicate issues.
  • [x] Provide a complete example of how to reproduce the bug, wrapped in triple backticks like this:
  • [x] If applicable, include full error messages/tracebacks.

I am trying to implement the function in Brainpy: x / (1.0 - bm.exp(-x)). When x->0, the function has a theoretical limit of 1.0, but if we directly set x=0, the function will output NaN.

In Numpy, this can be handled by: np.divide(x, 1.0-np.exp(-x), out=np.ones_like(x), where=np.abs(x)>=1.0e-6).

However, in Brainpy, the similar expression does not work: bm.divide(x, 1.0-bm.exp(-x), out=bm.ones_like(x), where=bm.abs(x)>=1.0e-6). The error message is: TypeError: true_divide() got an unexpected keyword argument 'where'.

How can we deal with situations like this in Brainpy?

CloudyDory avatar Aug 22 '23 01:08 CloudyDory

Thank you for raising this missing functionality in brainpy.

Unfortunately, brainpy.math does not support where argument currently. We will add this in the near future.

However, this function can be rewritten to:


def f(x):
  return bm.where(bm.abs(x)<1.0e-6, 
                             bm.ones_like(x),
                             x / (1.0 - bm.exp(-x)))

Hoping this message will fix this issue.

chaoming0625 avatar Aug 22 '23 05:08 chaoming0625