# mc_compute_stationary_sympy: demonstration

```
import numpy as np
from mc_compute_stationary_sympy import mc_compute_stationary_sympy
```

```
A = [[.4, .6], [.2, .8]]
mc_compute_stationary_sympy(A)
```

    ([1.00000000000000],
     [array([0.250000000000000, 0.750000000000000], dtype=object)])

```
type(_[1][0][0])
```

    sympy.core.numbers.Float

```
B = np.identity(2)
mc_compute_stationary_sympy(B)
```

    ([1.00000000000000],
     [array([1.00000000000000, 0], dtype=object),
      array([0, 1.00000000000000], dtype=object)])

```
C = np.identity(3)
mc_compute_stationary_sympy(C)
```

    ([1.00000000000000],
     [array([1.00000000000000, 0, 0], dtype=object),
      array([0, 1.00000000000000, 0], dtype=object),
      array([0, 0, 1.00000000000000], dtype=object)])

## KMR Markov matrices

Let us try matrices generated by the KMR model
where action 1 is risk-dominant,
so that the theory says that the unique stationary distribution is close to
the distribution that concentrates on the state $N$
(where the state is the number of players playing action 1).

```
from test_mc_compute_stationary import KMR_Markov_matrix_sequential, KMR_Markov_matrix_simultaneous
```

```
P = KMR_Markov_matrix_sequential(N=3, p=1./3, epsilon=1e-14)
eigvals_P, eigvecs_P = mc_compute_stationary_sympy(P)
```

```
eigvals_P, eigvecs_P
```

    ([1.00000000000000, 1.00000000000000],
     [array([-428914250225761., -2.14457125112880, 2.14457125112881,
             428914250225762.], dtype=object),
      array([4.99999999999991e-15, 7.49999999999982e-29, 1.49999999999996e-14,
             0.999999999999980], dtype=object)])

```
[val.evalf(50) for val in eigvals_P]
```

    [1.0000000000000017763568394002504646778106689453125,
     1.0000000000000048849813083506887778639793395996094]

```
[np.dot(vec, P) for vec in eigvecs_P]
```

    [array([-428914250225759., -2.14457125112881, 2.14457125112881,
            428914250225760.], dtype=object),
     array([4.99999999999991e-15, 7.49999999999987e-29, 1.49999999999997e-14,
            0.999999999999980], dtype=object)]

```
Q = KMR_Markov_matrix_simultaneous(N=5, p=1./3, epsilon=1e-15)
eigvals_Q, eigvecs_Q = mc_compute_stationary_sympy(Q)
```

```
eigvals_Q, eigvecs_Q
```

    ([1.00000000000000, 1.00000000000000],
     [array([1.00000000000001, 2.50000000000002e-15, 2.50000000000003e-30,
             -2.79568440577553e-44, -2.51812864961921e-29, -9.07251459847683e-15], dtype=object),
      array([0, 0, 0, 3.08148791101960e-30, 2.77555756156288e-15,
             0.999999999999997], dtype=object)])

```
[val.evalf(50) for val in eigvals_Q]
```

    [1.0000000000000024424906541753443889319896697998047,
     1.0000000000000026645352591003756970167160034179688]

```
[np.dot(vec, Q) for vec in eigvecs_Q]
```

    [array([1.00000000000001, 2.50000000000003e-15, 2.50000000000003e-30,
            -2.67068440577554e-44, -2.51812864961921e-29, -9.07251459847683e-15], dtype=object),
     array([5.27109897161535e-77, 4.74778387287991e-61, 1.71056941445903e-45,
            3.08148791101961e-30, 2.77555756156289e-15, 0.999999999999997], dtype=object)]

```
R = KMR_Markov_matrix_sequential(N=27, p=1./3, epsilon=1e-2)
eigvals_R, eigvecs_R = mc_compute_stationary_sympy(R)
```

```
eigvals_R, eigvecs_R
```

    ([], [])

mc_compute_stationary_sympy fails to return any eigenvalue or eigenvector for this $28 \times 28$ matrix.

## SymPy

mc_compute_stationary_sympy uses `Matrix.eigenvects()` from SymPy.

```
from sympy.matrices import Matrix
```

