```python
total = 0
for i in range(10):
    total = total + i
total
```




    45




```python
for i in range(3):
    print(i)
```

    0
    1
    2


$$\int_0^{+\infty} x^2 dx$$

$$ z = \frac{x}{y} $$

$$ P(A \mid B) = \frac{P(B \mid A) , P(A)}{P(B)} $$

$$ f(x) = x^2 $$

$ \sqrt 3 $


```python
def proportion(a, b):
    """
    We can use triple " to describe what our function does.
    
    Here for example: we're creating a function to calculate 
    the proportion of a of a + b
    """
    return a / (a + b)
```


```python
proportion?
```


```python
proportion(20, 21)
```




    0.4878048780487805




```python
import sympy as sym
```


```python
x = sym.symbols('x')
```


```python
x-x
```




    0




```python
a, b = sym.symbols('a, b')
```


```python
expr = (a + b) ** 2 
expr
```




    (a + b)**2




```python
sym.init_printing()
```


```python
expr
```


```python
expr.expand()
```


```python
sym.latex(expr.expand())
```




    'a^{2} + 2 a b + b^{2}'




```python
a, b, c, x = sym.symbols('a, b, c, x') 
```


```python
sym.solveset(a * x ** 2 + b * x + c, x)
```


```python
sym.solveset(x ** 2 + 1, x, domain=sym.S.Reals)
```


```python
sym.limit(1/x, x, 0, dir="+")
```


```python
sym.diff(x ** 2 - sym.cos(x), x)
```


```python
sym.integrate(x ** 2 - sym.cos(x), x)
```


```python
sym.integrate(x ** 2 - sym.cos(x), (x, 0, 5))
```


```python
%matplotlib inline
```


```python
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(20)
y = x**2

plt.plot(x, y)
```


```python
expr = x ** 2
p = sym.plot(expr);
```


```python
p.save("x_squared.pdf");
```

$$ A = \begin{pmatrix} 5 & 6 & 2 \\4 & 7 & 19 \\0 & 3 & 12 \end{pmatrix} $$
$$ B = \begin{pmatrix} 14 & -2 & 12\\ 4 & 4 & 5\\5 & 5 & 1\end{pmatrix} $$

import numpy as np


```python
A = np.matrix([[5, 6, 2],
               [4, 7, 19],
               [0, 3, 12]])
```


```python
A
```




    matrix([[ 5,  6,  2],
            [ 4,  7, 19],
            [ 0,  3, 12]])




```python
B = np.matrix([[14, -2, 12],
               [4, 4, 5],
               [5, 5, 1]])
```


```python
B
```




    matrix([[14, -2, 12],
            [ 4,  4,  5],
            [ 5,  5,  1]])




```python
5 * A
```




    matrix([[25, 30, 10],
            [20, 35, 95],
            [ 0, 15, 60]])




```python
A ** 3
```




    matrix([[ 557, 1284, 3356],
            [ 760, 2305, 6994],
            [ 288, 1074, 3519]])




```python
A + B
```




    matrix([[19,  4, 14],
            [ 8, 11, 24],
            [ 5,  8, 13]])




```python
A - B
```




    matrix([[ -9,   8, -10],
            [  0,   3,  14],
            [ -5,  -2,  11]])




```python
A * B
```




    matrix([[104,  24,  92],
            [179, 115, 102],
            [ 72,  72,  27]])



$$ A = \begin{pmatrix} 5 & 6 & 2 \\4 & 7 & 19 \\0 & 3 &12 \end{pmatrix} $$
$$ b = \begin{pmatrix} -1 \\2 \\1 \end{pmatrix} $$


```python
A = np.matrix([[5, 6, 2],
               [4, 7, 19],
               [0, 3, 12]])
b = np.matrix([[-1], [2], [1]])
```


```python
x = np.linalg.solve(A, b)
x
```




    matrix([[ 0.45736434],
            [-0.62790698],
            [ 0.24031008]])




```python
A * x
```




    matrix([[-1.],
            [ 2.],
            [ 1.]])




