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.

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
5 votes
Accepted

How to Force Evaluation of Derivative in a Pure Function Definition

Let's examine the problem as given. I will consider it as a general code rewriting problem. We have an expression of the form h[.., f[ Evaluate[g[x]] ],..] (* or more generally... *) h[.., f[ …
Michael E2's user avatar
  • 245k
5 votes

Implement gradient method in functional programming style

Here's a slight refactoring of the OP's solution. Expressions like #[[5]] are hard to read and therefore hard to debug. One can use a function with symbolic names for arguments to make the code ea …
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
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
8 votes

How to map and sum a list fast?

This addresses the underlying problem, which implies a really fast way is not likely to be found, and shows yet another way to sum a list, Tr. One reason the OP's sum is slow is that the sum is bigge …
Michael E2's user avatar
  • 245k
3 votes

Modifying codes in a more 'Mathematica' way

Another way: (* ad hoc def. of Mat[] *) ClearAll[Mat]; Mat[x_, y_] := { {2 y, x, 1}, {x + y, 0, -x}, {x, 2, y} }; Block[{x = 1.0}, First@ Differences@ MinMax[y /. NSolve[Det[Mat[x, y]] = …
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
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
3 votes

Multiplying block matrices

Another way: a = RandomReal[{0, 1}, {2, 8}]; b = RandomReal[{0, 1}, {2, 8, 2}]; m = First@Outer[Dot, {a}, b, 1]; Compare with Sjoerd's: s = a.# & /@ b; (* Sjoerd's *) s == m (* True *)
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
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
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

15 30 50 per page