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
1 vote
3 answers
70 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 ...
Arbuja's user avatar
  • 29
1 vote
3 answers
259 views

Creating an interactive plot that zooms into the function

Suppose we want an interactive plot for the code: ...
Arbuja's user avatar
  • 29
6 votes
4 answers
676 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-...
jjoIV's user avatar
  • 153
0 votes
0 answers
126 views

Codewars and Mathematica

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 ...
Michel's user avatar
  • 1,596
9 votes
1 answer
138 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. ...
Diffycue's user avatar
  • 1,721
2 votes
2 answers
336 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.
William's user avatar
  • 7,565
7 votes
5 answers
599 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: ...
Joshua Salazar's user avatar
6 votes
4 answers
375 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 ...
AlbertLew's user avatar
  • 453
1 vote
2 answers
108 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): ...
Deniz's user avatar
  • 215
-2 votes
1 answer
96 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{...
Ali Shadhar's user avatar
-1 votes
1 answer
59 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.
Ali Shadhar's user avatar
3 votes
4 answers
209 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: ...
Bob Werner's user avatar
2 votes
0 answers
160 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 ...
Gert's user avatar
  • 1,460
0 votes
0 answers
43 views

Finding the limit of the average of a sequence?

...
Arbuja's user avatar
  • 29
5 votes
0 answers
177 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 ...
Just Some Old Man's user avatar
0 votes
0 answers
118 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 ...
polfosol's user avatar
  • 930
0 votes
1 answer
43 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 ...
user62716's user avatar
  • 711
3 votes
0 answers
220 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 ...
I.M.'s user avatar
  • 2,761
2 votes
1 answer
187 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 ...
Ali Shadhar's user avatar
0 votes
1 answer
418 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 ...
MMS's user avatar
  • 87
6 votes
1 answer
183 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 ...
Picaud Vincent's user avatar
0 votes
1 answer
59 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-...
Qy Ln's user avatar
  • 69
2 votes
1 answer
136 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....
Qy Ln's user avatar
  • 69
4 votes
3 answers
411 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 ...
Qy Ln's user avatar
  • 69
0 votes
1 answer
67 views

Find the area which is between two curves using Nintegrate

...
Aran's user avatar
  • 363
0 votes
1 answer
69 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 &...
Aran's user avatar
  • 363
1 vote
0 answers
124 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 ...
Nam Nguyen's user avatar
  • 1,751
4 votes
0 answers
143 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 ...
Nam Nguyen's user avatar
  • 1,751
5 votes
2 answers
146 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 ...
Matt Murphy's user avatar
1 vote
2 answers
67 views

Need help with making code work on Mathematica

...
Aran's user avatar
  • 363
2 votes
0 answers
48 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 ...
Aran's user avatar
  • 363
3 votes
1 answer
161 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 ...
Jules Manson's user avatar
  • 2,397
2 votes
1 answer
102 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 ...
abcd's user avatar
  • 483
6 votes
0 answers
88 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 -...
Julian Moore's user avatar
  • 2,560
7 votes
1 answer
144 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: ...
M.R.'s user avatar
  • 31k
2 votes
1 answer
85 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 ...
ions me's user avatar
  • 751
5 votes
1 answer
135 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 ...
ions me's user avatar
  • 751
1 vote
1 answer
229 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]$ ...
primefinder's user avatar
4 votes
1 answer
159 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)...
DrMrstheMonarch's user avatar
9 votes
2 answers
392 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 ...
emnha's user avatar
  • 2,361
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]$ (...
Arbuja's user avatar
  • 29
4 votes
1 answer
102 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
  • 29
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 ...
Fixed Point's user avatar
3 votes
2 answers
297 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: ...
primefinder's user avatar
14 votes
2 answers
1k 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 ...
theorist's user avatar
  • 3,548
1 vote
1 answer
35 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): ...
Carlos's user avatar
  • 113
8 votes
0 answers
86 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. ...
M.R.'s user avatar
  • 31k
11 votes
3 answers
904 views

Angle increment spiral in Mathematica

What method cold I use to obtain a Plot similar to the one in the picture?
ash111's user avatar
  • 111
6 votes
1 answer
120 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 ...
Julian Moore's user avatar
  • 2,560
5 votes
1 answer
179 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 ...
Picaud Vincent's user avatar