python4oceanographers

Turning ripples into waves

python4oceanographers

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__)
SymPy version: 0.7.6.1
NumPy version: 1.10.2

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]:
$$\left[\begin{matrix}- c + \frac{1}{2 k} & - \frac{e^{- \frac{k}{2}}}{k} & \frac{e^{- k}}{2 k}\\\frac{e^{- \frac{k}{2}}}{2 k} & - c + 0.5 - \frac{1}{k} & \frac{e^{- \frac{k}{2}}}{2 k}\\\frac{e^{- k}}{2 k} & - \frac{e^{- \frac{k}{2}}}{k} & - c + \frac{1}{2 k}\end{matrix}\right]$$
In [6]:
det = A.det()
det
Out[6]:
$$- c^{3} + 0.5 c^{2} - \frac{0.5 c}{k} + \frac{3 c}{4 k^{2}} - \frac{c}{k^{2}} e^{- k} + \frac{c e^{- 2 k}}{4 k^{2}} + \frac{0.125}{k^{2}} - \frac{0.125}{k^{2}} e^{- 2 k} - \frac{1}{4 k^{3}} + \frac{e^{- k}}{2 k^{3}} - \frac{e^{- 2 k}}{4 k^{3}}$$
In [7]:
K = solve(det, c)
K
Out[7]:
$$\left [ \frac{0.25}{k^{2}} \left(k^{2} e^{k} - k e^{k} + k - \sqrt{k^{4} e^{2.0 k} - 2.0 k^{3} e^{k} - 6.0 k^{3} e^{2.0 k} - 10.0 k^{2} e^{k} + 9.0 k^{2} e^{2.0 k} + k^{2}}\right) e^{- k}, \quad \frac{0.25}{k^{2}} \left(k^{2} e^{k} - k e^{k} + k + \sqrt{k^{4} e^{2.0 k} - 2.0 k^{3} e^{k} - 6.0 k^{3} e^{2.0 k} - 10.0 k^{2} e^{k} + 9.0 k^{2} e^{2.0 k} + k^{2}}\right) e^{- k}, \quad \frac{0.5}{k} \left(e^{k} - 1.0\right) e^{- k}\right ]$$
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())
NaNs in C1: False
NaNs in C2: False
NaNs in C3: False

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())
Complex in C1: True
Complex in C2: True
Complex in C3: False

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]:

This post was written as an IPython notebook. It is available for download or as a static html.

Creative Commons License
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://ocefpaf.github.io/.

Now just some cells (from the same notebook):

In [6]:
det = A.det()
det
Out[6]:
$$- c^{3} + 0.5 c^{2} - \frac{0.5 c}{k} + \frac{3 c}{4 k^{2}} - \frac{c}{k^{2}} e^{- k} + \frac{c e^{- 2 k}}{4 k^{2}} + \frac{0.125}{k^{2}} - \frac{0.125}{k^{2}} e^{- 2 k} - \frac{1}{4 k^{3}} + \frac{e^{- k}}{2 k^{3}} - \frac{e^{- 2 k}}{4 k^{3}}$$

Include code examples:

[Code test] hello_world.py download
# -*- coding: utf-8 -*-
#
# hello_world.py
#
# purpose:
# author:   Filipe P. A. Fernandes
# e-mail:   ocefpaf@gmail
# web:      http://ocefpaf.tiddlyspot.com/
# created:  09-May-2013
# modified: Thu 09 May 2013 03:03:13 PM BRT
#
# obs:
#

print("Hello World!")

Seems to work fine!

Comments