| | |
Scalar product
Thread Solved |
•
•
Join Date: Dec 2006
Posts: 7
Reputation:
Solved Threads: 2
I need to calcualte the scalar product given two lists of numbers...
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
Python Syntax (Toggle Plain Text)
v1=input("Insert the first vector: ") v2=input("Insert the second vector: ") for i in range(len(v1)): v3=v1[i]*v2[i] 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 1:49 pm. Reason: added code tags
You can code it like this ...
A note:
Please use the [code] and [/code] tag pair to enclose your python code.
Python Syntax (Toggle Plain Text)
v1=input("Insert the first vector (eg, 1,2,3,4): ") v2=input("Insert the second vector (eg. 4,3,2,1): ") try: v3 = [] for i in range(len(v1)): v3.append(v1[i]*v2[i]) print v3 except IndexError: print "vectors/lists need to be equal in length!"
Please use the [code] and [/code] tag pair to enclose your python code.
May 'the Google' be with you!
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 152
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:
or even
Jeff
Thus I would code:
Python Syntax (Toggle Plain Text)
def scalar_prod(v1,v2): s = 0 for i in range(len(v1)): s += v1[i]*v2[i] return s
or even
Python Syntax (Toggle Plain Text)
def scalar_prod(v1,v2): return sum([v1[i]*v2[i] for i in range(len(v1))])
Jeff
•
•
Join Date: Apr 2006
Posts: 156
Reputation:
Solved Threads: 44
Or you could do this
Python Syntax (Toggle Plain Text)
import operator a = (1,2,3,4) b = (4,3,2,1) print sum(map(operator.mul,a,b))
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.
[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 2:33 am.
No one died when Clinton lied.
![]() |
Similar Threads
- trial version need product key??? (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: Merging python and java script
- Next Thread: What's wrong with this code?
Views: 3458 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
application array beginner c++ c/c++ change character class client code command convert coordinates count create csv ctypes database dictionary django dll error examples excel exe extensions fdlib file float format framework ftp function graphics gui homework image images import input library line linux list lists logging loop loops microcontroller mouse mysql mysqldb number numbers output parse parsing path port prime processing program programming py2exe pygame pygtk pyopengl pyqt python random raw_input recursion recursive redirect remote scrolledtext server socket sqlite ssh stdout string strings syntax table terminal text thread threading tkinter transparency tuple tutorial ubuntu unicode variable variables web windows wxpython






