Consider n vectors (or lists) v1, ..., vn of equal length, an n-
variate function f, and a bivariate function g.
I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
(reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
but without the intermediate vector (my vectors are long). I can of
course write a function to do this, but I am wondering if there is a
clever way to do it with CL library functions.
Thanks,
Tamas
Tamas wrote:
> Consider n vectors (or lists) v1, ..., vn of equal length, an n-
> variate function f, and a bivariate function g.
>
> I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
>
> (reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
>
> but without the intermediate vector (my vectors are long). I can of
> course write a function to do this, but I am wondering if there is a
> clever way to do it with CL library functions.
>
> Thanks,
>
> Tamas
For that simple example:
(loop for a across #(1 2 3)
for b across #(4 5 6)
sum (* a b))
=> 32
On May 23, 9:23 am, Tamas <······@gmail.com> wrote:
> Consider n vectors (or lists) v1, ..., vn of equal length, an n-
> variate function f, and a bivariate function g.
>
> I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
>
> (reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
>
> but without the intermediate vector (my vectors are long). I can of
> course write a function to do this, but I am wondering if there is a
> clever way to do it with CL library functions.
My GMAP macro is good for this kind of thing:
(gmap :sum #'* (:list '(1 2 3)) (:list 4 5 6))
You can find it at
http://common-lisp.net/project/misc-extensions/
-- Scott
On May 23, 11:57 pm, Scott Burson <········@gmail.com> wrote:
> On May 23, 9:23 am, Tamas <······@gmail.com> wrote:
>
> > Consider n vectors (or lists) v1, ..., vn of equal length, an n-
> > variate function f, and a bivariate function g.
>
> > I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
>
> > (reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
>
> > but without the intermediate vector (my vectors are long). I can of
> > course write a function to do this, but I am wondering if there is a
> > clever way to do it with CL library functions.
>
> My GMAP macro is good for this kind of thing:
>
> (gmap :sum #'* (:list '(1 2 3)) (:list 4 5 6))
>
> You can find it at
>
> http://common-lisp.net/project/misc-extensions/
>
> -- Scott
... and SERIES should do it as well.
Cheers
Marco
On May 24, 2:44 pm, Marco Antoniotti <·······@gmail.com> wrote:
> On May 23, 11:57 pm, Scott Burson <········@gmail.com> wrote:
>
>
>
> > On May 23, 9:23 am, Tamas <······@gmail.com> wrote:
>
> > > Consider n vectors (or lists) v1, ..., vn of equal length, an n-
> > > variate function f, and a bivariate function g.
>
> > > I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
>
> > > (reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
>
> > > but without the intermediate vector (my vectors are long). I can of
> > > course write a function to do this, but I am wondering if there is a
> > > clever way to do it with CL library functions.
>
> > My GMAP macro is good for this kind of thing:
>
> > (gmap :sum #'* (:list '(1 2 3)) (:list 4 5 6))
>
> > You can find it at
>
> > http://common-lisp.net/project/misc-extensions/
>
> > -- Scott
>
> ... and SERIES should do it as well.
>
> Cheers
>
> Marco
Thanks Marco (and others),
I ended up using SERIES, as I need general functions (summing was just
an example, but I guess that wasn't clear from my post).
Tamas
In article
<····································@m3g2000hsc.googlegroups.com>,
Tamas <······@gmail.com> wrote:
> Consider n vectors (or lists) v1, ..., vn of equal length, an n-
> variate function f, and a bivariate function g.
>
> I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
>
> (reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
>
> but without the intermediate vector (my vectors are long). I can of
> course write a function to do this, but I am wondering if there is a
> clever way to do it with CL library functions.
>
> Thanks,
>
> Tamas
(let ((sum 0))
(map nil
(lambda (a b)
(incf sum (* a b)))
'(1 2 3) '(4 5 6))
sum)
--
http://lispm.dyndns.org/
Tamas <······@gmail.com> wrote:
> Consider n vectors (or lists) v1, ..., vn of equal length, an n-
> variate function f, and a bivariate function g.
>
> I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
>
> (reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
>
> but without the intermediate vector (my vectors are long). I can of
> course write a function to do this, but I am wondering if there is a
> clever way to do it with CL library functions.
I don't think there is a way to do this sort of thing directly. It's
not difficult (or even particularly ugly) to do using LOOP, though. For
the case G = #'+, it's particularly nice:
(loop for x across first-vector
for y across second-vector
sum (funcall f x y))
but for general G, you need
(loop for x across first-vector
for y across second-vector
for temp = (funcall f x y)
for result = temp then (funcall result temp)
finally (return result))
(This only works if the input vectors are non-empty; you need to do
something a little messier otherwise.)
-- [mdw]
On May 23, 12:23 pm, Tamas <······@gmail.com> wrote:
> Consider n vectors (or lists) v1, ..., vn of equal length, an n-
> variate function f, and a bivariate function g.
>
> I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
>
> (reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) ; => 32
>
> but without the intermediate vector (my vectors are long). I can of
> course write a function to do this, but I am wondering if there is a
> clever way to do it with CL library functions.
>
> Thanks,
>
> Tamas
If any of the input vectors are modifiable (that is, you're willing to
change their entries), you can use map-into to avoid making a new
vector:
(let ((v1 #(1 2 3))
(v2 #(4 5 6))) ; v2 can be modified
(reduce #'+ (map-into v2 #'* v1 v2)))
=> 32
In article
<····································@2g2000hsn.googlegroups.com>,
Joshua Taylor <···········@gmail.com> wrote:
> On May 23, 12:23�pm, Tamas <······@gmail.com> wrote:
> > Consider n vectors (or lists) v1, ..., vn of equal length, an n-
> > variate function f, and a bivariate function g.
> >
> > I want to calculate (reduce g (map 'vector f v1 v2 ... vn)), eg
> >
> > (reduce #'+ (map 'vector #'* '(1 2 3) '(4 5 6))) �; => 32
> >
> > but without the intermediate vector (my vectors are long). �I can of
> > course write a function to do this, but I am wondering if there is a
> > clever way to do it with CL library functions.
> >
> > Thanks,
> >
> > Tamas
>
> If any of the input vectors are modifiable (that is, you're willing to
> change their entries), you can use map-into to avoid making a new
> vector:
>
> (let ((v1 #(1 2 3))
> (v2 #(4 5 6))) ; v2 can be modified
> (reduce #'+ (map-into v2 #'* v1 v2)))
>
> => 32
Generally that's right. But I fear in this specific example
#(4 5 6) is constant data and you are not allowed to modify
it according to ANSI CL. #(4 5 6) is contant data and
'#(4 5 6) is also constant data.
Whenever you are using direct syntax for the data in the source
you'll end up with 'constant data'. One should not modify it
in portable programs.
You get a modifiable vector from calling (VECTOR 4 5 6).
--
http://lispm.dyndns.org/