```
Matrix(P).transpose().eigenvects()
```

    [(1.00000000000000, 1, [Matrix([
       [   -0.999999999999998],
       [-4.99999999999999e-15],
       [              5.0e-15],
       [                  1.0]])]), (1.00000000000000, 1, [Matrix([
       [5.00000000000001e-15],
       [7.49999999999997e-29],
       [1.49999999999999e-14],
       [                 1.0]])]), (0.666666666666665, 1, [Matrix([
       [ 5.00000000000001e-15],
       [-5.00000000000003e-15],
       [                 -1.0],
       [                  1.0]])]), (-3.99638918680497e-18, 1, [Matrix([
       [-0.999999999999998],
       [  2.99999999999999],
       [              -3.0],
       [               1.0]])])]

```
Matrix(Q).transpose().eigenvects()
```

    [(0, 4, [Matrix([
       [-1.0],
       [ 1.0],
       [   0],
       [   0],
       [   0],
       [   0]]), Matrix([
       [   0],
       [   0],
       [-1.0],
       [ 1.0],
       [   0],
       [   0]]), Matrix([
       [   0],
       [   0],
       [-1.0],
       [   0],
       [ 1.0],
       [   0]]), Matrix([
       [   0],
       [   0],
       [-1.0],
       [   0],
       [   0],
       [ 1.0]])]), (1.00000000000000, 1, [Matrix([
       [   -110223024625157.0],
       [   -0.275557561562892],
       [-2.75557561562893e-16],
       [ 3.08148791101961e-30],
       [ 2.77555756156289e-15],
       [                  1.0]])]), (1.00000000000000, 1, [Matrix([
       [                   0],
       [                   0],
       [                   0],
       [3.08148791101961e-30],
       [2.77555756156289e-15],
       [                 1.0]])])]

```
Matrix(R).transpose().eigenvects()
```

    []

```

```


# RL Exercise 2 - Derivative Free Optimization

**GOAL:** The goal if this exercise is to show how to use Ray to implement a simple Monte Carlo algorithm for reinforcement learning.

The goal of reinforcement learning is to find a policy (parameterized by $\pi$), which solves the following optimization problem.

\begin{equation}
\max_{\pi} \sum_{t=0}^T R_t(\pi)
\end{equation}

Here, $R_t$ is the reward received at time $t$ when acting according to the policy $\pi$. Note that if the environment is stochastic or the policy is stochastic, then each $R_t$ will be a random variable. Also note that $T$ will be a random variable. Both $R_t$ and $T$ depend on $\pi$.

Though the setup is similar to supervised learning in that in both settings we want to minimize or maximize some objective function, in supervised learning we often have an explicit formula for the objective function in terms of the parameters of interest, which enables us to symbolically compute the gradient of the objective function. So in supervised learning, we can often directly apply gradient descent to optimize the objective.

In reinforcement learning, we often do not have an explicit formula for the reward function that we are trying to optimize, and so we can't easily compute gradients. For example, imagine an environment in which a robot walks until it falls over and the reward is the distance that the robot walked before it fell over. Computing the gradient of that reward with respect to the parameters of the robot's policy is not straightforward.

The difficulty of computing explicit gradients motivates the use of **derivative free optimization**. We will work through some examples below.

**NOTE:** There is a huge variety of much more sophisticated RL algorithms. Here we are walking through the details of implementing a simple Monte Carlo algorithm. Subsequent exercises will show how to apply more sophisticated algorithms.

```python
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import gym
import numpy as np
import ray
```

Start up Ray.

```python
ray.init()
```

    Waiting for redis server at 127.0.0.1:15494 to respond...
    Waiting for redis server at 127.0.0.1:35580 to respond...
    Warning: Reducing object store memory because /dev/shm has only 66178973696 bytes available. You may be able to free up space by deleting files in /dev/shm. If you are inside a Docker container, you may need to pass an argument with the flag '--shm-size' to 'docker run'.
    Starting local scheduler with 40 CPUs, 0 GPUs

    {'local_scheduler_socket_names': ['/tmp/scheduler26150633'],
     'node_ip_address': '127.0.0.1',
     'object_store_addresses': [ObjectStoreAddress(name='/tmp/plasma_store97764642', manager_name='/tmp/plasma_manager26655009', manager_port=34459)],
     'redis_address': '127.0.0.1:15494',
     'webui_url': ''}

The class below is a policy that chooses an action using a randomly-generated two-layer neural net.

