Printing difference of two waves
uhe
I have two waves a,b. I want to print their difference. In other scripting languages (Python, IDL) I can say: print a - b. In Igor I need
<br />
Make /N=(10) diff = a[p] - b[p] // need to know the number of elements, Make diff = a[p] - b[p] is not good enough<br />
print diff<br />
Really?
UHe
print
operation.In any computation (programming language) something has to store the result of an operation, even if it appears as temporary or "anonymous", like so (executed at the command line):
That being said there is something inconsistent for me going on here and it may be related. Take for example the following code:
wave& a
wave& b
matrixop/o/free res = a - b
return res
end
function test_print()
make/o/n=3 a = p
make/o/n=3 b = p+1
wave c = diff_wave(a,b)
print c
//print diff_wave(a,b) //will not compile
end
Regardless if executed in a function or at the command prompt, the following will give an error:
print diff_wave(a,b)
(provided,a
andb
exist in the global scope when executing from the command prompt).While the
test_print()
function prints the difference correctly:'_free_'[0]= {-1,-1,-1}
Why can't
print
take directly the wave reference fromdiff_wave()
but needs a specially defined reference as inwave c = diff_wave(a,b)
?best,
_sk
March 15, 2018 at 09:50 am - Permalink
UHe
March 17, 2018 at 10:56 am - Permalink
The free wave expires outside of the function runtime context. That is, once it hits the command line, it has already been disposed.
--Jim Prouty
Software Engineer, WaveMetrics, Inc.
March 17, 2018 at 12:22 pm - Permalink
I'm not sure this really answers the question.
I'd say in general that print needs a wave reference and not a function returning a wave.
Make/O data = p
return data
end
Function test_print()
print GetWave() //will not compile
end
If you only index a single point from the wave
print GetWave()[0]
then it compiles and does what you expect.March 17, 2018 at 03:05 pm - Permalink
I was under the impression that
function/wave
returns a wave reference and not a wave; in other words, there is no copy.But what you point out is correct. If print gets a single value then it chugs through it.
Even if the
/free
inmatrixop
is removed (in other words,res
is not cleared upon exiting thediff_wave
scope) the code below still fails.wave& a
wave& b
matrixop/o res = a - b
return res
end
//execute from the command line or from a within function fails
print diff_wave(a,b)
While we are at it, why doesn't the following work?
While this does work:
best,
_sk
March 18, 2018 at 03:31 pm - Permalink
Sorry my wording was inaccurate. It should have been "print needs a wave reference and not a function returning a wave reference".
March 20, 2018 at 11:44 am - Permalink
A function returning a wave reference should be equivalent to a wave reference, otherwise composability breaks. I think the reason for the error is that
print
,make
need the whole data and not a pointer to the whole data, whileduplicate
apparently doesn't.These inconsistencies in operations and functions throughout Igor are a sign of evolution and cruft accumulation through the years but, overall, contribute in a negative manner to the use of the environment as a whole.
best,
_sk
March 21, 2018 at 02:33 am - Permalink
This will print to the history the difference between tokens 'a' and 'b' but it will not leave behind a wave 'ff'. If you need to use ff for some later computation you can execute:
More details (especially about /P=2) will be provided in subsequent documentation.
A.G.
WaveMetrics, Inc.
March 21, 2018 at 03:10 pm - Permalink