# 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.

469 questions
Filter by
Sorted by
Tagged with
131 views

### How to use functional iteration with changing function?

I recently came across this problem on reddit. The problem is supposed to be read like a snake, and the result of each succesive operation is sent to the next operation: There are 9 blank boxes and ...
• 4,572
68 views

### How to implement this zip function? zip[g,{f[{a,b}],f[{c,d}]}] to f[{g[{a,c}],g[{b,d}]}]

I have a function that returns an expression in the form f[{a,b}], and I use this function twice and assemble the expression ...
319 views

### Get cumulative sums in a list without using a for loop [closed]

I want a list of the kind list = {a,a+b,a+b+c} for list plotting. The quantities of the list come from a function. Let us say this is ...
• 411
82 views

### PatternTest in Function

This is probably trivial and already asked but I did not find it. How to use PatternTest inside Function (...
• 20.1k
72 views

### Expression as parameter to function that evaluates it, best way to do it?

I need a function fSum that computes the angular sum for some expression, like this: fSum[3 Sin[2 x], 0, 2 π] Or from a predefined function, like this: f[x] = 2 Sin[3 x]; fSum[f, 0, 2 π] Or plots it ...
• 101
62 views

• 101
94 views

### Quickly processing long lists using SequencePosition

Overview SequencePosition does exactly what I need; however, it seems to be very slow for long lists. Is there an input option that would allow it to process faster? Or, perhaps an alternative ...
• 1,069
657 views

### Concise version of MATLAB code

Please see this MATLAB code: k=0:8; wk=2 * pi * k/17; ak=double(wk<0.5*pi) It generates ak with such a simple expression. To ...
• 889
81 views

### Undocumented behavior of Fold for arbitrary heads [closed]

Edit: I'm apparently blind. Sorry! And thanks to those who answered anyway. Apologies in advance if this is sort of a nitpicky question, but I'm generally very paranoid that I don't fully understand ...
• 111
98 views

### How to shorten the plotting code using slots and table? [closed]

Background Information I have the following Mathematica code: ...
• 2,433
60 views

### How to evaluate expressions which involve derivatives and evaluations of functions in a specific case, via a condition?

I have an expression involving the derivative of an arbitrary function r_0 c0 = Derivative[1][Subscript[r, 0]] Later I must replace r_0 by several specific ...
• 1,900
429 views

### Subdivide a sequence

Yesterday I couldn't solve a problem which seemed to be rather simple. I tried RepleaceRepeated, SequenceReplace and similar ...
• 81.7k
91 views

### How can I apply Residue in a functional-style way? [closed]

Let me suppose that I have got some expression, for example f_{1}(x)+f_{2}(x) and also a list of a form: {1,2} How can I apply ...
• 69
405 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. ...
• 81.7k
448 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,...
• 81.7k
674 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 ...
• 81.7k
220 views

Example data: data = {{"1", 50}, {"1", 60}, {"1", 40}, {"2", 15}, {"3", 18}, {"3", 18}}; The first ...
• 81.7k
695 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 ...
• 81.7k
377 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 ...
• 81.7k
271 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 ...
• 81.7k
171 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)...
• 81.7k
104 views

### Function[0], 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 ...
• 505
265 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 ...
• 69
43 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. ...
247 views

### How to Vectorize this Function?

I have been puzzling over how to vectorize the following function: ...
• 409
168 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 ...
• 41
120 views

### MapApply Operator Form

I have a list of matrix sequences (list of matrices) that I want to multiply, something like ...
• 153
84 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 ...
• 785
177 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 ...
415 views

### Better way to do such iteration

Consider such an iteration function ...
• 7,148
44 views

### How to fix accuracy error?

When I run the code this type of error appears: ...
• 1,883
461 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 (...
• 145
230 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; ...
165 views

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

I am trying to understand this code ...
• 1,883
393 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 ...
• 5,662
1 vote
40 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 ...
• 2,467
161 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 ...
• 1,035
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,620
1 vote
160 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 ...
• 2,467
243 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 ...
• 4,625
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 ...
• 4,625
85 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: ...
• 143
1 vote
97 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(...
• 231
1 vote
104 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 (...
• 69
442 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 ...
• 483
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 ...
• 115
174 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 ...
284 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[3], Range[3]], 1] I get ...
• 429