通过引用结合numpy的数组 [英] Combine NumPy Arrays by Reference

查看:159
本文介绍了通过引用结合numpy的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想两个数组为O组合成一个新的数组(1)。然后,我想改变在这个新的数组中的值来改变旧的阵列中的值。

I want to combine two arrays into a new array in O(1). Then, I want to change the values in this new array to change the values in the old arrays.

这是在pygame的的surfarray模块,它不具有一个返回一个RGBA(N * M * 4)阵列的功能的情况下 - 唯一的RGB(N * M * 3)和A(N * M)阵列分别。理想情况下,我会创造在O的新阵RGBA(1)引用RGB和A阵列。更改RGBA会改变这两个RGB和A,并因此改变了表面。

This is in the context of PyGame's surfarray module, which does not have a function that returns an RGBA (n*m*4) array--only RGB (n*m*3) and A (n*m) arrays separately. Ideally, I would create a new array "RGBA" in O(1) that references the "RGB" and "A" arrays. Changing "RGBA" would change both "RGB" and "A", and so change the surface.

我不知道这是可能的,因为我不能想办法与C.做到这一点,但我能想到的一些想法。例如,也许有一个封装了一些子阵一个numpy的数组类型,并在内部重定向到索引在C级正确的。这将是很好。

I don't know if this is possible, since I can't think of a way to do it with C. However, I can think of some ideas. For example, maybe there's a numpy array type that encapsulates some subarrays, and internally redirects indexing to the correct one at the C level. This would be fine.

推荐答案

您可以创建自己的类,会管理的参考程序,如下面的例子。你应该在这个进一步的工作,包括与 __ getslice __ 切片能力__添加__ __ MUL __ ,等等。

You can create your own class that will manage the referencing procedures, as shown in the example below. You should work further on this to include slicing capabilities with __getslice__, __add__, __mul__, and so forth.

import numpy as np
a1 = np.arange(1,11)
a2 = np.arange(101,111)
class Combiner():
    def __init__(self, *args):
        self.arrays = [arg for arg in args]
        self.lens = [len(arg) for arg in args]
    def __getitem__(self,i):
        if i >=0:
            shift = 0
            acc = 0
            for j,l in enumerate(self.lens):
                acc += l
                if i<acc:
                    return self.arrays[j][i-shift]
                shift += l
        if i<0:
            shift = 0
            acc = 0
            for j in xrange(len(self.lens)-1,-1,-1):
                l = self.lens[j]
                acc -= l
                if i>=acc:
                    return self.arrays[j][i+shift]
                shift += l

    def __setitem__(self,i,v):
        if i >=0:
            shift = 0
            acc = 0.
            for j,l in enumerate(self.lens):
                acc += l
                if i<acc:
                    self.arrays[j][i-shift] = v
                    return
                shift += l
        if i<0:
            shift = 0
            acc = 0
            for j in xrange(len(self.lens)-1,-1,-1):
                l = self.lens[j]
                acc -= l
                if i>=acc:
                    self.arrays[j][i+shift] = v
                    return
                shift += l

a3 = Combiner(a1,a2)
print a3[-10]
# 101
a3[-2] = 22222
a3[ 4] = 11111
print a1
#[    1     2     3     4 11111     6     7     8     9    10]
print a2
#[  101   102   103   104   105   106   107   108 22222   110]

这篇关于通过引用结合numpy的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