# 2/ Definitions


```python
# helper code needed for running in colab
if 'google.colab' in str(get_ipython()):
    print('Downloading plot_helpers.py to util/ (only neded for colab')
    !mkdir util; wget https://raw.githubusercontent.com/minireference/noBSLAnotebooks/master/util/plot_helpers.py -P util
```


```python
# setup SymPy
from sympy import *
x, y, z, t = symbols('x y z t')
init_printing()

# setup plotting
%matplotlib inline
import matplotlib.pyplot as mpl
from util.plot_helpers import plot_vec, plot_vecs, autoscale_arrows
```

## SymPy Matrix objects


```python
v = Matrix([1,2,3])
v
```


```python
# define symbolically
v_1, v_2, v_3 = symbols('v_1 v_2 v_3')
v = Matrix([v_1,v_2,v_3])
```


```python
v
```


```python
v.T
```


```python
A = Matrix(
    [   [1,7],
        [2,8], 
        [3,9]   ])
A
```


```python
# define symbolically
a_11, a_12, a_21, a_22, a_31, a_32 = symbols('a_11 a_12 a_21 a_22 a_31 a_32')
A = Matrix([
        [a_11, a_12],
        [a_21, a_22], 
        [a_31, a_32]])
```


```python
A
```

## Vector operations


```python
u_1, u_2, u_3 = symbols('u_1 u_2 u_3')
u = Matrix([u_1,u_2,u_3])
v_1, v_2, v_3 = symbols('v_1 v_2 v_3')
v = Matrix([v_1,v_2,v_3])
alpha = symbols('alpha')

u
```


```python
alpha*u
```


```python
u+v
```


```python
u.norm()
```


```python
uhat = u/u.norm()
uhat
```


```python
u = Matrix([1.5,1])
w = 2*u
uhat = u/u.norm()

fig = mpl.figure()
plot_vecs(u, w, uhat)
autoscale_arrows()
```

### Dot product


```python
u = Matrix([u_1,u_2,u_3])
v = Matrix([v_1,v_2,v_3])

u.dot(v)
```


```python
fig = mpl.figure()
u = Matrix([1,1])
v = Matrix([3,0])
plot_vecs(u,v)
autoscale_arrows()

u_dot_v = u.dot(v)
u_dot_v
```


```python
phi = acos( u.dot(v)/(u.norm()*v.norm()) )
print('angle between u and v is', phi)
u.norm()*v.norm()*cos(phi)
```

### Cross product


```python
u = Matrix([u_1,u_2,u_3])
v = Matrix([v_1,v_2,v_3])

u.cross(v)
```


```python
u = Matrix([1,0,0])
v = Matrix([1,1,0])
w = u.cross(v)      # a vector perpendicular to both u and v

mpl.figure()
plot_vecs(u, v, u.cross(v))

```


```python
print('length of cross product', w.norm())

phi = acos( u.dot(v)/(u.norm()*v.norm()) )

w.norm() == u.norm()*v.norm()*sin(phi)

```

    length of cross product 1





    True



## Projection operation


```python
def proj(vec, d):
    """Computes the projection of vector `vec` onto vector `d`."""
    return d.dot(vec)/d.norm() * d/d.norm()
```


```python
fig = mpl.figure()
u = Matrix([1,1])
v = Matrix([3,0])

pu_on_v = proj(u,v)

plot_vecs(u, v, pu_on_v)


# autoscale_arrows()
ax = mpl.gca()
ax.set_xlim([-1,3])
ax.set_ylim([-1,3])


```

# Matrix operations


```python
a_11, a_12, a_21, a_22, a_31, a_32 = symbols('a_11 a_12 a_21 a_22 a_31 a_32')
A = Matrix([
        [a_11, a_12],
        [a_21, a_22], 
        [a_31, a_32]])
b_11, b_12, b_21, b_22, b_31, b_32 = symbols('b_11 b_12 b_21 b_22 b_31 b_32')
B = Matrix([
        [b_11, b_12],
        [b_21, b_22], 
        [b_31, b_32]])
alpha = symbols('alpha')
```


```python
A
```


```python
A + B
```


```python
alpha*A
```


```python
v_1, v_2 = symbols('v_1 v_2')
v = Matrix([v_1,v_2])

A*v
```


```python
A[:,0]*v[0] + A[:,1]*v[1]
```


```python
A = Matrix([
        [a_11, a_12],
        [a_21, a_22], 
        [a_31, a_32]])
B = Matrix([
        [b_11, b_12],
        [b_21, b_22]])

A*B
```


```python
A.T
```


```python
print('the shape of v is', v.shape)
v
```


```python
print('the shape of v.T is', v.T.shape)
v.T
```


```python
u = Matrix([u_1,u_2,u_3])
v = Matrix([v_1,v_2,v_3])

u.T*v
```


```python
u * v.T
```


```python
A = Matrix([
        [3,       3],
        [2,  S(3)/2]
])
A
```


```python
A.inv()
```


```python
A * A.inv()
```


```python
A.inv() * A
```


```python
B = Matrix([
        [b_11, b_12],
        [b_21, b_22]])
B
```


```python
B.trace()
```


```python
B.det()
```


```python

```

e8df21af6cd8155f84e0364ce55f4be8af68bbfb
ipynb
Jupyter Notebook
chapter02_definitions.ipynb
minireference/noBSLAnotebooks
3d6acb134266a5e304cb2d51c5ac4dc3eb3949b4
MIT
2016-04-20T13:56:02.000Z
2022-03-30T08:55:08.000Z
chapter02_definitions.ipynb
minireference/noBSLAnotebooks
3d6acb134266a5e304cb2d51c5ac4dc3eb3949b4
MIT
2021-07-01T17:00:38.000Z
2021-07-01T19:34:09.000Z
chapter02_definitions.ipynb
minireference/noBSLAnotebooks
3d6acb134266a5e304cb2d51c5ac4dc3eb3949b4
MIT
2017-02-04T05:22:23.000Z
2021-12-28T00:06:50.000Z
Qwen/Qwen-72B
1. YES
2. YES