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

4
votes
3answers
150 views

Selective threading of the Rule

How can I change form1 to form2 form1 {10000, 10004} -> {2, 2, 637} form2 ...
8
votes
0answers
180 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
240 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
0answers
61 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
65 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
242 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
63 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
84 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
69 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
51 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
186 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
42 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
180 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
119 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
60 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
121 views

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

I wish to implement the following recursion efficiently. ...
-1
votes
4answers
232 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
121 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 ...
10
votes
5answers
258 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
285 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
0answers
66 views

Listable on first level

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 ...
3
votes
1answer
161 views

How to replace While construct to NestWhile(functional-style) correctly?

Recently,I find the program of @J.M. about Get a "step by step" evaluation in Mathematica is a very useful example for learning Mathematica programming. In the function ...
2
votes
2answers
103 views

Local variables whose definitions depend on each other [duplicate]

Consider the following (pseudo-)function definition: f[l_] := Let[{ a = Length[l], b = First[l], c = b/a, }, c*10] So for ...
4
votes
1answer
135 views

Can $\it{every}$ usage of While be changed into NestWhile?

Ok, I think this problem is more close to philosophy. :P I've some idea now. But I'm not sure whether it's correct. For the purpose of convenience, I will use some C-like code instead of MMA's. A ...
0
votes
0answers
122 views

Improving the speed of evaluating Table with nested functions

I am a Mathematica novice working with a stack of 1024 x 1024 WatershedComponents (or MorphologicalComponents) label matrices ...
7
votes
3answers
265 views

AssociationMap to query Dataset across levels?

Is there a way to operate on Dataset across levels to apply a function f to a key -> value pair in an Association as follows: ...
0
votes
1answer
49 views

Mapping with Trace[] [duplicate]

As part of some data anaylsis, I am using the function Trace to give me the name of an array of data, which I then use to label my plots. eg. my data array is called r1 so I run: ...
0
votes
0answers
31 views

Expressions to functions, and evaluation [duplicate]

When calculating derivatives a expression is the result: in:= f[x_,y_]:= 1 + 2 x + 3 x^2 y + 4 x y^2 in:= D[f[x,y],x] out= 2 + 6 x y + 4 y^2 how do i make this ...
2
votes
2answers
207 views

Defining a sequence slot programmatically

This question is related to: Composition of functions using slots `Slot` (#) interfering with evaluation Consider a toy example: ...
18
votes
1answer
373 views

Shorter syntax for Fold and FoldList?

I find it tedious to write out the following: a = {1, 2, 3, 4}; Fold[f, First @ a, Rest @ a] f[f[f[1, 2], 3], 4] It ...
5
votes
1answer
174 views

MapAt and generic Functions on Associations and Datasets

These are distinct but related questions about mapping functions to Associations and Datasets. Given: assoc = <| a -> 1, b -> 2, c -> 3|> And ...
0
votes
3answers
141 views

How can I use NestWhileList instead of While in this case?

OK, the problem occurred when I challenged with Project Euler No. 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. My code is: ...
3
votes
1answer
225 views

Gateaux (directional) derivatives and higher order differentials of a functional

I would like to calculate the Gateaux 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$ ...
6
votes
9answers
865 views

Find the smallest positive integer that satisfies a certain condition

I know Mathematica is really powerful when it comes to functional operations like applying a list of tasks to a list of variables. Sometimes I feel like it isn't the most powerful tool when it comes ...
1
vote
1answer
88 views

Nest Replace Function with different rules

I have a table of data that I want to filter. Any value of zero in the table means that value was not calculated, so it should get populated with the tag "NC". Next the table is filtered for all ...
0
votes
1answer
44 views

Resolving Error: Mathematica evaluation order on Lists

I have a table that is 27 x 27, each entry corresponds to an element from the periodic table. I use the replacement rules in element list to allow me to convert a list of elements into a list of ...
1
vote
0answers
121 views

Finding Values of All Nine Constants in q-hypergeometric Equation

I suspect that an identity of the form $$\sum_{j=-n}^{j=n}(-1)^jq^{j(5j+1)/2}=\sum_{r=0}^{n}f_{ar+bn+c}(-1)^{n-r}q^{(n-r)(dr+en+f)/2}\frac{(q;q)_{gr+hn+j}}{(q;q)_{n-r}}$$ for $a, \dots , j$ integer ...
0
votes
1answer
103 views

Dealing with nested lists

This question is strongly related to: Map and Apply a function on a nested list I am in a bit of a rut in terms of my MMA skills and looking to improve my coding. I've realized that rather than ...
1
vote
0answers
90 views

Is it possible to change to a functional approach in this game-based procedural algorithm?

First of all, I'm aware that Mathematica StackExchange prefer specific questions, not discussions, but I need some enlightenment with this issue for I have nowhere else to ask :) The thing is, our ...
0
votes
0answers
80 views

Improving a function that filters each table from a list of tables

I have written the following function which can take in a list of tables as an arguement, and then filters each one. The function I wrote is below, and the commands I combined are listed below the ...
4
votes
3answers
231 views

Conditional search along the columns of a matrix

I want to implement the following algorithm in Mathematica. Get element along main diagonal of a matrix. If the element along the main diagonal is positive, then return the positions of the elements ...
11
votes
2answers
703 views

Scan vs Map vs Apply

I cannot understand the difference between Apply, Scan, and Map. I have tried to play with ...
5
votes
4answers
305 views
11
votes
3answers
588 views

What is the most efficient way to map a linked list?

In order to work with lists effectively using common recursive patterns from functional programming, it's often most convenient to worked with the "linked list" representation, that looks like this: ...
1
vote
2answers
110 views

Using Mathematica to derive the PDF of the normal distribution

I am trying to use Mathematica, in this case in a well known and rather simple list of equations, but I get stuck whit Mathematica. Since I am just starting to use Mathematica for mathematical ...
1
vote
1answer
95 views

How to input pure function to package?

I would like to input a pure function into a custom written package, but am running into a variety of errors. The package I have created is as follows: ...
1
vote
2answers
168 views

Summing Over a Variable Number of Indices

I am trying to figure out the best way to include a variable number of indexes of summation in a program. For example: Suppose I want to define a function $g[n,s]$ that returns the sum ...
4
votes
2answers
208 views

How to set up and minimize a large system of equations to resolve the individual components

I am trying to set up a system of 400*N equations to deconvolute spectral data for four different compounds. I monitored 400 different wavelengths in N solvents (3 solvents in this case). In order ...
0
votes
1answer
75 views

How to write series of Do[ ]'s with a single Table[ ]?

Edited to provide more information (sorry I don't know how to format matrices here) I would like to replace: m = 4; ...
6
votes
2answers
198 views

Evaluation in lambda function

If I have a function, that returns a function, for example: f[a_] := a^2 * # & then f[3] == 3^2 # & Is there a way ...