The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [functional-style]

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

Filter by
Sorted by
Tagged with
6
votes
3answers
773 views

How can I rewrite this with NestList or FoldList?

The original code used For: ...
11
votes
4answers
608 views

How to write this without For loop

Suppose I have a few lists of numbers and want to exponentiate element-wise, then sum up everything into a polynomial. For example, if I have ...
0
votes
1answer
400 views

How can I improve my code with efficient mathematica built-in functions?

I have written a module for my assignment like below. V[n + 1] = rk[x[n], y[n] ,z[n], V[n]]; vs = V[n].V[n+1]; V[n + 1] = I want to modify it with efficient ...
9
votes
3answers
520 views

Map over list of functions

I would like to convert a list of $n$ complex equations to a list of $2n$ real ones. At the moment I am doing it like this: ...
6
votes
1answer
172 views

Functional Programming: modify EdgeList based on condition

I have a simple functional programming question: I have a simple EdgeList defined in Mathematica, I would like to modify the value of certain element of the list ...
14
votes
3answers
920 views

How can I improve my code for drawing a tree?

This code draws a tree, but it's not in the functional style. ...
5
votes
3answers
194 views

All values for a function with two arguments without Outer

Given two lists $l_a = \{a_1, a_2, a_3\}$, $l_b = \{b_1, b_2, b_3, b_4\}$ and some function $f$ accepting two arguments, how can I produce a list of all values $f(a, b)$ for $a\in l_a$ and $b \in l_b$ ...
4
votes
2answers
209 views

How can I improve this trial division procedure?

My background is procedural programming, so I find this construction quite natural. Is there a way to get rid of the While? ...
4
votes
6answers
267 views

Filter list with different list in it

I have a list like: {{{4, 14}, 1}, {{4, 15}, 1}, {{4, 16}, 1}, {{4, 17}, 1}, {{4, 18}, 1}, {{4, 14}, 3}, {4,15}, {{4, 16}, 2},{4,18}} Now I want to ...
0
votes
1answer
350 views

Iterative Function Mapping [duplicate]

Possible Duplicate: How to nest my own “times” function to get powers What I want to do is something like : Sin[...Sin[Sin[Sin[Sin[x]]]]] Basically, ...
3
votes
1answer
398 views

Evaluating a function on permutations of its arguments

Say I have a function "temp" of $n+1$ variables, $y,z1,z2,z3,...,zn$. I want to test if my function has certain symmetries like swapping $y$ with square of any $z$, swapping any two of the zs, ...
2
votes
1answer
487 views

#[[1]]//FullForm = 1?

I have a set of symbolic algebraic expressions that I'm trying to get some speed into. To illustrate the issue, I'll use a simple form like { k0 X, k1 Y, k1 Z}, ...
4
votes
1answer
1k views

Converting expressions into functions

I have defined an expression, such as a = x I would like to convert this into a function, such as f[x_]:=x However, in my ...
3
votes
4answers
319 views

Order of operations for the Table function [duplicate]

Possible Duplicate: Using pure functions in Table I have run into a situation that I do not understand when trying to generate a nested list of pure functions. I have the following code. ...
8
votes
3answers
677 views

Rewriting For loop to FoldList or similar

In CrossValidated, the answer to a question on prediction of number of viewers of Gangnam Style is given in R. I have mapped the answer directly to Mathematica: ...
13
votes
5answers
1k views

How to improve this code for solving the “Mr.S and Mr.P” puzzle?

