tags:

views:

400

answers:

3

May i know how to find the cross product of 2 different vectors without the use of programming libraries?

E.g given vector a = (1, 2, 3) vector b = (4, 5, 6)

A: 
import numpy as np
a = np.array([1,0,0])  
b = np.array([0,1,0])  
print np.cross(a,b)
poulejapon
'without use of programming libraries'...
Andrew Jaffe
sorry for that.
poulejapon
While not an answer to the requirements, Paul has a point:If you need that kind of things, you really *should* look into numpy!Also, if you are playing with 3D vectors in your studies, check out VPython - it makes visualizing these things immensely easy and fun.
Beni Cherniavsky-Paskin
thanks for the info! Got it!(:
blur959
+5  A: 

are you asking about the formula for the cross product? Or how to do indexing and lists in python?

The basic idea is that you access the elements of a and b as a[0], a[1], a[2], etc. (for x, y, z) and that you create a new list with [element_0, element_1, ...]. We can also wrap it in a function.

On the vector side, the cross product is the antisymmetric product of the elements, which also has a nice geometrical interpretation.

Anyway, it would be better to give you hints and let you figure it out, but that's not really the SO way, so...

def cross(a, b):
    c = [a[1]*b[2] - a[2]*b[1],
         a[2]*b[0] - a[0]*b[2],
         a[0]*b[1] - a[1]*b[0]]

    return c
Andrew Jaffe
thanks for the tag!
blur959
You're welcome (but there's no need to thank each of us individually). However, you could "accept" one of the answers -- hint, hint,...
Andrew Jaffe
A: 

If you want to implement the cross product yourself you may see http://en.wikipedia.org/wiki/Vector%5Fcross%5Fproduct or a math/physics book. Shortly (a1, a2, a3) X (b1, b2, b3) = (a2*b3-a3*b2, a3*b1-a1*b3, a1*b2-a2*b1)

smv
thanks for the info!Appreciate it!
blur959