```python
Ainv = np.linalg.inv(A)
Ainv
```




    matrix([[-0.20930233,  0.51162791, -0.7751938 ],
            [ 0.37209302, -0.46511628,  0.6744186 ],
            [-0.09302326,  0.11627907, -0.08527132]])




```python
A * Ainv
```




    matrix([[  1.00000000e+00,   4.99600361e-16,  -4.71844785e-16],
            [  1.38777878e-16,   1.00000000e+00,  -2.63677968e-16],
            [  1.11022302e-16,   1.11022302e-16,   1.00000000e+00]])




```python
np.linalg.det(A)
```


```python
import pandas as pd
```


```python
df = pd.read_excel("data/goldbach.xlsx")
```


```python
df.head()
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>N</th>
      <th>a</th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>4</td>
      <td>2</td>
      <td>2</td>
    </tr>
    <tr>
      <th>1</th>
      <td>6</td>
      <td>3</td>
      <td>3</td>
    </tr>
    <tr>
      <th>2</th>
      <td>8</td>
      <td>3</td>
      <td>5</td>
    </tr>
    <tr>
      <th>3</th>
      <td>10</td>
      <td>3</td>
      <td>7</td>
    </tr>
    <tr>
      <th>4</th>
      <td>10</td>
      <td>5</td>
      <td>5</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.tail()
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>N</th>
      <th>a</th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>2591</th>
      <td>500</td>
      <td>151</td>
      <td>349</td>
    </tr>
    <tr>
      <th>2592</th>
      <td>500</td>
      <td>163</td>
      <td>337</td>
    </tr>
    <tr>
      <th>2593</th>
      <td>500</td>
      <td>193</td>
      <td>307</td>
    </tr>
    <tr>
      <th>2594</th>
      <td>500</td>
      <td>223</td>
      <td>277</td>
    </tr>
    <tr>
      <th>2595</th>
      <td>500</td>
      <td>229</td>
      <td>271</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.describe()
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>N</th>
      <th>a</th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>count</th>
      <td>2596.000000</td>
      <td>2596.000000</td>
      <td>2596.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>311.466872</td>
      <td>72.297766</td>
      <td>239.169106</td>
    </tr>
    <tr>
      <th>std</th>
      <td>127.817425</td>
      <td>58.087254</td>
      <td>111.180401</td>
    </tr>
    <tr>
      <th>min</th>
      <td>4.000000</td>
      <td>2.000000</td>
      <td>2.000000</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>214.000000</td>
      <td>23.000000</td>
      <td>157.000000</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>329.000000</td>
      <td>59.000000</td>
      <td>239.000000</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>420.000000</td>
      <td>107.000000</td>
      <td>317.000000</td>
    </tr>
    <tr>
      <th>max</th>
      <td>500.000000</td>
      <td>241.000000</td>
      <td>491.000000</td>
    </tr>
  </tbody>
</table>
</div>




```python
df[df['N'] == 322]
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>N</th>
      <th>a</th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1250</th>
      <td>322</td>
      <td>5</td>
      <td>317</td>
    </tr>
    <tr>
      <th>1251</th>
      <td>322</td>
      <td>11</td>
      <td>311</td>
    </tr>
    <tr>
      <th>1252</th>
      <td>322</td>
      <td>29</td>
      <td>293</td>
    </tr>
    <tr>
      <th>1253</th>
      <td>322</td>
      <td>41</td>
      <td>281</td>
    </tr>
    <tr>
      <th>1254</th>
      <td>322</td>
      <td>53</td>
      <td>269</td>
    </tr>
    <tr>
      <th>1255</th>
      <td>322</td>
      <td>59</td>
      <td>263</td>
    </tr>
    <tr>
      <th>1256</th>
      <td>322</td>
      <td>71</td>
      <td>251</td>
    </tr>
    <tr>
      <th>1257</th>
      <td>322</td>
      <td>83</td>
      <td>239</td>
    </tr>
    <tr>
      <th>1258</th>
      <td>322</td>
      <td>89</td>
      <td>233</td>
    </tr>
    <tr>
      <th>1259</th>
      <td>322</td>
      <td>131</td>
      <td>191</td>
    </tr>
    <tr>
      <th>1260</th>
      <td>322</td>
      <td>149</td>
      <td>173</td>
    </tr>
  </tbody>
