Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:35:56+00:00 2026-05-26T12:35:56+00:00

In Mathematica there are a number of functions that return not only the final

  • 0

In Mathematica there are a number of functions that return not only the final result or a single match, but all results. Such functions are named *List. Exhibit:

  • FoldList
  • NestList
  • ReplaceList
  • ComposeList

Something that I am missing is a MapList function.

For example, I want:

MapList[f, {1, 2, 3, 4}]
{{f[1], 2, 3, 4}, {1, f[2], 3, 4}, {1, 2, f[3], 4}, {1, 2, 3, f[4]}}

I want a list element for each application of the function:

MapList[
  f,
  {h[1, 2], {4, Sin[x]}},
  {2}
] // Column
{h[f[1], 2], {4, Sin[x]}}
{h[1, f[2]], {4, Sin[x]}}
{h[1, 2], {f[4], Sin[x]}}
{h[1, 2], {4, f[Sin[x]]}}

One may implement this as:

MapList[f_, expr_, level_: 1] :=
 MapAt[f, expr, #] & /@
  Position[expr, _, level, Heads -> False]

However, it is quite inefficient. Consider this simple case, and compare these timings:

a = Range@1000;
#^2 & /@ a // timeAvg
MapList[#^2 &, a] // timeAvg
ConstantArray[#^2 & /@ a, 1000] // timeAvg

0.00005088

0.01436

0.0003744

This illustrates that on average MapList is about 38X slower than the combined total of mapping the function to every element in the list and creating a 1000×1000 array.


Therefore, how may MapList be most efficiently implemented?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:35:56+00:00Added an answer on May 26, 2026 at 12:35 pm

    I suspect that MapList is nearing the performance limit for any transformation that performs structural modification. The existing target benchmarks are not really fair comparisons. The Map example is creating a simple vector of integers. The ConstantArray example is creating a simple vector of shared references to the same list. MapList shows poorly against these examples because it is creating a vector where each element is a freshly generated, unshared, data structure.

    I have added two more benchmarks below. In both cases each element of the result is a packed array. The Array case generates new elements by performing Listable addition on a. The Module case generates new elements by replacing a single value in a copy of a. These results are as follows:

    In[8]:= a = Range@1000;
            #^2 & /@ a // timeAvg
            MapList[#^2 &, a] // timeAvg
            ConstantArray[#^2 & /@ a, 1000] // timeAvg
            Array[a+# &, 1000] // timeAvg
            Module[{c}, Table[c = a; c[[i]] = c[[i]]^2; c, {i, 1000}]] // timeAvg
    
    Out[9]=  0.0005504
    
    Out[10]= 0.0966
    
    Out[11]= 0.003624
    
    Out[12]= 0.0156
    
    Out[13]= 0.02308
    

    Note how the new benchmarks perform much more like MapList and less like the Map or ConstantArray examples. This seems to show that there is not much scope for dramatically improving the performance of MapList without some deep kernel magic. We can shave a bit of time from MapList thus:

    MapListWR4[f_, expr_, level_: {1}] :=
      Module[{positions, replacements}
      , positions = Position[expr, _, level, Heads -> False]
      ; replacements = # -> f[Extract[expr, #]] & /@ positions
      ; ReplacePart[expr, #] & /@ replacements
      ]
    

    Which yields these timings:

    In[15]:= a = Range@1000;
             #^2 & /@ a // timeAvg
             MapListWR4[#^2 &, a] // timeAvg
             ConstantArray[#^2 & /@ a, 1000] // timeAvg
             Array[a+# &, 1000] // timeAvg
             Module[{c}, Table[c = a; c[[i]] = c[[i]]^2; c, {i, 1000}]] // timeAvg
    
    Out[16]= 0.0005488
    
    Out[17]= 0.04056
    
    Out[18]= 0.003
    
    Out[19]= 0.015
    
    Out[20]= 0.02372
    

    This comes within factor 2 of the Module case and I expect that further micro-optimizations can close the gap yet more. But it is with eager anticipation that I join you awaiting an answer that shows a further tenfold improvement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Mathematica's list of built-in formats is pretty extensive; however, JSON is not on that
I've noticed that I can not use all unicode characters in my python source
Is there a builtin Mathematica function that parses strings representing numbers in hexadecimal form,
First note: mathematically, I'm not that skilled at all. I played a game on
I would like to provide all mathematical functions for the number-like objects created by
I wonder if there exists way to work with large files in Mathematica ?
The problem is that I want to output Mathematica compatible floating point numbers. The
Is there a library function in c# for the mathematical modulus of a number
Is there a Mathematica function like inject in Ruby? For example, if I want
It would not be my intention to put a link on my blog, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.