Scalar product

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2006
Posts: 7
Reputation: Shark7 is an unknown quantity at this point 
Solved Threads: 2
Shark7 Shark7 is offline Offline
Newbie Poster

Scalar product

 
0
  #1
Jan 27th, 2007
I need to calcualte the scalar product given two lists of numbers...

  1. v1=input("Insert the first vector: ")
  2. v2=input("Insert the second vector: ")
  3.  
  4. for i in range(len(v1)):
  5. v3=v1[i]*v2[i]
  6. print v3

this code will multiplie all the numbers in list v1 and the numbers in list v2 but the result doesn't come in a list...how can i make a list out of the results?how can i add all the numbers in a list?

i would apreciate if someone could help me...
thanks in advance
Last edited by vegaseat; Jan 27th, 2007 at 2:49 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,070
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Scalar product

 
0
  #2
Jan 27th, 2007
You can code it like this ...
  1. v1=input("Insert the first vector (eg, 1,2,3,4): ")
  2. v2=input("Insert the second vector (eg. 4,3,2,1): ")
  3. try:
  4. v3 = []
  5. for i in range(len(v1)):
  6. v3.append(v1[i]*v2[i])
  7. print v3
  8. except IndexError:
  9. print "vectors/lists need to be equal in length!"
A note:
Please use the [code] and [/code] tag pair to enclose your python code.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Scalar product

 
0
  #3
Jan 27th, 2007
You probably know this, but just to be clear: the scalar product is the single number sum(v1[i]*v2[i] for i in range(len(v1))).

Thus I would code:

  1. def scalar_prod(v1,v2):
  2. s = 0
  3. for i in range(len(v1)):
  4. s += v1[i]*v2[i]
  5. return s

or even
  1. def scalar_prod(v1,v2):
  2. return sum([v1[i]*v2[i] for i in range(len(v1))])

Jeff
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Scalar product

 
0
  #4
Jan 27th, 2007
Or you could do this
  1. import operator
  2. a = (1,2,3,4)
  3. b = (4,3,2,1)
  4. print sum(map(operator.mul,a,b))
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,284
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 177
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Scalar product

 
0
  #5
Jan 28th, 2007
You can also use module numpy:
[php]import numpy
v1 = numpy.array([1, 2, 3, 4])
v2 = numpy.array([4, 3, 2, 1])
# product
print v1*v2 # [4 6 6 4]
# scalar product
print numpy.add.reduce(v1*v2) # 20
[/php]Numpy is a free high speed numeric extension module used by engineers and scientists.
Last edited by sneekula; Jan 28th, 2007 at 3:33 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 7
Reputation: Shark7 is an unknown quantity at this point 
Solved Threads: 2
Shark7 Shark7 is offline Offline
Newbie Poster

Re: Scalar product

 
0
  #6
Feb 7th, 2007
thanks... =)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC