我想用C++ 11 lambda表达式作为访问功能boost::python的add_property,沿着下面的内容(拉姆达并不是必需在这个例子中,但将需要更复杂的东西拉姆达内部发生,如验证):
#include<boost/python.hpp>
struct A{
A(): a(2){};
int a;
};
BOOST_PYTHON_MODULE(boost_python_lambda)
{
boost::python::class_<A>("A")
// .def_readonly("a",&A::a) // the classical way: works fine
.add_property("a",[](const A& a){return a.a;})
;
}
Run Code Online (Sandbox Code Playgroud)
但是,使用clang ++(版本3.2)编译和-std=c++11(结果与g ++ 4.7相同),我收到此错误:
/usr/include/boost/python/class.hpp:442:66: error: no matching function for call to 'get_signature'
return python::make_function(f, default_call_policies(), detail::get_signature(f, (T*)0));
^~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/python/class.hpp:422:22: note: in instantiation of function template specialization 'boost::python::class_<A,
boost::python::detail::not_specified, boost::python::detail::not_specified,
boost::python::detail::not_specified>::make_fn_impl<A, <lambda at boost_python_lambda.cpp:12:21> >' requested here
return this->make_fn_impl(
^
/usr/include/boost/python/class.hpp:309:40: note: in instantiation of function …Run Code Online (Sandbox Code Playgroud) OpenCL语言不支持位域.不支持他们的原因是什么?与其他忽略的部分(递归,函数指针,......)不同,有明显的理由不支持它们,我没有看到一个用于位域.我确信这不是代表委员会的疏忽,但是原因是什么?
(我存储了一些打包在int中的位,并且代码可以更好地与它们一起读取.我理解bitfields是一种很好的语法,可以避免位移和来回屏蔽,无论如何它们都是在汇编中转换的.)
我想在几个数组上执行相同的操作,如:
#include<vector>
#include<algorithm>
int main(void){
std::vector<double> a, b;
for(auto& ab:{a,b}) std::sort(ab.begin(),ab.end()); // error
}
Run Code Online (Sandbox Code Playgroud)
此代码失败,因为它auto&是一个const引用.周围有优雅的方式吗?
我有对象结构,由shared_ptrs和weak_ptrs组成,以避免圆形.原始指针是一个不走的boost::serialization通过对象跟踪序列化时反序列化时,需要恢复共享和弱指针.物体寿命模式很复杂(粒子模拟),但完全可以预测.每当我使用时weak_ptr::lock(),我确定指针仍然有效.通常,我使用lock().get()因为我只需要很短的时间.
现在,lock().get()有性能影响,因为它将增加共享计数(in lock()),然后不久后减少它(临时shared_ptr被破坏).
2002年的这篇boost.devel帖子说,在weak_ptr开发过程中,直接访问原始指针的功能被认为(要命名unsafe_get或者leak),但从未实现过实际的实现.它的缺席迫使程序员在给定条件下使用次优接口.
现在,问题是如何模拟unsafe_get/ leak,换句话说,获取原始指针weak_ptr,在程序员的风险下无效,只读取(不写入)数据.我可以想象一些技巧,比如找出里面的原始指针的偏移shared_ptr或者这样就可以完成这项工作.
我正在使用boost::shared_ptr,但解决方案也适用于c ++ 11 std::shared_ptr.
我正在读VTK统一网格到python.当我通过Paraview中的数据可视化切片时,我得到以下(正确)图像:

然后我使用via numpy&pylab使用以下脚本可视化切片:
import vtk
from vtk.util.numpy_support import vtk_to_numpy
import pylab
imr=vtk.vtkXMLImageDataReader()
imr.SetFileName('flow.vti')
imr.Update()
im=imr.GetOutput()
nx,ny,nz=im.GetDimensions()
orig=im.GetOrigin()
extent=im.GetExtent()
spacing=im.GetSpacing()
flowVtk=im.GetPointData().GetArray("|flow|")
flow=vtk_to_numpy(flowVtk).reshape(nx,ny,nz)
# bottom z-slice
flowZ0=flow[:,:,0]
# set extent so that axes units are physical
img=pylab.imshow(flowZ0,extent=[orig[0],orig[0]+extent[1]*spacing[0],orig[1],orig[1]+extent[3]*spacing[1]],cmap=pylab.gray())
img.set_clim(vmin=0,vmax=1000)
pylab.show()
Run Code Online (Sandbox Code Playgroud)

这似乎是异相的.我尝试重新排序维度reshape(...),它做了一些事情,但它从未显示它实际应该显示的数据.
有什么明显的错误吗?
编辑:我也试过reshape((nx,ny,nz),order="F")(fortran订购),现在我得到了一个更好的图像(使用jet colormap以获得更好的清晰度)
这几乎是正确的,但是数据被怀疑地旋转了90°,而且我想要一些权威的解释,这些解释使用了什么以及为什么(VTK内部使用哪一个?).
编辑2:要获得与Paraview相同的视图,我必须这样做pylab.imshow(np.rot90(flowZ0)); 不知道为什么,所以问题仍然存在:

我曾经能够将 Eigen3 数组/矩阵传递给 spdlog,它内部使用 libfmt。从 libfmt 9.0.0 开始,这些类型不再由 libfmt 格式化,无需进一步的代码。
\nfmt 通过专门化类型来支持自定义fmt::formatter<T>类型;fmt9 文档进一步解释说,派生此专业化ostream_formatter将利用现有的operator<<,Eigen 类可以方便地提供这些。
Eigen使用 CRTP 继承,我将专门研究Eigen::DenseBase<T>这样的所有类型:
#include<fmt/ostream.h>\n#include<eigen3/Eigen/Core>\n#include<iostream>\n\ntemplate <typename T> struct fmt::formatter<T,std::enable_if_t<std::is_base_of_v<Eigen::DenseBase<T>,T>>>: ostream_formatter {};\n\nint main(){\n Eigen::Array3f a(1,2,3);\n std::cout<<fmt::format("{}",a)<<std::endl;\n}\nRun Code Online (Sandbox Code Playgroud)\n自从我发帖以来,很明显这并不顺利:
\nIn file included from /usr/include/fmt/format.h:48,\n from /usr/include/fmt/ostream.h:18,\n from /tmp/aa.cpp:1:\n/usr/include/fmt/core.h: In instantiation of \xe2\x80\x98constexpr fmt::v9::detail::value<Context> fmt::v9::detail::make_value(T&&) [with Context = fmt::v9::basic_format_context<fmt::v9::appender, char>; T = Eigen::Array<float, 3, 1>&]\xe2\x80\x99:\n/usr/include/fmt/core.h:1771:29: required from \xe2\x80\x98constexpr fmt::v9::detail::value<Context> fmt::v9::detail::make_arg(T&&) [with bool IS_PACKED = …Run Code Online (Sandbox Code Playgroud) 我在使用时遇到g ++ 4.6和boost 1.42的编译错误boost::make_shared<T>(...),而shared_ptr<T>(new T(...))编译得很好.遗憾的是,我无法隔离一个最小的例子(我尝试编译的任何内容都适合两者),但也许有人可以向我解释差异.
我正在实例化一个实例shared_ptr<ResidualsFunctor> f,其中ResidualsFunctor有以下ctor:
ResidualsFunctor(int,int,StaticEquilibriumSolver*)
Run Code Online (Sandbox Code Playgroud)
这个
f=shared_ptr<ResidualsFunctor>(new ResidualsFunctor(0,0,this)); // this is a StaticEquilibriumSolver*
Run Code Online (Sandbox Code Playgroud)
编译得很好,而
f=make_shared<ResidualsFunctor>(0,0,this);
Run Code Online (Sandbox Code Playgroud)
告诉我:
/usr/include/boost/smart_ptr/make_shared.hpp: In function 'boost::shared_ptr<T> boost::make_shared(Args&& ...) [with T = StaticEquilibriumSolver::ResidualsFunctor, Args = int, int, StaticEquilibriumSolver* const]':
pkg/sparc/SparcField.cpp:472:49: instantiated from here
/usr/include/boost/smart_ptr/make_shared.hpp:148:5: error: no matching function for call to 'forward(int&)'
/usr/include/boost/smart_ptr/make_shared.hpp:148:5: note: candidate is:
/usr/include/boost/smart_ptr/make_shared.hpp:90:40: note: template<class T> T&& boost::detail::forward(T&&)
Run Code Online (Sandbox Code Playgroud)
这是助推器中的一个错误吗?在gcc?我的错,我没看到?
我有一个3x3矩阵,其中我计算逆.只有当某些子表达式被新符号替换时,才能清晰地写入反转,因为它们多次出现.我可以努力寻找那些子表达并替换它们吗?我尝试了以下内容,没有成功:
from sympy import *
Ex, Ez, nuxy, nuxz = symbols('E_x E_z nu_xy nu_xz')
# compliance matrix for cross-anisotropic material
compl = Matrix([[1/Ex, -nuxy/Ex, -nuxz/Ez],
[-nuxy/Ex, 1/Ex, -nuxz/Ez],
[-nuxz/Ex, -nuxz/Ex, 1/Ez]])
# stiffness matrix
stiff = compl.inv()
# symbols I want to introduce
m, e = symbols('m e')
meSubs = {Ex/Ez: e, (1 - nuxy - 2*e*nuxz**2): m} # instead of these subexpressions
# stiff.simplify() returns None, is that a bug? that's why I apply simplify together with subs here: …Run Code Online (Sandbox Code Playgroud) 我正在考虑从boost :: python到pybind11移植一个复杂的代码,但是我对缺少类似的东西感到困惑boost::python::extract<...>().check()。我读到的pybind11::cast<T>可用于从中提取c ++对象py::object,但是检查是否可以进行强制转换的唯一方法是调用它并在强制转换失败时捕获异常。有什么我要注意的吗?