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
1 vote
0 answers
60 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(...
user avatar
  • 193
0 votes
0 answers
42 views

Slot a piecewise function [closed]

I would like to use slots while defining a Piecewise function. ...
user avatar
  • 305
1 vote
1 answer
89 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 (...
user avatar
  • 69
5 votes
4 answers
289 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 ...
user avatar
  • 289
0 votes
0 answers
50 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 ...
user avatar
  • 65
5 votes
1 answer
91 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 ...
user avatar
3 votes
5 answers
164 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 ...
user avatar
2 votes
1 answer
189 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} ]
user avatar
7 votes
1 answer
159 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 ...
user avatar
5 votes
2 answers
299 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, ...
user avatar
4 votes
4 answers
219 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}...
user avatar
  • 14.6k
14 votes
7 answers
749 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 ...
user avatar
  • 558
5 votes
2 answers
163 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 ...
user avatar
11 votes
3 answers
646 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 ...
user avatar
  • 3,446
1 vote
0 answers
21 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 ...
user avatar
  • 1,787
0 votes
2 answers
66 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: ...
user avatar
-2 votes
1 answer
70 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 ...
user avatar
0 votes
1 answer
58 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?
user avatar
2 votes
1 answer
60 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 ...
user avatar
0 votes
0 answers
39 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 ...
user avatar
2 votes
1 answer
82 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 ...
user avatar
4 votes
1 answer
87 views

Defnining a function with a parameter which is not a slot but a constant [closed]

One convenient way to define a function is f[S,x_]:= 3 f[G,x_]:= 7 where the first parameter is not a slot but a constant. This way of constructing functions is ...
user avatar
4 votes
1 answer
81 views

Apply function modification to curried function

This is based on A function that modifies functions - well-defined operation?, a previous question of mine. Lukas Lang's answer of course works very well, but now I encountered the following edge case:...
user avatar
  • 175
0 votes
1 answer
29 views

Create a function which takes a (or several) time-dependent plot(s) as input and produces a sequence of images

I want to use Wolfram Script to create a sequence of PNG images depending on some parameter (which I then turn into a movie). Currently I do this with a for loop. I would like to avoid having to write ...
user avatar
  • 339
1 vote
1 answer
32 views

Use result of function without repeatedly evaluating [closed]

I have a function which takes the determinant of a matrix whose values depend on two parameters: f[x_,y_]:= Det[M[x,y]]; where M is a predetermined matrix which is ...
user avatar
  • 133
3 votes
0 answers
170 views

Robust PCA (better Mathematica style)

I've parsed (almost verbatim) Python RPCA implementation to WM. Can it be rewritten using a better WM style? In particular, I'm not happy with ...
user avatar
  • 1,556
5 votes
1 answer
56 views

Functional iteration of list of functions

I'm getting completely stumped on writing a function makeIterate that constructs an iterated structure out of a list of functions e.g. ...
user avatar
1 vote
0 answers
49 views

Idiomatic pattern for subtracting a function from a list

I would like to know a more idiomatic way to subtract a function from a list of pairs than this: Table[point-{0,fitFunction[point[[1]]]}, {point,data}] I use this ...
user avatar
  • 1,448
1 vote
1 answer
89 views

How do I correctly iterate using Outer when an argument / slot is a nested list?

I have the following complex nested iteration situation. I begin with a list, data, that comprises 16 TemporalData objects Each TemporalData object contains 2 ...
user avatar
  • 95
3 votes
2 answers
124 views

Best way to write a system of conditions

