File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -49,6 +49,32 @@ PYBIND11_MODULE(numpycpp, m) {
4949#endif
5050 });
5151
52+ // -- astype dtype 匹配诊断 -------------------------------------------------
53+ // 返回 arr 的 dtype 匹配详情,用于验证 py::dtype::of<T>() vs kind()+itemsize 行为
54+ m.def (" _diag_astype_dtype" , [](const py::array& arr) -> py::dict {
55+ auto buf = arr.request ();
56+ auto dt = arr.dtype ();
57+
58+ char kind = dt.kind ();
59+ bool is_f32 = (kind == ' f' && buf.itemsize == 4 );
60+ bool is_f64 = (kind == ' f' && buf.itemsize == 8 );
61+
62+ bool dt_of_f32 = dt.is (py::dtype::of<float >());
63+ bool dt_of_f64 = dt.is (py::dtype::of<double >());
64+
65+ py::dict result;
66+ result[" dtype_str" ] = py::str (dt);
67+ result[" kind" ] = std::string (1 , kind);
68+ result[" itemsize" ] = buf.itemsize ;
69+ result[" format" ] = buf.format ;
70+ result[" is_f32(kind)" ] = is_f32;
71+ result[" is_f64(kind)" ] = is_f64;
72+ result[" dtype.is(float)" ] = dt_of_f32;
73+ result[" dtype.is(double)" ]= dt_of_f64;
74+ result[" fallback_works" ] = (is_f32 || is_f64 || dt_of_f32 || dt_of_f64);
75+ return result;
76+ });
77+
5278 // -- linalg submodule --------------------------------------------------
5379 py::module_ la = m.def_submodule (" linalg" , " numpy.linalg equivalents" );
5480 la.def (" norm" , static_cast <float (*)(const py::array_t <float >&)>(&numpy::linalg::norm));
You can’t perform that action at this time.
0 commit comments