Numpy C-Api array_equal [英] Numpy C-Api array_equal

查看:75
本文介绍了Numpy C-Api array_equal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到比较两个PyArrayObject的函数-类似于 numpy array_equal 但我什么都没找到.你知道这样的功能吗?

I've tried to find function comparing two PyArrayObject - something like numpy array_equal But I haven't found anything. Do you know function like this?

如果没有-如何将此numpy array_equal导入到我的C代码中?

If not - How to import this numpy array_equal to my C code?

推荐答案

以下是array_equal的代码:

def array_equal(a1, a2):
    try:
        a1, a2 = asarray(a1), asarray(a2)
    except:
        return False
    if a1.shape != a2.shape:
        return False
    return bool(asarray(a1 == a2).all())

如您所见,它不是c-api级功能.确保两个输入均为数组且形状匹配后,将执行元素==测试,然后执行all.

As you can see it is not a c-api level function. After making sure both inputs are arrays, and that shape match it performs a element == test, followed by all.

这不适用于浮子.可以使用整数和布尔值.

This does not work reliably with floats. It's ok with ints and booleans.

c-api中可能有某种相等函数,但是您可能不需要它的克隆.

There probably is some sort of equality function in the c-api, but a clone of this probably isn't what you need.

PyArray_CountNonzero(PyArrayObject* self)

可能是一个很好的功能.我记得从前面的代码中可以了解到,PyArray_Nonzero使用它来确定要分配和返回的数组大小.您可以为它提供一个对象,该对象比较2个数组的元素(以dtype给出的适当方式),然后测试非零计数.

might be a good function. I remember from digging into the code earlier that PyArray_Nonzero uses it to determine how big of an array to allocate and return. You could give it an object that compares the elements of your 2 arrays (in what ever way is appropriate given the dtype), and then test for a nonzero count.

或者您可以构造自己的迭代器,一旦它获得不相等的一对元素,它就会立即失效.使用nditer获取完整的阵列广播能力.

Or you could construct your own iterator that bails out as soon as it gets a not-equal pair of elements. Use nditer to get the full array broadcasting power.

这篇关于Numpy C-Api array_equal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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