
wave assignment and multiple return statements

ChrLie
I wonder if there is some way to assign the elements of two waves simultaneously with one function, something like the following (silly) MWE:
function [variable out1, variable out2] sub(variable in) out1 = in/2 out2 = in*2 return [out1, out2] end function main() make/O/N=(2,2) W_in=p*q+1, W_out1, W_out1 [W_out1[][], W_out2[][]] = sub(W_in[p][q]) end
Main() doesn't compile. The alternative is to do an explicit loop in Main(), or to split sub() into to two separate functions. Neither seems attractive. The actual use-case here is to split a string expression to return two values.
In case the two returns is not possible, with what you want, would it work to put the return the two strings in a 1x2 text wave function?
W_out1[][] = splittextwave(w_in[p])[q]
October 3, 2024 at 09:23 am - Permalink
I haven't thought about any performance consequences of this, but if you change to inputting and returning waves instead of variables, it will compile and I think do what you want.
October 3, 2024 at 09:38 am - Permalink
JJ, I had considered something like that, to return a one point, two layer wave, but I didn't get this to work.
Ben, the wave approach will not work in my case, which is given below. I want to extract data and error values from string expressions in which the error is reported in terms of last significant digit. The strings are copied & pasted into a 2D text wave. The main function works, I just hoped I could get rid of the nested loops, mainly for aesthetic reasons.
October 4, 2024 at 12:42 am - Permalink
ok, I think I do it like this, but thanks for the comments!
October 4, 2024 at 12:58 am - Permalink
Seems like overhead to go through the split expression function twice.
October 5, 2024 at 09:06 am - Permalink
JJ, thanks, but I think your version will not work for the general case of a 2D text wave as input, at least it doesn't for the example data in my second post. Nonetheless, it inspired my to go back to the earlier thought to return a one point two layer wave. I initially didn't get this to work, because I couldn't fit the r-index into the function call, and it just didn't occur to me at the time that it must be outside (as in your example with [q]), but yes, of course!
Here the 3D return wave version:
October 7, 2024 at 01:35 am - Permalink
In reply to Seems like overhead to go… by jjweimer
the implicit loop also runs SplitExpression twice for each string.
October 7, 2024 at 01:05 pm - Permalink
Tony -- I wonder about the administrative overhead of what I see as (loading + running + unloading)x2 (as per Chris' original) versus (loading + (running)x2 + unloading). Both run twice, but the second option loads/unloads once (as far as I understand the implicit behavior).
October 7, 2024 at 01:36 pm - Permalink
ChrLie's original runs a little faster than the revised version (for me the second method is about 30% slower). jjweimer's version, after adjusting to work on all the values in the 2D input wave, runs at about the same speed as ChrLie's revised version.
multithreading (by simply adding the threadsafe keyword before the function definition and multithread before the wave assignment) will make a difference, even for as few as 100 values in the input wave. About a factor of three speed difference for me.
implicit loops are appealingly succinct, but not necessarily more efficient than multiple assignments. I have caught myself putting conditionals in an implicit loop that don't depend on p [q, r,..], which is just bad.
October 8, 2024 at 02:26 am - Permalink