Tag Info

Accepted

Simulating molecular dynamics efficiently

Okay, here is a way to compute the forces much faster: We create a CompiledFunction (called getForces). It eats a list of points ...
Accepted

Better way to sort course grades

This is a great use for the Association data structure, which makes so many tasks in Mathematica that much more pleasant. First, we can just write out a ranking ...
• 18.6k

Is there a more elegant way to write this statement?

Total[Range[CubeRoot[10000]]^3] 53361
• 36.2k
Accepted

Simplifying code that displays a square rolling inside a circle

EDIT 1. Fixed kernel crashing. The problem was WhenEvent choking, because it was continually checking whether the current pivot (which lies exactly on the bounding ...
• 1,235
Accepted

Problems with stress distribution using FEM

Your boundary conditions seem to be not quite correct according to the mechanical problem. Sorry, I don't have the time to go through your code today, but I got a version running, although this will ...
• 5,503
Accepted

How to avoid procedural loops in this example?

As is demonstrated very well in this post you can use a criteria for your pattern, thereby only applying your function as long as you are searching and not to all elements. Also there is a specific <...
• 8,014
Accepted

Sound synthesizer using Manipulate

First let me observe that your coding style makes debugging difficult, I highly recommend breaking giant expressions into manageable pieces. Second, in the code below I have used a different ...
• 85.2k
Accepted

The most efficient algorithm to find all possible integer pairs which sum to a given integer

I think IntegerPartitions[m, {2}, listOfIntegers] does exactly what you want, and seems pretty efficient.
• 49k

Simplify the code

☺lookMaNoLetters☺ = 1 ## & @@@ # & /@ # &; ☺lookMaNoLetters☺ @ mylist {{y1 y2 y3, y3 y4 y5}, {w1 w2 w3, w4 w5 w6}} Further variations: ...
• 399k
Accepted

How to check the validity of an option value

Implementation This is indeed an important problem. It is usually best to have a separate function testing various options. Here is the solution I propose: a wrapper that would factor out the testing ...
• 115k
Accepted

Enneper's minimal surface

The trick here is to use the plotting function to generate the mesh lines, but there is no way to apply a ColorFunction for a ...
• 69.3k

How to avoid procedural loops in this example?

SelectFirst would work: SelectFirst[Range[10], f[#] == 10 &] You can tell that it only executes ...
• 1,051

Optimizing Mathematica Code

Here is an optimized version of your function ...
• 3,180
Accepted

Brute Force for solving Diophantine equation

In version 10.1 on my circa 2011 PC: ...
• 272k
Accepted

Optimizing Mathematica Code

A version of mmeent's code in a more functional style: ...
• 85.2k
Accepted

Reduce Code Length

since the result of the new computation depends on the previous one we can use NestList ...
• 9,020

Simulating molecular dynamics efficiently

To expand on @HenrikSchumacher's comment, compare: ...
• 131k
Accepted

Simplify the code

Since you appear to want to multiply the list only when all of its elements are atomic, how about just mylist /. {s__?AtomQ} :> Times[s] It appears to be the ...
• 5,281

Using Mathematica to derive analytic form of single variable function

If one makes a table of the first few values of $n$, then a pattern becomes evident: Table[{n, f[n]}, {n, 3, 8}] // TableForm One can see that the leading term is ...
• 42.1k
Accepted

Checking if a number is right sorted

f1 = OrderedQ @* Rest @* IntegerDigits; f1 /@ {51369, 51396} {True, False} ...
• 399k
Accepted

Mathematica for Theoretical Physics by Gerd Baumann : Henonmap

HenonMap[{T_, W_}, α_] uses T in args but θ in body ...
• 3,106

How to avoid procedural loops in this example?

1 + LengthWhile[Range@10, f@# != 10 &] 5
• 399k

Better way to sort course grades

I had to deal with similar problems many times, and I usually do this with associations. We start by defining an exhaustive list of grades in our desired order. I am not that familiar with the ...
• 236k
Accepted

Is there a faster way to vectorize a matrix than this?

Note that here you are wasting a lot of time by transposing twice (this is the slowest operation here). See the breakdown of timings of your code here: ...
• 918
Accepted

Faster "Closest Pair of Points Problem" implementation?

Here's a refinement of @Henrik's approach. The key difference is that using Nearest[pts->"Distance"] is over an order of magnitude faster than using <...
• 131k
Accepted

Is there a more elegant solution than f[a, b, Sequence@@#3,...]?

It seems to me that there should be a way to avoid constructing lists and deconstructing them immediately Maybe there is a way to avoid lists, but I don't see the motivation. Lists are useful for ...
• 69.3k
Accepted

Computing the twin prime constant with Mathematica

The problem with your code is that N[Product[...]] calls NProduct[...] which turns integers into reals before ...
• 2,194
Accepted

Concise way to draw this pyramid

n = 5; Graphics3D[Table[Cuboid[{#[[1]], #[[2]], -i}] & /@ Tuples[Range[-i/2, i/2], 2], {i, 0, n - 1}]] A more concise version (thanks: Vitaliy Kaurov): <...
• 399k
The function $x \mapsto P_x([a,b])$ is continuous and piecewise linear, so it can be easily and exactly integrated with the trapezoidal rule. Then the resulting quadrature rule can be compiled for ...