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
2
votes
3answers
614 views

Having trouble translating Matlab fsolve into Mathematica

I searched the site and found that someone said FindRoot is equivalent to fsolve, however, without proper options, ...
14
votes
3answers
821 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 ...
1
vote
2answers
269 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 ...
0
votes
0answers
26 views

Improvement of a method to plot values of a section of a line that moves with time

I have this code which animates a layer of fluid moving on the outside of an ellipse with a moving black point on the cylinder surface itself. I want to plot a line from the origin to the outer layer ...
3
votes
2answers
98 views

How can I map edgeweights from a matrix onto edges along the paths generated by another matrix

Suppose that we have two matrices with identical dimensions: matA and matB. I want to identify a set of pathways using ...
0
votes
0answers
54 views

help with improving my code

I am fairly new to MMA-12 and have written the following code to calculate possible coordinates (for given constraints [see If[]] in the code) and get the ...
4
votes
1answer
485 views

Is this a bug or my mistake?

This: Graph[{1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 4 \[DirectedEdge] 1}] AdjacencyMatrix[%] // MatrixForm generates: But, this ...
2
votes
1answer
68 views

How to automize Shannon's Entropy calculation using a MM function

With the following code, I calculate Shannon's Entropy function HH1 for a given matrix mat. ...
0
votes
2answers
63 views

How can I index a function with the type of dataset used

I have the following code which serves my purpose for a single matrix input. For one or two matrices, the operation can be done manually, but I have 65 matrices and ...
1
vote
1answer
43 views

How can I use DeleteMissing[] to delete “complicated rules” from a matrix

The following works as intended: ...
0
votes
1answer
76 views

Solving an equation in integers giving an errormessage

I've the following code: ...
8
votes
6answers
328 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 ? ...
2
votes
1answer
59 views

How can I plot a 2D `LayeredGraph` on a 3D plane with a MMA function

In my previous question, I wanted to plot a 2-dimensional MMA LayeredGraph[...] on a 3-dimensional surface (or plane), and @KGLR answered the question in the ...
1
vote
4answers
132 views

Compute moments, variance, skewness, mean from the PDF of a non standard distribution

I have tried to compute Skewness and variance and moment using the code below, I have tried first to create a distribution from my PDF using the command ...
0
votes
1answer
63 views

Defining relations between functions in Mathematica

I would like to find a way to implement the following in Mathematica: Let $F$ be the set of real functions $f_1,f_2,\ldots f_n$. The function $\min$ is defined as follows... $$\min F=f_i\iff \forall ...
0
votes
2answers
104 views

How can I transform my calculation into a function?

I have written the following code, which serves my purpose. However, I like to have a Mathematica function such as ...
2
votes
1answer
97 views

Saving Dot-ed eigenvectors in a special comparison [closed]

We have produced a 4*4 matrix in any iteration containing nx from 0 to 2 by steps of 0.5. and in any iteration we have saved sorted orthogonalized-eigenvectors based on their eigenvalues with this ...
2
votes
2answers
118 views
8
votes
3answers
265 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 ...
1
vote
1answer
103 views

Correct use of Reap/Sow to optimize the code

I am trying to compare two functions on the end points of an interval. For instance, $f_1(t)=t^3$ and $f_2(t)=t^4$. From my code below I check whether $f_1(t)\leq f_2(t)$ when $t=50$ on $[50, 51]$ ...
0
votes
0answers
38 views

NIntegrate convergence problem

I have an integral that does not converge satisfactorily for some values and appears random. I have included the code (and plot). However, since the integral depends of imported data, it cannot be ...
0
votes
0answers
31 views

Why is my code for density using Folner Sequences giving incorrect values? [duplicate]

I am creating a code that calculates or approximates $$D(S\cap[a,b])=\lim_{n\to\infty}\frac{\left|S\cap{F_n\cap[a,b]}\right|}{\left|F_n\cap[a,b]\right|}$$ where $D$ is the density of $S\cap[a,b]$ (...
0
votes
1answer
55 views

Why is Manipulation not creating a graph?

Why does manipulation not give a graph? ...
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)]...
5
votes
2answers
124 views

