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.
157 questions
5
votes
1
answer
447
views
How to understand or define the programming paradigm of Mathematica? [closed]
Recently, I have been struggling with the task of writing a Mathematica programming tutorial for my graduate students. My goal is to teach my students how to use Mathematica for complex scientific ...
-1
votes
2
answers
119
views
Code in one line without using compound expression [closed]
data = Range[10]^2
Table[Style[data[[n]], FontSize->data[[n]]], {n, Length[data]}]
I understand that the above can also be coded in one line. But I could do so ...
2
votes
1
answer
257
views
NDSolve: how to solve "ndnum error"
I have a problem of finding the shape of a current. please see the details here NDSolve does not accept the boundary condition
I wrote a code but it gives the "ndnum" error. I tested the ...
0
votes
0
answers
107
views
NDSolve does not accept the boundary condition
I am new at Mathematica and trying to solve a nonlinear PDE with the help of NDSolve.
H, the height of the current is a function of x and t. Based on the context I know the current has a parabolic ...
1
vote
1
answer
156
views
Customising the editor: (real) monospace font and dark theme (and more)
I don't know how to make the code editor in Mathematica present the code in a real 'monospace' font, with a dark, customisable theme (syntax highlighting and so on). This is something that keeps me ...
0
votes
1
answer
156
views
How to make code for boundary value problem?
I am trying to make graphs given by using this code but I failed. Please correct this code.
...
1
vote
3
answers
148
views
Labeling point coordinates in path graph using VertexLabels
Question: How do we label point coordinates in Pathgraph using VertexLabels.
Suppose we have a list of points ...
1
vote
3
answers
396
views
Creating an interactive plot that zooms into the function
Suppose we want an interactive plot for the code:
...
7
votes
4
answers
943
views
How to elegantly implement this one useful object-oriented feature in Mathematica?
I wrote some Mathematica code that defines a function I want to use repeatedly (f) inside another function (finit) that pre-...
0
votes
0
answers
144
views
Codewars and Mathematica [duplicate]
I looked up the Wolfram Language on Codewars as the site is great to improve coding.
WL is not among the listed languages (I checked the beta versions as well). I assume this is because the language ...
9
votes
1
answer
206
views
Best practices or hidden drawbacks to define multiple-argument functions
I was wondering if there were any important differences or stylistic considerations for deciding between different ways to define functions of multiple variables, e.g.
...
2
votes
2
answers
412
views
Would it be poor advice to assign roman numerals as top variables?
For example like so.
x = 10;
xi = 11;
xv = 15;
Is there an automated way to spit these numbers out maybe 1-1000.
8
votes
6
answers
909
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:
...
8
votes
4
answers
590
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 ...
1
vote
2
answers
169
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):
...
-1
votes
1
answer
141
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{...
0
votes
1
answer
138
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.
3
votes
4
answers
450
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:
...
2
votes
0
answers
214
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 ...
0
votes
0
answers
87
views
5
votes
0
answers
261
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 ...
0
votes
0
answers
253
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 ...
0
votes
1
answer
75
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 ...
3
votes
0
answers
297
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 ...
2
votes
1
answer
234
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
...
0
votes
1
answer
618
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 ...
6
votes
1
answer
252
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 ...
0
votes
1
answer
109
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-...
2
votes
1
answer
175
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....
4
votes
3
answers
751
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 ...
0
votes
1
answer
100
views
0
votes
1
answer
109
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 &...
3
votes
0
answers
167
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 ...
4
votes
0
answers
184
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 ...
5
votes
2
answers
210
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 ...
1
vote
2
answers
110
views
2
votes
0
answers
70
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
...
4
votes
1
answer
269
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 ...
2
votes
1
answer
161
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 ...
6
votes
0
answers
114
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 -...
7
votes
1
answer
208
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:
...
2
votes
1
answer
107
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 ...
5
votes
1
answer
170
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 ...
2
votes
1
answer
317
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]$ ...
5
votes
1
answer
255
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)...
9
votes
2
answers
446
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 ...
0
votes
0
answers
40
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]$ (...
4
votes
1
answer
160
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.
...
2
votes
0
answers
115
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 ...
3
votes
2
answers
406
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:
...