Random numbers

numpy.random.randn

random.randn(d0, d1, ..., dn) ====> Return a sample (or samples) from the “standard normal” distribution.

Parameters d0, d1, …, dn ===> The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.

Random Module

The random module is a built-in module to generate the pseudo-random variables.

The random.random() method returns a random float number between 0.0 to 1.0. The function doesn't need any arguments.

Returning any random integer from 0 to 9

Returning any random integer from 2 to 20 with step 2

Returning any random integer from 2 to 20 with step 3

Returning a list of 5 random numbers in the range of (0, 1000)

Returning a secure random integer number less than 10

Returning only -2 or 2 randomly

Returning randomly selected element from a non-empty sequence

Reorders randomly the elements in a list

The seed() method is used to initialize the random number generator.

The random number generator needs a number to start with (a seed value), to be able to generate a random number.

By default the random number generator uses the current system time.

Use the seed() method to customize the start number of the random number generator.

Note: If you use the same seed value twice you will get the same random number twice.

NumPy is short for Numerical Python

It is a package for numerical computation in python, including:

(1) ndarray, i.e., a multidimensional array providing fast arithmethic operations

(2) Random number generation

(3) Linear algebra

(4) Mathematical functions for fast operations on entire arrays

(5) Tools for writing and reading array data to disk and working with memory wrapped files

(6) Fourier transform capabilities

(7) A C API for connecting NumPy with libraries written in C, C++, and Fortran

C API (Common Application Programming Interface) is an international standard interface that application s can use to communicate directly with ISDN equipment.

Integrated Services Digital Network (ISDN) is a set of communication standards for simultaneous digital transmission of voice, video, data, and other network services over the digitalised circuits of the public switched telephone network.

The C API maked it easy to pass data to external libraries written in a low level language and also for xternal libraries to return data to Python as NumPy arrays. This feature often makes Python a choice for wrapping egacy C/C++/Fortran codebases and giving them an easy to use interface.

ndarray: Multidimensional Array

ndarray is a fast and flexible container for large data sets in python.

Creating ndarrays

Converting regular old lists to nparrays as follows:

Nested sequences (like a list of lists) can be converted into a multidimensional numpy array

number of dimensions, ndim, and shape of an array

Creating an empty array

Creating a unity array

Arithmetic with NumPy Arrays

Summary and Apllications

Creating Arrays

Other mathematical functions:

https://numpy.org/doc/stable/reference/routines.math.html

Array Indexing/Slicing

Derivatives

Integrals

Multi-Dimensional Arrays

Transforming an n-dimensional array to a 1-dimenssional array using ravel() function

Boolean indexing

Element indeing

2D functions

Basic Linear Algebra

Matrix Operations

Systems of Equations

$ x + 2y + 3z = 4$

$ 5x + 6y + 7z = 8$

$ 9x + 10y + 11z = 12$

$ x + 2y = 3$

$ 4x + 5y = 6$

Finding Eigenvalues

$\sigma_x = \left\lceil

\begin{matrix} 0 & 1\\ 1 & 0 \end{matrix}

\right\rceil, \sigma_y = \left\lceil \begin{matrix} 0 & -i\\ i & 0 \end{matrix} \right\rceil, \sigma_z = \left\lceil \begin{matrix} 1 & 0\\ 0 & 1 \end{matrix} \right\rceil$

$\frac{1}{\sqrt{2}} = 0.70710678$

Examples

Question 0

For $f(x) = x$, first plot f(x) versus x and then find the area under f(x) from 0 to 1

Question 1

Let $f(x,y) = e^{-(x^2+y^2)} \cdot \sin(x)$ for $-2 \leq x \leq 2$ and $-2 \leq y \leq 2$

  1. Make a contour plot of $f$
  2. Find the volume $|f(x,y)|$ in the specified $x$ and $y$ range
  3. Find the volume $|f(x,y)|$ only in the region where $\sqrt{x^2+y^2}>0.5$

Question 2

After examining a circuit full of resistors, you find that the voltage at 4 specified points is given by

$ 3V_1 + 2V_2 + 3V_3 + 10V_4 = 4$

$ 2V_1 - 2V_2 + 5V_3 + 8V_4 = 1$

$ 3V_1 + 3V_2 + 4V_3 + 9V_4 = 3$

$ 3V_1 + 4V_2 - 3V_3 - 7V_4 = 2$

