Skip to main content
added 406 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

The following method is broken for packed arrays. It works for many functions besides Equal, but some functions such as Equal, Unequal, and SameQ behave unexpectedly with regard to the Listable attribute and packed arrays. I am exploring this further. A far less clean but valid substitute is:

Block[{$},
  SetAttributes[$, Listable];
  A ~$~ B /. $ -> Equal
]

Another option is to use Listable itself:

{A, B} = RandomInteger[3, {2, 4, 3, 2}];

Block[{Equal},
 SetAttributes[Equal, Listable];
 A == B
]

{{{False, True}, {False, True}, {False, False}}, {{False, True}, {False, False}, {False, False}}, {{False, False}, {False, True}, {False, False}}, {{False, False}, {False, False}, {False, True}}}

Somewhat more efficient is to use the undocumented function Internal`InheritedBlock which behaves as though one did SetAttributes[Equal, Listable] directly, saving an extra step of evaluation that occurs with the method above.

Internal`InheritedBlock[{Equal},
  SetAttributes[Equal, Listable];
  A == B
]

Another option is to use Listable itself:

{A, B} = RandomInteger[3, {2, 4, 3, 2}];

Block[{Equal},
 SetAttributes[Equal, Listable];
 A == B
]

{{{False, True}, {False, True}, {False, False}}, {{False, True}, {False, False}, {False, False}}, {{False, False}, {False, True}, {False, False}}, {{False, False}, {False, False}, {False, True}}}

Somewhat more efficient is to use the undocumented function Internal`InheritedBlock which behaves as though one did SetAttributes[Equal, Listable] directly, saving an extra step of evaluation that occurs with the method above.

Internal`InheritedBlock[{Equal},
  SetAttributes[Equal, Listable];
  A == B
]

The following method is broken for packed arrays. It works for many functions besides Equal, but some functions such as Equal, Unequal, and SameQ behave unexpectedly with regard to the Listable attribute and packed arrays. I am exploring this further. A far less clean but valid substitute is:

Block[{$},
  SetAttributes[$, Listable];
  A ~$~ B /. $ -> Equal
]

Another option is to use Listable itself:

{A, B} = RandomInteger[3, {2, 4, 3, 2}];

Block[{Equal},
 SetAttributes[Equal, Listable];
 A == B
]

{{{False, True}, {False, True}, {False, False}}, {{False, True}, {False, False}, {False, False}}, {{False, False}, {False, True}, {False, False}}, {{False, False}, {False, False}, {False, True}}}

Somewhat more efficient is to use the undocumented function Internal`InheritedBlock which behaves as though one did SetAttributes[Equal, Listable] directly, saving an extra step of evaluation that occurs with the method above.

Internal`InheritedBlock[{Equal},
  SetAttributes[Equal, Listable];
  A == B
]
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Another option is to use Listable itself:

{A, B} = RandomInteger[3, {2, 4, 3, 2}];

Block[{Equal},
 SetAttributes[Equal, Listable];
 A == B
]

{{{False, True}, {False, True}, {False, False}}, {{False, True}, {False, False}, {False, False}}, {{False, False}, {False, True}, {False, False}}, {{False, False}, {False, False}, {False, True}}}

Somewhat more efficient is to use the undocumented function Internal`InheritedBlock which behaves as though one did SetAttributes[Equal, Listable] directly, saving an extra step of evaluation that occurs with the method above.

Internal`InheritedBlock[{Equal},
  SetAttributes[Equal, Listable];
  A == B
]