Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
2answers
63 views

When computation takes a long time, how to find out if this is normal or if my script contains an error?

I study mechanical engineering and I' working on my first bigger project on Mathematica right now. It's a multibody problem that leads to a system of 4 large equations I'm supposed to solve ...
1
vote
1answer
51 views

Find and number sublists

I implemented the following routine. array1 = {12, -12, 14, 12, 1, -3, 1, 1, 2, 7, 8, 102, 2, 3, 3, 1, 332, 11, 23, 2, -2, 13, 12, 1, 1, 1, 1, 1, 1, -121, 131}; ...
1
vote
0answers
74 views

How can I make my code containing a For loop more Mathematica-style?

I know there are a lot of questions on this site regarding the use of For loops and how to avoid them in Mathematica. However, in my case I believe it is justified,...
0
votes
1answer
48 views

Trying to obtain a numeric solution to an inequality. Code might be not working as intended

I need to check whether an inequality can have solutions in the real numbers, given some extra constraints on the variables. I suspect that no solutions exist but I would like some verification of ...
0
votes
1answer
53 views
5
votes
3answers
103 views

Better code for reversing and joining matrices

I am writing a function to reverse a matrix and join the reversed version to the original one, making a new matrix which I call A. And after doing this, I reverse <...
8
votes
1answer
425 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?
6
votes
2answers
383 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: ...
15
votes
6answers
784 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 ...
3
votes
1answer
46 views

What is a good way to implement a versatile folder structure checking system?

I'm writing an 'application' (set of *.wl and *.wls files) that requires a given folder structure to function; I am not sure how this structure will eventually look like, because it changes as I ...
6
votes
1answer
277 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: ...
5
votes
3answers
147 views

Improving the run time of random walk simulation

One of our professors told us that during his thesis he used Mathematica in simulating it since it is about networks, I don't know how he did it but all I know is he said it took about two weeks to ...
5
votes
1answer
88 views

Can a certain pair of expressions be compressed into one?

I've been employing this pair of statements ...
1
vote
1answer
53 views

Is this decryption function idiomatic?

This is my first non-trivial Mathematica notebook, which I wrote for a cryptology homework. It encrypts/decrypts strings using an affine cipher. Is the code readable/idiomatic? How could it be ...
0
votes
1answer
74 views

Finding the limit of a function

I have the following code: ...
1
vote
3answers
102 views

Is there any better way to generate random unique quadratic equations with rational zeros, simplest integer coefficients?

This is my attempt but I want to make it more compact if possible. ...
2
votes
1answer
63 views

How to use `Pattern` and `Slot` to match n-element conditions?

Extract the qualifying sublists. – Condition: The elements are all in the Reals and greater than zero. Question If the sublist contains many elements, how can it ...
1
vote
2answers
111 views

How can I make this code simpler, readable and more efficient?

I am running an iterative routine that I want to export to a file while each iteration is computed, instead of storing everything in memory and then exporting to a file. My solution is to write to ...
1
vote
0answers
47 views

Get Ticks both in Frame as in Axes

Why have the numbers disappeared from in the $X$ Axis ? Is there some way to keep the Frame and having the numbers on the ...
3
votes
1answer
134 views

Exporting a Manipulate expression to .avi and .gif

I've the following code: ...
1
vote
1answer
55 views

Using two functions to plot one function

I want to plot the following function, where Uin is the variable (that has to be on the x-axis): ...
1
vote
1answer
110 views

Convert MATLAB Code to Mathematica

I have written a MATLAB code as following, in order to calculate an integration involving Modified Bessel Function with variable limits: ...
5
votes
1answer
89 views