</table>
</div>




```python
s = df['N'].value_counts()
s
```




    420    30
    480    29
    462    28
    450    27
    390    27
    456    24
    468    24
    330    24
    486    23
    474    23
    498    23
    360    22
    492    22
    378    22
    414    21
    444    21
    300    21
    438    21
    426    21
    396    21
    324    20
    408    20
    354    20
    490    19
    336    19
    294    19
    210    19
    270    19
    432    19
    384    19
           ..
    58      4
    80      4
    88      4
    92      4
    152     4
    42      4
    26      3
    30      3
    24      3
    40      3
    22      3
    98      3
    52      3
    56      3
    62      3
    128     3
    44      3
    38      2
    18      2
    10      2
    14      2
    16      2
    20      2
    68      2
    28      2
    32      2
    12      1
    8       1
    6       1
    4       1
    Name: N, dtype: int64




```python
df = pd.DataFrame(s.rename('counts'))
df.head()
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>counts</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>420</th>
      <td>30</td>
    </tr>
    <tr>
      <th>480</th>
      <td>29</td>
    </tr>
    <tr>
      <th>462</th>
      <td>28</td>
    </tr>
    <tr>
      <th>450</th>
      <td>27</td>
    </tr>
    <tr>
      <th>390</th>
      <td>27</td>
    </tr>
  </tbody>
</table>
</div>




```python
df["normalised_counts"] = df['counts'] / df.index
```


```python
df.head()
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>counts</th>
      <th>normalised_counts</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>420</th>
      <td>30</td>
      <td>0.071429</td>
    </tr>
    <tr>
      <th>480</th>
      <td>29</td>
      <td>0.060417</td>
    </tr>
    <tr>
      <th>462</th>
      <td>28</td>
      <td>0.060606</td>
    </tr>
    <tr>
      <th>450</th>
      <td>27</td>
      <td>0.060000</td>
    </tr>
    <tr>
      <th>390</th>
      <td>27</td>
      <td>0.069231</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.describe()
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>counts</th>
      <th>normalised_counts</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>count</th>
      <td>249.000000</td>
      <td>249.000000</td>
    </tr>
    <tr>
      <th>mean</th>
      <td>10.425703</td>
      <td>0.051769</td>
    </tr>
    <tr>
      <th>std</th>
      <td>5.897948</td>
      <td>0.029921</td>
    </tr>
    <tr>
      <th>min</th>
      <td>1.000000</td>
      <td>0.017588</td>
    </tr>
    <tr>
      <th>25%</th>
      <td>6.000000</td>
      <td>0.030973</td>
    </tr>
    <tr>
      <th>50%</th>
      <td>9.000000</td>
      <td>0.044776</td>
    </tr>
    <tr>
      <th>75%</th>
      <td>13.000000</td>
      <td>0.062500</td>
    </tr>
    <tr>
      <th>max</th>
      <td>30.000000</td>
      <td>0.250000</td>
    </tr>
  </tbody>
</table>
</div>




```python
%matplotlib inline
```


```python
df['counts'].hist();
```


```python
df['normalised_counts'].hist();
```


```python
df.to_csv("data/goldbach_counts.csv")
```


```python
import numpy as np
from sympy import *
from __future__ import division

init_printing()
```


```python
x = Symbol("x")
k = Symbol("k")
y = Symbol("y")

b = Symbol("b")

t = sin(2*pi*x*(k**2))/ (4*(pi**2)*(k**5)) + (x**2) / (2*k)

t
```


```python
t1 = diff(t,x)

t1
```


```python
t2 = diff(t1,x)

t2
```