Building Truncatable Primes using Nest(List), While, Fold

Here is a video on Truncatable Primes. I tried to build it myself. Here is what I have tried using the method in the video for left Truncatable Primes. ...
4
votes
1answer
78 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. ...
3
votes
2answers
104 views

Extract outputs from a WHILE loop in a table format

How can I convert my multiple outputs of a while loop in a table form and save it for later use. Here is a sample code: ...
4
votes
2answers
127 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
2answers
104 views

How to make my code to generate polytopes more efficient?

The main purpose of my code is to generate polytopes by starting with a point. With this point, I then apply 3 actions which generates at most 3 others points: ...
1
vote
1answer
75 views

Improve code style

I want to calculate r, theta and phi. My code is very ugly. How would you propose to obtain the same results? ...
0
votes
0answers
54 views

What techniques have you employed to avoid loop in Mathematica? [duplicate]

It appears to me that loop in mathematica is very slow (compared with matlab). Since many people still use mathematica to finish their work, there must be some techniques to bypass this problem. ...
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, ...
0
votes
0answers
64 views

Fourier Transform and Inverse Fourier Transform of Lists

I am trying to compute the Fourier transform of a list, say an array of the form {{t1, y[t1]},.....{tn, y[tn]}}; apply some filters in the spectral components, and then back transform in time domain. ...
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 ...
2
votes
1answer
169 views

Monte Carlo Simulation of a Double Well Potential (Edited)

I am looking for a possible way to simulate the temperature effect on a collection of atoms sitting within a double well potential, say something like ...
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: <...
3
votes
1answer
136 views

User implemented SHA-1 isn't outputting correct hash

EDIT: I'm actually Thinking the issue is in the initialization phase, where we pad the message with 1's, 0's, and the message ...
2
votes
1answer
43 views

User Implemented SHA-1 is failing for strings of length >512 bits

So, earlier, I posed a question regarding getting a user implemented SHA-1 hash function to work correctly. Well, I fixed the problems, cleaned it up a little bit, but now I have the problem for when ...
3
votes
2answers
1k views

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

I wrote a module in Mathematica which finds all possible pairs of integers from a specified list of integers (which can be negative, zero, or positive) which sum to a specified integer m. The only ...
2
votes
1answer
61 views

Cell formatting and hiding code

I've written some code that's been getting longer and longer, Is there a way to "hide" the section of the code that does not require input so that i can just give it my two inputs and jump straight to ...
1
vote
0answers
114 views

Sum over cyclic permutation of indices

To define the Schouten bracket I need to be able to sum over a cyclic permutation of the indices: $$ [\Phi,\Xi]_S=\mathfrak S_{i,j,k} \left(\Phi^{is}\partial_s\Xi^{jk}+ \Xi^{is}\partial_s\Phi^{jk}\...
1
vote
1answer
69 views

Mathematica only graphs the upper part of my cylinder

I have this code which first is supposed to make random points, draw lines through the points and then make these lines radical axes of cylinders. I have two problems with my code. First, it only ...
1
vote
1answer
1k views

Converting Matlab code to Mathematica [closed]

I have following Matlab code: ...
5
votes
1answer
151 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. ...
3
votes
2answers
117 views

Dealing with stiffness in solving Mukhanov-Sasaki equation

The code that I wrote is rather long, but I have included the whole code. ...
0
votes
1answer
61 views

I have a working code, but work very slowly. want to improve the code

I have working code which gives the result as expected by me. But the problem is it is taking too much of time to give me those results. I have a determinant function which depends on z1,z2,z3, kt,m, ...
0
votes
0answers
27 views

Relative Chop for Fractions with Trigonometric Terms

I have a fraction, which contains long sums of various terms that might include trigonometric functions in both the numerator and denominator. I want to get rid of the negligible terms. This is my ...
3
votes
2answers
195 views

Animated Lissajous Curves Parametric Plot

I love mathematical GIFs' power to illustrate and communicate math concepts widely. I haven't had much luck with the build-in ListAnimate on MMA Online but ...
1
vote
1answer
64 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}; ...
0
votes
0answers
103 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,...

1
2 3 4 5
7