# 2/ Linearity


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

## Simplest linear function


```python
b, m = symbols('b m')

def f(x):
    return m*x
```


```python
f(1)
```


```python
f(2)
```


```python
f(1+2)
```


```python
f(1) + f(2)
```


```python
expand(f(x+y)) ==  f(x) + f(y)
```




    True



## What about vector inputs?


```python
m_1, m_2 = symbols('m_1 m_2')

def T(vec):
    """A function that takes a 2D vector and returns a number."""
    return m_1*vec[0] + m_2*vec[1]
```


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


```python
T(u)
```


```python
T(v)
```


```python
T(u) + T(v)
```


```python
expand( T(u+v) )
```


```python
simplify( T(alpha*u + beta*v) - alpha*T(u) - beta*T(v) )
```

# Linear transformations

A linear transformation is function that takes vectors as inputs, and produces vectors as outputs:

$$
   T: \mathbb{R}^n \to \mathbb{R}^m.
$$

See [page 136](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=43) in v2.2 of the book.


```python
m_11, m_12, m_21, m_22 = symbols('m_11 m_12 m_21 m_22')

def T(vec):
    """A linear transformations R^2 --> R^2."""
    out_1 = m_11*vec[0] + m_12*vec[1]
    out_2 = m_21*vec[0] + m_22*vec[1]
    return Matrix([out_1, out_2])
```


```python
T(u)
```


```python
T(v)
```


```python
T(u+v)
```

## Linear transformations as matrix-vector products 

See [page 133](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=40) in v2.2 of the book.


```python
def T_impl(vec):
    """A linear transformations implemented as matrix-vector product."""
    M_T = Matrix([[m_11, m_12], 
                  [m_21, m_22]])
    return M_T*vec
```


```python
T_impl(u)
```


```python

```

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