Skip to main content

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
41 votes
2 answers
1k 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, ...
Murta's user avatar
  • 26.3k
29 votes
8 answers
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 ...
F'x's user avatar
  • 10.8k
19 votes
12 answers
1k views

Thread over list in different levels

I have these two lists, both with the same length in the first dimension: ...
Murta's user avatar
  • 26.3k
15 votes
1 answer
3k 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 ...
Joel D's user avatar
  • 151
45 votes
10 answers
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: <...
Marco Aviles's user avatar
15 votes
4 answers
1k views

Button evaluation inside DynamicModule

Someone know why this Button works when clicked: ...
Murta's user avatar
  • 26.3k
15 votes
5 answers
3k 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 ...
Murta's user avatar
  • 26.3k
5 votes
3 answers
3k views

Using DSolve to solve for x[t,parameter]

I'm trying to solve an ODE with two independent variables (a cannon firing from a cliff incorporating wind resistance dependent on velocity). I've tried the following for the x-component: ...
xaxXos's user avatar
  • 77
11 votes
1 answer
397 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 ...
QuantumDot's user avatar
  • 19.7k
5 votes
2 answers
315 views

how to generate repeatable Unique streams?

I need to label some data in arbitrarily nested ragged lists, with a stream of unique labels. It's very convenient if the labels are consecutive integers, starting at ...
Reb.Cabin's user avatar
  • 8,693
41 votes
1 answer
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 ...
bobthechemist's user avatar
16 votes
3 answers
1k 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 ...
xyz's user avatar
  • 625
3 votes
1 answer
248 views

Unbounded solution at boundaries with few combination of values in this BVP solution

I had asked a question here regarding solution to a BVP problem. bbgodfrey provided an excellent answer using the method of integrated least squares. However, for a few specific set of values of ...
Avrana's user avatar
  • 297
8 votes
2 answers
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 ...
Murta's user avatar
  • 26.3k
6 votes
2 answers
224 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 ...
rogerl's user avatar
  • 4,209
5 votes
6 answers
3k views

Why is this Mandelbrot set's implementation infeasible: takes a massive amount of time to do?

The Mandelbrot set is defined by complex numbers such as $z=z^2+c$ where $z_0=0$ for the initial point and $c\in\mathbb C$. The numbers grow very fast in the iteration. ...
hhh's user avatar
  • 2,613
4 votes
3 answers
354 views

Is there something wrong with this piece of code involving Manipulate?

I am playing around with the Manipulate sentence. I think the following code is right, and actually Mathematica 9.0 doesn't return any error message, but I am ...
Vicent's user avatar
  • 1,101
27 votes
2 answers
2k 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 ...
anderstood's user avatar
  • 14.3k
16 votes
1 answer
397 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 ...
halirutan's user avatar
  • 113k
15 votes
6 answers
935 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 ...
bobthechemist's user avatar
13 votes
2 answers
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. ...
Szabolcs's user avatar
  • 236k
13 votes
1 answer
1k 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: ...
Gonza_'s user avatar
  • 153
7 votes
1 answer
431 views

What is wrong with this code? (Usage of FunctionInterpolation and how to make code efficient)

I am trying to create an interpolation out of a function that comes from an improper integral. The end goal is to make a 3d plot over a specific region. Specifically, I have the following code: ...
AG1123's user avatar
  • 571
7 votes
1 answer
363 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 ...
Ali Hashmi's user avatar
  • 9,000
5 votes
2 answers
721 views

Collatz Tool Box --- any speed ups possible?

I have created a tool box for experimenting with the Collatz conjecture. Info here: Wolfram MathWorld and here: Wikipedia The patterns I have found allow us to make the conjecture jump through some ...
Fred Daniel Kline's user avatar
5 votes
1 answer
210 views

Which NDSolve method is most effective for the given chaotic system?

Note: This question is for those chaotic systems where the order of maximal Lyapunov exponent is around $10^{-3}$. So it's different from normal chaotic systems like Hénon–Heiles system, Yang-Mills-...
codebpr's user avatar
  • 2,273
4 votes
2 answers
2k views

Numerical Integration Via Adaptive Simpson's Method

I have written a code that uses the Adaptive Simpson's method to approximate integration. For those who are unaware of this Adaptive Simpson's method; Adaptive Simpson's method In my code, I count ...
larry's user avatar
  • 43
4 votes
1 answer
206 views

How to index multi-dimensional vectors in NDSolve/WhenEvent?

