http://askubuntu.com/questions/359254/how-to-install-numpy-and-scipy-for-python
Ok, lets follow the installation guide:
- It says you need python 2.7 (which you already have):
$ python --version Python 2.7.4
- Then it says that you need the numpy package too, version >= 1.4.1:
apt-cache policy python-numpy python-numpy: Installed: (none) Candidate: 1:1.7.1-1ubuntu1 Version table: 1:1.7.1-1ubuntu1 0 500 http://archive.ubuntu.com/ubuntu/ raring/main amd64 Packages
As you can see, I have available numpy version 1.7.1, so lets proceed to install it:
sudo apt-get install python-numpy
- Now it says that we need cython, lets check if that package is availabe:
apt-cache policy cython cython: Installed: (none) Candidate: 0.17.4-0ubuntu1 Version table: 0.17.4-0ubuntu1 0 500 http://archive.ubuntu.com/ubuntu/ raring/main amd64 Packages
We have it, we install it:
sudo apt-get install cython
Please, do notice that there are other packages that are dependency that are being installed too.
- Oddly enough, we also need the scipy module too:
sudo apt-get install python-scipy
- Testing. Open python in a terminal and type the following:
$ python Python 2.7.4 (default, Sep 26 2013, 03:20:26) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> import scipy >>> import cython >>> exit()
The above, has to be without errors. If something went wrong, go up and read the guide again, you forgot/skiped a step.
original from http://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector
and modified little bit.
import numpy as np import timeit from math import cos, sin, sqrt import numpy.random as nr from scipy import weave def rotation_matrix_weave(axis, theta, mat = None): if mat == None: mat = np.eye(3,3) support = "#include <math.h>" code = """ double x = sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]); double a = cos(theta / 2.0); double b = -(axis[0] / x) * sin(theta / 2.0); double c = -(axis[1] / x) * sin(theta / 2.0); double d = -(axis[2] / x) * sin(theta / 2.0); mat[0] = a*a + b*b - c*c - d*d; mat[1] = 2 * (b*c - a*d); mat[2] = 2 * (b*d + a*c); mat[3*1 + 0] = 2*(b*c+a*d); mat[3*1 + 1] = a*a+c*c-b*b-d*d; mat[3*1 + 2] = 2*(c*d-a*b); mat[3*2 + 0] = 2*(b*d-a*c); mat[3*2 + 1] = 2*(c*d+a*b); mat[3*2 + 2] = a*a+d*d-b*b-c*c; """ weave.inline(code, ['axis', 'theta', 'mat'], support_code = support, libraries = ['m']) return mat def rotation_matrix_numpy(axis, theta): ax = axis mat = np.eye(3,3) axis[0] = axis[0]/sqrt(np.dot(ax, ax)) axis[1] = axis[1]/sqrt(np.dot(ax, ax)) axis[2] = axis[2]/sqrt(np.dot(ax, ax)) a = cos(theta/2.) b = -1*axis[0]*sin(theta/2.) c = -1*axis[1]*sin(theta/2.) d = -1*axis[2]*sin(theta/2.) return np.array([[a*a+b*b-c*c-d*d, 2*(b*c-a*d), 2*(b*d+a*c)], [2*(b*c+a*d), a*a+c*c-b*b-d*d, 2*(c*d-a*b)], [2*(b*d-a*c), 2*(c*d+a*b), a*a+d*d-b*b-c*c]]) ax = [1.0,-1.0,0.0] th = np.pi/4 print(rotation_matrix_numpy(ax,th))