Part and Span: is there a reason this *should* not work?

我们两清 提交于 2019-12-22 08:09:23

问题


This is a question of design, not of existing functionality.

I would like to use:

{1, 2, 3, 4, 5}[[{1 ;; 3, 2 ;; 5}]]

I expect:

{{1, 2, 3}, {2, 3, 4, 5}}

But it is not valid:

During evaluation of In[1]:= Part::pspec: Part specification {1;;3,2;;5} is neither an integer nor a list of integers. >>

I am not asking why this does not work (simple: it's not supported).

Rather, is there a reason it should not work? That is, is there a logical reason this is not supported?


By the way, I specifically did not ask about the nested list syntax like:

{1, 2, 3, 4, 5}[[{{1, 2, 3}, {2, 3, 4, 5}}]]

because I believe that is less "regular" and more volatile, while Span is more defined and controlled.


回答1:


I think the real question that you are asking is why this does not work:

In[15]:= {1,2,3,4,5}[[{{1,2,3},{2,3,4,5}}]]

During evaluation of In[15]:= Part::pspec: Part specification 
{{1,2,3},{2,3,4,5}} is neither an integer nor a list of integers. >>

Out[15]= {1,2,3,4,5}[[{{1,2,3},{2,3,4,5}}]]

because Span seems to be a more high-level wrapper implemented on top of Part (this is a guess). This same question has been asked very recently on MathGroup. There wasn't a satisfactory answer, and my feeling is that from the user's viewpoint, this is just an omission - I don't see a fundamental reason why this should not work. Moreover, this feature would make life a lot easier in some cases.

From the technical/implementation viewpoint, I can speculate that this will go at odds with the extended Part assignment functionality. Specifically, we know that Part can be used not just for element extraction but for efficient element assignments, where whole regular structures (rectangular sub-matrices) can be assigned simultaneously, like

In[18]:= 
a = Table[i+j,{i,2},{j,4}]

Out[18]= {{2,3,4,5},{3,4,5,6}}

In[21]:= 
a[[All,{2,3}]] = {{7,8},{9,10}};
a

Out[22]= {{2,7,8,5},{3,9,10,6}}

Should Part allow nested lists specification for positions, it should immediately provide assignment functionality for the non-rectangular sub-structures, or it will become much less intuitive to use (since there will be corner cases, etc). And I suspect that the latter is not easy to implement, because extended Part assignment functionality is probably based directly on the array memory layout. This will also create problems with packed arrays (for the same reason - they can not be ragged, must be rectangular). Perhaps, should Mathematica have very efficient ragged array structure (like linked lists) built-in, and this could be solved.

So, to summarize: it looks like, from the implementation viewpoint, such new functionality will introduce several tough problems, which may explain why this has not been done yet (again, this is a guess). Note also, that for element extraction, one can use Extract with prepared list of positions, to extract arbitrary substructures, and that will be almost as efficient as with using Part.



来源:https://stackoverflow.com/questions/6174443/part-and-span-is-there-a-reason-this-should-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!