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
2
votes
1answer
58 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 ...
0
votes
0answers
36 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 ...
2
votes
1answer
68 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 ...
0
votes
0answers
42 views

How to create and store a plot for each iteration of a Do loop

I have created the following code below. Its purpose is for me to divide the plot into grids and for each grid I reterive the center point's co-ordinates. ...
4
votes
1answer
79 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 ...
4
votes
1answer
63 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:...
0
votes
1answer
28 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 ...
1
vote
1answer
31 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 ...
6
votes
2answers
435 views

Pure functions instead of For loop

How to get following loop output using pure functions such as NestList? For[i = 0; v = 0, i < 4 && v < 4, i++; v++, Print[x^i + b^v]] and the ...
4
votes
2answers
507 views

Writing Fold in terms of Map or MapThread?

Consider Fold[f,z,{a,b,c}] ==> f[f[f[z,a],b,c]] I'm looking for a way to write this (in general) using Map or ...
4
votes
2answers
435 views

Defining a sequence slot programmatically

This question is related to: Composition of functions using slots `Slot` (#) interfering with evaluation Consider a toy example: ...
2
votes
0answers
51 views

Folow-up on combining pure functions [duplicate]

This is a follow-up to this question. Consider: f = #^2& g = #^3& Compose[f, g] (#1^3 &)^2 Am I the only one to think this is a bug?
8
votes
0answers
170 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 ...
3
votes
0answers
151 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 ...
5
votes
1answer
49 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. ...
112
votes
9answers
5k views

Functional style using lazy lists?

Let's say I want to answer the question "what are the first 400 palindromic prime numbers?" The first approach that comes to my mind from the set of languages that I know is to use some sort ...
1
vote
1answer
81 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 ...
1
vote
0answers
41 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 ...
5
votes
3answers
411 views

About auto-compiling and performance between Do and Fold

I was investigating how Fold could improve performance vs Do. I tested the code ...
3
votes
2answers
113 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 ...
2
votes
2answers
137 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: ...
35
votes
4answers
810 views

FoldWhile and FoldWhileList

Mathematica has had NestWhile and NestWhileList for some time. But, to date, it has not implemented a built-in ...
0
votes
2answers
119 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 ...
7
votes
2answers
260 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 ...
2
votes
2answers
120 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 ...
2
votes
1answer
69 views

How to change list type from CountryData to regular text? [closed]

When you access a list of countries from CountryData the outputs are boxed and is not normal text. How do I convert the type of items inside the list so I can modify the style? For example I have: <...
5
votes
1answer
73 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 ...
1
vote
1answer
78 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(...
3
votes
2answers
101 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_{...
10
votes
2answers
452 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 ...
4
votes
1answer
168 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 ...
7
votes
7answers
471 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: ...
4
votes
0answers
71 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] ...
8
votes
3answers
921 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 ...
1
vote
1answer
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 ...
33
votes
3answers
756 views

Analogies to Help New Users

In many of the classes that I teach, I require students to learn the basics of Mathematica which we use throughout the semester to do computations and to submit homeworks (in notebook form). Some ...
2
votes
0answers
88 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 ...
3
votes
1answer
788 views

Gâteaux (directional) derivatives and higher order differentials of a functional

I would like to calculate the Gâteaux derivative of a functional (i.e. a function depending on functions). A simple example for the Dirichlet functional: $L(u(x))=\int_0^1 \frac{1}{2} (u'(x))^2 dx$ ...
8
votes
3answers
221 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 ...
0
votes
1answer
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 ...
2
votes
1answer
44 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: ...
28
votes
3answers
4k views

Programming a numerical method in the functional style

I am new to Mathematica and I would like to learn a bit more about functional programming. At the moment I have assignments like programming different numerical methods (for integration: ...
6
votes
1answer
752 views

Does Mathematica have a functional programming idiom to loop over a list till a condition is met? [duplicate]

The module below takes a list of integers and performs fadic addition on it (if a bit slowly). ...
1
vote
0answers
58 views

Implements the binary-searching style code in functional way

Given that I have a code snippet written in binary-searching way: ...
4
votes
2answers
64 views

Replace Solution arrow in list of list

I have solutions like ...
1
vote
0answers
36 views

Plot is passing a bizarrely shifted x_min value to the user-specified Ticks function

The documentation for Mathematica's Ticks option says that you can specify the ticks for an axis by passing a function rather than a plain list description. The ...
10
votes
4answers
426 views

Operator currying: how to convert f[a,b][c,d] to {a+c,b+d}?

This question is related to this golfing question (but I'm not interested in golfing, only in functional operator composition): How can we convert f[a,b][c,d] to <...
1
vote
1answer
38 views

Applying functions to members of members... of lists of lists..., etc

Suppose that I have a list of depth $k$, and I want to apply a function $f$ to all elements at level $n$ - e.g. $f,\{\{\{a,b\},\{b,c\}\},\{\{c,d\}\}\},3\mapsto\{\{\{f(a),f(b)\},\{f(b),f(c)\}\},\{\{f(c)...
6
votes
2answers
387 views

I'm not able to delete -> in {{x -> a}, {x -> b}, {x -> c, x -> d}}

Here is what I have done. Input DeleteCases[{{x -> a}, {x -> b}, {x -> c, x -> d}}, Rule, {2}] Output ...

1
2 3 4 5
9