Questions tagged [code-review]

This tag is intended for the review and improvement of style for already functioning code. It not meant to change the objective of the code.

Filter by
Sorted by
Tagged with
44
votes
10answers
7k views

Programming paradigm change

I'm new to Mathematica, I've been learning by myself, and, being a Java developer for some time, it makes it hard to adapt. I've tried to solve a Reddit challenge, and I came up with this solution: <...
41
votes
0answers
2k views

Fast Spherical Harmonics radiative transfer

This is a rather specific question and I apologize for spamming you with some lengthy code. But it could be interesting for some reader and maybe you can help out, so please bear with me. I am using ...
39
votes
1answer
2k views

May the Fourth be with you

Happy Star Wars day. I was playing around with the image manipulation functions in Mathematica in an attempt to recreate the Star Wars crawl. There's an HTML/CSS implementation which is in my ...
33
votes
2answers
846 views

Copying one symbol into another

I would like to know how can I completely copy one Symbol to another. When I say copy, a refer to UpValues, DownValues, ...
29
votes
8answers
2k views

Splitting words into specific fragments

I am looking into splitting words into a succession of chemical elements symbols, where possible. For example: Titanic = Ti Ta Ni C (titanium, tantalum, nickel, carbon) A word may or may not be ...
24
votes
1answer
989 views

Strategies for solving problems involving searches

As an example of a search-oriented problem, consider finding all constrained $n$-colorings of a graph. An "$n$-coloring" associates one of $n \ge 1$ colors to each vertex in such a way that no edge ...
22
votes
2answers
1k views

Simulating molecular dynamics efficiently

The following website offers a very nice molecular dynamics simulation: http://physics.weber.edu/schroeder/md/. It is pretty neat and quite a few physical phenomena can be described from that small ...
20
votes
1answer
525 views

Segmentation of sunspots

I recently went for a walk with my camera and snapped a few pictures of the sun. The pictures didn't turn out too well, but this was to be expected without a telescope. I thought it might be a nice ...
16
votes
8answers
2k views

How to avoid procedural loops in this example?

I want to iterate over a list, and return the index as long as I found the first element satisfying my condition. Written in a for loop: ...
15
votes
6answers
829 views

Better way to sort course grades

