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 4999

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

2 votes

Defining a function to determine when convergence is guaranteed

You're missing a & in your question, which might be simply a typo. iterationsteps[function_, seed_, steps_] := Length[Select[FixedPointList[function, seed, steps] - FixedPoint[function, seed], # >= …
Michael E2's user avatar
  • 245k
1 vote

Applying a function of several arguments to the rows of a matrix

Anon's answer is the way I would do it, but it's fun to figure out a solution that works for single vectors as well as a list of vectors: f[a_, b_, u_] := Map[Composition[#^b &, Total], u, {-2}].a …
Michael E2's user avatar
  • 245k
11 votes
Accepted

Why are we so sure about Map results?

I'm afraid my comment was too obscure to be noticed. Further, I disagree with one premise somewhere in the commentary, and I wish to make a fuller explanation to see if I understand correctly or inco …
Michael E2's user avatar
  • 245k
2 votes
Accepted

How do I operate on this list elegantly?

If you have a large amount of calibData, then preserving packed arrays can greatly improve speed. Convert it all to Reals and then apply logarithm. calibData = 10 RandomInteger[{1, 20}, {10^6, 2}]; …
Michael E2's user avatar
  • 245k
3 votes

Through: how to use it with subtraction of functions?

I sometimes want to apply a linear combination of functions to an argument. This can be more difficult if the linear combinations are generated by code, so that rewriting them some form to make Throu …
Michael E2's user avatar
  • 245k
21 votes
Accepted

Can this be written well, without loops?

There are some features of this specific problem one can take advantage of. The boundary of the x,y,z,n domain represented by val <= max is linear in x,y,z and only quadratic in n; furthermore val in …
Michael E2's user avatar
  • 245k
1 vote

Differentiating space curves

Here is a simple fix of the OP's definitions: norm[x_] := Sqrt[x.x] tangent[γ_] := Function[s, Evaluate[γ'[s]/norm[γ'[s]]]] normal[γ_] := Function[s, Evaluate[tangent[γ]'[s]/norm[tangent[γ]'[s]]]] …
Michael E2's user avatar
  • 245k
5 votes

Computing distance matrix for a list

If the purpose is as in the following, The reason I want to do that is to filter the list to find the matrix closest to the target one, then an alternative would be to use Nearest: SeedRandom[2 …
Michael E2's user avatar
  • 245k
5 votes

fastest way to perform the following summation

Here's another way that's quite a bit faster on David Stork's example: #.Y.# &[A.X] SeedRandom[0]; A = Table[RandomReal[], {3000}]; X = Table[RandomReal[], {3000}, {8000}]; Y = Table[RandomReal[], {8 …
Michael E2's user avatar
  • 245k
2 votes
Accepted

How to organize expression by symbols (like Collect), but apply different functions to each ...

Maybe the following. Simplifying a left-over constant term along with the other coefficients seems hard to comprise in a single, simple function. forms = {_f, _e, _g, _h}; funcs = {simpF, simpE, sim …
Michael E2's user avatar
  • 245k
9 votes

Applying Function with SlotSequence and Lists

This gets the desired result: (MyFunction @@ #1) @@ #2 & @@ {{1, 2, 3}, {4, 5, 6}} (* MyFunction[1, 2, 3][4, 5, 6] *)
Michael E2's user avatar
  • 245k
18 votes

What is the fastest method to set the diagonals of a matrix to 1?

A long comment...I'm using V13.1, Mac M1 Pro, which might matter... First, I would point out the "About" section of the profile of user @WReach. Toward the end, the question "Why don't you like to ans …
Michael E2's user avatar
  • 245k
7 votes

K-Mean Clustering Algorithm

Compiled, parallelized ordering plus a more functional approach. The switch between DistanceMatrix and Outer (for dm) may be system dependent. ordC = Compile[{{x, _Real, 1}}, First@Ordering[x, 1], …
Michael E2's user avatar
  • 245k
15 votes

Iterate over # without needing to define a variable

Best on packed arrays... PadLeft[Transpose@{#2}, {Automatic, 2}, {#1}] &[ n0, {n1, n2, n3, n4, n5}] (* {{n0, n1}, {n0, n2}, {n0, n3}, {n0, n4}, {n0, n5}} *) PadLeft[ArrayReshape[#2, {Length@#2, …
Michael E2's user avatar
  • 245k
6 votes

How to find first list element that differs from average of N previous elements by more than...

Here's a way using NestWhile: f[lst_?(VectorQ[#, NumericQ] &), n_Integer?Positive, p_?Positive] := If[Length @ # > n, #[[n + 1]], None] &@ NestWhile[Rest, lst, Length @ # > n && Abs[Mea …
Michael E2's user avatar
  • 245k

15 30 50 per page