有没有什么好的方法如何在Mathplotlib中将复数的二维数组绘制成图像?
将复数的大小映射为"亮度"或"饱和度"并将相位映射为"Hue"非常有意义(无论如何Hue只不过是RBG颜色空间中的相位). http://en.wikipedia.org/wiki/HSL_and_HSV
但据我所知,imshow只接受标量值,然后使用某些色阶进行映射.没有什么比投影真实的RGB图片更好的了吗?
我很容易实现一个版本,它接受3个浮点数的元组(向量)的二维数组或形状浮点数的ndarray [:,:,3].我想这通常是usefful功能.它对于绘制真实的RGB colord图像(例如从OpenCL输出的纹理)也很有用
使用堆分配数组的常用方法是:
SomeType * arr = new SomeType[15454];
//... somewhere else
delete [] arr;
Run Code Online (Sandbox Code Playgroud)
为了delete [] arr
使C运行时必须知道与指针相关的内存缓冲区的长度.我对吗?
所以原则上应该可以以某种方式访问信息?可以使用某个库访问吗?我是在想.我知道它不是语言的核心部分,因此它将取决于平台.
Plots.jl
我正在调试一个脚本(与后端一起使用GKS QtTerm
)。所以我多次运行该脚本。当我从终端运行它时,bash> julia pointPlacement.jl
需要很长时间才能初始化 Julia 和 Plots.jl(与 python 相比,这是一大不便)。因此,我宁愿让 Julia 保持打开状态并从内部运行脚本,例如julia> include( "pointPlacement.jl" )
grid = [ [ix*0.01 iy*0.01] for ix=1:100, iy=1:100 ]
grid = vcat(ps...)
centers = hexGrid( 2, 0.2 )
using Plots
display(scatter!( grid[:,1], grid[:,2], markersize = 1, markerstrokewidth = 0, aspect_ratio=:equal ))
display(scatter!( centers[:,1], centers[:,2], markersize = 2, markerstrokewidth = 0, aspect_ratio=:equal ))
Run Code Online (Sandbox Code Playgroud)
问题是地块不断累积。这是运行 9 次之后的结果。应该只有 2 个数据集,而不是 18 个:
我想关闭(杀死、摧毁)它们
如果我!
像这样删除,它会有所帮助
display(scatter( grid[:,1], grid[:,2], markersize = 1, markerstrokewidth …
Run Code Online (Sandbox Code Playgroud) 抱歉还有一个关于dynamic module does not define init function
. 我确实通过了较旧的问题,但我没有找到一个专门解决我的案子的问题。
我有一个 C++ 库,它应该将几个函数导出到 python(比如在extern "C" {}
block 中定义的 ~5 个函数)。当我每次导入库时重新编译它时,它工作得很好。但是,如果我在没有重新编译的情况下导入它,则会出现错误ImportError: dynamic module does not define init function (initmylib)
重现相同行为(错误)的非常简化的示例如下所示:
文件中的 C++ 库代码 mylib.cpp
#include <math.h>
// there are some internal C++ functions and classes
// which are not exported, but used in exported functions
extern "C"{
// one of the functions which should be accessible from python
void oscilator( double dt, int n, double * abuff, double * …
Run Code Online (Sandbox Code Playgroud) 我经常遇到一种情况,当我有复杂的类(例如,实现一些数值算法,如偏微分方程求解器)和数据数组时,它可以根据用例拥有或从外部上下文绑定。问题是如何为这样的类制作健壮的析构函数。简单的方法是制作布尔标志,指示是否拥有该数组。例如
// simplest example I can think about
class Solver{
int nParticles;
bool own_position;
bool own_velocity;
double* position;
double* velocity;
// there is more buffers like this, not just position and velocity, but e.g. mass, force, pressure etc. each of which can be either owned or binded externally independently of each other, therefore if there is 6 buffers, there is 2^6 variants of owership (e.g. of construction/destruction)
void move(double dt){ for(int i=0; i<n; i++){ position[i]+=velocity[i]*dt; } }
~Solver(){ …
Run Code Online (Sandbox Code Playgroud) 小故事:
我有通过输出变量的引用传递的函数
void acum( float2 dW, float4 dFE, float2 *W, float4 *FE )
Run Code Online (Sandbox Code Playgroud)
如果满足某些条件,则假定增量变量 *W, *FE, by dW, dFE。我想让这个函数通用——输出变量可以是局部的也可以是全局的。
acum( dW, dFE, &W , &FE ); // __local acum
acum( W, FE, &Wout[idOut], &FEout[idOut] ); // __global acum
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时出现错误
error: illegal implicit conversion between two pointers with different address spaces
Run Code Online (Sandbox Code Playgroud)
有可能以某种方式制作它吗???我在想是否可以使用宏而不是函数(但我对 C 中的宏不是很熟悉)。
另一种可能性可能是:
背景(不需要阅读):
我正在尝试使用 OpenCL 对一些热力学采样进行编程。因为统计权重 W = exp(-E/kT) 很容易在低温下溢出浮点数 (2^64),所以我创建了一个组合数据类型 W = float2(W_digits, W_exponent) 并定义了函数来操纵这些数字“acum”。
我尽量减少全局内存操作的数量,所以我让 work_items 超过 Vsurf …
我只是在游戏中测试几种轨道动力学的集成方案.我在这里采用RK4进行恒定和自适应步骤 http://www.physics.buffalo.edu/phy410-505/2011/topic2/app1/index.html
我将它与简单的verlet集成(和euler,但它的性能非常差)进行了比较.似乎RK4具有恒定步长优于verlet.具有自适应步骤的RK4更好,但不是那么多.我想知道我做错了什么?或者说在哪种意义上说RK4比verlet更优越?
我们认为Force的每个RK4步骤评估为4x,但每个verlet步骤只评估1x.所以为了获得相同的性能,我可以为verlet设置更小的time_step 4x.4x较小的时间步长verlet比RK4更精确,具有恒定步长,几乎可与具有自适应步骤的RK4相比.
查看图片:https: //lh4.googleusercontent.com/-I4wWQYV6o4g/UW5pK93WPVI/AAAAAAAAA7I/PHSsp2nEjx0/s800/kepler.png
10T表示10个轨道周期,以下编号48968,7920,48966是所需的力评估数
python代码(使用pylab)如下:
from pylab import *
import math
G_m1_plus_m2 = 4 * math.pi**2
ForceEvals = 0
def getForce(x,y):
global ForceEvals
ForceEvals += 1
r = math.sqrt( x**2 + y**2 )
A = - G_m1_plus_m2 / r**3
return x*A,y*A
def equations(trv):
x = trv[0]; y = trv[1]; vx = trv[2]; vy = trv[3];
ax,ay = getForce(x,y)
flow = array([ vx, vy, ax, ay ])
return flow
def SimpleStep( x, dt, flow ): …
Run Code Online (Sandbox Code Playgroud) 我刚在Julia安装了PyPlot.当我从julia的互动环境中运行它时,它工作正常.但是当我从bash运行.jl脚本时,不会显示绘图图形.
我很熟悉matplotlib(pylab),其中show()命令用于查看数字.我可能不会在这里找不到PyPlot的自述文件https://github.com/stevengj/PyPlot.jl
您可以通过调用gcf()将当前数字作为Figure对象(matplotlib.pyplot.Figure的包装器)获取.图类型支持Julia的多媒体I/O API,因此您可以使用display(fig)来显示fig :: PyFigure
如果我运行此脚本:
using PyPlot
x = linspace(0,2*pi,1000); y = sin(3*x + 4*cos(2*x));
plot(x, y, color="red", linewidth=2.0, linestyle="--")
title("A sinusoidally modulated sinusoid")
fig1 = gcf()
display(fig1)
Run Code Online (Sandbox Code Playgroud)
我在屏幕上没有图形,只是带有图形对象地址的文本输出
$ julia pyplottest.jl
Loading help data...
Figure(PyObject <matplotlib.figure.Figure object at 0x761dd10>)
Run Code Online (Sandbox Code Playgroud)
我也不确定为什么需要这么长时间以及" 加载帮助数据...... "的含义
如果我使用include("pyplottest.jl")从Julia环境内部运行相同的脚本,情节确实显示正常
我有一个python脚本,它为不同的参数(Q
,K
)进行了许多模拟,绘制结果并将其存储到磁盘.
每组参数(Q,K
)产生一个200x200x80数据点的3D体积网格数据,这需要~100 MB的数据.然后,该体积网格的一部分逐层绘制,产生~60个图像.
问题是python显然不会在此过程中释放内存.我不确定内存泄漏在哪里,或者规则如何控制python如何决定哪些对象被释放.我也不确定numpy
数组或matplotlib
图形对象中的内存是否丢失.
代码的相关部分在这里(但是,它不会运行...模拟代码的大部分包括ctypes
C++/python接口被省略,因为它太复杂了):
import numpy as np
import matplotlib.pyplot as plt
import ProbeParticle as PP # this is my C++/Python simulation library, take it as blackbox
def relaxedScan3D( xTips, yTips, zTips ):
ntips = len(zTips);
print " zTips : ",zTips
rTips = np.zeros((ntips,3)) # is this array deallocated when exiting the function?
rs = np.zeros((ntips,3)) # and this? …
Run Code Online (Sandbox Code Playgroud) 我有一个将 3D numpy 数组绘制为图像堆栈的函数:
def plotImages(
prefix, F, slices,
extent=None, zs = None, figsize=default_figsize,
cmap=default_cmap, interpolation=default_interpolation, vmin=None, vmax=None, cbar=False,
):
for ii,i in enumerate(slices):
print " plotting ", i
plt.figure( figsize=figsize )
plt.imshow( F[i], origin='image', interpolation=interpolation, cmap=cmap, extent=extent, vmin=vmin, vmax=vmax )
if cbar:
plt.colorbar();
plt.xlabel(r' Tip_x $\AA$')
plt.ylabel(r' Tip_y $\AA$')
if zs is None:
plt.title( r"iz = %i" %i )
else:
plt.title( r"Tip_z = %2.2f $\AA$" %zs[i] )
plt.savefig( prefix+'_%3.3i.png' %i, bbox_inches='tight' )
plt.close()
Run Code Online (Sandbox Code Playgroud)
我在循环中调用此函数,用于约 20 个数据网格(3D numpy 数组),每个数据网格有约 50 …
matplotlib ×5
python ×4
c++ ×3
julia ×2
numpy ×2
ctypes ×1
destructor ×1
opencl ×1
ownership ×1
plots.jl ×1
runge-kutta ×1