Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options answers only not deleted user 12

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

12 votes

Simple while loop alternative in Mathematica

You are looking for a pattern suitable for loops which must terminate on a condition (rather than run a fixed number of times). While is a good solution for this, but the key is really Break[], which …
Szabolcs's user avatar
  • 236k
4 votes
Accepted

Functional programming approach to avoid traditional loops

You can use ListConvolve or ListCorrelate, like this: ListConvolve[ {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, mat]
Szabolcs's user avatar
  • 236k
25 votes

Why is Unevaluated[#]& different from Unevaluated?

I thought that for any function f we could use "f" interchangeably with "f[#]&". That is only true if the function has no special attributes. Function effectively removes those attributes, and a …
Szabolcs's user avatar
  • 236k
16 votes
Accepted

Meaning of ## #

Plus[## #] & is the same as Plus[Times[##, #1]], which always computes to the same as the simpler Times[##, #1] (because Plus[x] is just x). Thus this computes the same as Table[i^2 * j, {i, 2}, {j, …
Szabolcs's user avatar
  • 236k
35 votes
Accepted

What is the difference between Composition (@*) and sequential applications (@)?

Clearly the @ notation is inspired by the usual mathematical notation for function composition. f@g[x] looks very similar to the mathematical notation $(f\circ g)(x)$. But it is important to underst …
Szabolcs's user avatar
  • 236k
2 votes

Update a function avoiding infinite recursion

Do you mean you need to update the definition of the function in terms of the previous definition? I do not get infinite recursion when doing the following: Make sure x has no assigned value when …
Szabolcs's user avatar
  • 236k
4 votes
Accepted

Summing Over a Variable Number of Indices

The function g you describe can be implemented in a simple way like this: g[n_, s_] := Total[Multinomial @@@ IntegerPartitions[n, {s}]] I'll admit I didn't go through your code. Is the above helpf …
Szabolcs's user avatar
  • 236k
4 votes

How to force evaluation on the right of an @ operator?

Instead of f @ (g @@ x) you can use f @ Apply[g] @ x (since Mathematica 10). This is more verbose, but it saves parentheses. Since visually matching parentheses is hard, I think that the latter vari …
Szabolcs's user avatar
  • 236k
3 votes

Making a list-function acting one-to one in a list of elements

There isn't a reasonable way to avoid computing both f1 and f2 in this case. But we can avoid storing the whole matrix, which would (temporarily) take up a lot of memory. One way is MapIndexed. f = …
Szabolcs's user avatar
  • 236k
9 votes
Accepted

How to select the fastest approach for large numerical data computations?

The main question here is, there are too many approaches to perform the same operation. And normally, I didn't know which approach is the most optimal way in terms of efficiency. Mathematica's perfo …
Szabolcs's user avatar
  • 236k
12 votes
Accepted

Using vtx[] instead of vtx

The reason to use 1. is readability. There is no difference in function or performance. Simply because of established convention, most people when they see f[vtx], they assume that vtx is "a variable …
Szabolcs's user avatar
  • 236k