5 ms·
”Can not express this data structure"? It can of course, there's not much to express. Go and write a (slow) tensor library yourself in pure Python. To be fair,
by aldanor 6y ago
”Can not express this data structure"? It can of course, there's not much to express. Go and write a (slow) tensor library yourself in pure Python.
To be fair, there's even a standard library `array` module that is a direct wrapper around C arrays, and NumPy shares some functionality with it (namely in regards to buffers, memoryviews, data type encoding etc).
Edit: there's also a built-in buffer type that supports multi-dimensional strides, suboffsets and all that (which, again both `array` and `numpy` make use of in establishing a standardized buffer api).
- enriquto 6y ago> Go and write a (slow) tensor library yourself in pure Python. Paraphrasing Alexander Stepanov, "complexity is part of of the specification" [0]. If your data is not contiguous (as the third figure in the article shows), it is not the same data structure. Thus you cannot implement this data structure in pure Python, period. The "array" module is just a wrapper to C data just like numpy is, it is not a pure Python construction. [0] Here's the full quote, from http://www.stlport.org/resources/StepanovUSA.html http://www.stlport.org/resources/StepanovUSA.html In 1976, still back in the USSR, I got a very serious case of food poisoning from eating raw fish. While in the hospital, in the state of delirium, I suddenly realized that the ability to add numbers in parallel depends on the fact that addition is associative. (So, putting it simply, STL is the result of a bacterial infection.) In other words, I realized that a parallel reduction algorithm is associated with a semigroup structure type. That is the fundamental point: algorithms are defined on algebraic structures. It took me another couple of years to realize that you have to extend the notion of structure by adding complexity requirements to regular axioms.
- aldanor 6y agoIf you're talking about non-contiguity due to multi-dimensionality (or strided slicing), PyBuffer already internally supports that (and thus memoryviews, stdlib array, etc) - [0], is that "pure Python" enough? [0] https://docs.python.org/3/c-api/buffer.html#c.Py_buffer.strides https://docs.python.org/3/c-api/buffer.html#c.Py_buffer.stri...
- enriquto 6y ago> is that "pure Python" enough? Not at all! The PyBuffer library is all about interfacing to C data. As far as I know, creating a (mutable) contiguous array of floats is impossible in the Python language. This seems to be a deliberate language design decision.
- axi1 6y agohttps://docs.python.org/3/library/array.html https://docs.python.org/3/library/array.html
- enriquto 6y agoMy point exactly. The "array" class cannot be implemented in Python.
- aldanor 6y agoUhh, so: Python has a language library feature that allows creating contiguous arrays, and hence it cannot be done in Python. Ok. // Pretty much the entirety of CPython is written in C, including lists, dicts and everything else, not much different than the array.