I use the following code to create a histogram of grades. Grades are provided as a List of strings such as "A-", "D+", "E" (don't ask), etc. The +/- substitution hack is necessary to sort the grades ...
15
votes
4answers
1k views

Button evaluation inside DynamicModule

Someone know why this Button works when clicked: ...
15
votes
3answers
445 views

Clever use of DownValues, or failure to understand the “Mathematica Way.”

I needed to look at the changes in the values of about 4000 items from one year to the next. I have a table with identifiers and values for each year. Not all of the items are in each list. The ...
15
votes
1answer
2k views

Poisson solver using Mathematica

I am looking for some help with a Poisson solver I am writing in Mathematica. The code is quite long with Arrays plugged in, so the full details can be found at http://pastebin.com/uSrSDcW6 I am ...
15
votes
1answer
326 views

Extract what symbol is set by SetDelayed, Set, TagSet, UpSet, SetAttributes, etc

One of the recent features of the Mathematica Plugin for IntelliJ IDEA (www.mathematicaplugin.halirutan.de) is a Structure View which let's you see information about several definitions that are done ...
14
votes
9answers
915 views

Thread over list in different levels

I have these two lists, both with the same length in the first dimension: ...
14
votes
3answers
816 views

How to check the validity of an option value

I have a user-defined function, func, which takes the following form: func[arg1, arg2, options] Here is the actual code ...
14
votes
2answers
948 views

What's the most “functional” way to do Cholesky decomposition?

I can do Cholesky in a procedural style, such as: ...
13
votes
7answers
927 views

How to sort digits in a string while keeping other characters untouched?

Now there is a string with digits and letters, for instance, "95uge678r3gi89hgfe30kgh063d51". And the expected output is ...
13
votes
2answers
1k views

Parsing a CSV file using Interpreter

The new Interpreter functionality in version 10 looks like it has the potential to make parsing custom data formats very easy. I'm trying to create a CSV parser. ...
13
votes
4answers
2k views

Simpler way to fill date gaps with zero values

One basic problem when working with data like sales and dates is the case where you don't have sales in all days, so you have to fill it to take information as average or to make a plot. See this toy ...
13
votes
3answers
650 views

Concise way to draw this pyramid

I wrote the following to draw a pyramid, this approach uses functions TranslationTransform and Tuples, is there a way to do the ...
13
votes
2answers
274 views

The equivalent of dplyr mutate with Dataset

In R, there is a package "dplyr" that works on data.frame objects, which is one of the origins of the Mathematica Dataset construct. One of the things dplyr can do is "mutate" the columns of a data....
12
votes
4answers
755 views

Reduce Code Length

I have the following that works good, and computes in one shot,but the code is so long. Can someone please help me to reduce the size of this? ...
12
votes
1answer
946 views

Problems with stress distribution using FEM

I'm trying to get the displacements u(x,y) and v(x,y) of a beam simply suported, the stress distribution in the middle section should be a linear function: but i'm getting this: My code is: ...
12
votes
1answer
546 views

How to visualize pattern matching process?

I want a function that does the same job like ReplaceRepeated, and can make the matching process visible. I searched the Internet but didn't find any existing code ...
11
votes
1answer
994 views

How to construct the RHS of Wick's theorem

I've been trying this for a few days now, but I just can't seem to find an efficient (speed-wise) functional way to code the following task. Context: I would like to construct an expressions that is ...
11
votes
1answer
342 views

Fine tuning compiled code that computes dilogarithm function

As an exercise in writing a good Compile function, I want to do the simple task of coding a routine that outputs the real part of the dilogarithm function ...
10
votes
7answers
2k views

Is there a more elegant way to write this statement?

Total[TakeWhile[Map[#^3 &, Range[1, 200]], # < 10000 &]]
9
votes
2answers
655 views

Optimizing Mathematica Code

I have implemented the whole Baum-Welch Algorithm for training Hidden Markov Models as described in Rabiner's paper Here is my code that calculates the Forward Probabilities` ...
9
votes
1answer
905 views

TemporalData frequency option as in R

I'm studying R, and am very impressed by the simplicity of some commands and in the way R handles temporal data. For example, in R, if you have some one-dimensional temporal data, you can easily ...
8
votes
6answers
322 views

Brute Force for solving Diophantine equation

I'm trying to solve the following Diophantine equation using Mathematica: $$2^x=5^y+3$$ I have a code,but is very slow. Can we speed up ? ...
8
votes
4answers
907 views

Loop optimization

I wrote this to generate a 6D distribution of particles. ...
8
votes
2answers
1k views

Speed up Mathematica code involving Convolve

I'm trying to plot the averaged autocorrelation function of a random wire profile $\xi(x)$ which is build from unitsteps which are each L units long. To calculate the average I'm generating nMax ...
8
votes
3answers
262 views

Faster way to solve android lock valid patterns

With the password grid shown in the Android home screen, how many valid passwords are possible? The rules for creating a pattern is as follows. We must use four nodes or more to make a pattern at ...
8
votes
1answer
308 views

Optimization of the following code

Consider the function $$ h:[-1,1]\times I_{\sigma}\to I_{\sigma} $$ $$ (\omega, x)\mapsto \sqrt[3]{x + \sigma \omega} $$ where $ \sigma > \frac{2}{3\sqrt{3}}, I_{\sigma} = [x_-(\sigma), x_+(\sigma)]...
8
votes
2answers
756 views

Feedback on Functional Programming: Propagation of uncertainty

Lately I've been trying to use functional prgramming and tried to implement the gaussian propagation of uncertainty (without correlation) in an elegant way, here is the outcome: ...
8
votes
1answer
541 views

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

I want to put all the columns stacked up, as a vector. The fastest code I could come up with was Transpose[{Flatten[Transpose[A]]}] Is there a faster way?
8
votes
1answer
328 views

Simpler code evaluating Dickman's function?

I've read here that Mathematica 10 can Obtain symbolic solutions to delay differential equations. Would that help in numericaly evaluating Dickman's function $\rho(u)$ ? It is a delay differential ...
8
votes
1answer
364 views

Selecting a list from a list of lists

In working through Programming Paradigms Via Mathematica (A First Course) I have come across this two part question: A permutation of length "n" is a list of the first n positive integers in some ...
8
votes
2answers
173 views

How to implement this simple procedural style algorithm efficiently in Mathematica style code?

I am trying to move my C solution code for project euler problem 135 to Mathematica. But I encountered serious performance problem. The algorithm is very simple, here is a plain translation: ...
7
votes
8answers
2k views

Simplify the code

I have a function that grabs the second part of the list and change it to times, ...
7
votes
2answers
771 views

Finding large Collatz Sequences, trying to improve speed

Background: I am already very much in love with Mathematica but I am also a novice. So far I have only gathered experience in procedural programming in Python. Generally speaking I am trying to ...
7
votes
2answers
434 views

Computing the twin prime constant with Mathematica

The twin prime constant is defined as $$ C _2 = { \prod \limits_{p \geqslant 3 }} \, \frac{p(p-2)}{(p-1)^2}. $$ Translating this directly in Mathematica code gives: ...
7
votes
3answers
1k views

Mathematica style (drawing cubes)

I wanted to draw (lattice-aligned) unit cubes inside a sphere with a given radius centered on the origin, so I wrote this program: ...
7
votes
2answers
305 views

Efficient Lloyd Sampling of Images

I want to sample a grayscale image so that every voronoi cell contains approximately the same total intensity using lloyd sampling. My current code is kind of slow and I was hoping for some advice to ...
7
votes
1answer
441 views

Porting code from MATLAB

I'd like to switch from MATLAB to Mathematica for my research in digital signal processing. I used Mathematica a lot during the '90s but I haven't used it since then, so I might as well be a complete ...
7
votes
1answer
289 views

The curious case of missing random-walking particles in the box

Note: I added the entire code I wrote below to generate the initial configuration and have explained step by step what each part of the code does Kindly bear with the code and the explanation. Since ...
7
votes
2answers
1k views

Elegant way to handle columns operations

Inspired by this question from @sjdh and by my recurrent use of columns operations in database sets, I was looking for one way to make columns operations more symetric, so I can handle with matrix and ...
6
votes
2answers
413 views

Sound synthesizer using Manipulate

Sound synthesizers, both digital and analog, often include a parameter section for shaping the amplitude of a sound wave over time. This parameter section is commonly known as an "ADSR EG" (attack, ...
6
votes
2answers
204 views

Looking for feedback on this function

I'm not sure this is an appropriate question; if not, I'm sure someone will close it :) I've got the following function, listed below; its job is to accept a graph (function) presented either as a ...

1
2 3 4 5
7