我有一个简单的程序来测试python开发文件的可用性:
#include<Python.h>
int main(){Py_Initialize(); Py_Finalize(); }
Run Code Online (Sandbox Code Playgroud)
我编译它(安装了python 2.7)as gcc -I/usr/include/python2.7 -lpython2.7 p.c.它在其他机器上工作正常,除了在Ubuntu 12.04(精确)的大多数干净的chroot我不断得到
/tmp/ccj8Mgjb.o: In function `main':
p.c:(.text+0x5): undefined reference to `Py_Initialize'
p.c:(.text+0xa): undefined reference to `Py_Finalize'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
已安装标头,/usr/lib/libpython2.7.so但链接器仍然失败.该符号列在.so文件中,gcc正在读取右侧libpython2.7.so:
$ nm -D libpython2.7.so.1.0 | grep Py_Initialize
00000000000c9c20 T Py_Initialize
00000000000c9260 T Py_InitializeEx
$ strace -f gcc -I/usr/include/python2.7 -lpython2.7 /tmp/p.c 2>&1 |grep libpython2.7 |grep open
[pid 10618] open("/usr/lib/gcc/x86_64-linux-gnu/4.6/libpython2.7.so", O_RDONLY) = -1 ENOENT (No such file or directory)
[pid 10618] …Run Code Online (Sandbox Code Playgroud) 我有一个偶尔从GigE相机获得一个帧的功能,并希望它快速返回.标准程序是这样的:
// ...
camera.StartCapture();
Image img=camera.GetNextFrame();
camera.StopCapture(); // <-- takes a few secs
return img;
Run Code Online (Sandbox Code Playgroud)
返回数据准备就绪GetNextFrame()并且StopCapture()非常慢; 因此,我想img尽快返回,并产生一个后台线程StopCapture().但是,在(不太可能)再次启动采集的情况下,我希望通过互斥锁保护访问.有些地方可以抛出异常,所以我决定使用RAII风格的锁,它将在范围退出时释放.同时,我需要将锁转移到后台线程.像这样的东西(伪代码):
class CamIface{
std::mutex mutex;
CameraHw camera;
public:
Image acquire(){
std::unique_lock<std::mutex> lock(mutex); // waits for cleanup after the previous call to finish
camera.StartCapture();
Image img=camera.GetNextFrame();
std::thread bg([&]{
camera.StopCapture(); // takes a long time
lock.release(); // release the lock here, somehow
});
bg.detach();
return img;
// do not destroy&release lock here, do it in the bg thread …Run Code Online (Sandbox Code Playgroud) 我有几个模块为一些简单类型定义转换器(例如int的列表std::vector<int>); 它们是独立模块的一部分,但有时它们都用在一个脚本中,这导致了
RuntimeWarning: to-Python converter for std::vector<int, std::allocator<int> > already registered; second conversion method ignored.
Run Code Online (Sandbox Code Playgroud)
如何检查某种类型的转换器是否已定义并跳过第二次注册?
我有一个大量使用的c ++代码shared_ptr和STL.一个常见的标题说
#include<boost/shared_ptr.hpp>
using boost::shared_ptr; // for shared_ptr
using namespace std; // for STL
Run Code Online (Sandbox Code Playgroud)
我现在想切换到c ++ 0x来使用语言功能,使用gcc 4.6 -std=c++0x.然而std::shared_ptr现在也存在,导致未指明shared_ptr(boost::shared_ptrvs std::shared_ptr)的模糊性.
切换到std::shared_ptr相反时,如下所示:
#include<memory>
using namespace std; // for STL; also imports std::shared_ptr
Run Code Online (Sandbox Code Playgroud)
然后我遇到了问题boost::python,它boost::shared_ptr只是有效地工作(至少没有进一步的摆弄):
/usr/include/boost/python/object/make_ptr_instance.hpp:30:52: error: no matching function for call to 'get_pointer(const std::shared_ptr<Cell>&)'
Run Code Online (Sandbox Code Playgroud)
我的问题是
boost::shared_ptr和std::shared_ptr(比不使用的C++ 0x现在其他),并且还boost::shared_ptr最终只是一个别名std::shared_ptr; 这将自动解决我的问题.谢谢!
我有点困惑,我设法不小心写了相当于这个的代码
int a=a; // there is not a declared before this line
Run Code Online (Sandbox Code Playgroud)
并且编译器愉快地编译了它 - gcc和clang,它们非常符合标准且具有良好的诊断功能.(用-Wall,gcc 4.8警告未初始化的变量; clang不警告).
我认为分配的RHS将在LHS之前进行评估,因此导致a在RHS上未定义.我可以对这个语法合法的原因进行一些简单的澄清吗?
我有一个派生类pydantic.BaseModel,并希望创建一个“假”属性,即计算属性。property 关键字似乎不能以通常的方式与 Pydantic 配合使用。下面是 MWE,其中类存储值并定义具有half明显含义的调用的读/写属性。使用 Pydantic 读取属性可以正常工作,但赋值失败。
我知道 Pydantic 正在修改属性访问的低级细节;也许有一种方法可以在 Pydantic 中以不同的方式定义计算域?
import pydantic
class Object(object):
def __init__(self,*,value): self.value=value
half=property(lambda self: .5*self.value,lambda self,h: setattr(self,'value',h*2))
class Pydantic(pydantic.BaseModel):
class Config:
extra='allow'
value: float
half=property(lambda self: .5*self.value,lambda self,h: setattr(self,'value',h*2))
o,p=Object(value=1.),Pydantic(value=1.)
print(o.half,p.half)
o.half=p.half=2
print(o.value,p.value)
Run Code Online (Sandbox Code Playgroud)
half输出(值=1。在 Pydantic 情况下未通过分配进行修改):
0.5 0.5
4 1.0
Run Code Online (Sandbox Code Playgroud) 我想使用OpenCL将物理模拟算法移植到GPU以获得性能; 我没有使用OpenCL的经验,我正在四处寻找.计算主要是小密集矩阵(3x3)和矢量积,交叉积等.
这些基本操作是否有一些"标准"/推荐库?我当然不想自己编码矩阵乘法和反演(不是时间,而且它会是无效的)
由于OpenCL没有类,运算符重载等,我是否必须编写mmul(a,mtrans(b))而不是a*b.transpose()例如?
是否有一些(计划的)OpenCL(或预处理器)的扩展/演变,以使符号更像数学?我有回到四十五年的印象.(我知道有CUDA,但它受供应商约束)
我有一个浮点数的数组,这是无序的.我知道价值总是在几个点附近,这是不知道的.为了说明,这个列表
[10.01,5.001,4.89,5.1,9.9,10.1,5.05,4.99]
Run Code Online (Sandbox Code Playgroud)
有价值聚集在5和10左右,所以我想[5,10]作为答案.
我想找到那些具有1000+值的列表的簇,其中簇的nunber可能大约为10(对于某些给定的容差).如何有效地做到这一点?
func计算n-1大小数组的后续元素的平均值的函数是什么n(即窗口宽度为 2 的移动平均值)?
func(numpy.array([1,2,3,4,5]))
# return numpy.array([1.5, 2.5, 3.5, 4.5])
Run Code Online (Sandbox Code Playgroud) 我想用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) c++11 ×3
boost-python ×2
c++ ×2
numpy ×2
python ×2
average ×1
boost ×1
c ×1
converter ×1
lambda ×1
linker ×1
mutex ×1
opencl ×1
properties ×1
pydantic ×1
python-c-api ×1
shared-ptr ×1
syntax ×1