Re: [Axiom-developer] Summing over a list

Martin Rubey <[email protected]>
Newsgroups gmane.comp.mathematics.axiom.general
Message-ID <[email protected]>
Dear Alasdair,

(redirecting to axiom-math, where usage questions belong)

"Alasdair McAndrew" <[email protected]> writes:

> Given two lists,for example:
> 
> z:=[1,2,3,4,5,6]
> w:=[1,0,0,1,1,1]
> 
> it seems that the sum of the pairwise product is obtained by
> 
> reduce(+,[z.i*w.i for i in 1..6])3B

yes. Or, if you prefer

reduce(+, [ez*ew for ez in z for ew in w])

or, if you are really thinking of the standard inner product

dot(z, w)

> which is all very well.  But why doesn't
> 
> sum(z.i*w.i,i=1..6)
> 
> work?  I mean, sum(i^2,i=1..6) is fine.

For a (very long) explanation see

http://wiki.axiom-developer.org/IndexedVariables

Short explanation: unlike Mma or Maple, in axiom evaluation is extremely
simple: first the arguments are evaluated, then the function.  So, in your
case, first axiom tries to evaluate

  z.i*w.i

and

  i=1..6

but there is no operation elt (which the dot is syntactig sugar for) that takes
a list and a symbol -- try to type z.i into the interpreter!  To make things
clearer, note that there *is* an operation "=" that takes a symbol and a
segment.

Hope that helps,

Martin
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.