我正在尝试使用clang ++编译器在OS X 10.8上编译一些C++代码(包括C++ 11特性).我有一个生成目标文件的makefile,然后在命令上:
clang++ -o Analysis.so -shared DataFile.o CR39DataFile.o
Run Code Online (Sandbox Code Playgroud)
关于架构x86_64找不到符号,我收到很多错误.该代码在使用g ++的*nix系统上正常工作,并适当地更改编译器标志以支持C++ 11.要编译*.o我这样做:
clang++ -c -Wall -std=c++11 -stdlib=libc++ -I../src ../src/DataFile.cc
Run Code Online (Sandbox Code Playgroud)
编辑:链接命令的输出是:
clang++ -o Analysis.so -shared DataFile.o CR39DataFile.o
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::find(char const*, unsigned long, unsigned long) const", referenced from:
CR39DataFile::read_thread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in CR39DataFile.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::find(char, unsigned long) const", referenced from:
CR39DataFile::trim(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in CR39DataFile.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::compare(char const*) const", referenced from:
CR39DataFile::read_thread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in CR39DataFile.o …Run Code Online (Sandbox Code Playgroud) 我对matplotlib有些新意.我要做的是编写代码,将几个数字保存到eps文件,然后生成一个复合图形.基本上我想做的就是有类似的东西
def my_plot_1():
fig = plt.figure()
...
return fig.
def my_plot_2():
fig = plt.figure()
...
return fig
def my_combo_plot(fig1,fig2):
fig = plt.figure()
gs = gridspec.GridSpec(2,2)
ax1 = plt.subplot(gs[0,0])
ax2 = plt.subplot(gs[0,1])
ax1 COPY fig1
ax2 COPY fig2
...
Run Code Online (Sandbox Code Playgroud)
之后我可以做点什么
my_combo_plot( my_plot_1() , my_plot_2() )
Run Code Online (Sandbox Code Playgroud)
并且拥有所有的数据和设置也会从由前两个函数返回的情节抄袭,但我无法弄清楚如何做到这一点与matplotlib来完成.
我正在制作一些contour标有通过标记的等高线图clabel.问题是轮廓标签倾向于与轴重叠:

(其他一些标签很乱,请忽略它).对于左图,10 ^ -3和10是有问题的.在右边,10 ^ 3是唯一的问题.以下是生成其中一个的代码:
fig = plt.figure(figsize=(6,3))
ax = fig.add_subplot(121)
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel(r'$T_e$ (eV)', fontsize=10)
ax.set_ylabel(r'$n_e$ (1/cm$^3$)', fontsize=10)
ax.set_xlim(0.1, 1e4)
ax.set_ylim(1e16, 1e28)
CS = ax.contour(X, Y, Z, V, colors='k')
ax.clabel(CS, inline=True, inline_spacing=3, rightside_up=True, colors='k', fontsize=8, fmt=fmt)
Run Code Online (Sandbox Code Playgroud)
有没有什么方法clabel可以更好地表现它的位置?
我正在编写Python代码来生成和绘制"超高斯"函数,如:
def supergaussian(x, A, mu, sigma, offset, N=8):
"""Supergaussian function, amplitude A, centroid mu, st dev sigma, exponent N, with constant offset"""
return A * (1/(2**(1+1/N)*sigma*2*scipy.special.gamma(1+1/N))) * numpy.exp(-numpy.absolute(numpy.power(x-mu,N))/(2*sigma**N)) + offset
init_x = numpy.arange(-100,100,1.0)
init_y = supergaussian(init_x, 1, 0, 25, 0, N=12)
Run Code Online (Sandbox Code Playgroud)
下面的代码就是一个情节.由于我无法理解的原因,当使用默认值8 N或者N最多13的值时,此代码可以正常工作.当N为14或更高时,该函数崩溃并显示错误消息:
AttributeError: 'float' object has no attribute 'exp'
Run Code Online (Sandbox Code Playgroud)
在函数定义的返回行.有任何想法吗?因为在该行中唯一使用.exp的是numpy.exp错误消息似乎意味着它numpy被解释为一个浮点数,但仅适用于大值N......
我正在使用numpy 1.7.1和scipy 0.12.0运行python 3.3.2
我的功能评估有点慢.我试图通过使用线程加快速度,因为有三件事可以并行完成.单线程版本是
return dEdx_short(E) + dEdx_long(E) + dEdx_quantum(E);
Run Code Online (Sandbox Code Playgroud)
这些函数的评估分别需要〜250us,~250us和~100us.所以我实现了一个三线程解决方案:
double ret_short, ret_long, ret_quantum; // return values for the terms
auto shortF = [this,&E,&ret_short] () {ret_short = this->dEdx_short(E);};
std::thread t1(shortF);
auto longF = [this,&E,&ret_long] () {ret_long = this->dEdx_long(E);};
std::thread t2(longF);
auto quantumF = [this,&E,&ret_quantum] () {ret_quantum = this->dEdx_quantum(E);};
std::thread t3(quantumF);
t1.join();
t2.join();
t3.join();
return ret_short + ret_long + ret_quantum;
Run Code Online (Sandbox Code Playgroud)
我预计需要大约300us,但实际需要大约600us - 与单线程版本基本相同!这些本质上都是线程安全的,所以没有等待锁.我检查了我的系统上的线程创建时间,它是〜25us.我没有使用我的所有核心,所以我有点困惑的是为什么并行解决方案如此之慢.这与lambda创作有关吗?
我试图绕过lambda,例如:
std::thread t1(&StopPow_BPS::dEdx_short, this, E, ret_short);
Run Code Online (Sandbox Code Playgroud)
重写被调用的函数后,但这给了我一个错误attempt to use a deleted function...
我正在尝试使用Batik库将BufferedImage绘制到SVG文件中.我有非常相似的代码适用于EPS/PS文件,但由于某种原因,以下代码:
// Get a DOMImplementation and create an XML document
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
Document document = domImpl.createDocument(null, "svg", null);
// Create an instance of the SVG Generator
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// draw from image into the svg:
svgGenerator.drawImage(image,new RescaleOp((float)1.0,(float)0.0,null),0,0);
// Write svg file
OutputStream outputStream = new FileOutputStream(svgFile);
Writer out = new OutputStreamWriter(outputStream, "UTF-8");
//svgGenerator.stream(out, true /* use css */);
// write and close file:
outputStream.flush();
outputStream.close();
Run Code Online (Sandbox Code Playgroud)
导致svgGenerator.drawImage(...)行上的NullPointerException.由于基本相同的命令适用于AbstractPSDocumentGraphics2D(在另一种方法中),我不知道这里有什么问题.
编辑:图像在别处声明(类变量,此代码在该类的方法内).
这是一个堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.apache.batik.svggen.ImageHandlerBase64Encoder.encodeImage(ImageHandlerBase64Encoder.java:157) …Run Code Online (Sandbox Code Playgroud) 我只是想在开始编写扩展之前先创建一个 numpy 数组。这是一个超级简单的程序:
#include <stdio.h>
#include <iostream>
#include "Python.h"
#include "numpy/npy_common.h"
#include "numpy/ndarrayobject.h"
#include "numpy/arrayobject.h"
int main(int argc, char * argv[])
{
int n = 2;
int nd = 1;
npy_intp size = {1};
PyObject* alpha = PyArray_SimpleNew(nd, &size, NPY_DOUBLE);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序在PyArray_SimpleNew调用时出现段错误,我不明白为什么。我试图遵循之前的一些问题(例如numpy array C api和C array to PyArray)。我究竟做错了什么?
我最近将ipython升级到2.0.0,无法使内联图工作.如果我试试
%pylab inline
plot([1,2],[1,2])
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
Populating the interactive namespace from numpy and matplotlib
[<matplotlib.lines.Line2D at 0x10ffcf080>]
/usr/local/lib/python3.4/site-packages/IPython/core/formatters.py:239: FormatterWarning: Exception in image/png formatter: _image_module::readpng: png_create_read_struct failed
FormatterWarning,
<matplotlib.figure.Figure at 0x10e77ca58>
Run Code Online (Sandbox Code Playgroud)
在控制台中,我有以下错误:
ERROR:tornado.application:Uncaught exception in /api/kernels/0a214dee-3143-4d34-89cb-9d65ce154fe6/shell
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/tornado/websocket.py", line 322, in wrapper
return callback(*args, **kwargs)
File "/usr/local/lib/python3.4/site-packages/IPython/html/services/kernels/handlers.py", line 122, in on_message
self.session.send(self.zmq_stream, msg)
File "/usr/local/lib/python3.4/site-packages/IPython/kernel/zmq/session.py", line 646, in send
stream.send_multipart(to_send, copy=copy)
AttributeError: 'NoneType' object has no attribute 'send_multipart'
Run Code Online (Sandbox Code Playgroud)
知道这里发生了什么吗?
编辑:我在python 3.4.0,ipython 2.0.0和matplotlib 1.3.1
我正在使用应用程序tkinter的主题(ttk)GUI工具包.尝试将一些统一样式应用于主窗口中的小部件:
s = ttk.Style()
s.configure('.', background='#eeeeee')
s.configure('.', font=('Helvetica', 14))
self.configure(background='#eeeeee')
Run Code Online (Sandbox Code Playgroud)
字体更改效果很好,但由于某种原因,窗口小部件(即ttk.Label和ttk.Button)似乎并不反映背景更改,由于窗口背景和窗口小部件之间的对比,这在视觉上非常明显.如果我检查它设置为:
label1.cget('background')
Run Code Online (Sandbox Code Playgroud)
它返回了'',显然它没有被设置,但我不明白给出ttk.Label和样式的文档有什么不对.尝试直接设置单个标签的背景:
label1.configure(background='#eeeeee')
Run Code Online (Sandbox Code Playgroud)
也不起作用(即没有变化).有任何想法吗?
所以我正在编写一些需要从 CSV 文件中提取配置/数据的代码,这些文件与应用程序一起打包。据我所知,使用pkgutil是执行此操作的“正确”方法。所以我想做的是:
import pkgutil
MatFile = pkgutil.get_data('impy.implosions', 'LILAC_Materials.csv')
Run Code Online (Sandbox Code Playgroud)
它工作正常并给了我文件的字节。但我不知道如何以csv.reader一种干净的方式将其输入。我发现了这个老问题,但它的解决方案是这样的:
MatFile = io.StringIO(MatFile)
dataReader = csv.reader(MatFile , delimiter=',')
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为StringIO需要一个 str。中的补充功能io将是BytesIO,但这对我没有帮助,因为csv.reader无法处理它。看起来这应该有一个简单的解决方案,但我不熟悉在 python 中处理字节数据。谢谢!