From: ·······@cad.strath.ac.uk
Subject: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <91dcr2$3n$1@nnrp1.deja.com>
Hello,

I would like to get an average value of an array.

Let's say, an Array R := (1 2 3 4 5 6 7 8 9 10)

One condition is, I don't know the total length of an array because it will
be changed every time. Maybe I could write down as (although it looks
complicated..),

(setf total-value 0)
(loop for i from 0 to arr-index
         do (setf total-value (+ total-value (aref arr i)))
         finally (setf ave-value (/ total-value (+ arr-index 1))))

However, I just wonder that is there any function or macro which can add
whole values in an array? I want use this function with one simple line.... I
couldn't find that kind of things, but maybe something exist? Thanks for
advance. =)

Sungwoo


Sent via Deja.com
http://www.deja.com/

From: Joe Marshall
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <itolaahu.fsf@content-integrity.com>
·······@cad.strath.ac.uk writes:

> Hello,
> 
> I would like to get an average value of an array.
> 
> Let's say, an Array R := (1 2 3 4 5 6 7 8 9 10)

  (/ (reduce #'+ R)
     (length R))

Watch out for zero-length arrays.


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
From: ·······@cad.strath.ac.uk
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <91dk3b$6ps$1@nnrp1.deja.com>
Thanks =)

Sungwoo


Sent via Deja.com
http://www.deja.com/
From: Johan Kullstam
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <m31yv9k26z.fsf@sysengr.res.ray.com>
·······@cad.strath.ac.uk writes:

> Hello,
> 
> I would like to get an average value of an array.
> 
> Let's say, an Array R := (1 2 3 4 5 6 7 8 9 10)
> 
> One condition is, I don't know the total length of an array because it will
> be changed every time. Maybe I could write down as (although it looks
> complicated..),

do you mean an array or do you mean a list?

because if it's really an array, you can get its dimension easily.
reduce #'+ can sum a vector.

(defun vector-average (one-dim-array)
  (let ((n (array-dimension vector 0)))
    (/ (reduce #'+ vector) n)))

this can be extended to handle more dimensions, but you get the idea.

> (setf total-value 0)
> (loop for i from 0 to arr-index
>          do (setf total-value (+ total-value (aref arr i)))
>          finally (setf ave-value (/ total-value (+ arr-index 1))))
> 
> However, I just wonder that is there any function or macro which can add
> whole values in an array? I want use this function with one simple line.... I
> couldn't find that kind of things, but maybe something exist? Thanks for
> advance. =)
> 
> Sungwoo
> 
> 
> Sent via Deja.com
> http://www.deja.com/

-- 
J o h a n  K u l l s t a m
[········@ne.mediaone.net]
sysengr
From: ·······@cad.strath.ac.uk
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <91dn9r$9u0$1@nnrp1.deja.com>
> do you mean an array or do you mean a list?

I mean 'ARRAY' as I wrote. =)

>
> because if it's really an array, you can get its dimension easily.
> reduce #'+ can sum a vector.
>
> (defun vector-average (one-dim-array)
>   (let ((n (array-dimension vector 0)))
>     (/ (reduce #'+ vector) n)))
>

Thanks, =)

Sungwoo


Sent via Deja.com
http://www.deja.com/
From: Frode Vatvedt Fjeld
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <2hsnnptzyk.fsf@dslab7.cs.uit.no>
·······@cad.strath.ac.uk writes:

> I would like to get an average value of an array.

LOOP does well at taking sums, and so the average of an array can be
expressed as:

(/ (loop for x across <array> sum x)
   (length <array>))

-- 
Frode Vatvedt Fjeld
From: ·······@cad.strath.ac.uk
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <91dk03$6o4$1@nnrp1.deja.com>
Thanks =)

Sungwoo


Sent via Deja.com
http://www.deja.com/
From: Erik Naggum
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <3185890967472817@naggum.net>
* ·······@cad.strath.ac.uk
| One condition is, I don't know the total length of an array because it
| will be changed every time.  Maybe I could write down as (although it
| looks complicated..),
| 
| (setf total-value 0)
| (loop for i from 0 to arr-index
|          do (setf total-value (+ total-value (aref arr i)))
|          finally (setf ave-value (/ total-value (+ arr-index 1))))
| 
| However, I just wonder that is there any function or macro which can
| add whole values in an array?  I want use this function with one
| simple line.... I couldn't find that kind of things, but maybe
| something exist? Thanks for advance. =)

  I should give you the hints and the pointers, but I'm tired, so here's
  how you compute the average of a vector of numbers:

              (/ (reduce #'+ <vector>) (length <vector>))

  Incidentally, the standard deviation is also very useful if you ever
  work with averages:

(let* ((mean (/ (reduce #'+ <vector>) (length <vector>)))
       (sdev (sqrt (/ (reduce #'+ <vector> :key #'(lambda (elt) (expt (- elt mean) 2)))
		      (1- (length <vector>))))))
  ...)

  As you may have noticed, this is idiomatic Common Lisp for the sum
  operator.

#:Erik
-- 
  The United States of America, soon a Bush league world power.  Yeee-haw!
From: Kalle Olavi Niemitalo
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <iznk891fz8g.fsf@iki.fi>
·······@cad.strath.ac.uk writes:

> (setf total-value 0)
> (loop for i from 0 to arr-index
>          do (setf total-value (+ total-value (aref arr i)))
>          finally (setf ave-value (/ total-value (+ arr-index 1))))

This seems simpler:

(setf ave-value (/ (loop :for i :from 0 :below array-length
                         :summing (aref arr i))
                   array-length))

Or, perhaps you could LOOP ACROSS the array, if it doesn't have extra
elements.
From: ·······@cad.strath.ac.uk
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <91djur$6nm$1@nnrp1.deja.com>
Thanks =)

Sungwoo


Sent via Deja.com
http://www.deja.com/
From: Janis Dzerins
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <87bsudwt0p.fsf@asaka.latnet.lv>
·······@cad.strath.ac.uk writes:

> I would like to get an average value of an array.
> 
> Let's say, an Array R := (1 2 3 4 5 6 7 8 9 10)

If the array you have is vector (ie. one dimensional), you can use
REDUCE:

cl-user(3): (defun average (arr)
              (/ (reduce #'+ arr)
                 (length arr)))
average
cl-user(4): (average #(1 2 3 4 5 6 7 8 9 10))
11/2
cl-user(5): 

> However, I just wonder that is there any function or macro which can add
> whole values in an array?

Is REDUCE what you want?

Janis Dzerins
-- 
  If million people say a stupid thing it's still a stupid thing.
From: ·······@cad.strath.ac.uk
Subject: Re: Q: total addition within an array. (any function or macro?)
Date: 
Message-ID: <91dl1s$7p1$1@nnrp1.deja.com>
> Is REDUCE what you want?

yes, it give me right answer. I little bit worried about the meaning of the
word 'REDUCE'. If 'REDUCE' function actually reduce the array size, it is not
suitable for me... But after took a look the reference my silly worries gone.
=) Thanks =)

Sungwoo


Sent via Deja.com
http://www.deja.com/