48 votes
Accepted

2D random walk within a bounded area

To answer your question: I don't think it's a bad or good idea to use If. It depends on how you do it. To demonstrate I'll use If...
user avatar
  • 68.9k
27 votes
Accepted

Quick way to use conditioned patterns when defining multi-argument function?

Ramblings Arguments of the left-hand-side head are evaluated in the course of function definition, therefore you can use a utility function that constructs the patterns that you want. For example: <...
user avatar
  • 265k
24 votes

2D random walk within a bounded area

I suggest using Mod - a natural thing for looped boundary conditions on a torus. Finite torus surface area is your bounded region. 2D random walk generally is ...
user avatar
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 <...
user avatar
19 votes

Difference between Piecewise, Which, If?

The main difference is their intended use. Which is a "programming function". It is meant for flow control. It is an equivalent of ...
user avatar
  • 227k
17 votes
Accepted

Question about Rules and Pattern (The operator patt/;test)

When using Set rather than SetDelayed you will need to hang the Condition on the left-hand-...
user avatar
  • 265k
16 votes
Accepted

Generating Tuples with restrictions

Select[ IntegerPartitions[24, {8}, Range[5]], #.# == 86 & ] ...
user avatar
  • 133k
15 votes

Flatten not working

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

Arrange a list in ascending order by deleting list elements

You can use DeleteDuplicates with Greater as the second argument: ...
user avatar
  • 350k
14 votes
Accepted

Multiple lines in an If statement

It seems this question is now purely about style and will probably be closed. However I think you may be looking for this: ...
user avatar
  • 265k
14 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 ...
user avatar
  • 13.5k
13 votes

2D random walk within a bounded area

Here's my implement of a random walk within a circle using If and FoldList. Please see @Pickett's answer for more thorough ...
user avatar
  • 5,011
13 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 ...
user avatar
  • 227k
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....
user avatar
  • 124k
12 votes
Accepted

Weird behavior of conditions when using OptionsPattern and OptionValue

Note: This is an incomplete analysis and leads to the wrong conclusion about the cause of the difficulty. Mr.W's answer below correctly identifies the culprit as ...
user avatar
  • 33.3k
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}
user avatar
  • 350k
11 votes

Add assumptions to Solve command

The ConditionalExpression output by Solve in your case does not really depend on the whole argument of the ...
user avatar
  • 61.5k
11 votes
Accepted

How to restrict the domain of a function

I think the easiest way is to use Condition which has the operator form /;. ...
user avatar
  • 106k
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 ...
user avatar
  • 34.6k
10 votes
Accepted

Switch inside Manipulate controls statement

Perhaps this? ...
user avatar
  • 219k
10 votes
Accepted

Why does pattern test work only with pure functions?

The two ways of attaching a test to a pattern, Condition and PatternTest, have fundamentally different semantics. Your mistake ...
user avatar
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 ...
user avatar
  • 12.4k
10 votes
Accepted

Generate "nice" random matrix

You can create random matrices until the eigenvalues do not have a Root form: ...
user avatar
  • 124k
10 votes

How to avoid applying Cross to a single argument?

If it is only for displaying purposes you can use Row: ...
user avatar
  • 133k
10 votes

Flatten not working

Put Flatten inside Module: ...
user avatar
  • 350k
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. ...
user avatar
  • 7,382
10 votes
Accepted

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

Change it to === and not == (examples at end) ...
user avatar
  • 112k
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. ...
user avatar
8 votes

2D random walk within a bounded area

I chose the WienerProcess as the underlying random process, as this will simulate a Brownian motion. Until Boundary Hit ...
user avatar
  • 27.1k
8 votes

2D random walk within a bounded area

You can use RegionWithin in Mathematica $11.1$ ...
user avatar
  • 8,638

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