Scientific computing involves the following:
NumPy does not have array data type in the base language, instead it has List. Lists are versatile but inefficient for numerical computations. Required packages are:
range(n) - Create a list starting from zero, up to but not including n, incremeting at 1range(n1, n2) - Create a list starting from n1, up to but not including n2, incrementing at 1range(n1, n2, delta) - Create a list starting from n1, up to but not including n2, incrementing at delta, where delta is an integer which is positive if n1 < n2 and negative if n2 < n1.
In [1]:
a = range(10)
print a, type(a)
In [2]:
b = range(1, 10)
print b, type(b)
In [3]:
c = range(2, 10, 2)
print c, type(c)
In [9]:
d = range(10, 0)
print d, type(d)
In [10]:
d = range(10, 0, -2)
print d, type(d)
In [4]:
from numpy import arange
a = arange(5)
print a, type(a)
In [5]:
b = arange(1, 5)
print b, type(b)
In [6]:
c = arange(1, 5, 0.5)
print c, type(c)
In [7]:
d = arange(10, 5)
print d, type(d)
In [8]:
d = arange(5, 0, -0.5)
print d, type(d)
In [9]:
from numpy import linspace
a = linspace(0, 5, 11)
print a, type(a), a.ndim
In [10]:
b = linspace(5, 0, 11)
print b, type(b), b.ndim
In [11]:
from numpy import pi
x = linspace(0, 2*pi, 501)
print type(x), x.ndim, x.shape, x.size
In [12]:
from numpy import array
a = array([1, 2, 3, 4, 5])
print a, type(a), a.ndim, a.shape, a.size
To create a two dimensioned array, use a two dimensioned list
In [13]:
b = array([ [1, 2, 3], [4, 5, 6] ])
print b, type(b), b.ndim, b.shape, b.size
zeros() - array with all elements zerosones() - array with all elements oneseye() - Identity matrixdiag() - Matrix with elements in given list on main diagonal or parallel to main diagonalWhen creating arrays this way, keep in mind the following points:
In [14]:
import numpy as np
a = np.zeros((3,5), dtype=float)
print a, type(a), a.ndim, a.shape, a.size
In [15]:
b = np.ones((5,3), dtype=int)
print b, type(b), b.ndim, b.shape, b.size
In [16]:
c = np.eye(5, dtype=float)
print c, type(c), c.ndim, c.shape, c.size
In [17]:
d = np.diag([10, 20, 30])
print d, type(d), d.ndim, d.shape, d.size, d.dtype
In [18]:
x = np.diag([1.0, 2.0, 3.0])
print x, type(x), x.ndim, x.shape, x.size, x.dtype
In [19]:
y = np.diag([10, 20, 30], 1)
print y
In [20]:
z = np.diag([10, 20, 30], -1)
print z
In [21]:
a = np.random.rand(3,5)
print a
In [22]:
b = np.random.rand(5, 3) * 10 + 1
print b
In [23]:
a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([ [2, 4, 6], [8, 10, 12], [14, 16, 18] ])
c = a + b
print b
In [24]:
d = a * b
print d
In [25]:
x = 2 * a
print x
In [26]:
y = 10 * a - 2 * b
print y
In [27]:
from matplotlib import pyplot as plt
%matplotlib inline
x = np.linspace(0, 2*np.pi, 501)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.title('Harmonic function')
plt.legend(loc=3)
plt.show()
In [28]:
a = np.array([ [10, 3, -4], [2, 8, -1], [3, 5, -9] ], dtype=float)
b = np.array([25, -20, 30], dtype=float)
x = np.linalg.solve(a, b)
print x
In [29]:
print np.dot(a, x) - b
In [30]:
print np.linalg.det(a)
In [31]:
print np.linalg.norm(a)
In [32]:
w, X = np.linalg.eig(a)
print w
print X