首页 \ 问答 \ 并发编程c ++?(Concurrent programming c++? [closed])

并发编程c ++?(Concurrent programming c++? [closed])

我不断听到每个地方的并发编程。 你们可以点亮它是什么,以及c ++新标准如何促进这样做?


I keep on hearing about concurrent programing every where. Can you guys throw some light on what it's and how c++ new standards facilitate doing the same?


原文:https://stackoverflow.com/questions/218786
更新时间:2023-06-16 18:06

最满意答案

curvefit给你一个常数(一条平线)的原因是因为你使用你定义的模型传递了一个不相关的数据集!

让我先重新创建你的设置:

argon = np.genfromtxt('argon.dat')
copper = np.genfromtxt('copper.dat')

f1 = 1 - np.exp(-argon[:,1] * 1.784e-3 * 6.35)
f2 = np.exp(-copper[:,1] * 8.128e-2 * 8.96)

现在请注意, f1基于文件argon.dat数据的第二列。 它与第一列无关,虽然没有什么可以阻止你绘制第二列的修改版本与第一列的比较,这就是你绘制时所做的:

import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

plt.semilogy(copper[:,0]*1000, f2, 'r-')  # <- f2 was not based on the first column of that file, but on the 2nd. Nothing stops you from plotting those together though...
plt.semilogy(argon[:,0]*1000, f1, 'b--')
plt.ylim(1e-6,1)
plt.xlim(0, 160)

def model(x, a, b, offset):
    return a*np.exp(-b*x) + offset

备注:在您的模型中,您有一个名为b的参数未使用。 传递给拟合算法总是一个坏主意。 摆脱它。

现在就是诀窍:你使用指数模型基于第二列创建了f1 。 所以你应该将curve_fit作为第二列作为自变量(在函数的doc-string中标记为xdata ),然后将f1作为因变量。 喜欢这个:

popt1, pcov = curve_fit(model, argon[:,1], f1)
popt2, pcov = curve_fit(model, cupper[:,1], f2)

这将非常有效。

现在,当您想要绘制2个图形的乘积的平滑版本时,您应该从自变量中的公共区间开始。 对你而言,这是光子能量。 两个数据文件中的第二列取决于:有一个函数(一个用于氩,另一个用于铜),它将μ/ρ与光子能量相关联。 因此,如果您有很多能量数据点,并且您设法获得这些功能,那么μ/ρ将有许多数据点。 虽然这些功能是未知的,但我能做的最好的事情就是简单地进行插值。 但是,数据是对数的,因此需要对数插值,而不是默认的线性。

所以现在,继续获得大量的光子能量数据点。 在数据集中,能量点呈指数增长,因此您可以使用np.logspace创建一组不错的新点:

indep_var = argon[:,0]*1000
energy = np.logspace(np.log10(indep_var.min()),
                     np.log10(indep_var.max()),
                     512)  # both argon and cupper have the same min and max listed in the "energy" column.

它的优势在于两个数据集中的能量具有相同的最小值和最大值。 否则,您将不得不减少此日志空间的范围。

接下来,我们(对数)插值关系energy -> μ/ρ

interpolated_mu_rho_argon = np.power(10, np.interp(np.log10(energy), np.log10(indep_var), np.log10(argon[:,1]))) # perform logarithmic interpolation
interpolated_mu_rho_copper = np.power(10, np.interp(np.log10(energy), np.log10(copper[:,0]*1000), np.log10(copper[:,1])))

这是对刚刚完成的工作的直观表示:

f, ax = plt.subplots(1,2, sharex=True, sharey=True)
ax[0].semilogy(energy, interpolated_mu_rho_argon, 'gs-', lw=1)
ax[0].semilogy(indep_var, argon[:,1], 'bo--', lw=1, ms=10)
ax[1].semilogy(energy, interpolated_mu_rho_copper, 'gs-', lw=1)
ax[1].semilogy(copper[:,0]*1000, copper[:,1], 'bo--', lw=1, ms=10)
ax[0].set_title('argon')
ax[1].set_title('copper')
ax[0].set_xlabel('energy (keV)')
ax[0].set_ylabel(r'$\mu/\rho$ (cm²/g)')

对数插值

标有蓝点的原始数据集已经过精细插值。

现在,最后的步骤变得简单了。 因为已经找到了将μ/ρ映射到某个指数变量(我已重命名为f1f2的函数)的模型参数,所以它们可用于生成存在的数据的平滑版本,以及作为这两个功能的产物:

plt.figure()
plt.semilogy(energy, model(interpolated_mu_rho_argon, *popt1), 'b-', lw=1)
plt.semilogy(argon[:,0]*1000, f1, 'bo ')

plt.semilogy(copper[:,0]*1000, f2, 'ro ',)
plt.semilogy(energy, model(interpolated_mu_rho_copper, *popt2), 'r-', lw=1) # same remark here!

argon_copper_prod = model(interpolated_mu_rho_argon, *popt1)*model(interpolated_mu_rho_copper, *popt2)
plt.semilogy(energy, argon_copper_prod, 'g-')

plt.ylim(1e-6,1)
plt.xlim(0, 160)
plt.xlabel('energy (keV)')
plt.ylabel(r'$\mu/\rho$ (cm²/g)')

