Questions about Mathematica's functional programming style, including the use of pure functions (Function[], #, &) and functions such as Map, Apply, Nest, and Through.

learn more… | top users | synonyms

2
votes
3answers
111 views

Given a list of triangle vertices, how to find the neighbors for each vertex?

A triangle is represented by {a,b,c}, indicting that its vertex IDs are a, b, and ...
7
votes
1answer
109 views

Unevaluated @ {args} versus Unevaluated /@ {args}

Working my way through Robby Villegas's lovely notes on withholding evaluation, I almost got Polish Notation on my first try. Here is my final solution, which seems to work well enough: ...
2
votes
1answer
66 views

Use Map with multiple arguments over a nested list

I noticed there are a few similar questions to mine on here, but I couldn't figure out how to accomplish this mapping. I have sets of data I want to plot, which represent intensity and frames, and I ...
0
votes
0answers
50 views
2
votes
2answers
135 views

Multiply pure function with scalar

Somtimes I still get lost in the Hold, HoldAll etc. functions of Mathematica. I want to scale a simple PDF function with a previously calculated variable e.g. ...
0
votes
1answer
40 views

Mapping Linear Model Fit Over An Arbitrary Number of sets

I have multiple sets of data, and I want to apply Linear Model Fit sequentially to each set of data. For example, below tstlst contains 3 data sets. Right now I ...
-1
votes
0answers
50 views

Building Pythonic Programming style to Mathematica with visually-accessible list comprehensions?

The question List comprehension in Mathematica has comment about this Functional style using lazy lists? where they build some sort of style for lazy lists: I understand it so that it is impossible to ...
4
votes
1answer
51 views

How to define a function to save a list to a same-name file

I want a convenient way to save lots of intermediate data lists into files, so that next time the files can be imported automatically to homonym list variables in the notebook, without having to ...
2
votes
2answers
81 views

Better to use loop or function when populated list?

In the past I have pretty much stuck to expression building when using Mathematica, but recently I have been writing a procedure to populate an array, which I guess are called "lists" in Mathematica. ...
12
votes
3answers
233 views

Mimic a procedural, recursive clustering algorithm for site percolation using functional programming

Sorry in advance for my logorrhea: I just want to make sure all of the information is here. Context and Question I am investigating site percolation on a square lattice. I have a working, ...
3
votes
3answers
100 views

How to force evaluation on the right of an @ operator?

Is there a way to force evaluation on the right before the Prefix operator (@) without using parentheses? I came across this question when I was working with the ...
0
votes
0answers
61 views

Loop in a loop, or how to evaluate complex expressions

