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
6
votes
0answers
105 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
1answer
46 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
1answer
109 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 ...
1
vote
1answer
89 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]$ ...
3
votes
1answer
74 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
2answers
370 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
1answer
48 views

How to extract the last output from the Maximize

Pitty on me! Still a beginner. After using Mathematica for 8 years and spending an hour on google, I remained unsuccessful to find an answer of: How to extract the last element of the output of ...
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]$ (...
4
votes
1answer
76 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
0answers
77 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 ...
0
votes
1answer
56 views

Assert that prints message and aborts computation?

I'd like to add asserts which check for condition, print useful message and abort computation on failure. IE, ...
3
votes
2answers
95 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: ...
13
votes
2answers
392 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 ...
1
vote
1answer
18 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): ...
8
votes
0answers
72 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. ...
11
votes
3answers
832 views

Angle increment spiral in Mathematica

What method cold I use to obtain a Plot similar to the one in the picture?
4
votes
1answer
86 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 ...
5
votes
1answer
128 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 ...
1
vote
0answers
35 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 ...
3
votes
0answers
70 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 ...
2
votes
0answers
66 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 ...
7
votes
1answer
641 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: ...
3
votes
1answer
92 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 ...
5
votes
1answer
136 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: ...
1
vote
0answers
73 views

Postfix operator //, when to use it [closed]

The postfix operator // seems more legible when applying formatting functions or functions that provide general information about an object, like Length does. Is it acceptable to use it in those ...
6
votes
1answer
253 views

Notebook layout best practices [closed]

I realise this is a very broad and subjective topic, but I wanted to know if there are any good sources for best practices on organising the layout of a notebook. I often find my documents hard to ...
2
votes
3answers
140 views

Problem with writing a basic function to produce number of multiples

I'm working my way through Wolfram Challenges, the first question was Ok but tripped myself up on the second question. I have a basic function which takes a positive integer n and returns the number ...
7
votes
1answer
94 views

Strategies for creating links to external systems

Often I find myself creating links to external systems, be it a converter from Mathematica to python, a link to Git/GitHub or to an electronic structure package. And I am far from the only one. I've ...
0
votes
1answer
52 views

RegionPlot: Generate region specification {x, -5, 5}, {y, -5, 5} etc. from a list?

A simple working example of RegionPlot3Dis ...
5
votes
3answers
131 views

Changing deep parts of expression specified by complex query

asso = <|"x" -> { <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>, <|"a" -> 6, "b" -> "z", "c" -> {}|>} |> I want to ...
0
votes
2answers
110 views

Is there a way to make $g$ changeable independently of $f$ in this code?

My problem is that in the below code, every time I want to change $g$ I also have to change $f$. Let's say we have three functions: $f$, $h$, and $g$ ($h$ is not really relevant for the question, but ...
6
votes
2answers
320 views

How do I improve the style, efficiency, and versatility of this code?

I have a math problem that I have solved analytically. I now want to generate a number of graphs based on different parameter values. My problem is threefold: It takes a couple of minutes to produce ...
1
vote
1answer
79 views

A line on a torus intersecting a region

I have a two-dimensional function that is periodic in both variables. We can then think of this function on the surface of a torus. Now imagine a line that endlessly spirals and wraps around the torus....
4
votes
1answer
123 views

Is there a better way to achieve the same result with {f[#],g[#]}&@list?

I have found myself recently typing over and over again similar code, namely something that looks like {f[#],g[#]}&@list. Is there a 'better' way to achieve the ...
0
votes
0answers
37 views

What are the differences between the following two functions, explaining to a MMA novice? [duplicate]

Obviously the following two functions bring the same results: ...
0
votes
0answers
458 views

How to run Mathematica source code in Linux command line when some external file need to be imported and the result need to export?

I want to run a short code like that in the linux command line ...
1
vote
2answers
97 views

Is there a quick way to replace backwards after long sequence of ReplaceAll?

I have done a lot of variable replacements in order to simplify equation so that it can be solved by mathematica command. After getting the solution I have to go backwards in Replace All. Is there a ...
6
votes
2answers
1k views

How to eliminate == sign from the solution?

I run the Eliminate command on the expression that gave me the symbolic solution like this for example: a + b == ab - c + d I assigned the solution to be ...
14
votes
3answers
560 views

Combining # & pure functions with post-fix notation

I can't find the link anymore but there was a talk at a Wolfram conference (referencing v7 I think) where the authors highlight that stringing together functions like this: ...
0
votes
2answers
116 views

Adding extra lines to the If[condition,t,f] command [duplicate]

I wish that the Mathematica documentation for the If command showed examples that have more than one statement for the t and f responses. All three of these work: No parentheses or braces: ...
12
votes
1answer
152 views

Clarify when to use Style vs Directive vs List

I am confused about when to use a simple list of style options, the Directive function or the Style function. As shown below, ...
0
votes
2answers
108 views

How to look at local variables from the most recent execution of Module?

xxx[a_] := Module[{b}, b = a + 2; b + 2]; xxx[2] Is there a way to examine value of b was during last invocation? The ...
0
votes
1answer
349 views

bifurcation code [closed]

Hi can you help me in graphing this bifurcation image, i can not make the layout with the red thick line ]
7
votes
2answers
169 views

Defining Function with right T arrow

I found a tutorial that preferred the use of the right T arrow (Esc fn Esc) to define functions which I had never seen before. It took me a while to find it in the Function help page buried in the ...
6
votes
1answer
216 views

Proper style for function documentation?

Is there a good convention for documenting functions/return values? If I just put them in comments on top of function, the alignment is all over the place.
2
votes
3answers
183 views

How to find all prime power factorizations of an integer

I want to put in $n$ and get the possible factorizations into prime powers. Let's say for 24 I want {{8,3}, {4,2,3}, {2,2,2,3}}. The algorithm for doing this on ...
11
votes
2answers
481 views

Is there a convention on which name to use for a throwaway variable? (Like underscore in Python) [duplicate]

I would prefer to write {_, foo} = LongFunctionName[arg1, arg2] instead of foo = LongFunctionName[arg1, arg2][[2]] or ...
13
votes
1answer
235 views

Code indenting of // postfix functions

Preamble Following the recommendation of a presentation or article which I now only vaguely remember I have been experimenting with a particular code format: ...
4
votes
1answer
877 views

Is it possible to write Matlab-like scripts in Mathematica?

I have been using the Notebook in Mathematica for quite a while. While I like its interface, the problem is successive statements sometimes get read into different Inputs, and when I am rerunning the ...
82
votes
3answers
12k views

Why should I avoid the For loop in Mathematica?

Some people advise against the use of For loops in Mathematica. Why? Should I heed this advice? What is wrong with For? What ...