本文整理汇总了C++中py::module::def方法的典型用法代码示例。如果您正苦于以下问题:C++ module::def方法的具体用法?C++ module::def怎么用?C++ module::def使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类py::module
的用法示例。
在下文中一共展示了module::def方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: init_ex10
void init_ex10(py::module &m) {
// Vectorize all arguments of a function (though non-vector arguments are also allowed)
m.def("vectorized_func", py::vectorize(my_func));
// Vectorize a lambda function with a capture object (e.g. to exclude some arguments from the vectorization)
m.def("vectorized_func2",
[](py::array_dtype<int> x, py::array_dtype<float> y, float z) {
return py::vectorize([z](int x, float y) { return my_func(x, y, z); })(x, y);
}
);
// Vectorize a complex-valued function
m.def("vectorized_func3", py::vectorize(my_func3));
}
示例2: init_ex12
void init_ex12(py::module &m) {
/* Important: use the wrapper type as a template
argument to class_<>, but use the original name
to denote the type */
py::class_<PyExample12>(m, "Example12")
/* Declare that 'PyExample12' is really an alias for the original type 'Example12' */
.alias<Example12>()
.def(py::init<int>())
/* Reference original class in function definitions */
.def("run", &Example12::run)
.def("pure_virtual", &Example12::pure_virtual);
m.def("runExample12", &runExample12);
m.def("runExample12Virtual", &runExample12Virtual);
}
示例3: define_target
void define_target(py::module &m) {
// Disambiguate some ambigious methods
int (Target::*natural_vector_size_method)(const Type &t) const = &Target::natural_vector_size;
bool (Target::*supports_type1_method)(const Type &t) const = &Target::supports_type;
bool (Target::*supports_type2_method)(const Type &t, DeviceAPI device) const = &Target::supports_type;
auto target_class =
py::class_<Target>(m, "Target")
.def(py::init<>())
.def(py::init<const std::string &>())
.def(py::init<Target::OS, Target::Arch, int>())
.def(py::init<Target::OS, Target::Arch, int, std::vector<Target::Feature>>())
.def("__eq__", [](const Target &value, Target *value2) { return value2 && value == *value2; })
.def("__ne__", [](const Target &value, Target *value2) { return !value2 || value != *value2; })
.def_readwrite("os", &Target::os)
.def_readwrite("arch", &Target::arch)
.def_readwrite("bits", &Target::bits)
.def("__repr__", &target_repr)
.def("__str__", &Target::to_string)
.def("to_string", &Target::to_string)
.def("has_feature", &Target::has_feature)
.def("features_any_of", &Target::features_any_of, py::arg("features"))
.def("features_all_of", &Target::features_all_of, py::arg("features"))
.def("set_feature", &Target::set_feature, py::arg("f"), py::arg("value") = true)
.def("set_features", &Target::set_features, py::arg("features"), py::arg("value") = true)
.def("with_feature", &Target::with_feature, py::arg("feature"))
.def("without_feature", &Target::without_feature, py::arg("feature"))
.def("has_gpu_feature", &Target::has_gpu_feature)
.def("supports_type", supports_type1_method, py::arg("type"))
.def("supports_type", supports_type2_method, py::arg("type"), py::arg("device"))
.def("supports_device_api", &Target::supports_device_api, py::arg("device"))
.def("natural_vector_size", natural_vector_size_method, py::arg("type"))
.def("has_large_buffers", &Target::has_large_buffers)
.def("maximum_buffer_size", &Target::maximum_buffer_size)
.def("supported", &Target::supported)
.def_static("validate_target_string", &Target::validate_target_string, py::arg("name"));
;
m.def("get_host_target", &get_host_target);
m.def("get_target_from_environment", &get_target_from_environment);
m.def("get_jit_target_from_environment", &get_jit_target_from_environment);
m.def("target_feature_for_device_api", &target_feature_for_device_api);
}
示例4: define_module
void define_module(py::module &m) {
auto module_class = py::class_<Module>(m, "Module")
.def(py::init<const std::string &, const Target &>(), py::arg("name"), py::arg("target"))
.def("target", &Module::target)
.def("name", &Module::name)
.def("auto_schedule", &Module::auto_schedule)
.def("buffers", &Module::buffers)
.def("submodules", &Module::submodules)
.def("append", (void (Module::*)(const Buffer<> &)) &Module::append, py::arg("buffer"))
.def("append", (void (Module::*)(const Module &)) &Module::append, py::arg("module"))
.def("compile", &Module::compile, py::arg("outputs"))
.def("compile_to_buffer", &Module::compile_to_buffer)
.def("resolve_submodules", &Module::resolve_submodules)
.def("remap_metadata_name", &Module::remap_metadata_name)
.def("get_metadata_name_map", &Module::get_metadata_name_map)
.def("set_auto_schedule", &Module::set_auto_schedule)
// TODO: ExternalCode-related methods deliberately skipped for now.
// .def("append", (void (Module::*)(const ExternalCode &)) &Module::append, py::arg("external_code"))
// .def("external_code", &Module::external_code)
// TODO: Internal::LoweredFunc-related methods deliberately skipped for now.
// .def("functions", &Module::functions)
// .def("get_function_by_name", &Module::get_function_by_name, py::arg("name"))
// .def("append", (void (Module::*)(const Internal::LoweredFunc &)) &Module::append, py::arg("function"))
.def("__repr__", [](const Module &m) -> std::string {
std::ostringstream o;
o << "<halide.Module '" << m.name() << "'>";
return o.str();
})
;
m.def("link_modules", &link_modules, py::arg("name"), py::arg("modules"));
m.def("compile_standalone_runtime", (void (*)(const std::string &, Target)) &compile_standalone_runtime, py::arg("filename"), py::arg("target"));
m.def("compile_standalone_runtime", (Outputs (*)(const Outputs &, Target)) &compile_standalone_runtime, py::arg("outputs"), py::arg("target"));
// TODO: compile_multitarget() deliberately skipped for now.
}
示例5: define_lambda
void define_lambda(py::module &m) {
// TODO: 'lambda' is a reserved word in Python, so we
// can't use it for a function. Using 'lambda_func' for now.
m.def("lambda_func", [](py::args args) -> Func {
auto vars = args_to_vector<Var>(args, 0, 1);
Expr e = args[args.size() - 1].cast<Expr>();
Func f("lambda" + Internal::unique_name('_'));
f(vars) = e;
return f;
});
}
示例6: init_ex4
void init_ex4(py::module &m) {
m.def("test_function", &test_function1);
m.def("test_function", &test_function2);
m.def("test_function", &test_function3);
m.attr("some_constant") = py::int_(14);
py::enum_<EMyEnumeration>(m, "EMyEnumeration")
.value("EFirstEntry", EFirstEntry)
.value("ESecondEntry", ESecondEntry)
.export_values();
py::class_<Example4> ex4_class(m, "Example4");
ex4_class.def_static("test_function", &Example4::test_function);
py::enum_<Example4::EMode>(ex4_class, "EMode")
.value("EFirstMode", Example4::EFirstMode)
.value("ESecondMode", Example4::ESecondMode)
.export_values();
m.def("return_bytes", &return_bytes);
m.def("print_bytes", &print_bytes);
}
示例7: pyqpp_cell_export
void pyqpp_cell_export (py::module m) {
py_cell_export<float>(m, "periodic_cell_f");
qpp::gen_cell<float, qpp::matrix3<float> >::py_export(
m, "point_group_f");
qpp::gen_cell<float, qpp::rotrans<float,false> >::py_export(
m, "crystal_group_f");
qpp::gen_cell<float, qpp::rotrans<float,true> >::py_export(
m, "finite_crystal_group_f");
qpp::array_group<qpp::matrix3<float> >::py_export(
m, "array_point_group_f");
qpp::array_group<qpp::rotrans<float,true> >::py_export(
m, "array_fincryst_group_f");
m.def("generator_form", py_generator_form<float, qpp::matrix3<float>,
qpp::array_group<qpp::matrix3<float> > > );
m.def("generator_form", py_generator_form<float, qpp::rotrans<float,true>,
qpp::array_group<qpp::rotrans<float,true> > >);
#ifdef PYTHON_EXP_EXT
py_cell_export<double>(m, "periodic_cell_d");
qpp::gen_cell<double, qpp::matrix3<double> >::py_export(
m, "point_group_d");
qpp::gen_cell<double, qpp::rotrans<double,false> >::py_export(
m, "crystal_group_d");
qpp::gen_cell<double, qpp::rotrans<double,true> >::py_export(
m, "finite_crystal_group_d");
qpp::array_group<qpp::matrix3<double> >::py_export(
m, "array_point_group_d");
qpp::array_group<qpp::rotrans<double,true> >::py_export(
m, "array_fincryst_group_d");
m.def("generator_form", py_generator_form<double, qpp::matrix3<double>,
qpp::array_group<qpp::matrix3<double> > >);
m.def("generator_form", py_generator_form<double, qpp::rotrans<double,true>,
qpp::array_group<qpp::rotrans<double,true> > >);
#endif
}
示例8: init_ex11
void init_ex11(py::module &m) {
m.def("kw_func", &kw_func, py::arg("x"), py::arg("y"));
m.def("kw_func2", &kw_func, py::arg("x") = 100, py::arg("y") = 200);
m.def("kw_func3", [](const char *) { }, py::arg("data") = std::string("Hello world!"));
/* A fancier default argument */
std::vector<int> list;
list.push_back(13);
list.push_back(17);
m.def("kw_func4", &kw_func4, py::arg("myList") = list);
m.def("call_kw_func", &call_kw_func);
m.def("args_function", &args_function);
m.def("args_kwargs_function", &args_kwargs_function);
using namespace py::literals;
m.def("kw_func_udl", &kw_func, "x"_a, "y"_a=300);
m.def("kw_func_udl_z", &kw_func, "x"_a, "y"_a=0);
}
示例9: bind_vector
// ----------------------------------------------------------------------------------------
void bind_vector(py::module& m)
{
{
py::class_<cv, std::shared_ptr<cv>>(m, "vector", "This object represents the mathematical idea of a column vector.")
.def(py::init())
.def("set_size", &cv_set_size)
.def("resize", &cv_set_size)
.def(py::init(&cv_from_object))
.def("__repr__", &cv__repr__)
.def("__str__", &cv__str__)
.def("__len__", &cv__len__)
.def("__getitem__", &cv__getitem__)
.def("__getitem__", &cv__getitem2__)
.def("__setitem__", &cv__setitem__)
.def_property_readonly("shape", &cv_get_matrix_size)
.def(py::pickle(&getstate<cv>, &setstate<cv>));
m.def("dot", &dotprod, "Compute the dot product between two dense column vectors.");
}
{
typedef point type;
py::class_<type>(m, "point", "This object represents a single point of integer coordinates that maps directly to a dlib::point.")
.def(py::init<long,long>(), py::arg("x"), py::arg("y"))
.def("__repr__", &point__repr__)
.def("__str__", &point__str__)
.def_property_readonly("x", &point_x, "The x-coordinate of the point.")
.def_property_readonly("y", &point_y, "The y-coordinate of the point.")
.def(py::pickle(&getstate<type>, &setstate<type>));
}
{
typedef std::vector<point> type;
py::bind_vector<type>(m, "points", "An array of point objects.")
.def("clear", &type::clear)
.def("resize", resize<type>)
.def("extend", extend_vector_with_python_list<point>)
.def(py::pickle(&getstate<type>, &setstate<type>));
}
}
示例10: define_operators
void define_operators(py::module &m) {
m.def("max", [](py::args args) -> Expr {
if (args.size() < 2) {
throw py::value_error("max() must have at least 2 arguments");
}
int pos = (int) args.size() - 1;
Expr value = args[pos--].cast<Expr>();
while (pos >= 0) {
value = max(args[pos--].cast<Expr>(), value);
}
return value;
});
m.def("min", [](py::args args) -> Expr {
if (args.size() < 2) {
throw py::value_error("min() must have at least 2 arguments");
}
int pos = (int) args.size() - 1;
Expr value = args[pos--].cast<Expr>();
while (pos >= 0) {
value = min(args[pos--].cast<Expr>(), value);
}
return value;
});
m.def("clamp", &clamp);
m.def("abs", &abs);
m.def("absd", &absd);
m.def("select", [](py::args args) -> Expr {
if (args.size() < 3) {
throw py::value_error("select() must have at least 3 arguments");
}
if ((args.size() % 2) == 0) {
throw py::value_error("select() must have an odd number of arguments");
}
int pos = (int) args.size() - 1;
Expr false_value = args[pos--].cast<Expr>();
while (pos > 0) {
Expr true_value = args[pos--].cast<Expr>();
Expr condition = args[pos--].cast<Expr>();
false_value = select(condition, true_value, false_value);
}
return false_value;
});
m.def("tuple_select", [](py::args args) -> py::tuple {
_halide_user_assert(args.size() >= 3)
<< "tuple_select() must have at least 3 arguments";
_halide_user_assert((args.size() % 2) != 0)
<< "tuple_select() must have an odd number of arguments";
int pos = (int) args.size() - 1;
Tuple false_value = args[pos--].cast<Tuple>();
bool has_tuple_cond = false, has_expr_cond = false;
while (pos > 0) {
Tuple true_value = args[pos--].cast<Tuple>();;
// Note that 'condition' can be either Expr or Tuple, but must be consistent across all
py::object py_cond = args[pos--];
Expr expr_cond;
Tuple tuple_cond(expr_cond);
try {
tuple_cond = py_cond.cast<Tuple>();
has_tuple_cond = true;
} catch (...) {
expr_cond = py_cond.cast<Expr>();
has_expr_cond = true;
}
if (expr_cond.defined()) {
false_value = tuple_select(expr_cond, true_value, false_value);
} else {
false_value = tuple_select(tuple_cond, true_value, false_value);
}
}
_halide_user_assert(!(has_tuple_cond && has_expr_cond))
<<"tuple_select() may not mix Expr and Tuple for the condition elements.";
return to_python_tuple(false_value);
});
m.def("sin", &sin);
m.def("asin", &asin);
m.def("cos", &cos);
m.def("acos", &acos);
m.def("tan", &tan);
m.def("atan", &atan);
m.def("atan", &atan2);
m.def("atan2", &atan2);
m.def("sinh", &sinh);
m.def("asinh", &asinh);
m.def("cosh", &cosh);
m.def("acosh", &acosh);
m.def("tanh", &tanh);
m.def("atanh", &atanh);
m.def("sqrt", &sqrt);
m.def("hypot", &hypot);
m.def("exp", &exp);
m.def("log", &log);
m.def("pow", &pow);
m.def("erf", &erf);
//.........这里部分代码省略.........
示例11: init_ex8
void init_ex8(py::module &m) {
py::class_<Object, ref<Object>> obj(m, "Object");
obj.def("getRefCount", &Object::getRefCount);
py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj)
.def(py::init<int>());
m.def("make_object_1", &make_object_1);
m.def("make_object_2", &make_object_2);
m.def("make_myobject1_1", &make_myobject1_1);
m.def("make_myobject1_2", &make_myobject1_2);
m.def("print_object_1", &print_object_1);
m.def("print_object_2", &print_object_2);
m.def("print_object_3", &print_object_3);
m.def("print_object_4", &print_object_4);
m.def("print_myobject1_1", &print_myobject1_1);
m.def("print_myobject1_2", &print_myobject1_2);
m.def("print_myobject1_3", &print_myobject1_3);
m.def("print_myobject1_4", &print_myobject1_4);
py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2")
.def(py::init<int>());
m.def("make_myobject2_1", &make_myobject2_1);
m.def("make_myobject2_2", &make_myobject2_2);
m.def("print_myobject2_1", &print_myobject2_1);
m.def("print_myobject2_2", &print_myobject2_2);
m.def("print_myobject2_3", &print_myobject2_3);
m.def("print_myobject2_4", &print_myobject2_4);
py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3")
.def(py::init<int>());
m.def("make_myobject3_1", &make_myobject3_1);
m.def("make_myobject3_2", &make_myobject3_2);
m.def("print_myobject3_1", &print_myobject3_1);
m.def("print_myobject3_2", &print_myobject3_2);
m.def("print_myobject3_3", &print_myobject3_3);
m.def("print_myobject3_4", &print_myobject3_4);
py::implicitly_convertible<py::int_, MyObject1>();
}
示例12: ExportNgsolve
void NGS_DLL_HEADER ExportNgsolve(py::module &m ) {
m.def ("Tcl_Eval", &Ng_TclCmd);
m.def ("_Redraw",
([](bool blocking, double fr)
{
static auto last_time = std::chrono::system_clock::now()-std::chrono::seconds(10);
auto now = std::chrono::system_clock::now();
double elapsed = std::chrono::duration<double>(now-last_time).count();
if (elapsed * fr > 1)
{
Ng_Redraw(blocking);
last_time = std::chrono::system_clock::now();
}
}),
py::arg("blocking")=false, py::arg("fr") = 25
);
m.def ("Draw",[](shared_ptr<MeshAccess> mesh)
{
mesh->SelectMesh();
Ng_TclCmd ("set ::selectvisual mesh;\n");
}
);
m.def("SetVisualization",
[](py::object deformation, py::object min, py::object max,
/* py::object clippnt, */ py::object clipnormal, py::object clipping)
{
bool need_redraw = false;
if (py::extract<bool>(deformation).check())
{
bool def = py::extract<bool>(deformation)();
Ng_TclCmd ("set ::visoptions.deformation "+ToString(def)+";\n");
Ng_TclCmd ("Ng_Vis_Set parameters;\n");
need_redraw = true;
}
if (py::extract<double>(min).check())
{
Ng_TclCmd ("set ::visoptions.autoscale 0\n");
Ng_TclCmd ("set ::visoptions.mminval "+ToString(py::extract<double>(min)())+";\n");
Ng_TclCmd ("Ng_Vis_Set parameters;\n");
need_redraw = true;
}
if (py::extract<double>(max).check())
{
Ng_TclCmd ("set ::visoptions.autoscale 0\n");
Ng_TclCmd ("set ::visoptions.mmaxval "+ToString(py::extract<double>(max)())+";\n");
Ng_TclCmd ("Ng_Vis_Set parameters;\n");
need_redraw = true;
}
if (py::extract<py::tuple>(clipnormal).check())
{
py::tuple norm = py::extract<py::tuple>(clipnormal)();
if (py::len(norm)==3)
{
// cout << "setting clipping normal" << endl;
// tclstring << "set ::viewoptions.clipping.enable 1" << endl
Ng_TclCmd ("set ::viewoptions.clipping.nx "+ToString(py::extract<double>(norm[0])())+";\n");
Ng_TclCmd ("set ::viewoptions.clipping.ny "+ToString(py::extract<double>(norm[1])())+";\n");
Ng_TclCmd ("set ::viewoptions.clipping.nz "+ToString(py::extract<double>(norm[2])())+";\n");
// << "set ::viewoptions.clipping.dist " << clipdist << endl;
need_redraw = true;
}
}
if (py::extract<bool>(clipping).check())
{
bool clip = py::extract<bool>(clipping)();
Ng_TclCmd ("set ::viewoptions.clipping.enable "+ToString(int(clip))+";\n");
Ng_TclCmd ("Ng_SetVisParameters");
need_redraw = true;
}
if (need_redraw)
Ng_Redraw(true);
},
py::arg("deformation")=DummyArgument(),
py::arg("min")=DummyArgument(),
py::arg("max")=DummyArgument(),
// py::arg("clippnt")=DummyArgument(),
py::arg("clipnormal")=DummyArgument(),
py::arg("clipping")=DummyArgument()
)
;
m.def ("Draw",
[](shared_ptr<GridFunction> gf, int sd, bool autoscale, double min, double max)
{
// cout << "in draw" << endl;
// cout << "gf in draw = " << *gf << endl;
// cout << "dims of gf = " << gf->Dimensions() << endl;
gf->GetMeshAccess()->SelectMesh();
// Visualize (gf, gf->GetName());
// netgen::SolutionData * vis = new VisualizeCoefficientFunction (gf->GetMeshAccess(), gf);
auto gfcf = make_shared<GridFunctionCoefficientFunction> (gf);
netgen::SolutionData * vis = new VisualizeCoefficientFunction (gf->GetMeshAccess(), gfcf);
//.........这里部分代码省略.........
示例13: init_eigen
void init_eigen(py::module &m) {
typedef Eigen::Matrix<float, 5, 6, Eigen::RowMajor> FixedMatrixR;
typedef Eigen::Matrix<float, 5, 6> FixedMatrixC;
typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> DenseMatrixR;
typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> DenseMatrixC;
typedef Eigen::SparseMatrix<float, Eigen::RowMajor> SparseMatrixR;
typedef Eigen::SparseMatrix<float> SparseMatrixC;
// Non-symmetric matrix with zero elements
Eigen::MatrixXf mat(5, 6);
mat << 0, 3, 0, 0, 0, 11, 22, 0, 0, 0, 17, 11, 7, 5, 0, 1, 0, 11, 0,
0, 0, 0, 0, 11, 0, 0, 14, 0, 8, 11;
m.def("fixed_r", [mat]() -> FixedMatrixR {
return FixedMatrixR(mat);
});
m.def("fixed_c", [mat]() -> FixedMatrixC {
return FixedMatrixC(mat);
});
m.def("fixed_passthrough_r", [](const FixedMatrixR &m) -> FixedMatrixR {
return m;
});
m.def("fixed_passthrough_c", [](const FixedMatrixC &m) -> FixedMatrixC {
return m;
});
m.def("dense_r", [mat]() -> DenseMatrixR {
return DenseMatrixR(mat);
});
m.def("dense_c", [mat]() -> DenseMatrixC {
return DenseMatrixC(mat);
});
m.def("dense_passthrough_r", [](const DenseMatrixR &m) -> DenseMatrixR {
return m;
});
m.def("dense_passthrough_c", [](const DenseMatrixC &m) -> DenseMatrixC {
return m;
});
m.def("sparse_r", [mat]() -> SparseMatrixR {
return Eigen::SparseView<Eigen::MatrixXf>(mat);
});
m.def("sparse_c", [mat]() -> SparseMatrixC {
return Eigen::SparseView<Eigen::MatrixXf>(mat);
});
m.def("sparse_passthrough_r", [](const SparseMatrixR &m) -> SparseMatrixR {
return m;
});
m.def("sparse_passthrough_c", [](const SparseMatrixC &m) -> SparseMatrixC {
return m;
});
}
示例14: init_ex18
void init_ex18(py::module & m) {
m.def("example18", &example18);
}