The reason behind ControlToBoxes (DynamicDump`)

Background I'm trying to establish best practices for custom controllers programming. This question is about one top level aspect of it. WRI suggested (few talks I don't remember right now) to ...
1
vote
1answer
142 views

Making a slow code with symbolic manipulations faster

I have a code with symbolic manipulations. It works fine but very slow (e.g. for w=14 it's reasonably fast but I want it to work for ...
0
votes
1answer
88 views

an answer for my yesterday question about determinant of matrix

Yesterday and about five months ago, I asked two similar questions but nobody could answer me; maybe because my question was ambiguous. But, I myself tried and I could finally find my desired answer. ...
2
votes
1answer
83 views

How to program MapList? [closed]

I would like to know if there is a nice functional way to code up a MapList which applies f to each element of ...
1
vote
0answers
73 views

Problem with Wick Contraction

I'm trying to compute wick contractions knowing that: $\langle a_a a_b \rangle = \delta_{ab}$. So far I've the following ...
0
votes
0answers
47 views

Why does my code find minima outside the square that constrains my search?

I have written a script to find the minima of a function, and then manipulate them. I have a physical expectation on what the result should look like, but I am not seeing this and I am wondering ...
0
votes
1answer
54 views

Plotting a dynamical system code fix needed! [closed]

So I'm trying to plot a system of differential equations separately and look at how their solutions behave: $$\dot{x}_{1} = x_{1} (K_{1} - x_{1}) + \epsilon (x_{2} - x_{1})$$ $$\dot{x}_{2} = x_{2} (...
1
vote
0answers
35 views
0
votes
1answer
51 views

Creating a numerical sequence with Do [closed]

I have the following code: n = 10 nt = 2 ns = nt * n inte = Range[n - 1] Do[i = inte/n, {i, nt}] I am trying to create a list that has the following result: 1/...
6
votes
2answers
96 views

Efficient way to partition list of expressions based on its contents

I have a list of polynomials polys linear in a set of variables vars. How do I partition the list based on successive dependence ...
1
vote
1answer
87 views

Writing function restrictions more efficiently

I define the two functions: ...
3
votes
1answer
72 views

Generating numbers palindromic in two number bases

The purpose of the code below is to generate numbers that are $2d+1$ digit palindromes in number base $b+1$, and are also palindromic in number base $b+3$, where: The ...
4
votes
0answers
236 views

Wick Contractions; help with a piece of code

An alternative title to this thread could be, understanding another person's code. A few years back there was a workshop on Mathematica and applications to theoretical physics; if you want I can ...
6
votes
2answers
120 views

Checkboxes to ranked list

So, my aim is to produce a user interface with checkboxes not only returning True/False, but also the order in which they were ...
3
votes
1answer
68 views

Poor performance from Manipulate and Sow-Reap

I am not a mathematician, and I barely know how to use Mathematica. But, over the course of many months and with help from this site awhile back, I managed to generate dynamic output that does exactly ...
1
vote
1answer
540 views

Converting Matlab code to Mathematica

I have following Matlab code: ...
3
votes
2answers
118 views

Evaluation of function with options affecting complexity

I am writing a function that produces a tensor with certain symmetries and I use a function option indicating the symmetry. My function definition has the form: ...
5
votes
1answer
128 views

Build this neural network layer (NoisyAnd) more elegantly?

I want to build the neural network in "Classifying and segmenting microscopy images with deep multiple instance learning". It is a fairly straightforward CNN, except that they define a custom layer ...
-1
votes
1answer
133 views

How to directly/conveniently use variable names as filenames?

Purpose To use variable name as file name conveniently. Code Special thanks to @J.M.'s tips! ...
0
votes
0answers
43 views

Select from list: has elements from another list

Consider a data list data, and a rule list rule. I want to select from data all elements ...
0
votes
0answers
66 views

Problem with a numeric 1/0 in NDSolveValue

The following code tries to solve three coupled differential equations with some fixed parameters ...
0
votes
1answer
57 views

Graph equation code fix needed! [closed]

I am trying to graph this image using this line of code ...
2
votes
0answers
124 views

Improve inverted 3D Truchet tiling?

Here is a 3D inverted Truchet tiling, p is "radius of the tubes", n is "volume". ...
0
votes
1answer
55 views

Simplify a short code

Is there a better way to write the following? I am trying to make my code as clean as possible. ...
0
votes
0answers
34 views

Eigenvectors calculation doesn't match from two identical results

I have a $3\times 3$ matrix, depending on a single parameter. My job is to find the eigenvectors for the matrix. I tried two ways, shown below ...
0
votes
2answers
99 views

How to avoid the redundant curly brackets {} when passing list parameters/arguments?

(This question comes from How to swap multiple columns at once directly?) In: ...