```python
class TwoLayerPolicy(object):
    def __init__(self, num_inputs, num_hiddens, num_outputs=1):
        self.num_inputs = num_inputs
        self.num_hidden_units = num_hiddens
        self.num_outputs = num_outputs
        self.weights1 = np.random.normal(size=(num_hiddens, num_inputs))
        self.biases1 = np.random.normal(size=num_hiddens)
        self.weights2 = np.random.normal(size=(num_outputs, num_hiddens))
        self.biases2 = np.random.normal(size=num_outputs)

    def __call__(self, state):
        hiddens = np.maximum(np.dot(self.weights1, state) + self.biases1, 0)
        output = np.dot(self.weights2, hiddens) + self.biases2
        assert output.size == 1
        return 0 if output[0] < 0 else 1

policy = TwoLayerPolicy(4, 5)
# You can get an action by applying the policy to a state.
action = policy(np.random.normal(size=4))
print(action)
```

    1

The remote function `evaluate_random_policy` defined below generates a random `TwoLayerPolicy`, performs some rollouts using a CartPole environment, and returns the average reward over those rollouts.

```python
# NOTE: You may find the helper function 'rollout_policy' helpful.
# This implementation here is the solution to one of the exercises
# from the previous notebook.
def rollout_policy(env, policy):
    state = env.reset()
    cumulative_reward = 0
    done = False

    # Keep looping as long as the simulation has not finished.
    while not done:
        # Choose an action.
        action = policy(state)
        # Take an action.
        state, reward, done, _ = env.step(action)
        # Update the cumulative reward.
        cumulative_reward += reward

    return cumulative_reward

@ray.remote
def evaluate_random_policy(num_rollouts):
    # Generate a random policy.
    policy = TwoLayerPolicy(4, 5)

    # Create an environment.
    env = gym.make('CartPole-v0')

    # We evaluate the same policy multiple times and then take the average
    # in order to evaluate the policy more accurately (the environment is
    # stochastic).
    return np.mean([rollout_policy(env, policy) for _ in range(num_rollouts)])

average_reward = ray.get(evaluate_random_policy.remote(10))
print(average_reward)
```

    12.7

**EXERCISE:** Using the `evaluate_random_policy` remote function, evaluate 100 randomly generated policies. Make a note of the best score. Try taking the best of 1000.

**NOTE:** The best possible score should be 200.

```python
# Evaluate 100 randomly generated policies.
#raise NotImplementedError

rewards = []
for i in range(1000):
    average_reward = ray.get(evaluate_random_policy.remote(10))
    rewards.append(average_reward)

print(max(rewards))
#Print the best score obtained.
#raise NotImplementedError
```

    200.0

```python

```


```python
import sympy
from sympy import Piecewise, log, ITE, piecewise_fold, exp
from sympy.abc import x, y, z, w, k, j
from sympy import *
from sympy.plotting import plot
```

```python
psi = Piecewise((1, (0 <= x) & (x < 0.5)), (-1, (0.5 <= x) & (x < 1)), (0, True))
f = exp(-abs(x))
psi
_psi = 2**(j/2) * psi.subs(x, 2**j * y - k)
# yo = lambda y: psi.subs(x, y)
```

```python
w_coeffs = []
for i in range(10):
    for q in range(10):
        _psi = 2**(j/2) * psi.subs([(x, 2**j * x - k)])
        p = _psi.subs([(j, i), (k, q)]) * f
        w_coeffs.append(integrate(p, (x, -oo, oo)))

```

```python
# for i in range(10):
#     for q in range(10):
#         w_coeffs.append(w_c.subs([(j, i), (k, q)]))
# print(w_coeffs)

resy = sum([coeff * f for coeff in w_coeffs])
# resy
# w_coeffs[0]
```

```python
resy
```

