5.4 C
New York
Wednesday, March 29, 2023

New in 13.1: Past Listability: Introducing Threaded—Wolfram Weblog

Final 12 months we launched Model 13.0 of the Wolfram Language. Listed here are the updates in listability since then, together with the newest options in 13.1.


Past Listability: Introducing Threaded

From the very starting of Mathematica and the Wolfram Language we’ve had the idea of listability: if you happen to add two lists, for instance, their corresponding components will likely be added:

{a, b, c} + {x, y, z}

It’s a really handy mechanism, that sometimes does precisely what you’d need. And for 35 years we haven’t actually thought-about extending it. But when we have a look at code that will get written, it typically occurs that there are elements that mainly implement one thing very very like listability, however barely extra common. And in Model 13.1 we’ve a brand new symbolic assemble, Threaded, that successfully permits you to simply generalize listability.

Take into account:

{{1, 2}, {3, 4}} + {x, y}

This makes use of strange listability, successfully computing:

{{1, 2} + x, {3, 4} + y}

However what in order for you as an alternative to “go down a stage” and thread {x,y} into the bottom elements of the primary listing? Effectively, now you should use Threaded to try this:

{{1, 2}, {3, 4}} + Threaded

By itself, Threaded is only a symbolic wrapper:


However as quickly because it seems in a perform—like Plus—that has attribute Listable, it specifies that the listability ought to be utilized after what’s specified inside Threaded is “threaded” on the lowest stage.

Right here’s one other instance. Create a listing:


How ought to we then multiply every ingredient by {1,–1}? We may do that with:

(#1*{1, -1} & ) /@ Table

However now we’ve acquired Threaded, and so as an alternative we are able to simply say:


You may give Threaded as an argument to any listable perform, not simply Plus and Instances:


You need to use Threaded and strange listability collectively:

{{1, 2}, {3, 4}} + Threaded

You possibly can have a number of Threadeds collectively as nicely:

{{1, 2}, {3, 4}} + Threaded

Threaded, by the best way, will get its title from the perform Thread, which explicitly does “threading”, as in:


By default, Threaded will all the time thread into the bottom stage of a listing:

{{{3, 4}, {4, 5}}, {{4, 5}, {5, 6}}} + Threaded
{{{{4, 5}, {5, 6}}, {{5, 6}, {6, 7}}}, {{{5, 6}, {6, 7}}, {{6, 7}, {7, 8}}}} + Threaded

Right here’s a “real-life” instance of utilizing Threaded like this. The information in a 3D colour picture consists of a rank-3 array of triples of RGB values:


This multiplies each RGB triple by {0,1,2}:


More often than not you both need to use strange listability that operates on the prime stage of a listing, otherwise you need to use the default type of Threaded, that operates on the lowest stage of a listing. However Threaded has a extra common type, in which you’ll be able to explicitly say what stage you need it to function at.

Right here’s the default case:

{{{3, 4}, {4, 5}}, {{4, 5}, {5, 6}}} + Threaded

Right here’s stage 1, which is rather like strange listability:

{{{3, 4}, {4, 5}}, {{4, 5}, {5, 6}}} + Threaded

And right here’s threading into stage 2:

{{{3, 4}, {4, 5}}, {{4, 5}, {5, 6}}} + Threaded

Threaded gives a really handy method to do all types of array-combining operations. There’s extra complexity when the article being “threaded in” itself has a number of ranges. The default on this case is to align the bottom stage within the factor being threaded in with the bottom stage of the factor into which it’s being threaded:

{{{3, 4}, {4, 5}}, {{4, 5}, {5, 6}}} + Threaded

Right here now could be “strange listability” conduct:

{{{3, 4}, {4, 5}}, {{4, 5}, {5, 6}}} + Threaded

For the arrays we’re right here, the default conduct is equal to:

{{{3, 4}, {4, 5}}, {{4, 5}, {5, 6}}} + Threaded

Generally it’s clearer to write down this out in a type like

{{{3, 4}, {4, 5}}, {{4, 5}, {5, 6}}} + Threaded

which says that the primary stage of the array contained in the Threaded is to be aligned with the second stage of the surface array. Usually, the default case is equal to –1 → –1, specifying that the underside stage of the array contained in the Threaded ought to be aligned with the underside stage of the array outdoors.

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles