X-Received: by 2002:a05:620a:46a7:b0:774:20c8:bf4b with SMTP id bq39-20020a05620a46a700b0077420c8bf4bmr18003qkb.7.1695813451731; Wed, 27 Sep 2023 04:17:31 -0700 (PDT) X-Received: by 2002:a05:6830:1508:b0:6c4:aa6a:c4db with SMTP id k8-20020a056830150800b006c4aa6ac4dbmr458302otp.0.1695813451507; Wed, 27 Sep 2023 04:17:31 -0700 (PDT) Path: csiph.com!tncsrv06.tnetconsulting.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.graphics.apps.gnuplot Date: Wed, 27 Sep 2023 04:17:30 -0700 (PDT) In-Reply-To: Injection-Info: google-groups.googlegroups.com; posting-host=159.197.186.54; posting-account=k7QKYwoAAAAxTCjjeJuy2wn5CWBNXWOL NNTP-Posting-Host: 159.197.186.54 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <01876985-e443-42e8-bdf3-ef3af7b10aa4n@googlegroups.com> Subject: Re: How to: Assign an array[7] variable (with scope local to just one plot command) the array[7] value returned by a function block From: najevi Injection-Date: Wed, 27 Sep 2023 11:17:31 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 6779 Xref: csiph.com comp.graphics.apps.gnuplot:4541 On Tuesday, September 26, 2023 at 11:54:51=E2=80=AFPM UTC+10, najevi wrote: > I am using gnuplot v6 rc1 as I am relying on a function block to implemen= t a non-trivial algorithm.=20 >=20 > I have a function block returning a 7 element array based on each data po= int in a dataset. The function, $f(), consumes just one data point ($1) and= returns an array[7] value. So something like:=20 >=20 > local array x[7] =3D $f($1)=20 >=20 > The data set is not complex - being just one value per line in a file. Th= e length of a dataset can be very large. Currently ~20,000,000 data points = is the longest dataset but I test with datasets of fewer than 600 data poin= ts.=20 >=20 > Ideally I'd assign the return value of the function to an array variable = and then use references to that local array variable within a single plot c= ommand however I cannot see how to achieve such a local array variable assi= gnment within the syntax of a plot command.=20 >=20 > As you can see from the pseudo-code below, I use 5 elements of the return= ed array in a plot command and since I currently do so by calling the funct= ion 5 times I think it is terribly wasteful of CPU cycles. (NOTE: I current= ly use the other 2 elements of the returned array to dramatically skip over= function block code whenever ARGV[1] and ARGC have not changed since the l= ast time the function was called.)=20 >=20 > I'd appreciate learning how to achieve the same result more efficiently.= =20 >=20 > pseudo code to illustrate:-=20 >=20 > =3D =3D =3D start of pseudo code =3D =3D =3D=20 > array lastFNvalue[7] =3D [0,0,0,0,0,0,0]=20 >=20 > function $f << _EOFD=20 > if ( (lastFNvalue[2] =3D=3D ARGC) && (lastFNvalue[1] =3D=3D ARGV[1]) ) { = return lastFNvalue }=20 > :=20 > : # algorithm yielding 5 values from one input value=20 > :=20 > local array s[7] =3D [ ARGV[1], ARGC, azimuth, r, delTheta, delR, density= ]=20 > lastFNvalue =3D s=20 > return s=20 > _EOFD=20 >=20 > :=20 > :=20 >=20 > plot @inFile \=20 > using ($f($1)[3]) : ($f($1)[4]) : ($f($1)[5]) : ($f($1)[6]) : ($f($1)[7])= \=20 > with sectors linecolor palette \=20 > , @inFile \=20 > using ($f($1)[3]+$f($1)[5]/2) : ($f($1)[4]-0.4) : 1 : (-fixT*( ($f($1)[3]= +$f($1)[5]/3) + pi/2 ) ) : ($f($1)[7]) \=20 > with labels nopoint center rotate variable textcolor palette font "Times,= 8" \=20 > , '-' using 1:2:3:4:5 with sectors linecolor variable=20 > 0 0 2 @N 0=20 > EOD=20 > =3D =3D =3D end of pseudo code =3D =3D =3D=20 >=20 > So you can see that elements 3,4,5,6 of the array are used to plot (annul= ar) sectors and element 7 (the density) indexes a color palette to yield a = meaningful color for each radius of this plot .. which clearly uses polar c= oordinates.=20 >=20 > I'd imagined I could use one comma-delimited:=20 > ,=20 > to assign the returned value of $f($1) to an array variable and thereby a= void calling $f() multiple times within the one plot command. When I could = not discover a suitable syntax for doing so I capitulated and settled for c= alling $f() multiple times and simply inserted a conditional test (as shown= above) to bypass the complex algorithm whenever the $1 data value being pa= ssed to $f() has not changed since the last time $f() was called.=20 >=20 > ** Do you know of a legitimate syntax that enables (what hopefully the re= ader will agree is) intuitive/expected behaviour from the pseudo-code I des= cribe below:-=20 >=20 > specifically that plot-element:=20 > local array x[7] =3D $f($1) ,=20 >=20 > =3D =3D =3D=20 > plot @inFile \=20 > local array x[7] =3D $f($1) \=20 > , using (x[3]) : (x[4]) : (x[5]) : (x[6]) : (x[7]) \=20 > with sectors linecolor palette \=20 > , using (x[3]+x[5]/2) : (x[4]-0.4) : 1 : (-fixT*( (x[3]+x[5]/3) + pi/2 ) = ) : (x[7]) \=20 > with labels nopoint center rotate variable textcolor palette font "Times,= 8" \=20 > , '-' using 1:2:3:4:5 with sectors linecolor variable=20 > 0 0 2 @N 0=20 > EOD=20 > =3D =3D =3D=20 >=20 > The intent being that array x[7] has scope local to just that one plot co= mmand and therefore $f($1) is called only the once per data point in any da= ta set.=20 >=20 > Thank you for considering this specific application of gnuplot.=20 >=20 > FWIW:=20 > I did toy with having $f() return a string resembling the "using" syntax:= =20 >=20 > "using (azimuth) : (r) : (delTheta) : (delR) : (density) "=20 >=20 > with a view to substitution of that string variable as a macro via:=20 > @$f($1)=20 > style of dereferencing but that was frustratingly unsuccessful. One satisfactory solution I have found is to use the so-called "comma opera= tor" within the parenthesized 1st-term of a using-clause: using (x =3D $f($1), x[3]) : (x[4]) : (x[5]) : (x[6]) : (x[7])=20 and using (x =3D $f($1), x[3] + x[5]/2) : (x[4]-0.4) : 1 : (-fixT * (x[3] + x[5= ]/2 + pi/2)) : (x[7])=20 reduces the number of times $f() is called to just once per using-clause ..= so this can still mean multiple times per plot command but this is nonethe= less a significant improvement compared to the prior method I was using. Parentheses are important because without parentheses the enclosed expressi= ons will not evaluate each time plot reads a data point/data record. So thi= s helps me to better understand what 'triggers' plot to call $f($1). This s= olution seems robust viz a viz running replot - so that is an added benefit= .=20