I need to find $h_{0j}$ for every $1\leq j\leq300$, where \begin{multline*} h_{0j} = \sum_{q=1}^{Q}g_qe^{-\lambda_q j\Delta}\frac{e^{\lambda_q\Delta}-1}{\lambda_q} + \sum_{p=1}^{P}\frac{e^{-\alpha_p ...
8
votes
4answers
143 views

Handy / flexible directory tree operations

Is there any shorter / more handy / more flexible way to get directory tree as a nested Rules/Associations list? I tried to incorporate ...
0
votes
1answer
54 views

Returning functions that are partly evaluated

I have some performance issues when calling Table[{h, Slow1[h, n]}, {n, nlist}, {h, 0.1, 10, 0.5}]; for a given list ...
5
votes
0answers
87 views

How can I do a two dimensional `Fold`?

I'd like to Fold over higher dimensional objects than lists. Semantically, the format would be something like ...
0
votes
2answers
88 views

Is it possible to sum the results of a Map - Apply?

I am fairly new to functional programming and would like to replace a For[] loop that sums the results over a Dataset. This may be a bit different since the ...
7
votes
1answer
158 views

How would I use Map on a Dataset column?

I am somewhat new to functional programming. To improve the performance of a package, I would like to re-write a package in a functional style. I have the following ...
2
votes
3answers
90 views

find position of nearest nonzero element in an array (functionally)

Given an array sel and an index position i0, how can I find the position of the nearest (left or right) nonzero element? I'm ...
10
votes
1answer
937 views

How to Fold over lazy lists and over tasks, just as with Lists?

Many modern languages, such as Haskell, feature lazy lists, which can model infinite sequences of values, albeit synchronously. Lately, the reactive programming movement has shown that asynchronous ...
10
votes
11answers
916 views

Elegant functional equivalent to a nested loop?

I have a square $n \times n$ matrix $m$, and need to apply a function $f$ to all elements on and above the diagonal. This is of course easy to do using a nested table: ...
19
votes
2answers
549 views

What is the difference between Composition (@*) and sequential applications (@)?

What is the difference between f@*g@*h@x and f@g@h@x Both evaluate to f[g[h[x]]] ...
2
votes
3answers
156 views

How can I get from a list of characters representing a permutation to graphics showing the cycles?

I am trying to graphically represent cycles in various permutations of the alphabet, and have a question in two parts. I have ...
1
vote
1answer
90 views

Applying functions repeatedly on parts of complicated expressions

I am dealing with expressions like $$ \textrm{expr}=5D_{4,1}\langle12\rangle\langle23\rangle\big(-\langle15\rangle^2\langle24\rangle\langle34\rangle + ...
0
votes
1answer
58 views

How to define the following bracket?

My quetion is about the definition of the so-called Courant Bracket. Given two generalised vector field, defined as a formal sum of a vector and a one-form, i.e. $X = x + \lambda,\ Y = y + \omega$, ...
8
votes
4answers
339 views

How to evaluate a function on a list until stable?

I need to evaluate a function func on a range of integers (NOT recursively, just sequentially) until the result is stable enough. I can write a not very clever ...
3
votes
2answers
134 views

The usage of MapIndexed, can it be removed?

project euler 67 When I'm solving this problem, my solution is as following. You can download the data file there. ...
7
votes
5answers
204 views

Help going from iterative to functional

The following code segment shows what I'd like to do. I'm a procedural programmer trying to learn the Mathematica functional style. Any help on this would be appreciated. ...
5
votes
1answer
155 views

Functional-programming virus infection

Update: The question may seem too broad, but the answer provided by belisarius responds perfectly to the question, as the use of $ListConvolve$ and $NestList$ meets the desired criteria in in addition ...
1
vote
1answer
52 views

Let's do this list-rearrangement prettier [duplicate]

Suppose that i have a list in this format= $list=\{ \{a,obj1 \} ,\{c,obj2 \}, \{a,obj3 \}, \{b,obj4 \} \dots\}$ I want to construct a new list in this way: $ newlist= \{ \{ \{a,obj1\} , \{a,obj3\} ...
1
vote
1answer
58 views

Function taking arbitrary number of arguments

I have a very similar function to that which a solution was provided by kguler, but am unsure as to how to adapt the solution that was given there. That post can be seen here. The adaptation is only ...
4
votes
4answers
234 views

Iterating over a list, in a functional way

I have learned a lot about functional programming just from messing around in Mathematica, but I've come across a problem that I don't know how to solve in a "functional" way. Any help would be ...
3
votes
1answer
95 views

Combining MapAt and MapIndexed

The very last example in the documentation for MapAt is: MapAt applies f repeatedly if a part is mentioned repeatedly: ...
0
votes
2answers
64 views

Supply a function as an argument to another, and find its minimum

I would like to minimize a function, which is supplied as an argument to another function, and report back the estimates of the values of the variables that result in its minimum. In specific, I would ...
1
vote
1answer
154 views

General function taking general number of arguments

The following is an edit of this post, for which a working solution was provided by John McGee. I have a different function and am not sure how to adapt the solution for it: I want to construct a ...
7
votes
3answers
358 views

Functional equivalent of a For loop referring to consecutive elements in a list

I have implemented this function which smooths the values in a list by, if it encounters a zero, replacing this entry by the average of its two neighbours (apart from the first and last values of the ...
0
votes
1answer
87 views

How to improve this function that selects parameters from arbitrary distribution using Recursion?

Below, I have defined a function using Which to select model parameters based upon various conditions. My questions are: Is there any danger in writing this function using Recursion? Is there a more ...
3
votes
1answer
164 views

Functional programming approach to avoid traditional loops

I'm currently learning from the wonderful post: Where can I find examples of good Mathematica programming practice? And i'm correcting my habits using Nest/Map/Thread instead of loops. The problem ...
3
votes
3answers
284 views

fastest way to perform the following summation

I have a vector $A$ with components $A_{i}$, and two matrices $X$ and $Y$ with components $X_{ij}$, and $Y_{ij}$ respectively. What is the fastest way of performing the following summation ...
0
votes
2answers
66 views

How to continuous apply a function to an argument? [duplicate]

In order to calculate x = sqrt(1 + x) for a given number x, 40 times, I tried this: ...
4
votes
3answers
163 views

Selective threading of the Rule

How can I change form1 to form2 form1 {10000, 10004} -> {2, 2, 637} form2 ...
11
votes
2answers
262 views

Choose between `Apply` and `Map`

Even though this might be too general question(or not answerable), I want to get some advice here. I apologize in advance if it is not a relevant question. I frequently have to process large list of ...
5
votes
3answers
294 views

Multiplying block matrices

I have a 2*8 matrix A, and a 2*8*2 matrix B. So B[[1]] and B[[2]] are both 8*2 matrices. I need a neat way to multiply A by B so that the first list in the result is A.B[[1]] and the second list ...
1
vote
1answer
91 views

The problem (time) caused by ?NumericQ

In here, I asked a related (similar) question. Using ?NumericQ seems have slowed down the entire calculation. But I now have a more serious problem: ...
1
vote
0answers
75 views

What are the differences between these functions when used for NMaximize?

In short, the question is how to construct the function you want to optimize? ...
5
votes
1answer
493 views

Simple while loop alternative in Mathematica

I have noticed that there are a few examples that go into some detail regarding While loop alternatives. (See here for example: Alternatives to While Loops?) However, I wanted to provide a simpler ...
1
vote
0answers
76 views

Updating procedural fitting algorithm to more efficient style?

For some time now, I have used this procedural programming approach to fit data in my research that is too cumbersome for the built in Mathematica functions. (Although this simple example works fine ...
1
vote
3answers
89 views

How to distribute but skipping one level

I would like to write a Rule that mimics built-in Distribute[], but one that distributes ...
0
votes
2answers
75 views

How do I tell pattern searcher the order in which to search for patterns given in general form?

I have a really long sequence of Symbols inside a function, say nonCommute[a,b,c,e,e,f,g,b,i,j], and I am implementing a ...