简体   繁体   English

半整数索引

[英]Half-integer indices

I am working on a fluid dynamics simulation tool in Python. 我正在使用Python开发流体动力学仿真工具。 Traditionally (at least in my field), integer indices refer to the center of a cell. 传统上(至少在我的领域中),整数索引指的是单元格的中心。 Some quantities are stored on the faces between cells, and in the literature are denoted by half-integer indices. 一些数量存储在单元格之间的面上,在文献中用半整数索引表示。 In codes, however, these are shifted to integers to fit into arrays. 但是,在代码中,它们会转换为整数以适合数组。 The problem is, the shift is not always consistent: do you add a half or subtract? 问题是,移位并不总是一致的:您是加一半还是减? If you switch between codes enough, you quickly lose track of the conventions for any particular code. 如果您在代码之间进行了足够的切换,则会很快失去对任何特定代码的约定的了解。 And honestly, there are enough quirky conventions in each code that eliminating a few would be a big help... but not if the remedy is worse than the original problem. 坦白地说,每个代码中都有足够多的古怪约定,因此消除一些约定将有很大帮助...但是,如果补救措施比原始问题更糟,则不是。

I have thought about using even indices for cell centers and odd for faces, but that is counterintuitive. 我曾考虑过将偶数索引用于细胞中心,将奇数用于面部,但这是违反直觉的。 Also, it's rare for a quantity to exist on both faces and in cell centers, so you never use half of your indices. 而且,数量很少同时存在于面和单元格中心,因此您永远不会使用一半的索引。 You could also implement functions plus_half(i) and minus_half(i), but that gets verbose and inelegant (at least in my opinion). 您还可以实现plus_half(i)和minus_half(i)函数,但是这变得冗长而乏味(至少在我看来)。 And of course floating point comparisons are always problematic in case someone gets cute in how they calculate 1/2. 当然,如果有人对如何计算1/2很满意,那么浮点比较总是有问题的。

Does anyone have a suggestion for an elegant way to implement half-integer indices in Python? 有没有人建议用一种优雅的方法在Python中实现半整数索引? I'm sure I'm not the first person to wish for this, but I've never seen it done in a simple way that is obvious to a user (without requiring the user to memorize the shift convention you've chosen). 我确定我不是第一个希望这样做的人,但是我从未见过以对用户来说显而易见的简单方式完成此操作(不需要用户记住您选择的换档约定)。

And just to clarify: I assume there is likely to be a remap step hidden from the user to get to integer indices (I intend to wrap NumPy arrays for my data grids). 只是为了澄清一下:我假设可能有一个隐藏的重映射步骤对用户隐藏,以便获得整数索引(我打算为我的数据网格包装NumPy数组)。 I'm thinking of the interface exposed to the user, rather than how the data is stored. 我正在考虑向用户公开的界面,而不是数据的存储方式。

您可以使__getitem__接受任意对象作为索引(尤其是浮点数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM