Monday, October 23, 2006

Vector Fun

Here is a simple program to help you find the magnitude, dot product, 
cross product of 2 vectors of size 3, and the projection vectors of each.
This is nothing ground breaking, but it shows you how fast you can code
something like this up in python with out much work. Some sample output:

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Enter in vector x: (separated by spaces)
Enter in vector y: (separated by spaces)

--------------------------------------------
ERROR: vectors entered are not the same size
--------------------------------------------


Vectors entered:
x = < 1 , 1 , 1 , 1 >
y = < -1 , 1 , -1 >

Magnitude of x is sqrt(4)=2.0
Magnitude of y is sqrt(3)=1.73205080757

Enter another pair of vectors [y/n]?
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Enter in vector x: (separated by spaces)
Enter in vector y: (separated by spaces)

Vectors entered:
x = < 3 , 2 , 1 >
y = < -1 , -2 , 4 >

Magnitude of x is sqrt(14)=3.74165738677
Magnitude of y is sqrt(21)=4.58257569496

Dot product of x and y is -3

The angle between x and y (in degrees) is: 1.74666507689

The cross product of x and y:
x cross y = < 10 , -13 , -4 >

The projection of y onto x:
proj_x(y) = < -9/14 , -3/7 , -3/14 >

The projection of x onto y:
proj_y(x) = < 1/7 , 2/7 , -4/7 >

Enter another pair of vectors [y/n]?
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Enter in vector x: (separated by spaces)
Enter in vector y: (separated by spaces)

Vectors entered:
x = < 0 , 1 >
y = < -1 , 0 >

Magnitude of x is sqrt(1)=1.0
Magnitude of y is sqrt(1)=1.0

Dot product of x and y is 0

The angle between x and y (in degrees) is: 1.57079632679

The projection of y onto x:
proj_x(y) = < 0 , 0 >

The projection of x onto y:
proj_y(x) = < 0 , 0 >

Enter another pair of vectors [y/n]?
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Enter in vector x: (separated by spaces)
Enter in vector y: (separated by spaces)

--------------------------------------------
ERROR: vectors entered are not the same size
--------------------------------------------


Vectors entered:
x = < 1 , 1 , 1 >
y = < -1 , 1 >

Magnitude of x is sqrt(3)=1.73205080757
Magnitude of y is sqrt(2)=1.41421356237

Enter another pair of vectors [y/n]?
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Enter in vector x: (separated by spaces)
Enter in vector y: (separated by spaces)

Vectors entered:
x = < 1 , 1 , 1 , 1 , 1 >
y = < -1 , 1 , -1 , 1 , -1 >

Magnitude of x is sqrt(5)=2.2360679775
Magnitude of y is sqrt(5)=2.2360679775

Dot product of x and y is -1

The angle between x and y (in degrees) is: 1.77215424759

The projection of y onto x:
proj_x(y) = < -1/5 , -1/5 , -1/5 , -1/5 , -1/5 >

The projection of x onto y:
proj_y(x) = < 1/5 , -1/5 , 1/5 , -1/5 , 1/5 >

Enter another pair of vectors [y/n]?
You did not enter a y/n so we exit anyways.









No comments: