25 votes

Difference between Piecewise, Which and If?

The main difference is their intended use. Which is a "programming function". It is meant for flow control. It is an equivalent of ...
Szabolcs's user avatar
  • 235k
21 votes
Accepted

Double Sum Involving Condition

A story of incremental improvement Let's look at the OP's original expression again, for reference: $$\sum_{m=1}^{c}\frac{1}{m}\sum_{d \mid m}\mu(d)n^{m/d}$$ Most people here are familiar with <...
J. M.'s missing motivation's user avatar
17 votes
Accepted

Generating Tuples with restrictions

Select[ IntegerPartitions[24, {8}, Range[5]], #.# == 86 & ] ...
Kuba's user avatar
  • 137k
16 votes
Accepted

How to define conditional function with Mathematica?

I think it's easier just to define this straight up, rather than compute something procedurally. f[1, 0] = 77; f[0, 1] = 66; f[_, _] = 0; Mathematica is ...
John Doty's user avatar
  • 13.7k
16 votes

Flatten not working

f[...] := Module[{...}, ... /; condition] is a very special syntax (documented in the Details section of Module). It only works ...
Szabolcs's user avatar
  • 235k
15 votes

Arrange a list in ascending order by deleting list elements

You can use DeleteDuplicates with Greater as the second argument: ...
kglr's user avatar
  • 395k
14 votes
Accepted

Can one compile the Switch function?

A drawback of the Which command is that it evaluates x repeatedly until one of the conditions of Which is satisfied. This is time consuming when x is a complicated expression or/and when the set of ...
Szabolcs's user avatar
  • 235k
13 votes
Accepted

How to optimize the following function?

Using Ramp and vectorized operations: y = -.02 Range[0, 4 10^5]; Total @ Sqrt[Ramp[1 - 2 Exp[y]]] //AbsoluteTiming {0....
Carl Woll's user avatar
  • 131k
12 votes
Accepted

How to restrict the domain of a function

I think the easiest way is to use Condition which has the operator form /;. ...
m_goldberg's user avatar
  • 108k
12 votes
Accepted

Checking what a function depends on

Perhaps Internal`DependsOnQ: h = f[x, g[y]]; Internal`DependsOnQ[h, #] & /@ {x, y, g[y], w} {True, True, True, False}
kglr's user avatar
  • 395k
11 votes
Accepted

Generate "nice" random matrix

You can create random matrices until the eigenvalues do not have a Root form: ...
Carl Woll's user avatar
  • 131k
11 votes
Accepted

conditional probability of dependent random variables

As @BobHanlon points out that the probability is zero for a specific value. But probabilities are not (necessarily) 0 in intervals. So we can get a probability statement for an interval and then ...
JimB's user avatar
  • 41.7k
10 votes

Generating Tuples with restrictions

A slightly different approach with Reduce (or Solve) We define: matC = Range[5]; (* the list of the integers from which we build a list *) Let us use a vector ...
gwr's user avatar
  • 13.5k
10 votes

How to avoid applying Cross to a single argument?

If it is only for displaying purposes you can use Row: ...
Kuba's user avatar
  • 137k
10 votes

Flatten not working

Put Flatten inside Module: ...
kglr's user avatar
  • 395k
10 votes
Accepted

Take elements from a list based on two criteria

Select[list, Count[#, 1] == 1&] {{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}} Also ...
kglr's user avatar
  • 395k
10 votes

Need four random values in a range which total unity

Because you have very tight constraints, the number of allowed points is not very large, so you can generate all of them and then sample. ...
Domen's user avatar
  • 24.7k
10 votes
Accepted

Can someone tell me what's wrong with this nested IF?

Change it to === and not == (examples at end) ...
Nasser's user avatar
  • 143k
9 votes
Accepted

Using "If" statement to change a list values

Use MapThread MapThread[If[# < 60, #2 + 20, #2] &, {Sample, Sample2}] or alternatively ...
Coolwater's user avatar
  • 20.3k
9 votes
Accepted

DeleteCases does not work with EvenQ? While `Select` works fine

For the selection criterion, Select uses a (pure) function, while *Cases uses a pattern. Code below works. ...
Αλέξανδρος Ζεγγ's user avatar
9 votes

Make a list of the first 100 primes, keeping only ones whose last digit is less than 3

SolveValues[x <= Prime[100] && Mod[x, 10] < 3, x, Primes] ...
kglr's user avatar
  • 395k
9 votes
Accepted

Confused about If statement

Translation to make it easier to see. When you write If[n1 == 0, n2 == 1] This translates to ...
Nasser's user avatar
  • 143k
8 votes

Double Sum Involving Condition

Please see J.M.'s answer (in comments) using DirichletConvolve. I would vote for this as an answer. I present my answer: ...
ubpdqn's user avatar
  • 60.7k
8 votes

2D random walk within a bounded area

You can use RegionWithin in Mathematica $11.1$ ...
vito's user avatar
  • 8,958
8 votes

How to select points from a table that are within some region

You should parametrize your region. c looks to me like sort of a radius and it will help to cut off half a plane. ...
Vitaliy Kaurov's user avatar
8 votes
Accepted

ContourPlot with certain contour values omitted

You can use the option RegionFunction: ...
kglr's user avatar
  • 395k
8 votes
Accepted

Finding the condition for root of a third degree polynomial

Assuming real coefficients and a=1, following Daniel Lichtblau comment, conditions are quickly found by: ...
Andrzej Odrzywolek's user avatar
8 votes

How to create a sublist of elements with a congruence relation

You can write a very explicit and readable code by using Select and AllTrue ...
Batracos's user avatar
  • 2,105
8 votes

How to create a sublist of elements with a congruence relation

Indeed, Pick is very fast. A bit faster is using some integer vector arithmetic to create a scalar condition for picking: ...
Henrik Schumacher's user avatar
8 votes
Accepted

Unevaluated Which

...
eyorble's user avatar
  • 9,458

Only top scored, non community-wiki answers of a minimum length are eligible