Quite simply, I am wondering if there is a cleaner (and more easily altered) way to write a system of conditions for an evaluation. For instance, suppose I have something like (I don't think a real ...
user avatar
  • 353
2 votes
2 answers
140 views

Functional realisation of do-loop

I'm currently optimising my code and I'm using a lot of loops, so I was wondering if there is a smarter or faster way to do things like: ...
user avatar
0 votes
2 answers
133 views

How do I integrate a For loop to generate iterations of the Sierpinski's Triangle?

I'm sorry if this is a basic question—I don't know anything about programming. I know you can create a graphic of Sierpinski's Triangle with a single command, but I'd like to know how to create one ...
user avatar
7 votes
2 answers
276 views

How to select the fastest approach for large numerical data computations?

I really love the flexibility of Mathematica: there are several ways to perform one task. However, to get the performance of the intense numeric calculation, it can ...
user avatar
  • 1,609
8 votes
0 answers
184 views

Performance of pure function - Best way to define a function?

In general, to define a function, we often use f[x_]. Another way is to use pure function (full form and shorthand). I want to test the performance of these ...
user avatar
  • 1,609
2 votes
2 answers
159 views

How to implement fast series multiplication

I need to generalize the SeriesData object for my own purposes. One of the things I need to do is to reimplement code to multiply product of series. I've made two ...
user avatar
3 votes
2 answers
135 views

Chained sums with Nest

I'm looking to calculate a chained sum like this $$ C_m = \sum\limits_{i_1=1}^N \;\sum\limits_{i_2=i_1+1}^N \;\sum\limits_{i_3=i_2+1}^N \cdots \;\sum\limits_{i_m=i_{m-1}+1}^N A_{i_1}A_{i_2}A_{...
user avatar
  • 1,045
10 votes
2 answers
461 views

Breaking "Functional Loops" and Doing Lazy Evaluation In Mathematica

Alright, so this is a question about the functional way to break a for/while loop. Since we're on the Mathematica SE, I'm interested in the ways a Mathematica vet would handle this, however the ...
user avatar
  • 391
5 votes
1 answer
194 views

Why Does Parallelization Not Speed Up these Seemingly Well-Parallelizable, Simple Functions?

Original Example Consider function f, a parallelized version fPar, and a coarsest-grained parallelized version ...
user avatar
7 votes
7 answers
479 views

Creating a list with Thread

I have a list like this one: aaah= {{11, 21, 31}, {12, 22, 32}, {13, 23, 33}} and I finally want to get a list which looks like this one: ...
user avatar
  • 73
4 votes
0 answers
83 views

How do I correctly use OperatorApplied?

Starting with ClearAll[f, list] list = {"a", "the"} f[s_, n1_, n2_] := {s, n1, n2} Then f[list, 20, 10] ...
user avatar
9 votes
3 answers
1k views

How can I replace a set of Nested For Loops with a Functional Code Alternative?

Despite having looked over various posts concerning optimizing nested For loop expressions into a functional programming equivalent I still can not figure out how to actually to do this. Such ...
user avatar
  • 1,496
1 vote
1 answer
36 views

Style function not working inside Print Row functions

About Watch[...] is a helper function for watching a list of variables or other functions inside a Module[...] during ...
user avatar
  • 1,787
2 votes
0 answers
97 views

Using Fold[ or Map[ Better?

I'm working through MMA Cookbook by Mangano, page 54. He shows Map is a compact form of Fold function. Is using ...
user avatar
  • 590
0 votes
1 answer
24 views

How to style a row of lists of lists created with a Table and return all lists?

I want to return a list of all lists with elements in a single first list styled if it meets a condition so that it stands out from other lists. My attempt at this only returns all data with no row ...
user avatar
  • 1,787
8 votes
3 answers
234 views

Fold a list but alternate used functions

Is there a clean way to fold a list but alternate functions used? Say we call such function FoldAlternating. Then for example, The ...
user avatar
  • 612
2 votes
1 answer
45 views

Results of recursion grouped in list [closed]

I made a recursion where I should plot resulting points. When I do Print[point1], it just gives me result: ...
user avatar
  • 83
1 vote
0 answers
62 views

Implements the binary-searching style code in functional way

Given that I have a code snippet written in binary-searching way: ...
user avatar
5 votes
1 answer
75 views

What is the most efficient way to implement NestPairWhileList[f,expr,test,g]

Question NestWhileList[f,x,test] lets you iterate a function until a condition is satisfied, outputting the list of iterated values ...
user avatar
1 vote
1 answer
87 views

Leibniz rule with unknown orders of imput

I'm trying to find a simple way to define a bilinear and Leibniz functional/map "$B(\cdot,\cdot)$", that eats two functions $f(x),f(y)$ or products theirof and produces the following, $$ B(f(...
user avatar
4 votes
2 answers
67 views

Replace Solution arrow in list of list

I have solutions like ...
user avatar
  • 2,020

1
2 3 4 5
9