在此处输入图像描述

你去吧 总结一下:

  1. 生成足够数量的自变量数据点以获得平滑结果
  2. 插值关系photon energy -> μ/ρ
  3. 将函数映射到插值μ/ρ

The reason curvefit is giving you a constant (a flat line), is because you're passing it a dataset that is uncorrelated using the model you have defined!

Let me recreate your setup first:

argon = np.genfromtxt('argon.dat')
copper = np.genfromtxt('copper.dat')

f1 = 1 - np.exp(-argon[:,1] * 1.784e-3 * 6.35)
f2 = np.exp(-copper[:,1] * 8.128e-2 * 8.96)

Now notice that f1 is based on the 2nd column of the data in the file argon.dat. It is NOT related to the first column, although nothing stops you from plotting a modified version of the 2nd column vs the first of course, and that is what you did when you plot:

import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

plt.semilogy(copper[:,0]*1000, f2, 'r-')  # <- f2 was not based on the first column of that file, but on the 2nd. Nothing stops you from plotting those together though...
plt.semilogy(argon[:,0]*1000, f1, 'b--')
plt.ylim(1e-6,1)
plt.xlim(0, 160)

def model(x, a, b, offset):
    return a*np.exp(-b*x) + offset

Remark: in your model you had a parameter called b that was unused. That is always a bad idea to pass to a fitting algorithm. Get rid of it.

Now here's the trick: you made f1 based on the 2nd column, using an exponentional model. So you should pass curve_fit the 2nd column as the independent variable (which is labelled as xdata in the function's doc-string) and then f1 as the dependent variable. Like this:

popt1, pcov = curve_fit(model, argon[:,1], f1)
popt2, pcov = curve_fit(model, cupper[:,1], f2)

And that will work perfectly well.

Now, when you want to plot a smooth version of the product of the 2 graphs, you should start from a common interval in the independent variable. For you, this is the photon energy. The 2nd column in both datafiles depends on that: there is a function (one for argon, another for copper) that relates the μ/ρ to the photon energy. So, if you have lots of datapoints for the energy, and you managed to get those functions, you will have many datapoints for μ/ρ. As those functions are unknown though, the best thing I can do is to simply interpolate. However, the data is logarithmic, so logarithmic interpolation is required, not the default linear.

So now, continue by getting lots of datapoints for the photon energy. In the dataset, the energypoints are exponentially increasing, so you can create a decent new set of points by using np.logspace:

indep_var = argon[:,0]*1000
energy = np.logspace(np.log10(indep_var.min()),
                     np.log10(indep_var.max()),
                     512)  # both argon and cupper have the same min and max listed in the "energy" column.

It works to our advantage that the energy in both datasets have the same minimum and maximum. Otherwise, you would have had to reduce the range of this logspace.

Next, we (logarithmically) interpolate the relation energy -> μ/ρ:

interpolated_mu_rho_argon = np.power(10, np.interp(np.log10(energy), np.log10(indep_var), np.log10(argon[:,1]))) # perform logarithmic interpolation
interpolated_mu_rho_copper = np.power(10, np.interp(np.log10(energy), np.log10(copper[:,0]*1000), np.log10(copper[:,1])))

Here's a visual representation of what has just been done:

f, ax = plt.subplots(1,2, sharex=True, sharey=True)
ax[0].semilogy(energy, interpolated_mu_rho_argon, 'gs-', lw=1)
ax[0].semilogy(indep_var, argon[:,1], 'bo--', lw=1, ms=10)
ax[1].semilogy(energy, interpolated_mu_rho_copper, 'gs-', lw=1)
ax[1].semilogy(copper[:,0]*1000, copper[:,1], 'bo--', lw=1, ms=10)
ax[0].set_title('argon')
ax[1].set_title('copper')
ax[0].set_xlabel('energy (keV)')
ax[0].set_ylabel(r'$\mu/\rho$ (cm²/g)')

logarithmic interpolation

The original dataset, marked with blue dots, has been finely interpolated.

Now, the last steps become easy. Because the parameters of your model that maps μ/ρ to some exponential variant (the functions that I have renamed as f1 and f2) have been found already, they can be used to make a smooth version of the data that was present, as well as the product of both these functions:

plt.figure()
plt.semilogy(energy, model(interpolated_mu_rho_argon, *popt1), 'b-', lw=1)
plt.semilogy(argon[:,0]*1000, f1, 'bo ')

plt.semilogy(copper[:,0]*1000, f2, 'ro ',)
plt.semilogy(energy, model(interpolated_mu_rho_copper, *popt2), 'r-', lw=1) # same remark here!

argon_copper_prod = model(interpolated_mu_rho_argon, *popt1)*model(interpolated_mu_rho_copper, *popt2)
plt.semilogy(energy, argon_copper_prod, 'g-')

plt.ylim(1e-6,1)
plt.xlim(0, 160)
plt.xlabel('energy (keV)')
plt.ylabel(r'$\mu/\rho$ (cm²/g)')

enter image description here

And there you go. To summarize:

  1. generate a sufficient amount of datapoints of the independent variable to get smooth results
  2. interpolate the relationship photon energy -> μ/ρ
  3. map your function to the interpolated μ/ρ

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。