Mr.S and Mr. P puzzle — "Formalization of two Puzzles Involving Knowledge", McCarthy, John (1987) We pick two numbers $a$ and $b$, such that $a\geq b$ and both numbers are within the range $(2,...
5
votes
3answers
512 views

Generate a Combination of letters by a number

I'm trying to write a function f. example: ...
7
votes
2answers
695 views

Define product derivative

How do I define the $n$th product derivative of a function in Mathematica? The first product derivative $f^\ast$ of a function $f$ is $$ f^\ast(x)=\exp\left(\frac{f^\prime(x)}{f(x)}\right) $$ The $n$...
9
votes
4answers
3k views

Alternatives to While Loops?

I am using Mathematica to run a probabilistic simulation. Essentially, I have a list of members of a population (they only have one, numerical, attribute, so it's just implemented as a list of ...
11
votes
2answers
350 views

From iterative to functional

How to write this small piece in a functional way (ie. without state variables)?: ...
5
votes
1answer
272 views

Timing differences between multiple executions of identical code

I have a list of pairs of numbers and I'd like to change the sign of the second element in each pair i.e. $(a,b)\rightarrow (a,-b)$. I'm sure there are many ...
2
votes
2answers
824 views

How to create a list of variable names to some function of that variable

For example, for a single variable I can write Print[Unevaluated[a], "=", f[a]] But if I try the next thing I'd think of doing, assuming I want a list, it doesn'...
18
votes
3answers
5k views

How do I convert an argument list to a sequence of arguments?

I think this is a basic question, but I am having difficulty finding the answer in the documentation. Thread is not what I am looking for, I think. Suppose that I ...
1
vote
2answers
194 views

How can define a set of nested parameters

I would like to know how I to define a function with a nested set of parameters (or whatever you would like to call it). For example, how would I get the following function ...
12
votes
4answers
562 views

Thread a function over a list and with a non-atomic 2nd parameter?

From the documentation, Thread's behavior on functions where the first parameter is a List and the second is an atomic expression, is this: ...
38
votes
12answers
3k views

Map a function across a list conditionally

It seems that this is a really basic question, and I feel that the answer should be obvious to me. However, I am not seeing. Can you please help me? Thanks. Suppose I have a list of data ...
0
votes
2answers
144 views

About minimizing calculations when using ComposeList

I have a lot of functions to use in a iterative way, and I need some of the calculation results. For example: ...
6
votes
5answers
345 views

How to nest my own “times” function to get powers

I have a "times" function. I'd like to create a power function using it. It should look like this for an 6th power: ...
6
votes
0answers
152 views

What are some general strategies to avoid using For loops? [duplicate]

Possible Duplicate: Iterating over lists in Mathematica While there are some cases where a For loop might be reasonable, it's a general mantra – one I ...
41
votes
4answers
3k views

Function in Table

I need a table with the elements made of pure functions and list elements. This is a simplified example: I need a list as: ...
-1
votes
1answer
246 views

GatherBy Option to group results?

How can GatherBy be programmed to take an option to group results as follows: ...
2
votes
3answers
231 views

Using Nest inside NestList

I need results like this: {Cos[Cos[x]], Sin[Cos[Cos[x]]],Cos[Cos[Sin[Cos[Cos[x]]]]], Sin[Cos[Cos[Sin[Cos[Cos[x]]]]]], ... } I've tried to use ...
20
votes
4answers
608 views

How to keep some of the results of the NestList

I'm using some iterative arithmetics to calculate wave propagation with the help of NestList. I have to use a small step size for iteration to guarantee the accuracy, which lead to too much data (e.g, ...
2
votes
3answers
307 views

Ways to change an explicit function into a pure function

I'm trying to use FixedPoint to solve a transcendental equations, but the first argument of the FixedPoint should be a pure ...
14
votes
2answers
939 views

What's the most “functional” way to do Cholesky decomposition?

I can do Cholesky in a procedural style, such as: ...
24
votes
0answers
805 views

How can I trace a functional expression's evaluation visually?

Ok, so the goal is to visualize the execution of this expression foo /@ {3 + 1, bar /@ {1 + 2, {4 + 7}}} in the following fashion using nested rectangles with ...
35
votes
3answers
1k views

Prefix operator with low precedence

The question is simple, but I will elaborate on the background as well for those interested in the idea: How to define a new operator with specified precedence value? Background Mathematica was ...
14
votes
2answers
762 views

Reproducing nested loops using Map?

I keep producing bits of code like the following: ...
5
votes
1answer
231 views

Composite graphics with Row, Column, data aggregation and layout control

This chromogram shown here represents data (mouse activity patterns during visits) for ~20 physicians in a study, each of which may have a variable number of visits in the study (physicians are color ...
4
votes
1answer
263 views

3x+1 on arithmetic chains, tree pruning and NestList

Given the following function Collatz: ...
10
votes
2answers
421 views

Cases[data,Colon[key,_]] vs. Cases[data,key:_] toward XPath, XQuery

Exploring methods to index and search tree-structured key:value pairs via named-entity index (key paths) as opposed to Position-based indexing. Using Rule as ...
9
votes
3answers
414 views

Can this code be written in a more functional style

Referencing this question, I wonder if the following code can be written more concisely using a functional style, i.e. without For loops or ...
33
votes
5answers
2k views

Does every Symbol in Mathematica induce a monad?

From my question about types in Mathematica, I assume that every Symbol in Mathematica is a type if it appears as a Head of some ...
25
votes
2answers
2k views

Using a list of tuples in a pure function

I want to use a list of tuples within a function to make assignments. Say I want to make assignments of the form value[i,j] = val What I have is a list of ...
4
votes
1answer
426 views

Explanation on why Compile statement works only if input and output sizes work

I found the following code: Compile[{{m, _Real, 2}}, Fourier[m]][Table[N[i - j], {i, 4}, {j, 4}]] which doesn't work correctly. But the following was posted as a ...
19
votes
1answer
505 views

Are there advantages to using additional arguments of common functions rather than alternative ways of calculating with lists?

(Apologies for the long question title.) One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can ...
39
votes
8answers
3k views

Can this be written well, without loops?

Inspired by this question I would like to know if the following code can be written without explicit loops (For, While, etc.) in a clean, efficient and non-contrived way. I have been unable to do so. ...
11
votes
3answers
11k views

Using sets of arguments from a list

Please Consider : list={{5,2,6},{2,8,3}} I need to get the product of all the element within list : As of now I do this : ...
18
votes
4answers
956 views

Is there a more elegant and efficient way to write brainf*** style loops in Mathematica?

I'm having some fun writing a brainf*** interpreter in Mathematica. See this wiki article for more info. It works nicely but I'd like to find an elegant, preferably functional, way to handle the ...
21
votes
3answers
946 views

Can someone explain this snippet: (#[#] &)[#[#][#] &]

A friend showed me this snippet of code today: (#[#] &)[#[#][#] &] I can sort of see that we're constructing an anonymous function that takes a function ...