防止gnuplot评估每个数据点的功能 [英] Preventing gnuplot from evaluating function for each data point
问题描述
我遇到一些执行耗时很长的gnuplot plot
命令的麻烦.基本上,当您对数据文件执行以下操作时:
I have trouble with some gnuplot plot
command that is taking very long to execute. Basically, when you do the following with a data file:
f(x) = sin(x) # for example!
plot "data" u ($1-f($1)):2
将为每个要绘制的数据点评估
f($ 1)
.现在,我有一个外部函数:
f($1)
will be evaluated for each data point that is to be plotted. Now, I have an external function:
a(i) = real(system(sprintf("awk 'NR==1 {print $6}' dos_%i", i)))
其中不同的 dos_1
, dos_2
等是我的数据文件.此函数返回从每个文件读取的实数.
where the different dos_1
, dos_2
, etc. are my data files. This function returns a real number that is read from each file.
问题是当在 plot
命令中使用此功能时,说
The problem is that when this function is used in a plot
command, say
plot "dos_1" u ($1+a(1)):2
将对每个数据点评估函数 a(1)
,即使它没有变化.这可能要花费很多时间,因为这是调用外部命令的功能.现在,有人可能(正确地)建议如果a(1)不变,那么我将使用一个常量:
the function a(1)
will be evaluated for each data point, even though it does not change. This can take a lot of time, because it is a function invoking an external command. Now, someone might (rightfully) suggest that if a(1) doesn't change I use a constant:
a1 = a(1)
plot "dos_1" u ($1+a1):2
,它将更快地执行并获得相同的结果.我的问题是我必须绘制许多文件:
which will execute considerably faster and give the same results. My problem is that I have to plot many files:
plot for [i=1:agazillion] "dos_".i u ($1+a(i)):2
永无止境的
如何将函数值的存储(和使用)自动化到静态变量(或更轻的函数)上,如下所示:
How do I automatize the storage (and usage) of a function's value onto a static variable (or lighter function), schematically like this:
for [i=1:agazillion] "a".i = a(i) # This doesn't work!!!
plot for [i=1:agazillion] "dos_".i u ($1+"a".i):2 # This doesn't work either!
使事情复杂化的是,我可以访问的最新gnuplot是4.4.(也请提出适用于更高版本的解决方案,但声明它们仅适用于更高版本).
To complicate things, the latest gnuplot I have access to is 4.4. (Please also suggest solutions that work for later versions but state they only work for later versions).
想法?
推荐答案
我知道的唯一结构"将使用单词列表存储值.必须使用bash完成文件的迭代,因为版本4.4只能在绘图调用内循环,而不能使用 do for ...
:
The only "structure" I know would be using a word list to store the values. The iteration over the files must be done with bash, because version 4.4 can only loop inside a plot call, but not with do for ...
:
a_values = system("for file in dos_*; do awk -v ORS=' ' 'NR==1 {print $6}' $file; done")
plot for [i=1:words(a_values)] "dos_".i u ($1+word(a_values, i)):2
我对此进行了测试,可以与4.4.4一起使用.
I tested this to work with 4.4.4.
这篇关于防止gnuplot评估每个数据点的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!