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
0 votes
0 answers
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: ...
iron's user avatar
  • 1
6 votes
7 answers
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. ...
eldo's user avatar
  • 40.6k
8 votes
6 answers
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,...
eldo's user avatar
  • 40.6k
9 votes
9 answers
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 ...
eldo's user avatar
  • 40.6k
3 votes
4 answers
208 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
  • 40.6k
9 votes
4 answers
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 ...
eldo's user avatar
  • 40.6k
7 votes
6 answers
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 ...
eldo's user avatar
  • 40.6k
5 votes
2 answers
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 ...
eldo's user avatar
  • 40.6k
4 votes
2 answers
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)...
eldo's user avatar
  • 40.6k
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
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 ...
George's user avatar
  • 67
0 votes
1 answer
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. ...
L. Gorham's user avatar
5 votes
2 answers
227 views

How to Vectorize this Function?

I have been puzzling over how to vectorize the following function: ...
MMAUser's user avatar
  • 379
2 votes
0 answers
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 ...
kpaz's user avatar
  • 41
3 votes
1 answer
109 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
  • 431
4 votes
4 answers
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 ...
Sébastien Neukirch's user avatar
3 votes
3 answers
400 views

Better way to do such iteration

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

How to fix accuracy error?

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

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

I am trying to understand this code ...
Mam Mam's user avatar
  • 1,731
9 votes
6 answers
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 ...
expression's user avatar
  • 5,562
1 vote
0 answers
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 ...
Jules Manson's user avatar
  • 2,397
3 votes
1 answer
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 ...
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,460
1 vote
1 answer
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 ...
Jules Manson's user avatar
  • 2,397
10 votes
1 answer
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 ...
Craig Carter's user avatar
  • 3,926
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
  • 3,926
4 votes
1 answer
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: ...
davidcl's user avatar
  • 143
1 vote
0 answers
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(...
SaMaSo's user avatar
  • 231
1 vote
1 answer
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 (...
cgcg's user avatar
  • 69
6 votes
4 answers
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 ...
AlbertLew's user avatar
  • 463
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
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 ...
LambdaHaskell's user avatar
3 votes
5 answers
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[3], Range[3]], 1] I get ...
user236343's user avatar
2 votes
1 answer
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} ]
user236343's user avatar
7 votes
1 answer
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 ...
Patchouli Nine's user avatar
5 votes
2 answers
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, ...
Joshua Salazar's user avatar
4 votes
4 answers
247 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
  • 38k
14 votes
7 answers
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 ...
Lucas Mumbo's user avatar
7 votes
2 answers
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[0]]; f[999] returns Function[0, 999][0] but ...
Pierre ALBARÈDE's user avatar
11 votes
3 answers
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 ...
theorist's user avatar
  • 3,568
1 vote
0 answers
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 ...
Jules Manson's user avatar
  • 2,397
0 votes
2 answers
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: ...
khvillegas's user avatar
0 votes
1 answer
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 ...
Peter Burbery's user avatar
0 votes
1 answer
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?
Peter Burbery's user avatar
2 votes
1 answer
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 ...
babyblue's user avatar
0 votes
0 answers
41 views

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 ...
K. Claesson's user avatar
2 votes
1 answer
104 views

Considerations for Compiling and Parallelizing Code Outside or Inside Map[]

Assume f as well as Map[f,list] are parallelizable and compilable, such that parallelization and compilation give significant ...
Just Some Old Man's user avatar

1
2 3 4 5
10