• Average of a list

    From B. Pym@21:1/5 to All on Thu Jun 6 03:31:29 2024
    SP-Forth:

    REQUIRE list-all ~ygrek/lib/list/all.f
    list ALSO!
    REQUIRE CASE-INS lib/ext/caseins.f

    0 0
    %[ 10 % 20 % 30 % 40 % 50 % 60 % 70 % 80 % 90 % ]%
    :noname ( sum count n -- sum2 count2 ) 1 d+ ;
    iter
    / .

    ===>
    50

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to All on Thu Jun 6 06:17:32 2024
    : %[ 0 ; : % + ; : ]% . ;
    10 % 20 % 30 % 40 % 50 % 60 % 70 % 80 % 90 % 450 ok

    ( I don't know what your itered operation is supposed to be ).

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ahmed@21:1/5 to All on Thu Jun 6 06:46:04 2024
    Hi,

    : %[ 0 0 ; : % swap 1+ swap rot + swap ; : ]% / . ;

    %[ 10 % 20 % 30 % 40 % 50 % 60 % 70 % 80 % 90 % ]%
    gives 50

    Why all the requires to do just the mean?

    Ahmed

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mhx@21:1/5 to All on Thu Jun 6 08:47:38 2024
    FORTH> 0 value n : %[ 0 clear n ; : % + 1 +TO n ; : ]% n / . ; ok
    FORTH> %[ 10 % 20 % 30 % 40 % 50 % 60 % 70 % 80 % 90 % ]% 50 ok

    -marcel

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ahmed@21:1/5 to All on Thu Jun 6 08:34:13 2024
    Or better

    : %[ 0 0 ; : % rot + swap 1+ ; : ]% / . ;

    %[ 10 % 20 % 30 % 40 % 50 % 60 % 70 % 80 % 90 % ]%

    gives 50

    Ahmed

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to All on Sat Jun 8 10:49:34 2024
    Pym should have shown an example of DFT over a vector in Lisp,
    without loading some "fft" library.

    Otherwise "garbage in, garbage out".

    BTW, the Maxima CAS is written in Lisp.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ahmed@21:1/5 to All on Sat Jun 8 12:21:59 2024
    Some time ago, I wrote a gforth script for vector stack and defined some functions and operations on it.
    for example:

    1e 10e vvrange vvmean f. 5. ok
    1e 10e vvrange 10e vv*s vvmean f. 50. ok <---------- here, a solution
    for this thread
    1e 10e vvrange 10e vv*s 100e vv+s vvmean f. 150. ok

    the functions defined on the vector stack are the vectorized versions of
    the same functions on floats, for example:

    : vvfunction create ' , does> @ is function vstack-1 vstack-top 2dup (vfunction) vstack+1 ;

    vvfunction vvsin fsin
    vvfunction vvcos fcos
    vvfunction vvexp fexp

    and so on.

    An example of application:

    1e 10e vvrange 0.1e vv*s -0.5e vv+s vvsin vv|.
    -0.389418342308651
    -0.29552020666134
    -0.198669330795061
    -0.0998334166468281
    0.
    0.0998334166468282
    0.198669330795061
    0.29552020666134
    0.389418342308651 ok

    that gives sine of values in -0.5e to 0.5e with a step 0.1e


    Ahmed

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ahmed@21:1/5 to All on Sat Jun 8 12:24:58 2024
    I would say:
    from -0.4e to 0.4e with step 0.1e

    Ahmed

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to All on Tue Jun 18 18:29:08 2024
    I also use an array stack, with some syntactic sugar:

    MinForth 3.6 (32 bit) (fp matrix)
    # m[ 10 20 30 40 50 60 70 80 90 ] ok
    M: <1> | # m.
    [ 10 20 30 40 50 60 70 80 90 ] ok
    M: <1> | # ' f+ mreduce ok
    M: <1> | f: 450 | # mnumel s>f f/ ok
    M: <1> | f: 50 | # ok \ <- average
    # mdrop fdrop linspace[ -0.4 +0.4 9 ] ok
    M: <1> | # m.
    [ -0.4 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 ] ok
    M: <1> | # ' fsin mmap mtrans m. ok
    [ -0.389418
    -0.29552
    -0.198669
    -0.0998334
    0
    0.0998334
    0.198669
    0.29552
    0.389418 ] ok
    M: <2> | #

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From albert@spenarnc.xs4all.nl@21:1/5 to minforth on Wed Jun 19 11:00:55 2024
    In article <0de13463afd27f9e6b5693952ccd30dc@www.novabbs.com>,
    minforth <minforth@gmx.net> wrote:
    I also use an array stack, with some syntactic sugar:

    MinForth 3.6 (32 bit) (fp matrix)
    # m[ 10 20 30 40 50 60 70 80 90 ] ok
    M: <1> | # m.
    [ 10 20 30 40 50 60 70 80 90 ] ok
    M: <1> | # ' f+ mreduce ok
    M: <1> | f: 450 | # mnumel s>f f/ ok
    M: <1> | f: 50 | # ok \ <- average
    # mdrop fdrop linspace[ -0.4 +0.4 9 ] ok
    M: <1> | # m.
    [ -0.4 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 ] ok
    M: <1> | # ' fsin mmap mtrans m. ok
    [ -0.389418
    -0.29552
    -0.198669
    -0.0998334
    0
    0.0998334
    0.198669
    0.29552
    0.389418 ] ok
    M: <2> | #

    An array stack, a string stack ...

    Honestly I think for these it is preferred to use a heap aka ALLOCATE.
    Maybe it is a hassle to FREE them but that at least give more
    flexibility. And then there is the possibility of having sloppy
    garbage collectors, the once that inspect each possible pointer to
    heap space and if they are pointing in heap space, keep the item just in case.

    Addition of those is valuable for a host of other things.
    --
    Don't praise the day before the evening. One swallow doesn't make spring.
    You must not say "hey" before you have crossed the bridge. Don't sell the
    hide of the bear until you shot it. Better one bird in the hand than ten in
    the air. First gain is a cat purring. - the Wise from Antrim -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to All on Wed Jun 19 09:46:27 2024
    You are right, of course. The array stack does not contain
    vector/matrix data, but fat pointers (structs) to (re)allocated
    heap objects (the matrix elements). Sizing is done automatically.
    By the way, dynamic strings are treated like character vectors.

    At startup, the rather small array stack is preallocated from
    bottom to top with pointers and yet empty objects. On exit,
    the heap objects are freed by a loop that traverses the stack.
    The same procedure is used to external VECTOR or MATRIX values,
    which are linked for this purpose.

    Individual heap objects can be freed by setting the referencing
    stack element or external value to NIL. There is no garbage
    collector. I tried reference counting, but it was not worth the
    hassle because my applications rarely have more than about few
    dozen external objects alive at the same time.

    I can't remember ever having a memory leak because all the
    heap objects are referenced through the stack or through the
    chain of external VECTOR/MATRIX values. So all heap objects
    can be found and managed easily.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)