If I evaluate the following code
t = Developer`ToPackedArray[{0, 2/7, -(1/7)}];
Developer`PackedArrayQ[t]
Mathematica returns False.
Is there any way that I can pack an array of rational numbers?
If I evaluate the following code
t = Developer`ToPackedArray[{0, 2/7, -(1/7)}];
Developer`PackedArrayQ[t]
Mathematica returns False.
Is there any way that I can pack an array of rational numbers?
You can't. See the docs - it says Possible types are: Integer, Real, and Complex. If you check FullForm[2/7] it gives Rational[2,7] so it won't work.
So you either use reals / complex (machine) numbers, or you have two lists of integers, one for the numerator, one for the denominator, or have a list of pairs of integers.
Here's how you can pack a list of pairs of {numerator, denominator} instead since you are allowed to pack integers:
t = Developer`ToPackedArray[NumeratorDenominator[{0, 2/7, -(1/7)}]]
Developer`PackedArrayQ[t]
(* True *)
FullForm[2/7]isRational[2,7]so it won't work. You either use reals / complex (machine) numbers, or you have two lists of integers, one for the numerator, one for the denominator, or have a list of pairs of integers. – flinty Nov 18 '20 at 16:02