Skip to main content
edited body
Source Link
Henrik Schumacher
  • 109.4k
  • 7
  • 186
  • 322

You may exploit that UnitStep step is vectorized like this:

a = {1, 1, 1, 1};
b = {2, 2, 2, 2};
c = {4, 2, 4, 2};
{False, True}[[2 - UnitStep[(a + b) - c]]]

{True, False, True, False}

If you can life with 0 and 1 instead of False and True, the the following will serve your needs and is more efficient (for large lists):

Subtract[1, UnitStep[Subtract[(a + b), c]]]

In particular, it is two orders of magnitude faster than Thread:

n = 1000000;
a = RandomInteger[{0, 1000}, {n}];
b = RandomInteger[{0, 1000}, {n}];
c = RandomInteger[{0, 1000}, {n}];

r1 = Thread[(a + b) < c]; // RepeatedTiming // First
r2 = Subtract[1, UnitStep[Subtract[(a + b), c]]]; // RepeatedTiming // First
And @@ ({False, True}[[r2 + 1]] == r1)

0.34

0.0025

True

But of course, Thread is more convenient and also more readible.

SzabolczSzabolcs wrote a nice packaged that gives you the best of both worlds: It's called BoolEval.

You may exploit that UnitStep step is vectorized like this:

a = {1, 1, 1, 1};
b = {2, 2, 2, 2};
c = {4, 2, 4, 2};
{False, True}[[2 - UnitStep[(a + b) - c]]]

{True, False, True, False}

If you can life with 0 and 1 instead of False and True, the the following will serve your needs and is more efficient (for large lists):

Subtract[1, UnitStep[Subtract[(a + b), c]]]

In particular, it is two orders of magnitude faster than Thread:

n = 1000000;
a = RandomInteger[{0, 1000}, {n}];
b = RandomInteger[{0, 1000}, {n}];
c = RandomInteger[{0, 1000}, {n}];

r1 = Thread[(a + b) < c]; // RepeatedTiming // First
r2 = Subtract[1, UnitStep[Subtract[(a + b), c]]]; // RepeatedTiming // First
And @@ ({False, True}[[r2 + 1]] == r1)

0.34

0.0025

True

But of course, Thread is more convenient and also more readible.

Szabolcz wrote a nice packaged that gives you the best of both worlds: It's called BoolEval.

You may exploit that UnitStep step is vectorized like this:

a = {1, 1, 1, 1};
b = {2, 2, 2, 2};
c = {4, 2, 4, 2};
{False, True}[[2 - UnitStep[(a + b) - c]]]

{True, False, True, False}

If you can life with 0 and 1 instead of False and True, the the following will serve your needs and is more efficient (for large lists):

Subtract[1, UnitStep[Subtract[(a + b), c]]]

In particular, it is two orders of magnitude faster than Thread:

n = 1000000;
a = RandomInteger[{0, 1000}, {n}];
b = RandomInteger[{0, 1000}, {n}];
c = RandomInteger[{0, 1000}, {n}];

r1 = Thread[(a + b) < c]; // RepeatedTiming // First
r2 = Subtract[1, UnitStep[Subtract[(a + b), c]]]; // RepeatedTiming // First
And @@ ({False, True}[[r2 + 1]] == r1)

0.34

0.0025

True

But of course, Thread is more convenient and also more readible.

Szabolcs wrote a nice packaged that gives you the best of both worlds: It's called BoolEval.

added 136 characters in body
Source Link
Henrik Schumacher
  • 109.4k
  • 7
  • 186
  • 322

You may exploit that UnitStep step is vectorized like this:

a = {1, 1, 1, 1};
b = {2, 2, 2, 2};
c = {4, 2, 4, 2};
{False, True}[[2 - UnitStep[(a + b) - c]]]

{True, False, True, False}

If you can life with 0 and 1 instead of False and True, the the following will serve your needs and is more efficient (for large lists):

Subtract[1, UnitStep[Subtract[(a + b), c]]]

In particular, it is two orders of magnitude faster than Thread:

n = 1000000;
a = RandomInteger[{0, 1000}, {n}];
b = RandomInteger[{0, 1000}, {n}];
c = RandomInteger[{0, 1000}, {n}];

r1 = Thread[(a + b) < c]; // RepeatedTiming // First
r2 = Subtract[1, UnitStep[Subtract[(a + b), c]]]; // RepeatedTiming // First
And @@ ({False, True}[[r2 + 1]] == r1)

0.34

0.0025

True

But of course, Thread is more convenient and also more readible.

Szabolcz wrote a nice packaged that gives you the best of both worlds: It's called BoolEval.

