How to define such a simple function in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter nrqed
  • Start date Start date
  • Tags Tags
    Function Mathematica
Click For Summary

Discussion Overview

The discussion revolves around defining a simple function in Mathematica that behaves differently based on the values of two integer parameters, n and d. The function is intended to return specific values under certain conditions, including handling cases where the parameters are zero or negative.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant outlines the desired behavior of the function: it should return 1 when both d and n are 0, some expression when both d and n are greater than 0, and zero when either d or n is negative.
  • Another participant questions the behavior of the function when d=0 and n>0 or d>0 and n=0, suggesting that it should also return zero in those cases.
  • A later reply confirms that the function should indeed return zero for the cases where either d or n is zero but not both.
  • Participants propose using the Piecewise function in Mathematica to define the function according to the specified conditions.
  • One participant expresses satisfaction with the proposed solution using the Piecewise function.

Areas of Agreement / Disagreement

Participants generally agree on the behavior of the function under the specified conditions, although the initial question about the behavior when d=0 and n>0 or d>0 and n=0 prompted clarification. The discussion remains focused on refining the function definition rather than resolving any fundamental disagreements.

Contextual Notes

The discussion does not address potential limitations or assumptions regarding the expressions used when both d and n are greater than 0, nor does it explore the implications of using the Heaviside Theta function in the context of division by n.

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   Reactions: 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   Reactions: berkeman and Dale

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · 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