Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: total addition within an array. (any function or macro?)

2 views
Skip to first unread message

sun...@cad.strath.ac.uk

unread,
Dec 15, 2000, 10:20:11 AM12/15/00
to
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/

Kalle Olavi Niemitalo

unread,
Dec 15, 2000, 11:03:43 AM12/15/00
to
sun...@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.

Frode Vatvedt Fjeld

unread,
Dec 15, 2000, 11:24:19 AM12/15/00
to
sun...@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

Janis Dzerins

unread,
Dec 15, 2000, 11:25:58 AM12/15/00
to
sun...@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.

Joe Marshall

unread,
Dec 15, 2000, 11:57:01 AM12/15/00
to
sun...@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! =-----

sun...@cad.strath.ac.uk

unread,
Dec 15, 2000, 12:24:05 PM12/15/00
to
Thanks =)

sun...@cad.strath.ac.uk

unread,
Dec 15, 2000, 12:22:20 PM12/15/00
to
Thanks =)

sun...@cad.strath.ac.uk

unread,
Dec 15, 2000, 12:21:41 PM12/15/00
to
Thanks =)

Johan Kullstam

unread,
Dec 15, 2000, 12:46:12 PM12/15/00
to
sun...@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
[kull...@ne.mediaone.net]
sysengr

sun...@cad.strath.ac.uk

unread,
Dec 15, 2000, 12:40:13 PM12/15/00
to

> 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 =)

Erik Naggum

unread,
Dec 15, 2000, 12:42:47 PM12/15/00
to
* sun...@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!

sun...@cad.strath.ac.uk

unread,
Dec 15, 2000, 1:18:44 PM12/15/00
to

> 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, =)

0 new messages