# How to define such a simple function in Mathematica

• Mathematica
• nrqed
In summary, the conversation discusses finding an expression that satisfies certain conditions for two integers, n and d. The desired expression should be 1 when n=d=0, some other expression when both n and d are greater than 0, and 0 for all other cases. The conversation also mentions using Kronecker deltas and the Heaviside Theta function, but encountering issues with division by n. Ultimately, the solution proposed is a piecewise function that satisfies all the conditions.
nrqed
Homework Helper
Gold Member
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!

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]

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!

berkeman and Dale

## 1. What is a function in Mathematica?

A function in Mathematica is a set of instructions that maps input values to output values. It takes in one or more arguments and performs a specific task, such as solving an equation or generating a plot.

## 2. How do I define a function in Mathematica?

To define a function in Mathematica, use the syntax "functionName[arguments] := expression." The arguments can be variables or constants, and the expression can be any valid Mathematica code. For example, to define a function that calculates the square of a number, you can use "square[x_] := x^2."

## 3. Can I give my function a name other than "functionName"?

Yes, you can give your function any name you want as long as it follows the rules for valid variable names in Mathematica. The name cannot start with a number and should not contain any special characters or spaces. It is recommended to use descriptive names that reflect the purpose of the function.

## 4. Can a function in Mathematica have multiple arguments?

Yes, a function in Mathematica can have multiple arguments separated by commas. For example, a function that calculates the area of a rectangle can have two arguments: "area[l_, w_] := l * w" where l represents the length and w represents the width.

## 5. How do I use the function that I defined in my code?

To use a function in Mathematica, simply call the function with the appropriate arguments. For example, if you defined a function called "add[x_, y_]" that adds two numbers, you can use it by typing "add[3, 5]" which will return the value 8.

• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
328
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Replies
1
Views
857
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• Quantum Physics
Replies
0
Views
581