# Numpy & Scipy Review

I thought this would be the perfect opportunity to review some of the skills we've covered in Numpy and Scipy and introduce a few new skills.

This notebook will have you write a series of short functions that will perform deisred tasks. You may accomplish this with the Python `def` or `lambda` commands. Each one of your functions should be vector oriented (meaning I can pass in a numpy array and your function should still work). Most of what you'll be doing in this assignment is "wrapping" numpy and scipy functions, but this is designed to get you familiar with numerical processing in Python.

I'll go ahead and add code below for packages you may need. I'll also define several test data items below to refer to in the prompts.

I'll also lay out the function calls for you as well.


```python
# 3rd Part Imports
import numpy as np
import scipy.special
import scipy.linalg
from matplotlib import pyplot as plt

# Test Data
# 1D Data
a1d = np.arange(1, 4)
b1d = np.arange(4, 1, -1)
c1d = np.linspace(1, 2, 3)
x1dLarge = np.linspace(0, 5, 1001)
y1dLarge = np.linspace(0, 4, 501)

# Row Vectors
aRow = a1d[np.newaxis, :]
bRow = b1d[np.newaxis, :]

# Column Vectors
aCol = a1d[:, np.newaxis]
bCol = b1d[:, np.newaxis]

# Matrices
rng = np.random.default_rng(0)
A = rng.integers(0, 11, (3, 3))
B = rng.integers(0, 11, (3, 3))
```

## Example

### Element Wise Addition

An example prompt could read:

> Write a python function called `add` that takes two inputs, `a` and `b`, and adds the values together element-wise.

I'll show you two different ways this could be done here.


```python
# Add Function Definition 1
def add1(a, b):
    return a + b

# Add Function Definition 2
add2 = lambda a, b: a + b
```


```python
# Test the Functions here.
print(add1(A, B))
print(add2(A, B))
```

### Mesh Example

Here is another example of something I could ask similar to the previous example. It has to do with performing an operation over some kind of mesh. That is, consider a problem to be two dimensional, $x \times y$ for example, where $x$ is along the $\hat{i}$ dimension and $y$ is along the $\hat{j}$ direction. This can be solved numerically over a mesh of two 1D vectors `x` and `y`. A prompt may look like the following:

> Write a function that accepts two 1D Numpy vectors with size $m$ and $n$ respectively and returns the 2D value of $x \times y$. Call the function `mult2d`.

Here are my example solutions below.


```python
def mult2d(x, y):
    
    # Get the Gridded/Meshed x and y
    xM, yM = np.meshgrid(x, y)
    
    return xM * yM
```


```python
# My Test for the Code
z = mult2d(x1dLarge, y1dLarge)
_ = plt.contourf(x1dLarge, y1dLarge, z, 20)
_ = plt.xlabel('X')
_ = plt.ylabel('Y')
_ = plt.colorbar()
```

Numpy also has a series of functions called "outer" functions that can do this in a threaded basis, but computationally it is more complicated to understand.


```python
def mult2d(x, y):
    
    return np.multiply.outer(y, x)  # y has to come first since we want it along the rows
```


```python
# My Test for the Code
z = mult2d(x1dLarge, y1dLarge)
_ = plt.contourf(x1dLarge, y1dLarge, z, 20)
_ = plt.xlabel('X')
_ = plt.ylabel('Y')
_ = plt.colorbar()
```

## Assignment

Now, it is your turn take an opportunity to work the problems below.

### Area of a Trapezoid

> Write a function that accepts three values ($b_1$, $b_2$ and $h$) that calculates the area of a trapezoid. Call the function `trap`.

The equation of the trapezoid is

\begin{equation}
    \frac{b_1 + b_2}{2} h.
\end{equation}


```python
# Trapezoid Funcion
def trap(b1, b2, h):
    pass
```


```python
# You may test your function here

```

### Arc Length of Circle

> Write a function called `arc` that accepts the radius and interior angle (in degrees) of a circle which returns the arc length of the circle.

The formula for arc length of the circle is

\begin{equation}
    s = r \theta
\end{equation}

where $r$ is the radius and $\theta$ is the interior angle measured in *radians*. The Numpy [deg2rad](https://docs.scipy.org/doc/numpy/reference/generated/numpy.deg2rad.html) function may be useful.


```python
# Arc Length Function
def arc(r, th):
    pass
```


```python
# You may test your code here

```

### Sum Difference

> Write a Python function called `sumdiff` to calculate the difference between the squared sum of first n natural numbers and the sum of squared first n natural numbers.

The function should implement

\begin{equation}
    x = \left( \sum_{i=1}^N y_i \right)^2 - \sum_{i=1}^N y_i^2
\end{equation}


```python
def sumdiff(N):
    pass
```


```python
# You may test your code here

```

### Quadratic Roots

> Write a function called `quad` that accepts three arrays (`a`, `b` and `c`) and returns the two roots from the coefficients of the equation. If the inputs are of length $N$, the output should be of shape (N, 2).

Part of the solution is implemented below.


```python
def quad(a, b, c):
    
    # Store the plus sol here
    solP = []
    
    # Store the minus sol here
    solM = []
    
    return np.vstack((solP, solM))
```


```python
# You may test your code here

```

### Cross Product

> Write a function called `cross` that computes the cross product between two vectors.


```python
# Cross Product
def cross(a, b):
    pass
```


```python
# You May Test your code here

```

### Eigen Values

> Write a program to compute the eigen values of a matrix.


```python
# Eigen Value Function
def eig(A):
    pass
```


```python
# You may test your code here.

```

## Answer Checking

I will be checking your answers using the code below.


```python
print(trap(a1d, b1d, c1d))
print(arc(a1d, np.pi*c1d))
print(sumdiff(a1d))
print(quad(a1d, b1d, -c1d))
print(cross(a1d, b1d))
print(eig(A, B))
```

bc09d442384fc5304553d73ad9d5aec4533364fb
ipynb
Jupyter Notebook
06-NumpyScipyReview/NumpyScipyReview.ipynb
wwaldron/NumericalPythonGuide
8e0c2947251b9639cbc66d6462dd495c180e3faa
MIT
2021-09-22T02:29:11.000Z
2021-09-22T02:29:11.000Z
06-NumpyScipyReview/NumpyScipyReview.ipynb
wwaldron/NumericalPythonGuide
8e0c2947251b9639cbc66d6462dd495c180e3faa
MIT
06-NumpyScipyReview/NumpyScipyReview.ipynb
wwaldron/NumericalPythonGuide
8e0c2947251b9639cbc66d6462dd495c180e3faa
MIT
Qwen/Qwen-72B
1. YES
2. YES