$\displaystyle \left(- 16 \sqrt{2} \int\limits^{0} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0009765625} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{1}{512}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{0} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.00390625} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{1}{128}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{0} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.015625} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{1}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{0} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{0.0625} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{\frac{1}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{0} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{0.25} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{\frac{1}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \sqrt{2} \int\limits^{\frac{1}{512}} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0029296875} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{1}{256}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \sqrt{2} \int\limits^{\frac{1}{256}} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0048828125} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{3}{512}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \sqrt{2} \int\limits^{\frac{3}{512}} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0068359375} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{1}{128}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \sqrt{2} \int\limits^{\frac{1}{128}} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0087890625} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{5}{512}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{\frac{1}{128}} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.01171875} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{1}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \sqrt{2} \int\limits^{\frac{5}{512}} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0107421875} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{3}{256}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \sqrt{2} \int\limits^{\frac{3}{256}} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0126953125} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{7}{512}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \sqrt{2} \int\limits^{\frac{7}{512}} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0146484375} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{1}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \sqrt{2} \int\limits^{\frac{1}{64}} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0166015625} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{9}{512}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{\frac{1}{64}} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.01953125} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{3}{128}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \sqrt{2} \int\limits^{\frac{9}{512}} e^{- \left|{x}\right|}\, dx + 32 \sqrt{2} \int\limits^{0.0185546875} e^{- \left|{x}\right|}\, dx - 16 \sqrt{2} \int\limits^{\frac{5}{256}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{\frac{3}{128}} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.02734375} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{1}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{\frac{1}{32}} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.03515625} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{5}{128}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{\frac{1}{32}} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.046875} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{1}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{\frac{5}{128}} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.04296875} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{3}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{\frac{3}{64}} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.05078125} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{7}{128}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{\frac{7}{128}} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.05859375} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{1}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{\frac{1}{16}} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.06640625} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{9}{128}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{\frac{1}{16}} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.078125} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{3}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \sqrt{2} \int\limits^{\frac{9}{128}} e^{- \left|{x}\right|}\, dx + 16 \sqrt{2} \int\limits^{0.07421875} e^{- \left|{x}\right|}\, dx - 8 \sqrt{2} \int\limits^{\frac{5}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{\frac{3}{32}} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.109375} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{1}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{\frac{1}{8}} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.140625} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{5}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{\frac{1}{8}} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{0.1875} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{\frac{1}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{\frac{5}{32}} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.171875} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{3}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{\frac{3}{16}} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.203125} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{7}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{\frac{7}{32}} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.234375} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{1}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{\frac{1}{4}} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.265625} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{9}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{\frac{1}{4}} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{0.3125} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{\frac{3}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \sqrt{2} \int\limits^{\frac{9}{32}} e^{- \left|{x}\right|}\, dx + 8 \sqrt{2} \int\limits^{0.296875} e^{- \left|{x}\right|}\, dx - 4 \sqrt{2} \int\limits^{\frac{5}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{\frac{3}{8}} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{0.4375} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{\frac{1}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{\frac{1}{2}} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{0.5625} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{\frac{5}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{\frac{1}{2}} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{0.75} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{1} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{\frac{5}{8}} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{0.6875} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{\frac{3}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{\frac{3}{4}} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{0.8125} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{\frac{7}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{\frac{7}{8}} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{0.9375} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{1} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{1} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{1.0625} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{\frac{9}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{1} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{1.25} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{\frac{3}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \sqrt{2} \int\limits^{\frac{9}{8}} e^{- \left|{x}\right|}\, dx + 4 \sqrt{2} \int\limits^{1.1875} e^{- \left|{x}\right|}\, dx - 2 \sqrt{2} \int\limits^{\frac{5}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{\frac{3}{2}} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{1.75} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{2} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{2} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{2.25} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{\frac{5}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{\frac{5}{2}} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{2.75} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{3} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{3} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{3.25} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{\frac{7}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{\frac{7}{2}} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{3.75} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{4} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{4} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{4.25} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{\frac{9}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- \sqrt{2} \int\limits^{\frac{9}{2}} e^{- \left|{x}\right|}\, dx + 2 \sqrt{2} \int\limits^{4.75} e^{- \left|{x}\right|}\, dx - \sqrt{2} \int\limits^{5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{0} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.001953125} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{1}{256}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{0} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.0078125} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{1}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{0} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.03125} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{1}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{0} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{0.125} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{\frac{1}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{\frac{1}{256}} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.005859375} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{1}{128}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{\frac{1}{128}} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.009765625} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{3}{256}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{\frac{3}{256}} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.013671875} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{1}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{\frac{1}{64}} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.017578125} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{5}{256}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{\frac{1}{64}} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.0234375} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{1}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{\frac{5}{256}} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.021484375} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{3}{128}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{\frac{3}{128}} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.025390625} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{7}{256}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{\frac{7}{256}} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.029296875} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{1}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{\frac{1}{32}} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.033203125} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{9}{256}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{\frac{1}{32}} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.0390625} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{3}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 16 \int\limits^{\frac{9}{256}} e^{- \left|{x}\right|}\, dx + 32 \int\limits^{0.037109375} e^{- \left|{x}\right|}\, dx - 16 \int\limits^{\frac{5}{128}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{\frac{3}{64}} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.0546875} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{1}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{\frac{1}{16}} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.0703125} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{5}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{\frac{1}{16}} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.09375} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{1}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{\frac{5}{64}} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.0859375} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{3}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{\frac{3}{32}} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.1015625} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{7}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{\frac{7}{64}} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.1171875} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{1}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{\frac{1}{8}} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.1328125} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{9}{64}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{\frac{1}{8}} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.15625} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{3}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 8 \int\limits^{\frac{9}{64}} e^{- \left|{x}\right|}\, dx + 16 \int\limits^{0.1484375} e^{- \left|{x}\right|}\, dx - 8 \int\limits^{\frac{5}{32}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{\frac{3}{16}} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.21875} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{1}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{\frac{1}{4}} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.28125} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{5}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{\frac{1}{4}} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{0.375} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{\frac{1}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{\frac{5}{16}} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.34375} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{3}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{\frac{3}{8}} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.40625} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{7}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{\frac{7}{16}} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.46875} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{1}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{\frac{1}{2}} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.53125} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{9}{16}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{\frac{1}{2}} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{0.625} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{\frac{3}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{0.5} e^{- \left|{x}\right|}\, dx - \int\limits^{1} e^{- \left|{x}\right|}\, dx + \int\limits_{0}^{0.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 4 \int\limits^{\frac{9}{16}} e^{- \left|{x}\right|}\, dx + 8 \int\limits^{0.59375} e^{- \left|{x}\right|}\, dx - 4 \int\limits^{\frac{5}{8}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{\frac{3}{4}} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{0.875} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{1} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{1} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{1.125} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{\frac{5}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{\frac{5}{4}} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{1.375} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{\frac{3}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{\frac{3}{2}} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{1.625} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{\frac{7}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{1.5} e^{- \left|{x}\right|}\, dx - \int\limits^{2} e^{- \left|{x}\right|}\, dx + \int\limits_{1}^{1.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{\frac{7}{4}} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{1.875} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{2} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{2} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{2.125} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{\frac{9}{4}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(- 2 \int\limits^{\frac{9}{4}} e^{- \left|{x}\right|}\, dx + 4 \int\limits^{2.375} e^{- \left|{x}\right|}\, dx - 2 \int\limits^{\frac{5}{2}} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{2.5} e^{- \left|{x}\right|}\, dx - \int\limits^{3} e^{- \left|{x}\right|}\, dx + \int\limits_{2}^{2.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{3.5} e^{- \left|{x}\right|}\, dx - \int\limits^{4} e^{- \left|{x}\right|}\, dx + \int\limits_{3}^{3.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{4.5} e^{- \left|{x}\right|}\, dx - \int\limits^{5} e^{- \left|{x}\right|}\, dx + \int\limits_{4}^{4.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{5.5} e^{- \left|{x}\right|}\, dx - \int\limits^{6} e^{- \left|{x}\right|}\, dx + \int\limits_{5}^{5.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{6.5} e^{- \left|{x}\right|}\, dx - \int\limits^{7} e^{- \left|{x}\right|}\, dx + \int\limits_{6}^{6.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{7.5} e^{- \left|{x}\right|}\, dx - \int\limits^{8} e^{- \left|{x}\right|}\, dx + \int\limits_{7}^{7.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{8.5} e^{- \left|{x}\right|}\, dx - \int\limits^{9} e^{- \left|{x}\right|}\, dx + \int\limits_{8}^{8.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|} + \left(\int\limits^{9.5} e^{- \left|{x}\right|}\, dx - \int\limits^{10} e^{- \left|{x}\right|}\, dx + \int\limits_{9}^{9.5} e^{- \left|{x}\right|}\, dx\right) e^{- \left|{x}\right|}$

```python
p1 = plot(resy, show=False)
p2 = plot(f, show=False)
p1.append(p2[0])
p1.show()
```

```python
F = fourier_transform(f, x, k)
print(F)
inverse_fourier_transform(F, k, x)
```

```python

```