Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Q: total addition within an array. (any function or macro?)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
sungwoo  
View profile  
 More options Dec 15 2000, 10:30 am
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk
Date: Fri, 15 Dec 2000 15:20:11 GMT
Local: Fri, Dec 15 2000 10:20 am
Subject: Q: total addition within an array. (any function or macro?)
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/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kalle Olavi Niemitalo  
View profile  
 More options Dec 15 2000, 11:15 am
Newsgroups: comp.lang.lisp
From: Kalle Olavi Niemitalo <k...@iki.fi>
Date: 15 Dec 2000 18:03:43 +0200
Local: Fri, Dec 15 2000 11:03 am
Subject: Re: Q: total addition within an array. (any function or macro?)

sung...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frode Vatvedt Fjeld  
View profile  
 More options Dec 15 2000, 11:25 am
Newsgroups: comp.lang.lisp
From: Frode Vatvedt Fjeld <fro...@acm.org>
Date: 15 Dec 2000 17:24:19 +0100
Local: Fri, Dec 15 2000 11:24 am
Subject: Re: Q: total addition within an array. (any function or macro?)

sung...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Janis Dzerins  
View profile  
 More options Dec 15 2000, 11:30 am
Newsgroups: comp.lang.lisp
From: Janis Dzerins <jo...@latnet.lv>
Date: 15 Dec 2000 18:25:58 +0200
Local: Fri, Dec 15 2000 11:25 am
Subject: Re: Q: total addition within an array. (any function or macro?)

sung...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joe Marshall  
View profile  
 More options Dec 15 2000, 11:57 am
Newsgroups: comp.lang.lisp
From: Joe Marshall <j...@content-integrity.com>
Date: 15 Dec 2000 11:57:01 -0500
Local: Fri, Dec 15 2000 11:57 am
Subject: Re: Q: total addition within an array. (any function or macro?)

sung...@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! =-----


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sungwoo  
View profile  
 More options Dec 15 2000, 12:30 pm
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk
Date: Fri, 15 Dec 2000 17:24:05 GMT
Local: Fri, Dec 15 2000 12:24 pm
Subject: Re: Q: total addition within an array. (any function or macro?)
Thanks =)

Sungwoo

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sungwoo  
View profile  
 More options Dec 15 2000, 12:30 pm
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk
Date: Fri, 15 Dec 2000 17:22:20 GMT
Local: Fri, Dec 15 2000 12:22 pm
Subject: Re: Q: total addition within an array. (any function or macro?)
Thanks =)

Sungwoo

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sungwoo  
View profile  
 More options Dec 15 2000, 12:30 pm
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk
Date: Fri, 15 Dec 2000 17:21:41 GMT
Local: Fri, Dec 15 2000 12:21 pm
Subject: Re: Q: total addition within an array. (any function or macro?)
Thanks =)

Sungwoo

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Kullstam  
View profile  
 More options Dec 15 2000, 12:45 pm
Newsgroups: comp.lang.lisp
From: Johan Kullstam <kulls...@ne.mediaone.net>
Date: 15 Dec 2000 12:46:12 -0500
Local: Fri, Dec 15 2000 12:46 pm
Subject: Re: Q: total addition within an array. (any function or macro?)

sung...@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
[kulls...@ne.mediaone.net]
sysengr

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sungwoo  
View profile  
 More options Dec 15 2000, 12:50 pm
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk
Date: Fri, 15 Dec 2000 17:40:13 GMT
Local: Fri, Dec 15 2000 12:40 pm
Subject: Re: Q: total addition within an array. (any function or macro?)

> 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/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Dec 15 2000, 1:17 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: 15 Dec 2000 17:42:47 +0000
Local: Fri, Dec 15 2000 12:42 pm
Subject: Re: Q: total addition within an array. (any function or macro?)
* sung...@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!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
sungwoo  
View profile  
 More options Dec 15 2000, 1:30 pm
Newsgroups: comp.lang.lisp
From: sung...@cad.strath.ac.uk
Date: Fri, 15 Dec 2000 18:18:44 GMT
Local: Fri, Dec 15 2000 1:18 pm
Subject: Re: Q: total addition within an array. (any function or macro?)

> 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/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »