# Questions tagged [functional-style]

Questions about Mathematica's functional programming style, including the use of pure functions (Function[], #, &) and functions such as Map, Apply, Nest, and Through.

458 questions
Filter by
Sorted by
Tagged with
32 views

### Commutator for Solving Coefficients in a Hamiltonian

I am relatively new to Mathematica but I understand the basics. I want to solve for coefficients by commutator constraints and I have problems implementing the commutator. The setup is the following: ...
372 views

### Removing duplicates between sublists

I want do delete from each sublist those elements which are contained in one or more of the other sublists. The surviving elements must maintain their original order of appearance. ...
427 views

### Accumulate up and down

I want to accumulate positive and negative values separately. dt = RandomSample[Range[-10, 10]] {3, -5, 2, -9, 1, -1, 0, 8, 10, 4, -7, 7, -8, -3, 6, -4, -2, 5, -10,...
651 views

### Partition a random sequence composed of three numbers

I want to partition an array composed of 1s, 2s and 3s in such a way that, going from left ...
208 views

Example data: data = {{"1", 50}, {"1", 60}, {"1", 40}, {"2", 15}, {"3", 18}, {"3", 18}}; The first ...
624 views

### Smooth a list of integer values

I want to smooth a list of Integers so that outlying values become less spiky. The result vector must again contain Integer ...
345 views

### Replace duplicate list values with unique elements

My request is a follow-up on this question: Replacing duplicates instead of deleting them What if we want to replace duplicated values not with a single value but with newly created values distinct ...
262 views

### Create unique position values

Given a list of integers I want to create unique position-values. The following rules apply: Take first number. If first number exceeds list length, take 1. Take next number. If next number is lower ...
165 views

### Two simple vector partition rules

We have a vector of zeros and other numbers, f.e: vector = {0, 0, 0, 9, 0, 2, 0, 5, 0, 4, 0, 5, 6, 2, 0}; The two rules: Partition the vector in such a way that (1)...
103 views

### Function, Function[,0], and Function[{},0]: Is there any inherent distinction in the "academic" sense?

Note that Function[,0] is by default interpreted as Function[Null,0]. The following three calls all give ...
244 views

### Enumeration of a nested list

My problem is consisting of following: I have a nested list of following form: {{B, v ** v, b ** B}, {b, B ** b}, {b ** B, v ** v}, {}} And I want to enumerate ...
41 views

### My three demo script is too slow switching tabs

My three demo stripped down script runs fine but takes 6 sec to switch from demo 2 or 3 to demo 1. Actually I need nKs = 7 which takes 30 sec. I can't find examples of slow switching tabs. ...
227 views

### How to Vectorize this Function?

I have been puzzling over how to vectorize the following function: ...
97 views

### How to write 2D finite volume solver in a functional approach?

I'm solving 2D poisson problem of a finite volume problem using TDMA. The solution of the problem is given: \[Phi][x_, y_] := x^2 y + x y^2 + 1 The definitions are ...
109 views

### MapApply Operator Form

I have a list of matrix sequences (list of matrices) that I want to multiply, something like ...
82 views

### Would it be slower to place function definitions inside another function definition?

Will it be slower to place functions' defintions inside a main function? I usually do that if the subfunctions are short. However, with a long subfunction I usually place it outside as I think that ...
160 views

### How to I know how many nested functions have been called?

I have two functions, and the 1st calls the 2nd: f[x_]:=Block[{},x^2] g[y_]:=Block[{},Sin[f[y]]] I would like to know, in each ...
400 views

### Better way to do such iteration

Consider such an iteration function ...
42 views

### How to fix accuracy error?

When I run the code this type of error appears: ...
417 views

### How to make a function take another function as an input?

I am quite new to mathematica, and I simply want to know how to make a custom function, take another arbitrary function as an input. I have attached code that shows what I am trying to accomplish (...
92 views

### Create a function that can assign values to different variables in Mathematica

Here I am trying to create a function that can assign values to different variables in Mathematica. Assume the function f[a _, b _, c _]; x1 = a; x2 = b; x3 = c; ...
161 views

### Can it be rewritten in a less abbreviated version? [closed]

I am trying to understand this code ...
350 views

### Map function to the list and keep the previous

Give the following list, list = {{a1, a2, a3}, {b1, b2, b3, b4}, {c1, c2, c3, c4, c5}}; I want to get ...
1 vote
34 views

### How to create a Sort function that does not differentiate between String rule keys and Symbol rule keys

Im trying to Sort Rule-s according to Key-s (or names) but the normal ...
154 views

### How to replace a multiple iterator Table to speed up computation

I need to create an array of data which resembles to something like this Table[{i, Sin[j^2*i]}, {j, 2000}, {i, 0., Pi, Pi/10000.}] where each "row" of ...
32 views

### Attributes for functions with currying [duplicate]

Is there a way to set attributes for a function which holds for all arguments when currying? When evaluating, e.g., ...
1 vote
144 views

### Cannot find DockedCells with Cells or NotebookFind

Request 1: A workaround for Cells or NotebookFind needs to be found for locating and returning the ...
222 views

### How to programmatically construct a function or a compiled function?

I'd like to programmatically construct a function from a long expression with many duplicate terms. The objective is to programmatically create a set of compiled functions with expressions that were ...
1k views

### What is the fastest method to set the diagonals of a matrix to 1?

I am trying to replace the zeros along diagonal of a distance matrix to a list of constants. In this case, a diagonal of 1's. It appears that the LinearAlgebra ...
81 views

### Accumulate[] on an ordered HashTable

Hopefully this is a trivial question, but I keep getting lost looking for an answer in the documentation. I have an ordered HashTable containing indexed real numbers: ...
1 vote
88 views

### Numerical solution for multi-step PDEs

I am trying to numerically solve the following PDEs in MMA (v12.0) \partial_u f(x,u) = \left\{\begin{matrix}&-a f(x,u) + b \, \partial_x^2 f(x,u), & 0<u<T\\ &r(x;\alpha_1) \, f(...
1 vote
100 views

### While inside a function body does not work as expected [closed]

I'm trying to write a function that return a randomly created rational polynomial function that has a discontinuities points. I already have a function that generates random rational polynomial (...
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 ...
56 views

### Algorithm consisting of modules returning multiple errors

I'm trying to write a genetic algorithm that will take a list of lists of reals in interval [0,1], my "starting population", "score" them according to a fitness function, append ...
127 views

### K-Mean Clustering Algorithm

I have implemented the k-Means Clustering Algorithm using the Wolfram Language. However, I think it can be more efficient. Do you have any idea how to make it more efficient (e.g., by removing the ...
217 views

### How can I get a list of all distinct unordered pairs of numbers in a specific range? [duplicate]

If I do e.g. Flatten[Outer[List, Range, Range], 1] I get ...
337 views

### What's a functional alternative to nested Do loops?

How can I use functional code to achieve the same effect as the following procedural code Do[ Do[ f[i,j], {j,1,m} ], {i,1,n} ]
202 views

### How can I do "Map" on a binary function?

If there is a one-variable function, we can f/@{a,b,c} to get {f[a],f[b],f[c]} What to do with it if it comes to a binary ...
453 views

### Map a two argument function to every element on list

I'm new to use the amazing map function in Mathematica, and I've found it really elegant to program in simple lines of code. However, I'd like to perform more elegant stuff. I've found this question, ...
247 views

### Successive chunking of a list

I have shuffled and used the output of Range in order to create this minimal example: t = {19, 4, 12, 1, 17, 13, 7, 14, 10, 11, 6, 18, 20, 15, 16, 3, 8, 5, 2, 9}...
820 views

### Iterate over # without needing to define a variable

I want a function that takes in a number $n_0$ and a list $\{n_1, n_2, n_3, n_4, n_5\}$ and returns $\{\{n_0,n_1\},\{n_0,n_2\}, \{n_0,n_3\},... \}$. The reason I want this is to use it in Map, so I ...
190 views

### Is Function broken is this example?

This program f[x_] := g[Function[a, x]]; g[fn_] := Module[{h}, h[a_] := fn[a]; h]; f returns Function[0, 999] but ...
687 views

### Function that does for Map what Through does for Apply?

Through allows multiple functions to be easily applied to a single argument. For instance, suppose I wanted to count the number of characters in ...
1 vote
26 views

### Is it possible to insert a fixed-size Input Cell in a DockedCell toolbar that directs output if any to a specified Cell in same Notebook?

Ive been coding Mathematica for over a year but this language can do so much more than any other language hence beginners really do need a much longer gestation period. That is why this rarest of ...
107 views

### How to declare that a function is arbitrary then specify them later? [duplicate]

I need to evaluate an expression, sum in this case, involving an arbitrary function. Below is a simplified version of what I wanted to do: ...
187 views

### How to use the Wolfram Language Symbol entity type to list all the entity types? [closed]

I would like to map EntityValue[#,"SampleEntities"] over all the domains of entities, but before I spend 20 to 30 minutes typing every single entity, I would like to be aware of any way to ...
62 views

### How to find all functions that accept level specifications as an argument?

How can I find a list of every function that takes a level specification as an argument?
69 views

### Seeking for a general method to turn the term(s) in a formula into a list [closed]

If we want to make arbitary formula into a List, for example, input (1+a^3) and we want the output data to be {1,a^3}, we can use List@@(expr). However when input data becomes a^3, we need to change ...
### Finding the integer values of $p$ and $l$ for which a function $b(p, l)$ is divisible by $n$
I wrote the code in the attached image to find the integer values of $p$ and $l$ for which a function $b(p, l)$ is divisible by 3. Does anyone know of a nicer way to achieve the same result? Perhaps ...