I want to solve a system of coupled ODEs using NDSolve with WhenEvent. But I'm having a hard time trying to index the state ...
B. Chen's user avatar
  • 43
2 votes
1 answer
1k views

Passing function as argument

I've been mucking around with passing functions as arguments to other functions and via trial and error seem to have something that works. I was wondering if someone could have a quick look at some ...
Freakalien's user avatar
1 vote
2 answers
137 views

How to make the following code work faster?

The problem I would like to compute the Lorentz transformation $\mathbf{p}'$ of an arbitrary 3-momentum vector $\mathbf{p}$ given a boost characterized by some other momentum vector $\mathbf{p}_{N}$, ...
John Taylor's user avatar
  • 5,851
1 vote
2 answers
224 views

How to deal with the condition that a function own many options?

Assming that I have a function myFunc which has some options. ...
xyz's user avatar
  • 625
1 vote
1 answer
446 views

Lookup construction using data matrix and Reap/Sow versus AppendTo`s

I am a user of MMA9 and made my own kind of Lookup, which is now a standard command in Version 10. In my program there is a section in which by means of a StringReplace[ ]-command containing a lot of ...
Rombert's user avatar
  • 99
47 votes
0 answers
3k 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 ...
Markus Roellig's user avatar
25 votes
1 answer
1k 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 ...
whuber's user avatar
  • 20.6k
17 votes
10 answers
1k 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 ...
vapor's user avatar
  • 7,921
16 votes
3 answers
510 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 ...
George Wolfe's user avatar
  • 5,462
14 votes
2 answers
1k views

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

I can do Cholesky in a procedural style, such as: ...
Editortoise-Composerpent's user avatar
12 votes
1 answer
618 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 ...
partida's user avatar
  • 6,836
9 votes
3 answers
360 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 ...
matrix42's user avatar
  • 7,138
9 votes
1 answer
412 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 ...
Clif's user avatar
  • 697
6 votes
1 answer
218 views

Path construction using `Nearest` shows ill behavior?

I have a dataSet that I'm trying to sort choosing a starting point and going to its nearest neighbor, and then to its nearest neighbor and so on. To do so, I've ...
Pragabhava's user avatar
  • 1,619
5 votes
0 answers
108 views

Techniques in debugging with preview of possibly large expressions + garbage collection issues

First of all, all appreciate all suggestions how to rephrase the title to make it more friendly. Here's the background: Let's say your top level procedure flow is complicated, involves url calls, ...
Kuba's user avatar
  • 137k
5 votes
5 answers
657 views

Rotating squares

I have here an example of how a rotation matrix operates a parametric representation of a square. ...
JEM's user avatar
  • 1,165
5 votes
3 answers
3k views

How to fix BodePlot that comes with Mathematica?

I am using Mathematica to go through the examples and exercises on the book Modern Control Systems, 6th edition by Dorf. On page 605, there is a table (Table 8.5) with the Bode plot for several ...
Ed Mendes's user avatar
  • 618
5 votes
1 answer
172 views

Can someone please explain the code from another question

Source: Position of a pattern-matched part of an expression Can someone please explain the following code by Leonid Shifrin. ...
user13892's user avatar
  • 9,443
5 votes
1 answer
106 views

Can a certain pair of expressions be compressed into one?

I've been employing this pair of statements ...
Paul B. Slater's user avatar
4 votes
1 answer
103 views

Why is my version of Euler's Method not working?

I am creating Euler's method for $\frac{dw}{dt}=(3-w)(w+1)$ for $\Delta t=0.5$ from $0\le t \le 3$. I tried the following. ...
Arbuja's user avatar
  • 71
4 votes
2 answers
270 views

Making geometries with Mathematica for use in Ansys

I am trying to model deformations caused by identical, solid half-ball bearings on a thin, solid cylindrical plate in Ansys. But drawing the geometries is much easier in Mathematica, especially if I ...
Teg Louis's user avatar
4 votes
4 answers
444 views

A simpler or more concise way to divide the boundary of a polygon into equal arc lengths?

I am really looking just for the least amount of code needed to do this but it is still readable. Here is the same polygon but the border is respectively split up into 10, 20, and 30 points along the ...
Teg Louis's user avatar
4 votes
1 answer
98 views

Speed up functions using variable types but not Compile

I am writing a large numerical code and I have set up many functions that take several arguments as input. In the functions I am using several built in Mathematica functions, such as Sum[], Part[], ...
Matteo's user avatar
  • 283