Tag Info

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...
• 68.9k
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: <...
• 265k

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 ...
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 <...

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 ...
• 227k
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-...
• 265k
Accepted

Generating Tuples with restrictions

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

Flatten not working

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

Arrange a list in ascending order by deleting list elements

You can use DeleteDuplicates with Greater as the second argument: ...
• 350k
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: ...
• 265k
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 ...
• 13.5k

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 ...
• 5,011
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 ...
• 227k
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....
• 124k
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 ...
• 33.3k
Accepted

Checking what a function depends on

Perhaps InternalDependsOnQ: h = f[x, g[y]]; InternalDependsOnQ[h, #] & /@ {x, y, g[y], w} {True, True, True, False}
• 350k

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

How to restrict the domain of a function

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

Switch inside Manipulate controls statement

Perhaps this? ...
• 219k
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 ...
• 112k

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 ...
• 12.4k
Accepted

Generate "nice" random matrix

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

How to avoid applying Cross to a single argument?

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

Flatten not working

Put Flatten inside Module: ...
• 350k

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. ...
• 7,382
Accepted

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

Change it to === and not == (examples at end) ...
• 112k
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. ...
• 9,303

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 ...
• 27.1k

2D random walk within a bounded area

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

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