-2

I just want a simple formula like Dirac delta function, but using programming functions including trigonometric, floor, ceiling, min, max etc. The formula should output 1 at x=0 and should be 0 for all other values of x.

Someone at stack exchange provided opposite of this as: $$\left[ \frac{x^2}{x^2+1}\right]$$

PS: Yes, I am programming but I cannot use 'If' statement

TShiong
  • 1,270
  • Your formula does not work. Once you fix it, just subtract from $1$. – Ted Shifrin Sep 19 '23 at 20:06
  • The formula I provided works for all values of x. It gives 0 at x=0 and 1 for all other values of x. I just want opposite of it. So, could something like this using algerbraic formulations can be made? @TedShifrin – umair mughal Sep 19 '23 at 20:10
  • @TedShifrin I am so dumb. Thanks btw. – umair mughal Sep 19 '23 at 20:18
  • You are asking for the Kronecker delta. It would be most straightforward to encode it as you've written it, using an if statement. But if that's not at your disposal, you could use $d(x)=1-\operatorname{sign}\left(x\right)^{2}$. – Jam Sep 19 '23 at 20:28

3 Answers3

2

Many programming languages allow to use truth values as extressions.

  • In Python:

    y = 0 + (x == 0);
    
  • In C / C++:

    int y = x == 0;
    

Also, many languages support conditional assignment:

  • In C / C++ / Java:
    int y = x == 0 ? 1 : 0;
    

Using floating point might be prone to rounding errors, i.e. 1 / (1 + x^2) might evaluate to 1 even if x is not equal to 0. This is actually very likely, because the denominator will be evaluated with a precision of 1 machine epsilon, but float can represent values much smaller than one machine epsilon!

For example, with IEEE single, eps is around 1.2·10−7, but IEEE single can represent numbers down to 2·10−45. And squaring x will make things even worse.

2

This one works $$\Bigl\lfloor \frac{1}{x^2+1}\Bigr\rfloor$$

PNT
  • 4,295
1

How about $\lfloor{e^{-x^2}}\rfloor?$

  • 3
    This is a really uneconomical way of encoding it. You don't need to compute an exponential to test whether $x$ is nonzero. – Jam Sep 19 '23 at 20:29
  • @Jam Sure, I thought it might be exotic to use the bell curve, I dont know much about computing/coding though – UnsinkableSam Sep 19 '23 at 20:58