I'll try to write here some code snippets to help analyze, visualize and
explore data in oceans sciences.
This post is just a test!
Testing a full notebook:
In [1]:
from IPython.core.display import HTML
with open('creative_commons.txt', 'r') as f:
html = f.read()
name = '2013-05-06-First_post'
html = """
<small>
<p> This post was written as an IPython notebook. It is available for
<a href="https://ocefpaf.github.com/python4oceanographers/downloads/
notebooks/%s.ipynb">download</a> or as a static
<a href="https://nbviewer.ipython.org/url/ocefpaf.github.com/
python4oceanographers/downloads/notebooks/%s.ipynb">html</a>.</p>
<p></p>
%s """ % (name, name, html)
%matplotlib inline
from matplotlib import style
style.use('ggplot')
In [2]:
from __future__ import division
import sympy
import numpy
print("SymPy version: %s" % sympy.__version__)
print("NumPy version: %s" % numpy.__version__)
In [3]:
import numpy as np
import numpy.ma as ma
import matplotlib.pyplot as plt
from sympy.solvers import solve
from sympy.functions import exp
from sympy import Symbol, Matrix
from sympy.interactive import ipythonprinting
ipythonprinting.init_printing()
In [4]:
c = Symbol('c')
k = Symbol('k')
In [5]:
A = Matrix([[-c + 1 / (2 * k), -exp(-k / 2) / k, exp(-k) / (2 * k)],
[exp(-k / 2) / (2 * k), -1 / k + 1 / 2 - c, exp(-k / 2) / (2 * k)],
[exp(-k) / (2 * k), -exp(-k / 2) / (k), - c + 1 / (2 * k)]])
A
Out[5]:
In [6]:
det = A.det()
det
Out[6]:
In [7]:
K = solve(det, c)
K
Out[7]:
In [8]:
dk = np.arange(0.01, 2.5, 0.01)
C1, C2, C3 = [], [], []
for i in np.arange(np.size(dk)):
C1.append(K[0].subs(k, dk[i]))
C2.append(K[1].subs(k, dk[i]))
C3.append(K[2].subs(k, dk[i]))
C1, C2, C3 = map(lambda x: np.asanyarray(x, np.complex_), (C1, C2, C3))
In [9]:
print("NaNs in C1: %s" % np.isnan(C1).all())
print("NaNs in C2: %s" % np.isnan(C2).all())
print("NaNs in C3: %s" % np.isnan(C3).all())
In [10]:
print("Complex in C1: %s" % np.iscomplex(C1).all())
print("Complex in C2: %s" % np.iscomplex(C2).all())
print("Complex in C3: %s" % np.iscomplex(C3).all())
In [11]:
C3 = C3.real # No need to be imaginary.
In [12]:
fig, ax = plt.subplots()
l1, = ax.plot(dk, C1.real, ':', label='C1')
l2, = ax.plot(dk, C2.real, '--', label='C2')
l3, = ax.plot(dk, C3, '-', label='C3')
ax.legend()
ax.margins(0)
In [13]:
fig, ax = plt.subplots()
l1, = ax.plot(dk, C1.imag, '-,', label='C1')
l2, = ax.plot(dk, C2.imag, '--', label='C2')
l3, = ax.plot(dk, C3, '-', label='C3')
ax.legend()
ax.margins(0)
In [14]:
HTML(html)
Out[14]:
Now just some cells (from the same notebook):
In [6]:
det = A.det()
det
Out[6]:
Include code examples:
Seems to work fine!