Mathematica How to define such a simple function in Mathematica

Click For Summary
The discussion revolves around defining a mathematical function that behaves according to specific conditions based on two integers, n and d. The function must return 1 when both n and d are zero, evaluate to a given expression when both are greater than zero, and return zero when either n or d is negative. Initial attempts using Kronecker deltas and the Heaviside Theta function led to indeterminate results due to division by n. The final solution proposed involves using a Piecewise function in Mathematica, which correctly implements the desired behavior: returning the expression for positive values of n and d, 1 when both are zero, and zero otherwise. The proposed function effectively addresses all outlined conditions.
nrqed
Science Advisor
Messages
3,762
Reaction score
297
A very simple question but I can't find an answer.

I have an expression which depends on two integers, n,d.
Now, I want this expression to be

a) 1 when d=n=0,

b) some expression (that I won't write here) when both d and n are >0

c) zero when wither d or n negative.

At first I defined the function using Kronecker deltas and the Heaviside Theta function but the problem is that the expression that is valid when d and n are greater than 0 contains a factor d/n, and even if the Heaviside Theta function is zero when d=n=0, Mathematica gets an indeterminate result because of the division by n. I want to force it to be zero.

Thank you!
 
Physics news on Phys.org
What about when d=0 and n>0 or d>0 and n=0.
 
Dale said:
What about when d=0 and n>0 or d>0 and n=0.
Sorry, it is zero in that case.
So I have a function that

a) is defined to be some expression for both n and d >0

b) equal to 1 when d=n=0

c) zero in all other cases.
 
Try
Code:
f[n_Integer, d_Integer] :=
 Piecewise[{{expr, (d > 0) && (n > 0)}, {1, (d == 0) && (n == 0)}}, 0]
 
  • Like
Likes berkeman and nrqed
Dale said:
Try
Code:
f[n_Integer, d_Integer] :=
Piecewise[{{expr, (d > 0) && (n > 0)}, {1, (d == 0) && (n == 0)}}, 0]
Thank you!
Perfect!
 
  • Like
Likes berkeman and Dale

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
9K