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

0
votes
0answers
59 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 ...
5
votes
3answers
103 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
53 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
78 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
0answers
3 views

Show that A has an unique extension to a bounded operator on H [migrated]

if {e1 , e2 , ...} is an orthonormal basis for Hilbert space H and for each n there is a vector Ae_n in H such that Σ∥Ae_n∥ < ∞ .Show that A has an unique extension to a bounded operator on H.
0
votes
2answers
84 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
131 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
77 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
799 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 ...
8
votes
11answers
860 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
494 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
144 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
61 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
51 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
326 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
133 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
175 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
144 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
49 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
46 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
210 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
85 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
60 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
143 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
342 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
84 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
146 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
263 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
52 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
253 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
276 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
89 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
71 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
431 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
72 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
86 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
73 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 ...
2
votes
1answer
54 views

Searching for symbol except within subexpressions with a particular Head

I would like to write a searcher like FreeQ that looks for a Symbol (or Head) in an ...
1
vote
1answer
213 views

How can I call the R-programming packages in my Mathematica?

I am trying to call the R-programming package called highfrequency in my Mathematica so that I can use its function rCov. I ...
3
votes
1answer
43 views

Mapping also for one element (or telling it how to do so if head got changed)

Some code generates a polynomial, and has the form: expr = Plus[a[i],b[i],c[i],d[i]] I need to apply f to each term. To do ...
6
votes
3answers
195 views

How to get “Signature” of each element of “Permutation” output

I need to sum over all possible permutations, multiplied by the Signature of each permutation of a given list of symbols. But, I don't know how to determine the ...
4
votes
2answers
131 views

Can't figure out how to apply these functions repeatedly

I have the following problem, and even though I've tried Fold & Inner, Sow & Reap, et al. I can't figure out the clean way to do this. Here's the problem. I have a list that starts as ...
3
votes
2answers
63 views

Fast functional method for multiplying duplication numbers in a large list

Consider a list of symbols: mySet = {a, b, b, c, a, b, c, d}; I would like to form the product of duplication numbers. In the example above, I have 2 ...
3
votes
1answer
128 views

How to remove recursion, and write it functional and efficient way?

I wish to implement the following recursion efficiently. ...
-1
votes
4answers
413 views

Mathematica's equivalent to the Python's Zip command in List-comprehension? [closed]

What is Mathematica's equivalent to Python's ZIP command? Is this called nesting? It means iterating over sub-sets of values like the below example. How can I do it in Mathematica? Example in Python ...
3
votes
3answers
140 views

Working with calculations that depend on the previous value in a list

I'm trying to make calculations on a list that depend on the previous value. In my case I'm doing event detection - detecting the point where a value rises above some threshold for the first time ...
11
votes
5answers
340 views

Applying a function to a Dataset column conditional on another slot's value

How to apply a function to a column conditional on another slot's value? The Dataset documentation show only how to apply a function to named slots independently. The ...
4
votes
3answers
293 views

have a function redefine itself as it maps through a list

I have a list which contains sublists, e.g. list = {{a,b}, {a,b,c}, {a,b,a,a}, {b,c}, ...} I then map a function through each sublist. This function should ...
3
votes
1answer
97 views

Listable on first level [duplicate]

I have some function which take List as input. f[l_List]:=someActions[l] I need apply this function to a list of lists. Now I ...