# NumericArray versus PackedArray in MMA12

I just tried to understand the differences between NumericArray and PackedArray in MMA12. The memory footprint of a NumericArray appears to be less than for a PackedArray. However, what is the use of the NumericArray, if simple matrix multiplication of NumericArrays

A.B


returns unevaluated? In fact, Flatten[] works with NumericArray[] but Dot[] does not.

So, can someone explain to me the purpose and/or use of NumericArray[]?

• Some details here: chat.stackexchange.com/transcript/message/49966830#49966830 – Carl Lange Apr 18 at 9:23
• It is not accurate to refer to packed arrays as PackedArray. There is no such head. A packed array is just an alternative storage format for Lists with machine-representable numeric elements, not an expression type. – Szabolcs Apr 18 at 10:18

NumericArrays are unrelated to packed arrays.

"Packed array" refers to an internal efficient storage format used for Lists. There is no such head as PackedArray. Normally, packed arrays are not observable. Their usage is completely transparent. The only way to tell that a List is packed is either by performance measurements or by using some special developer functions (such as PackedArrayQ).

NumericArrays are an expression type in their own right.

NumericArray basically just provides a space-efficient and exact storage format. On a typical modern system, Mathematica stores both machine integers and machine reals in 64 bits. Even if you use BinaryRead to read a file byte-by-byte, 64-bits will be wasted for storing each byte. Clearly, 8 bits would be enough. NumericArray enables that, not only for bytes, but for all typical integer and floating point formats.

NumericArray is not really useful for computation at this point. It is useful as an exchange format. It can wrap raw arrays in C, so it is useful with LibraryLink. There's a LibraryLink API for it in M12.0. It can be serialized to WXF, which is partially used by ExternalEvaluate to communicate with Python. It appears in the InputForm of Image and Audio, thus serializing these atomic expressions into a compound one will not waste any space.

If you want to do computations, you'd do one of the following:

• Extract the values from the NumericArray using Normal (and thus increase the memory usage)

• Put it in a computable format like Image or Audio

• As a special case, neural net functions handle numeric arrays efficiently. You can give one as input to e.g. a linear layer and get another one as output.

• Write your own functions in C to handle them (LibraryLink). It is a convenience for C programmers to use NumericArray because now they can choose their own data format and have Mathematica adapt to it, rather than having to adapt to Mathematica's format.

Perhaps in the future more functions will handle numeric arrays without needing to extract data from them.

NumericArray did exist in previous versions in the form of the undocumented RawArray. I believe these are the same thing as the LibraryLink RawArray API works with NumericArrays and vice versa. Also, old RawArrays now always appear as NumericArray in M12.0. The syntax of NumericArray is slightly different from the old RawArray though.