```python
def sumatoria(a, termino, N=100):
    return sum(termino.subs(x,a).subs(k,b+1) for b in range(N))

def f(a):
    return sumatoria(a,t)

def f1(a):
    return sumatoria(a,t1)

def f2(a):
    return sumatoria(a,t2)
```


```python
plot(f(y), (y,0,1))

show()
```


```python
plot(f1(y), (y,0,1))

show()
```


```python
plot(f2(y), (y,0,1))

show()
```


```python
def CalcularyGraficar(t):
    t1 = diff(t,x)
    t2 = diff(t1,x)
    def f(a):
        return sumatoria(a,t)

    def f1(a):
        return sumatoria(a,t1)

    def f2(a):
        return sumatoria(a,t2)

    plot(f(y), (y,0,1))
    plot(f1(y), (y,0,1))
    plot(f2(y), (y,0,1))

    show()
```


```python
t = sin(2*pi*x*(k**2))/ (4*(pi**2)*(k**3)) + (x**2) / (2*k)

CalcularyGraficar(t)
```


```python
plot(f(y), (y,0,0.0001))

show()
```


```python
t = sin(2*pi*x*(k**2))/ (4*(pi**2)*(k**5))

CalcularyGraficar(t)
```


```python
t = sin(2*pi*x*(k**2))/ (4*(pi**2)*(k)) + (x**2) / (2*k)
    
CalcularyGraficar(t)
```


```python
t = sin(2*pi*x*(k**2))/ (4*(pi**2)*(k**10)) + (x**2) / (2*k)
    
CalcularyGraficar(t)
```


```python
%matplotlib inline
import numpy as np
import scipy as sp
import scipy.linalg as la
import matplotlib.pyplot as plt
```


```python
import scipy.integrate as sint

A = np.array([[1, -20],
              [3, 4]], dtype=np.double)

def yprime(y, t):
    return sp.dot(A, y)

t = np.linspace(0, 1, 51)
yzero = np.array([1, 0])

# Solve equation using odeint
y = sint.odeint(yprime, yzero, t) # Calls the Fortran library odeint
plt.plot(y[:, 0], y[:, 1]) # Blue line is y(t) for t in [0, 1]

plt.xlim([-4, 9])
plt.ylim([-3, 5])

# Solve for 10 t values using the exponential
tvals = np.linspace(0, 1, 11)

for tval in tvals:
    yval = sp.dot(la.expm(A*tval), yzero)
    plt.plot(yval[0], yval[1], 'rs') # Red squares are evaluated via the matrix exponential
```


```python
import networkx as nx

Adj = np.array([[0, 1, 1, 0, 0, 0], 
                [1, 0, 1, 1, 1, 0], 
                [1, 1, 0, 0, 0, 0], 
                [0, 1, 0, 0, 0, 1], 
                [0, 1, 0, 0, 0, 0],
                [0, 0, 0, 1, 0, 0]])

G = nx.from_numpy_matrix(Adj)
nx.draw(G, node_color='y', node_size=1000)
```


```python
centralities = np.diag(la.expm(np.array(Adj, dtype=np.double)))
nodeorder = np.argsort(centralities)[::-1]

print(np.array([nodeorder, centralities[nodeorder]]))

# Note: This is already built into networkx using the following command
# print nx.communicability_centrality_exp(G)
```

    [[ 1.          2.          0.          3.          4.          5.        ]
     [ 4.44723536  2.86427609  2.86427609  2.36018456  1.71615913  1.59432922]]



```python

```

fe71d2bd965f1562261a62cea33ae3f72dc7951f
ipynb
Jupyter Notebook
Samples.ipynb
zhongwei/calculusnote
e55647de457317dc74f50e42ea3a81c12f5e1a14
MIT
Samples.ipynb
zhongwei/calculusnote
e55647de457317dc74f50e42ea3a81c12f5e1a14
MIT
Samples.ipynb
zhongwei/calculusnote
e55647de457317dc74f50e42ea3a81c12f5e1a14
MIT
Qwen/Qwen-72B
1. YES
2. YES