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.

Filter by
Sorted by
Tagged with
3 votes
1 answer
86 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 ...
user6546's user avatar
  • 1,069
4 votes
3 answers
637 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 ...
metroidman's user avatar
5 votes
1 answer
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 ...
max's user avatar
  • 111
4 votes
1 answer
95 views

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

Background Information I have the following Mathematica code: ...
codebpr's user avatar
  • 2,082
0 votes
1 answer
56 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 ...
florin's user avatar
  • 1,798
6 votes
4 answers
415 views

Subdivide a sequence

Yesterday I couldn't solve a problem which seemed to be rather simple. I tried RepleaceRepeated, SequenceReplace and similar ...
eldo's user avatar
  • 62.2k
0 votes
1 answer
81 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 ...
George's user avatar
  • 69
0 votes
0 answers
40 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: ...
iron's user avatar
  • 1
6 votes
7 answers
387 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. ...
eldo's user avatar
  • 62.2k
8 votes
6 answers
440 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,...
eldo's user avatar
  • 62.2k
9 votes
9 answers
664 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 ...
eldo's user avatar
  • 62.2k
3 votes
4 answers
211 views

Add missing column keys

Example data: data = {{"1", 50}, {"1", 60}, {"1", 40}, {"2", 15}, {"3", 18}, {"3", 18}}; The first ...
eldo's user avatar
  • 62.2k
9 votes
4 answers
661 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 ...
eldo's user avatar
  • 62.2k
7 votes
6 answers
367 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 ...
eldo's user avatar
  • 62.2k
5 votes
2 answers
268 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 ...
eldo's user avatar
  • 62.2k
4 votes
2 answers
167 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)...
eldo's user avatar
  • 62.2k
4 votes
1 answer
103 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 ...
user688486's user avatar
4 votes
2 answers
256 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 ...
George's user avatar
  • 69
0 votes
1 answer
42 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. ...
L. Gorham's user avatar
5 votes
2 answers
238 views

How to Vectorize this Function?

I have been puzzling over how to vectorize the following function: ...
MMAUser's user avatar
  • 399
2 votes
0 answers
147 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 ...
kpaz's user avatar
  • 41
3 votes
1 answer
117 views

MapApply Operator Form

I have a list of matrix sequences (list of matrices) that I want to multiply, something like ...
Aaron's user avatar
  • 153
2 votes
2 answers
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 ...
internet's user avatar
  • 733
4 votes
4 answers
169 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 ...
Sébastien Neukirch's user avatar
3 votes
3 answers
409 views

Better way to do such iteration

Consider such an iteration function ...
matrix42's user avatar
  • 6,966
0 votes
1 answer
43 views

How to fix accuracy error?

When I run the code this type of error appears: ...
Mam Mam's user avatar
  • 1,773
3 votes
3 answers
449 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 (...
Robin Hansen's user avatar
0 votes
1 answer
170 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; ...
Math View's user avatar
2 votes
1 answer
162 views

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

I am trying to understand this code ...
Mam Mam's user avatar
  • 1,773
9 votes
6 answers
356 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 ...
expression's user avatar
  • 5,612
1 vote
0 answers
37 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 ...
Jules Manson's user avatar
  • 2,437
3 votes
1 answer
157 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 ...
Meclassic's user avatar
  • 953
2 votes
0 answers
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., ...
Gert's user avatar
  • 1,480
1 vote
1 answer
157 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 ...
Jules Manson's user avatar
  • 2,437
10 votes
1 answer
237 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 ...
Craig Carter's user avatar
  • 4,250
23 votes
3 answers
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 ...
Craig Carter's user avatar
  • 4,250
4 votes
1 answer
82 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: ...
davidcl's user avatar
  • 143
1 vote
0 answers
94 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(...
SaMaSo's user avatar
  • 231
1 vote
1 answer
103 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 (...
cgcg's user avatar
  • 69
6 votes
4 answers
413 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
  • 473
0 votes
0 answers
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 ...
J0ta's user avatar
  • 65
6 votes
1 answer
165 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 ...
LambdaHaskell's user avatar
3 votes
5 answers
251 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 ...
user236343's user avatar
2 votes
1 answer
372 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} ]
user236343's user avatar
7 votes
1 answer
218 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 ...
Patchouli Nine's user avatar
5 votes
2 answers
547 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, ...
Joshua Salazar's user avatar
4 votes
4 answers
249 views

Successive chunking of a list

I have shuffled and used the output of Range[20] 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}...
Syed's user avatar
  • 49.3k
14 votes
7 answers
835 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 ...
Lucas Mumbo's user avatar
7 votes
2 answers
193 views

Is Function broken is this example?

This program f[x_] := g[Function[a, x]]; g[fn_] := Module[{h}, h[a_] := fn[a]; h[0]]; f[999] returns Function[0, 999][0] but ...
Pierre ALBARÈDE's user avatar
12 votes
4 answers
756 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 ...
theorist's user avatar
  • 3,623

1
2 3 4 5
10