473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Finding a cross /dot product of two vectors

440 256MB
Hi,

Is there built in Math function is available to find the cross or dot product of two vectors?.



Thanks
PSB
Apr 10 '07 #1
3 17065
ghostdog74
511 Expert 256MB
Hi,

Is there built in Math function is available to find the cross or dot product of two vectors?.



Thanks
PSB
you can use operator module
then you can sum then up after multiplying them together.
eg
Expand|Select|Wrap|Line Numbers
  1. >>> a = (1,2,3)
  2. >>> b = (4,5,6)
  3. >>> reduce( operator.add, map( operator.mul, a, b))
  4. 32
  5. >>>                   
  6.  
Apr 10 '07 #2
bartonc
6,596 Expert 4TB
Hi,

Is there built in Math function is available to find the cross or dot product of two vectors?.



Thanks
PSB
The NumPy package provide all the vector math that you will ever need. Also check out SciPy for all your advanced scientific computing needs.
Apr 10 '07 #3
bvdet
2,851 Expert Mod 2GB
The NumPy package provide all the vector math that you will ever need. Also check out SciPy for all your advanced scientific computing needs.
Barton's suggestion is probably the way to go, but if you want to do your own, you may get what you need from this simple 3D vector class I wrote a few motths ago:
Expand|Select|Wrap|Line Numbers
  1. class Point(object):
  2.     def __init__(self, x=0.0, y=0.0, z=0.0):
  3.         self.x = float(x)
  4.         self.y = float(y)
  5.         self.z = float(z)
  6.  
  7.     def __str__(self):
  8.         return '(%0.4f, %0.4f, %0.4f)' % (self.x, self.y, self.z)
  9.  
  10.     def __add__(self, other):
  11.         return Point(self.x + other.x, self.y + other.y, self.z + other.z)
  12.  
  13.     def __sub__(self, other):
  14.         return Point(self.x - other.x, self.y - other.y, self.z - other.z)
  15.  
  16.     def __mul__(self, f):
  17.         return Point(self.x * f, self.y * f, self.z * f)
  18.  
  19.     def __repr__(self):
  20.         return 'Point(%0.8f, %0.8f, %0.8f)' % (self.x, self.y, self.z)
  21.  
  22.     def dot_prod(self, other):
  23.         return self.x*other.x + self.y*other.y + self.z*other.z
  24.  
  25.     def uv(self):
  26.         return Point(self.x/self.magnitude(), self.y/self.magnitude(), self.z/self.magnitude())
  27.  
  28.     def cp(self, other):
  29.         return Point(self.y*other.z - self.z*other.y, self.z*other.x - self.x*other.z, self.x*other.y - self.y*other.x)
  30.  
  31.     def magnitude(self):
  32.         return (self.x**2 + self.y**2 + self.z**2)**0.5
  33.  
  34. p1 = Point(2.5, 3.5)
  35. print "p1 =", p1
  36. print "repr(p1) =", repr(p1)
  37. print "eval(repr(p1)) =", eval(repr(p1))
  38. p2 =  p1 + (Point(2.5, 3.5, 7.7))
  39. print "p2 =", p2
  40. p3 = p2 - p1
  41. print "p3 =", p3
  42. p4 = p2 * 6
  43. print "p4 =", p4
  44. d34 = p3.uv().dot_prod(p4.uv())
  45. print "p3_uv dot p4_uv = d34 =", d34
  46. print "p4 unit vector =", p4.uv()
  47. print "p4 magnitude =", p4.magnitude()
  48. print 'p3 cross product p4 =', p3.cp(p4)
Expand|Select|Wrap|Line Numbers
  1. >>> p1 = (2.5000, 3.5000, 0.0000)
  2. repr(p1) = Point(2.50000000, 3.50000000, 0.00000000)
  3. eval(repr(p1)) = (2.5000, 3.5000, 0.0000)
  4. p2 = (5.0000, 7.0000, 7.7000)
  5. p3 = (2.5000, 3.5000, 7.7000)
  6. p4 = (30.0000, 42.0000, 46.2000)
  7. p3_uv dot p4_uv = d34 = 0.94562842201
  8. p4 unit vector = (0.4331, 0.6063, 0.6669)
  9. p4 magnitude = 69.2707730576
  10. p3 cross product p4 = (-161.7000, 115.5000, 0.0000)
  11. >>>
Apr 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Bernhard Hidding | last post by:
Hello, I'm a beginner in c++ and I would like to implement some math operations into my program. Explicitly, would like to perform cross product calculations. Is there some library or predefined...
4
by: David Peach | last post by:
Hello, hope somebody here can help me... I have a query that lists defects recorded in a user defined date range. That query is then used as the source for a Cross Tab query that cross-tabs count...
7
by: Efrain Marrero | last post by:
i want to now how to do this in python this is java for(int i=1 ; i<=lim ; i++){ for(int j=i+1; j<=lim+1; j++){ for(int k =j+1; k<=lim+2;k++){
5
by: Andy Leszczynski | last post by:
Hi, Short question: why (1,"abc",0.3)+(2,"def",10.2) != (3,"abcdef",10.5)? How to elegantly achieve (3,"abcdef",10.5) as a result of addition ... Andy
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
10
by: Christopher Nelson | last post by:
I've been developing a little web page full of JavaScript while using Firefox and it works well but when I try to view it in Opera and IE, it's incomplete. I suspect cross-browser issues in the...
3
nomad
by: nomad | last post by:
When I run choice == 2 I'm suppose to a an out.println back finding a product. but I get Null for all the values. Can someone help me with this. class Computer_listing { private String id; ...
5
by: thelightkeeper | last post by:
Hi, I have 1 table contains about 4 millions entries structure like below: ( AlarmID int, SetTime datetime )
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.