| | |
Scalar product
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
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 2: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: 150
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: 148
Reputation:
Solved Threads: 40
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 3: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?
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






