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
2answers
75 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 ...
3
votes
1answer
96 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
102 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
213 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
46 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
194 views

Defining a sequence slot programmatically

This question is related to: Composition of functions using slots `Slot` (#) interfering with evaluation Consider a toy example: ...
13
votes
1answer
260 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
133 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
128 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
134 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
817 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
78 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
43 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
116 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
91 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
84 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
71 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
203 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
300 views
9
votes
3answers
443 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
99 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
81 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
119 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
179 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
177 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
99 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
294 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
227 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
194 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
162 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 ...
2
votes
2answers
235 views

How can I iterate my code a 1000 times?

this is my solution to Exercise 5 of http://work.caltech.edu/homework/hw2.pdf Please help me make this more elegant. The exercise is about classifying random Points by a (randomly generated) target ...
2
votes
1answer
79 views

Behavior of ValueQ inside a function

The following code Cases[Function[{x}, x + 1], a_?ValueQ :> Hold[a], ∞, Heads -> True] unexpectedly returns {Hold[x + 1]} According to the manual, ...
0
votes
2answers
107 views

Where are the functional programming constructs: `every` and `some`? [duplicate]

Coming from other functional programming environments (e.g., Clojure) I've become accustomed to some functions that I haven't found equivalents for in Mathematica yet. The two I reached for last ...
0
votes
2answers
198 views

Replace Do loop with Nest

...
2
votes
2answers
504 views

Q-Function representation in Mathematica

I observed that there is no Q-function representation in Mathematica. The definition of Q-Function is: \begin{align} Q(x) &= \frac{1}{\sqrt{2\pi}} \int_x^\infty e^{-\frac{u^2}{2}}du \\ ...
3
votes
2answers
107 views

How to “explode” a list inside a function call argument list? [duplicate]

I'm trying to do something like below, but it doesn't work, since StringForm requires each item from parts to be given ...
-1
votes
1answer
176 views

Using functions to make loops

I'm trying to make a simulation of an electron that goes through a magnetic and electric field perpendicular to each other and to the velocity of the electron but I'm running in some troubles with the ...
1
vote
2answers
101 views

How do I operate on this list elegantly? [duplicate]

calibData = { {200, 10}, {100, 20} } MapAt[N, MapAt[Log10, calibData, {;; , 1}], {;; , 1}] I want to apply a base 10 Log to the first element of each tuple. ...
5
votes
7answers
568 views

Better code to find Narcissistic number

My code finding Narcissistic numbers is not that slow, but it's not in functional style and lacks flexibility: if $n \neq 7$, I have to rewrite my code. Could you give some good advice? ...
0
votes
1answer
75 views

Evaluate outer function first

I'm new in Mathematica so this is maybe super easy to do, but I can't figure out how. I've got a function plus[a,b] that sums two numbers and I need to implement a ...
1
vote
2answers
149 views

Using mathematical notation

Is it possible to write this function in mathematica exactly as shown, and then run the expression on a dataset? Please note I know that there are several other ways to do this. I am only ...
3
votes
1answer
168 views

Levels and slots

I am struggling with levels and #, if I understand this I think I will be able to grasp the entire concept. ...
12
votes
5answers
631 views

What's the cleanest functional-programming style way to perform this list operation?

I am looking for the correct method for accomplishing the unknown piece in the following code snippet: rolls = Tuples[Range[1, 4], {2}] ...
4
votes
4answers
429 views

Replace For-loop with functional code

I am very new to Mathematica. I have code written using for loops. I want to rewrite it using Map/Thread but I am not sure how ...
10
votes
5answers
486 views

How to find first list element that differs from average of N previous elements by more than a given amount?

Given a list of real numbers, I need to write a function that returns the value of the first element in the list that differs from the mean of the previous $N$ elements by greater than $P$ standard ...
3
votes
1answer
231 views

NestList with a list inside?

I'm new to Mathematica, and was looking for more efficient ways to implement the Do loop below. a, ...
5
votes
2answers
224 views

Overlapping pure functions

Let say I need and option for Plot: RegionFunction -> (-6 < #2 < 6 &) But I also want those ...