Preventing gnuplot from evaluating function for each data point

浪尽此生 提交于 2019-12-13 01:23:28

问题


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) 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)))

where the different dos_1, dos_2, etc. are my data files. This function returns a real number that is read from each file.

The problem is that when this function is used in a plot command, say

plot "dos_1" u ($1+a(1)):2

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

which never ends.

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!

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).

Ideas?


回答1:


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

I tested this to work with 4.4.4.



来源:https://stackoverflow.com/questions/23671742/preventing-gnuplot-from-evaluating-function-for-each-data-point

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!