簡體   English   中英

如何將默認的numpy數組參數傳遞給pybind11中的函數?

[英]How to pass a default numpy array argument to a function in pybind11?

我正在定義一個以py :: array_t <double>和py :: array_t <bool>作為參數的方法。 如何告訴pybind11將這些參數默認為我選擇的數組? (例如np.array([0,0,0]))

我曾嘗試通過“ Argument” _a = py :: array_T({0,0,0})添加默認值,但是當我調用它時,它告訴我“數組的維數不正確:3; 預期1'

m.def("foo", [](py::array_t<double> Arg1,                        
                py::array_t<bool> Arg2){

        auto bar = Arg1.unchecked<1>();
        auto bar2 = Arg2.unchecked<1>();

                    '''other simple code that doesn't alter bar or bar2'''

        return bar;
    },
    "Arg1"_a,
    "Arg2"_a = py_array<bool> ({0, 0, 0})
);

問題在於您的默認參數的值是零長度尺寸的3d數組,而不是三個元素的1d數組。

您使用py_array<bool> ({0, 0, 0})調用的構造函數:

    explicit array_t(ShapeContainer shape, const T *ptr = nullptr, handle base = handle())
        : array_t(private_ctor{}, std::move(shape),
                ExtraFlags & f_style ? f_strides(*shape, itemsize()) : c_strides(*shape, itemsize()),
                ptr, base) { }

https://github.com/pybind/pybind11/blob/c9d32a81f40ad540015814edf13b29980c63e39c/include/pybind11/numpy.h#L861

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM