Test if an array is broadcastable to a shape?

后端 未结 5 1892
灰色年华
灰色年华 2021-01-21 16:39

What is the best way to test whether an array can be broadcast to a given shape?

The \"pythonic\" approach of trying doesn\'t work for my case, because the

5条回答
  •  执念已碎
    2021-01-21 17:10

    I really think you guys are over thinking this, why not just keep it simple?

    def is_broadcastable(shp1, shp2):
        for a, b in zip(shp1[::-1], shp2[::-1]):
            if a == 1 or b == 1 or a == b:
                pass
            else:
                return False
        return True
    

提交回复
热议问题