You may exploit that UnitStep step is vectorized like this:

a = {1, 1, 1, 1};
b = {2, 2, 2, 2};
c = {4, 2, 4, 2};
{False, True}[[2 - UnitStep[(a + b) - c]]]

{True, False, True, False}

If you can life with 0 and 1 instead of False and True, the the following will serve your needs and is more efficient (for large lists):

Subtract[1, UnitStep[Subtract[(a + b), c]]]

In particular, it is two orders of magnitude faster than Thread:

n = 1000000;
a = RandomInteger[{0, 1000}, {n}];
b = RandomInteger[{0, 1000}, {n}];
c = RandomInteger[{0, 1000}, {n}];

r1 = Thread[(a + b) < c]; // RepeatedTiming // First
r2 = Subtract[1, UnitStep[Subtract[(a + b), c]]]; // RepeatedTiming // First
And @@ ({False, True}[[r2 + 1]] == r1)

0.34

0.0025

True

But of course, Thread is more convenient and also more readible.

You may exploit that UnitStep step is vectorized like this:

a = {1, 1, 1, 1};
b = {2, 2, 2, 2};
c = {4, 2, 4, 2};
{False, True}[[2 - UnitStep[(a + b) - c]]]

{True, False, True, False}

If you can life with 0 and 1 instead of False and True, the the following will serve your needs and is more efficient (for large lists):

Subtract[1, UnitStep[Subtract[(a + b), c]]]

In particular, it is two orders of magnitude faster than Thread:

n = 1000000;
a = RandomInteger[{0, 1000}, {n}];
b = RandomInteger[{0, 1000}, {n}];
c = RandomInteger[{0, 1000}, {n}];

r1 = Thread[(a + b) < c]; // RepeatedTiming // First
r2 = Subtract[1, UnitStep[Subtract[(a + b), c]]]; // RepeatedTiming // First
And @@ ({False, True}[[r2 + 1]] == r1)

0.34

0.0025

True

But of course, Thread is more convenient and also more readible.

Szabolcz wrote a nice packaged that gives you the best of both worlds: It's called BoolEval.

added 704 characters in body
Source Link
Henrik Schumacher
  • 109.4k
  • 7
  • 186
  • 322

You may exploit that UnitStep step is vectorized like this:

a = {1, 1, 1, 1};
b = {2, 2, 2, 2};
c = {4, 2, 4, 2};
{False, True}[[2 - UnitStep[(a + b) - c]]]

{True, False, True, False}

If you can life with 0 and 1 instead of False and True, the the following will serve your needs and is more efficient (for large lists):

Subtract[1, UnitStep[Subtract[(a + b), c]]]

In particular, it is two orders of magnitude faster than Thread:

n = 1000000;
a = RandomInteger[{0, 1000}, {n}];
b = RandomInteger[{0, 1000}, {n}];
c = RandomInteger[{0, 1000}, {n}];

r1 = Thread[(a + b) < c]; // RepeatedTiming // First
r2 = Subtract[1, UnitStep[Subtract[(a + b), c]]]; // RepeatedTiming // First
And @@ ({False, True}[[r2 + 1]] == r1)

0.34

0.0025

True

But of course, Thread is more convenient and also more readible.

You may exploit that UnitStep step is vectorized like this:

a = {1, 1, 1, 1};
b = {2, 2, 2, 2};
c = {4, 2, 4, 2};
{False, True}[[2 - UnitStep[(a + b) - c]]]

{True, False, True, False}

You may exploit that UnitStep step is vectorized like this:

a = {1, 1, 1, 1};
b = {2, 2, 2, 2};
c = {4, 2, 4, 2};
{False, True}[[2 - UnitStep[(a + b) - c]]]

{True, False, True, False}

If you can life with 0 and 1 instead of False and True, the the following will serve your needs and is more efficient (for large lists):

Subtract[1, UnitStep[Subtract[(a + b), c]]]

In particular, it is two orders of magnitude faster than Thread:

n = 1000000;
a = RandomInteger[{0, 1000}, {n}];
b = RandomInteger[{0, 1000}, {n}];
c = RandomInteger[{0, 1000}, {n}];

r1 = Thread[(a + b) < c]; // RepeatedTiming // First
r2 = Subtract[1, UnitStep[Subtract[(a + b), c]]]; // RepeatedTiming // First
And @@ ({False, True}[[r2 + 1]] == r1)

0.34

0.0025

True

But of course, Thread is more convenient and also more readible.

Source Link
Henrik Schumacher
  • 109.4k
  • 7
  • 186
  • 322
Loading