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
1answer
16 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
68 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
799 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
79 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
123 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
68 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
58 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
636 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
75 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
131 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
68 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
209 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
125 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
88 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
50 views

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

A simple working example of RegionPlot3Dis ...
5
votes
3answers
127 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
106 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
316 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
75 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
122 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
379 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
83 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
524 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
100 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: ...
0
votes
2answers
99 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
317 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
149 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
204 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
181 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
456 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 ...
12
votes
1answer
203 views

Code indenting of // postfix functions

(This question may fall into the "primarily opinion based" category, in which case it should be closed. I shall try to make it sufficiently focused but I expect no special treatment. I anticipate ...
4
votes
1answer
789 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 ...
79
votes
3answers
10k 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 ...
0
votes
1answer
225 views

Population growth model using cohort data method: Is there a way to make the repetitive code simpler?

This is for a little simple project in which we try to model population of a certain species of fish and the impacts of overfishing I do find it rather rudimentary to write it all out as such and was ...
46
votes
4answers
2k views

The Game of Hex in Mathematica

The Game of Hex is a game originally developed by Nash, and it's rules are very simple. You start out with a hexagonal tiling of some size: There are two players. The game is in turns, and every time ...
0
votes
1answer
73 views

why the result not contain imaginary, I? [closed]

Good day, First of all, sorry if my coding in this forum has mistake. I already learned it and this is the best try from me. May anyone help me troubleshoot my problem below. ...
22
votes
5answers
4k views

How do I increase the readability of this code?

Below is some code I am working on; being a beginner. How can I increase the readability of the part behind Manipulate? I can't read my own code anymore, because it is simply too long and unordered. ...
1
vote
1answer
145 views

Coding using time from system(computer)

Please help me to solve this problem below. I want to run this coding based on the time given by the system. Given, A = ( { {E^(I [Beta]1 + I [Beta]3) Cos[[Beta]2t], E^(I [Beta]1 - I [...
3
votes
3answers
220 views

Replace For with If in Mathematica way

I have this code: ...
9
votes
6answers
491 views

How to generate this structure of a list without `While` and the like?

I need to create a list that holds, for given integer d, the elements $1,\ldots,d,d+2,\ldots,2d,2d+3,\ldots,3d,3d+4,\ldots,d^2$ where $\ldots$ is just denoting ...
25
votes
3answers
720 views

Writing functions with “Method” options

I'd like to implement several behaviors for a particular function using a Method option added to the function definition: ...
47
votes
2answers
2k views

Granular versus terse coding

While reading Leonid's grand answers to General strategies to write big code in Mathematica? I came across something that goes against my own practices. I do not disagree with the principle but the ...
2
votes
0answers
75 views

Good coding for integral substituions

This question is aimed at improving both my coding and my understanding of MMa's capabilities. A lot of my code is still hacks and workarounds for things I don't know how to do properly. I want to ...
15
votes
3answers
619 views

stop notebook evaluation when Assert fails

I would like a notebook evaluation to entirely stop when an Assert fails, without quitting the kernel. Is this possible (e.g., by setting ...
2
votes
2answers
312 views

Good coding for a sequence of derivatives

People on this site keep telling me that good MMa practice avoids the use of loops. The code below calculates the same Table of derivatives two different ways, ...
7
votes
2answers
179 views

Good coding for a tricky expansion with convolutions, limits, substitutions

I am attempting to generate a power series in $v$ as $v\rightarrow0$ for $$g(x,v)\equiv \ln\int_{-\infty}^{\infty}e^{-y\left(z\right)}N\left(x-z,v\right)\,\mathrm{d}z $$ where $y(x)\equiv\sum_{i=1}...
8
votes
2answers
307 views

What is the idiomatic way to shift and extend a list by a number of elements?

I have a lists, list, and want shift it right by a given number of elements, m, extending the list by repeating the last element....