6
$\begingroup$

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]]

enter image description here

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.

$\endgroup$
5
  • $\begingroup$ It seems the underlying data is a 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 here Import["http://halirutan.github.io/Mathematica-SE-Tools/decode.m"]["https://i.sstatic.net/rFUEU.png"] and then evaluate the input. But again, I have not investigated much. $\endgroup$
    – halirutan
    Commented Aug 2, 2018 at 11:06
  • $\begingroup$ @halirutan the 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$
    – b3m2a1
    Commented Aug 2, 2018 at 16:24
  • $\begingroup$ This is a current limitation of Join with QuantityArray objects. With a non-default level specification (in your case 2), Join will normalize the QuantityArray objects (actually, any type of StructuredArray objects). $\endgroup$
    – jose
    Commented Aug 7, 2018 at 22:40
  • $\begingroup$ @jose the question is why does it have to be this way? $\endgroup$
    – b3m2a1
    Commented Aug 7, 2018 at 22:41
  • $\begingroup$ It doesn't have to be this way. It could be extended. Please, do send a suggestion to WRI. $\endgroup$
    – jose
    Commented Aug 7, 2018 at 22:44

0

Your Answer

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

Browse other questions tagged or ask your own question.