Find all the voltages

Question 3

An electric field is given by $\vec{E}(z,t) = E_0 \cos (z-t) \hat{x} + 2E_0 \cos(z-t+\pi/2) \hat{y}$.

  1. Find the magnetic field for all $z \in [0,4\pi]$ and $t \in [0,10]$ using $c\vec{B}(z,t) = \hat{z} \times \vec{E}(z,t)$
  2. Compute the Poynting vector $\vec{S} = \vec{E} \times \vec{B}$ for all $z$ and $t$

$E_x$ as function of $t$ at $z=0$

$E_x$ as function of $z$ at $t=0$

Question 4

Find the solutions to $\left(\frac{d^2}{dx^2} + (10x)^2\right) f = \lambda f$ with boundary conditions $f(0)=f(1)=0$

$\left(\frac{\partial^2}{\partial x^2} + h(x)\right) f(x) = \lambda f(x)$

How the operrator $\left(\frac{\partial^2}{\partial x^2} + h(x)\right)$ can be represented as a matrix

fig20-3.png

$\frac{\partial f}{\partial x}=\frac{f_{i+1}-f_i}{\Delta x}=\frac{f_{i}-f_{i-1}}{\Delta x}$

$\frac{\partial^2 f}{\partial x^2}=\frac{\partial}{\partial x}(\frac{\partial f}{\partial x})=\frac{\frac{f_{i+1}-f_i}{\Delta x}-\frac{f_{i}-f_{i-1}}{\Delta x}}{\Delta x}$

$\frac{\partial^2 f}{\partial x^2}=\frac{\frac{f_{i+1}-f_i-f_i+f_{i-1}}{\Delta x}}{\Delta x}$

$\frac{\partial^2 f}{\partial x^2}=\frac{f_{i+1}+f_{i-1}-2f_i}{(\Delta x)^2}$

$\left[\frac{\partial^2 }{\partial x^2}\right]f_1=\frac{f_{2}+f_{0}-2f_1}{(\Delta x)^2}$

$\left[\frac{\partial^2 }{\partial x^2}\right]f_2=\frac{f_{3}+f_{1}-2f_2}{(\Delta x)^2}$

$\vdots$

$\left[\frac{\partial^2 }{\partial x^2}\right]f_N=\frac{f_{N+1}+f_{N-1}-2f_N}{(\Delta x)^2}$

$\vec{f}_N = \begin{bmatrix} f_{1} \\ f_{2} \\ \vdots \\ f_{N} \end{bmatrix}$

$\frac{\partial ^2}{\partial x ^2}\vec{f}_N = \frac{1}{(\Delta x)^2}\begin{bmatrix}

f_{2}+0-2f_{1} \\
f_{3}+f_{1}-2f_{2} \\
\vdots \\
0+f_{N-1}-2f_N

\end{bmatrix}$

$\frac{\partial ^2}{\partial x ^2}\vec{f}_N = \frac{1}{(\Delta x)^2}

\begin{bmatrix} -2 & 1 & & &\\ 1 & -2 & 1 & & \\ & 1 & -2 & \ddots &\\ & & \ddots & \ddots& 1 \\ & & & 1 & -2 \end{bmatrix}\begin{bmatrix} f_{1} \\ f_{2} \\ f_{3} \\ \vdots \\ f_{N} \end{bmatrix}

$

$[h(x)]\vec{f} =

\begin{bmatrix} h_{1} & & &\\ & h_{2} & & \\ && \ddots & \\ && & h_{N} \end{bmatrix}\begin{bmatrix} f_{1} \\ f_{2} \\ \vdots \\ f_{N} \end{bmatrix}

$

$\left(\frac{\partial^2}{\partial x^2} + h(x)\right) f(x) = \

\left { \frac{1}{(\Delta x)^2} \begin{bmatrix} -2 & 1 & & &\\ 1 & -2 & 1 & & \\ & 1 & -2 & \ddots &\\ & & \ddots & \ddots& 1 \\ & & & 1 & -2 \end{bmatrix} + \begin{bmatrix} h_{1} & & &\\ & h_{2} & & \\ && \ddots & \\ && & h_{N} \end{bmatrix}\right } \begin{bmatrix} f_{1} \\ f_{2} \\ f_{3} \\ \vdots \\ f_{N} \end{bmatrix} \ =\lambda f(x)$