Questions tagged [coding-style]

Questions on how to write code in a better or different style, using Mathematica's capabilities for coding in multiple styles.

Filter by
Sorted by
Tagged with
7 votes
5 answers
387 views

Smart way to remove rules from lists, leaving just values, specifically from FindMinimum output

I would like to know if there is a direct way to remove the rules from a list of rules. I know that, simply, this can be done with: ...
user avatar
5 votes
4 answers
289 views

How can I efficiently define functions with different names but doing the same stuff?

Recently I have met with with this interesting question:How can I efficiently define functions with different names but doing the same stuff? Somebody may want to ask me why I want to do things like ...
user avatar
  • 289
0 votes
1 answer
43 views

How to organise definitions that contain named parameters

I have lots of definitions of (usually numeric) quantities that depend on parameters; they're mostly matrices (in the actual code, there are many more definitions): ...
user avatar
  • 205
-2 votes
1 answer
88 views

How to find the Mathematica command for the function $a_k?$ [closed]

I am trying to find the $n$-th derivative of $\csc(m\pi)$, so I took few cases: for simplicity let $x=\cot(m\pi)$ and $y=\csc(m\pi)$, $$\frac{d^0}{dm^0}\csc(m\pi)=\pi^0(\color{red}{1}x^0y^1)$$ $$\frac{...
user avatar
-1 votes
1 answer
48 views

what is the Mathematica command for the Euler numbers $E_k?$ [closed]

We know that the Euler numbers $(E_r)$ has many integral and series representations but I am wondering if there is a simpler Mathematica command.
user avatar
3 votes
4 answers
136 views

Fall through multiple statements in Switch[]

Is there a nice way in Mathematica to express the equivalent of Switch where statements (not cases) fall through? Here is a toy example in C: ...
user avatar
1 vote
0 answers
120 views

Coding style/formatting rules used by Wolfram developers [closed]

I wondered whether there is a known formatting guide that Wolfram employees (have to) use when writing Mathematica code? As stated in Mathematica style guide? there are many different formatting ...
user avatar
  • 928
0 votes
0 answers
40 views

Finding the limit of the average of a sequence?

...
user avatar
  • 199
4 votes
0 answers
116 views

What do “Analyze Notebook" and "Analyze Cells" Do?

What does "Analyze Notebook" and "Analyze Cells" do? They seem to, at least, point out some coding errors that are already highlighted in a notebook. Do they do anything else? Is ...
user avatar
0 votes
0 answers
67 views

Determining whether a matrix is in Jordan canonical form

At first, I was surprised that Mathematica doesn't have a built-in function for this, e.g. JordanMatrixQ. Anyway, we all know that a Jordan matrix is consisted of ...
user avatar
  • 789
0 votes
1 answer
38 views

Loop for different values

I have the following: ClearAll["Global`*"] n= 3; T = Transpose[Table[{t^i}, {i, 0, n}]]; B = DiagonalMatrix[Range[n], 1, n + 1]; Print[ T, B] I want ...
user avatar
  • 703
3 votes
0 answers
171 views

Robust PCA (better Mathematica style)

I've parsed (almost verbatim) Python RPCA implementation to WM. Can it be rewritten using a better WM style? In particular, I'm not happy with ...
user avatar
  • 1,556
2 votes
1 answer
176 views

An alternative command to compute a logarithmic integral [closed]

I am trying to see if Mathematica can calculate: $$\int_0^1\frac{\ln(x)\ln(1-x)\ln(1+x)}{x}dx,$$ which has a closed form found here. So I tried ...
user avatar
0 votes
1 answer
264 views

How to remove complex infinity

I have an equation for the effective potential for a neutral particle (non-spinning), i.e. VeffNonSPIN[r_, θ_] := (1 - 2/r)*(1 + LL^2/(r^2 Sin[θ]^2)); If I take ...
user avatar
  • 87
6 votes
1 answer
149 views

Mimicking named arguments, a good idea? (programming style)

With respect to code readability I like the possibility to use keyword (named) arguments. I would like to have this under Mathematica, but AFAIK there is no native support for that. By example, to ...
user avatar
0 votes
1 answer
58 views

How can I add the tangent line from Fun 2 to Fun 3 to this function?

Question: I need to move the point Fun1 to 10 positions in the interval [-3,0), and have the drawing updated each time (including the tangent and intersection points). I don't know how to add Fun2-...
user avatar
  • 69
2 votes
1 answer
131 views

Question about finding the area

Here is a link to my previous question Question about plotting a curve and tangent lines I want to know how can the area be found between the curve and tangent line for between Point Fun1 and Fun2....
user avatar
  • 69
4 votes
3 answers
270 views

Question about plotting a curve and tangent lines

This is a Mathematica question. I have a function $y=x^3$. “Fun1” is any point on the curve. At the point Fun1, draw a tangent line at that point to the curve. At another point where the curve and the ...
user avatar
  • 69
0 votes
1 answer
64 views

Find the area which is between two curves using Nintegrate

...
user avatar
  • 363
0 votes
1 answer
65 views

Question about finding tangents on mathematica?

How can I find the tangent at the point $(2,8)$ to the curve $y=x^2+4$ on Mathematica? How can I show the tangent of the curve at the point and I need to mark the point where they meet with the word &...
user avatar
  • 363
1 vote
0 answers
91 views

The danger of pattern matching?

I'm reading the book Mathematica® programming: an advanced introduction by Leonid Shifrin and there is a very nice evaluation. Here is: This, plus a large number of quite generic and efficient ...
user avatar
  • 1,609
4 votes
0 answers
130 views

What is the best coding style for performance?

The problem of Mathematica is there are too many ways to perform one task. That leads to confusion for new users because the performances of these methods are so different. I'm learning the best ...
user avatar
  • 1,609
5 votes
2 answers
137 views

Modify each element depending on previous elements

I'm looking to see if there's a more idiomatic/concise/neat solution than what I have for this. I have a list of boxes with heights and I want to stack them; that is, add to each box the distance to ...
user avatar
1 vote
2 answers
64 views

Need help with making code work on Mathematica

...
user avatar
  • 363
2 votes
0 answers
47 views

How to calculate how many points in function f are greater than the average of functions g and k?

Consider 3 functions f, g and k Suppose we want to know for 3. functions from P={(x, y )|x=-1,-0.9,-0.8,-0.7....,1 and y=-1,-0.9,-0.8,-0.7....1 this is what i have so far ...
user avatar
  • 363
3 votes
1 answer
134 views

What are some debugging helpers or tips/tricks in your Mathematica toolbag?

I am a novice so imagine my confusion with Mathematica's very poor error and trace messages. Debugging should be an easy thing to do even for beginning programmers but not with Mathematica. What are ...
user avatar
  • 1,787
2 votes
1 answer
79 views

Write repeated expressions within a rule more simply

Is it possible to shorten the length of a (delayed) rule (i.e. lhs :> rhs) when there are expressions which repeat several times? Those expressions involve ...
user avatar
  • 483
6 votes
0 answers
83 views

In over my head in trying to create a debugger

Apologies for a less than ideal question, but if I could have asked a good question the answer would probably have been revealed in the process... In answer to this question @LeonidShifrin gave some -...
user avatar
  • 2,410
7 votes
1 answer
137 views

Quick way to preview and tweak styles?

I find the old stylesheet UI clunky and hard to use: FrontEndExecute @ FrontEndToken @ "EditStyleDefinitions" There's also the clunky style previewer as well: ...
user avatar
  • 30.3k
2 votes
1 answer
79 views

BarChart simultaneous axes label and numerical label

So I want to be able to put axes labels simultaneously as the numerical labels. I can achieve that with the following, but is there a better/cleaner way? I can't use callout, because I am using ...
user avatar
  • 639
5 votes
1 answer
127 views

Best Practice: spending transactions to expense list

So lets say I have a list of spending transactions tlist = {{john,23},{bob,2},{john,7}...} and I want to have a list of the total expenses per person. I can do ...
user avatar
  • 639
1 vote
1 answer
184 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]$ ...
user avatar
4 votes
1 answer
129 views

Project and notebook design

I have been using Mathematica now for a little over a year...this started off in maybe a modern 'classic' way of having to solve symbolic mathematics and related plus the want to learn a CAS (and more)...
user avatar
9 votes
2 answers
390 views

Using vtx[] instead of vtx

There're two ways to generate a random tree as 1 and 2 below. Both work fine here. So what's the reason to use vtx[] instead of ...
user avatar
  • 2,147
0 votes
0 answers
35 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]$ (...
user avatar
  • 199
4 votes
1 answer
95 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. ...
user avatar
  • 199
2 votes
0 answers
87 views

How can I make this "Pick" inside a "Table" run faster?

I am working on becoming more and more familiar with MMA and on a scale from one to ten, I would rate myself as a four or a five. I understand vectorization and that loops-are-very-bad. I have ...
user avatar
3 votes
2 answers
233 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: ...
user avatar
14 votes
2 answers
771 views

Map vs. Table for index-specific operations on 2D arrays

Many experienced users on this site tend to use Map (and its variants, MapAt, MapIndexed, etc.) rather than Table. When applying the same operation to every element of an array, Map does seem more ...
user avatar
  • 3,446
1 vote
1 answer
31 views

RecursionLimit errors on Manipulate a DensityPlot expression

I am trying to do an interactive visualization of a very simple neural network (inspired by tensorflow playground): ...
user avatar
  • 113
8 votes
0 answers
84 views

Design Patterns for System Functions?

Using PrintDefinitions give one a great view into many system functions. Often the design of these symbols incorporate multiple patterns that I see again and again. ...
user avatar
  • 30.3k
11 votes
3 answers
868 views

Angle increment spiral in Mathematica

What method cold I use to obtain a Plot similar to the one in the picture?
user avatar
  • 111
4 votes
1 answer
105 views

How to improve on this Stylesheet Manipulation for Message Styling

The answer by @Kuba changing color of error messages did not seem to work for me (MMA 11.0.1 Win 10 64-bit). I couldn't work out why and implemented something else that does work for me, but it seems ...
user avatar
  • 2,410
5 votes
1 answer
158 views

How to safely forward/manage options between functions?

Algorithms can often be splitted into smaller and reusable functions (that's a good thing). However with this scheme it is not clear to me how to ensure the consistency of the option values and ...
user avatar
1 vote
0 answers
44 views

Generic symbols vs Generic functions arguments [closed]

I've got a lot recursion relation that takes multiple hours at a time to run. I'm looking for some subtle reasons why it takes so long, and hoping to be able to reduce the time it takes for the more ...
user avatar
  • 135
3 votes
0 answers
72 views

Good habits for handling intense computations with mathematica

I always used mathematica for more "theoretical" computations: maybe I had to handle intricated manipulations, but things that you could almost do on paper. Now I just joined a new project where we ...
user avatar
2 votes
0 answers
71 views

Another for loop question: efficiency versus readability

I have some sorted data: data = Sort[RandomInteger[{0, 15}, 10]] and I want to generate a duplicate array where a counter is ...
user avatar
8 votes
1 answer
660 views

Can we turn this for loop into a more elegant Mathematica code?

I am coding stuff manipulating indirection arrays and I have some code like: ...
user avatar
3 votes
1 answer
207 views

Whats the difference between Cases and Select? [duplicate]

Im trying to further my understanding of mathematica coding and came up across this question. What is the main difference between Cases and ...
user avatar
5 votes
1 answer
161 views

Create a graph from a huge list of edges

I have a graph with ~ 360 000 nodes and ~90000000 edges. Unfortunately, I can't create a graph from the list of edges, the process not converge. An example of the code is: ...
user avatar