I need to merge two QuantityArray
objects of mixed type. My first attempt looks something like:
q1 = QuantityArray[RandomReal[{-10, 10}, {10000, 4}], "Angstroms"];
q2 = QuantityArray[RandomReal[{-10, 10}, {10000, 3}], "Debyes"];
qnew = Join[q1, q2, 2]; // AbsoluteTiming // First
0.167756
But it turns out this builds a List
of List
of Quantity
objects:
qnew[[;; 3]]
{{Quantity[4.1543, "Angstroms"], Quantity[5.47902, "Angstroms"],
Quantity[-2.40716, "Angstroms"], Quantity[-6.48639, "Angstroms"],
Quantity[1.23, "Debyes"], Quantity[6.39028, "Debyes"],
Quantity[-3.68544, "Debyes"]}, {Quantity[-4.16621, "Angstroms"],
Quantity[3.38214, "Angstroms"], Quantity[-6.28603, "Angstroms"],
Quantity[-8.62747, "Angstroms"], Quantity[-2.42187, "Debyes"],
Quantity[5.7411, "Debyes"],
Quantity[-7.45621, "Debyes"]}, {Quantity[1.04501, "Angstroms"],
Quantity[8.00975, "Angstroms"], Quantity[-6.73537, "Angstroms"],
Quantity[9.31479, "Angstroms"], Quantity[-3.87661, "Debyes"],
Quantity[6.69572, "Debyes"], Quantity[4.10434, "Debyes"]}}
It's also super slow as that 10000
becomes closer to 50000
.
I found that this is faster and builds a proper QuantityArray
:
qnew =
QuantityArray[
Join[QuantityMagnitude@q1, QuantityMagnitude@q2, 2],
Join[First@QuantityUnit@q1, First@QuantityUnit@q2]
]; // AbsoluteTiming // First
0.006938
qnew[[;; 3]]
Likely as the packed arrays never unpack as they would in the first case.
Now the question is that I was wondering if there was a reason for this behavior. I.e. is there something that makes the first behavior of Join
more general than the second?
Otherwise I'm tempted to forward this to WRI as a suggestion.
StructuredArray
which can only represent values of one unit. I only quickly glanced at this, but I'm not sure this is possible. Look at this hereImport["http://halirutan.github.io/Mathematica-SE-Tools/decode.m"]["http://i.stack.imgur.com/rFUEU.png"]
and then evaluate the input. But again, I have not investigated much. $\endgroup$StructuredArray
setup is probably part of it, but it can in fact hold multiple units. The second thing I had there actually happens to work. $\endgroup$