Printing difference of two waves

I have little experience with Igor.
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
I think this is more of a limitation in the implementation of the command window (ctrl+j) or the 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):
matrixop/o res = a - b; print/f res; killwaves res


That being said there is something inconsistent for me going on here and it may be related. Take for example the following code:
function/wave diff_wave(a,b)
    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 and b 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 from diff_wave() but needs a specially defined reference as in wave c = diff_wave(a,b)?

best,
_sk
... looks like it is so. Thanks for the hint about using matrixop.
UHe
_sk wrote:


Why can't print take directly the wave reference from diff_wave() but needs a specially defined reference as in wave c = diff_wave(a,b)?
_sk


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.
JimProuty wrote:
_sk wrote:


Why can't print take directly the wave reference from diff_wave() but needs a specially defined reference as in wave c = diff_wave(a,b)?
_sk


The free wave expires outside of the function runtime context. That is, once it hits the command line, it has already been disposed.


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.

Function/wave GetWave()

    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.
thomas_braun wrote:

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.


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.

JimProuty wrote:

The free wave expires outside of the function runtime context. That is, once it hits the command line, it has already been disposed.


Even if the /free in matrixop is removed (in other words, res is not cleared upon exiting the diff_wave scope) the code below still fails.

function/wave diff_wave(a,b)
    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?
make/o/n=3 c = diff_wave(a,b) //same number of rows as a and b


While this does work:

duplicate/o diff_wave(a,b), c1


best,
_sk
_sk wrote:
thomas_braun wrote:

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.


I was under the impression that function/wave returns a wave reference and not a wave; in other words, there is no copy.


Sorry my wording was inaccurate. It should have been "print needs a wave reference and not a function returning a wave reference".
thomas_braun wrote:
_sk wrote:
thomas_braun wrote:

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.


I was under the impression that function/wave returns a wave reference and not a wave; in other words, there is no copy.


Sorry my wording was inaccurate. It should have been "print needs a wave reference and not a function returning a wave reference".


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, while duplicate 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
Try IP8 beta (after the next build). It should allow you to execute e.g.,
MatrixOP/O/P=2 ff=a-b

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:
 MatrixOP/O/P=1 ff=a-b


More details (especially about /P=2) will be provided in subsequent documentation.

A.G.
WaveMetrics, Inc.