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

5
votes
1answer
181 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
58 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
78 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
66 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
49 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
171 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
41 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
169 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
110 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
57 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
116 views

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

I wish to implement the following recursion efficiently. ...
-2
votes
4answers
192 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
113 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 ...
9
votes
5answers
216 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
278 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
63 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
149 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
90 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
126 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
121 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
247 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
48 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
201 views

Defining a sequence slot programmatically

This question is related to: Composition of functions using slots `Slot` (#) interfering with evaluation Consider a toy example: ...
15
votes
1answer
311 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
163 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
136 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
187 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
850 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
85 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
119 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
98 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
87 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
76 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
222 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 ...
5
votes
4answers
303 views
10
votes
3answers
538 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
106 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
91 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
151 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
203 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
74 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
192 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 ...
2
votes
1answer
108 views

Optimizing my code to generate symmetric tensors

I have written code that does carries out the following task: Construct a fully symmetric rank-$P$ "tensor" out of a collection of user-input rank-1 vectors, and a rank-2 symmetric tensor $h$. The ...
6
votes
1answer
299 views

What is the most elegant way to specifiy a start case for recursion?

When writing recursive code that computes the least value at which a condition is true and various other scenarios it is often necessary to specify a base case. What I have done to supply this is ...
1
vote
2answers
274 views

Mapping functions over multiple lists

I am not sure if this is a duplicate. Spent some time looking over list-manip questions. I’d like to know the Quickest Way to achieve the following Mapping of a function over multiple lists. ...
4
votes
2answers
202 views

About auto-compiling and performance between Do and Fold

I was investigating how Fold could improve performance vs Do. I tested the code ...
0
votes
1answer
81 views

Defining a formal Primitive Recursion

I'm searching for a way to define the function this wikipedia article call Primitive Recursion. It should have two functions as argument and it should return a function. I need this as helping in ...
1
vote
1answer
166 views

How should I go about writing more functional and list based code? [duplicate]

I've heard that Mathematica is usually faster when the code is list-based and/or uses a functional paradigm. I'm still working on getting the hang of this; there are a lot of ...