6
$\begingroup$

I have some work that utilizes the "ExplicitPositions" property of Sparse Arrays that was at least available starting at version 13.0. However, I am trying to run some of my work on a cluster at my university, and they only have Mathematica 12.0 installed. I have requested an updated version be installed, but just in case that request is denied for whatever reason, is there still an efficient way to obtain what "ExplicitPositions" would return without explicitly calling that property?

$\endgroup$

1 Answer 1

7
$\begingroup$

There is an undocumented property for SparseArray, namely "NonzeroPositions". If you don't want to modify your whole code, you can just reassign the property.

$Version
(* "12.3.1 for Microsoft Windows (64-bit) (June 24, 2021)" *)

Unprotect[SparseArray];
SparseArray /: (sa_SparseArray)["ExplicitPositions"] := sa["NonzeroPositions"];
Protect[SparseArray];

s = SparseArray[{{1, 1} -> 1, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4}]

s["ExplicitPositions"]
(* {{1, 1}, {1, 3}, {2, 2}, {3, 3}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.