signature_function/gaknot/signature.py

386 lines
14 KiB
Python
Raw Normal View History

2021-07-15 13:48:37 +02:00
# This file was *autogenerated* from the file /home/maria/signature_function/gaknot/signature.sage
from sage.all_cmdline import * # import sage library
_sage_const_0 = Integer(0); _sage_const_1 = Integer(1); _sage_const_2 = Integer(2); _sage_const_36 = Integer(36); _sage_const_10 = Integer(10); _sage_const_5 = Integer(5); _sage_const_0p3 = RealNumber('0.3'); _sage_const_0p7 = RealNumber('0.7'); _sage_const_0p05 = RealNumber('0.05'); _sage_const_4 = Integer(4); _sage_const_3 = Integer(3)#!/usr/bin/env sage -python
2020-11-05 15:10:12 +01:00
from collections import Counter
import matplotlib.pyplot as plt
import inspect
from PIL import Image
from pathlib import Path
import warnings
2021-01-13 21:21:05 +01:00
from .utility import mod_one
2020-11-05 15:10:12 +01:00
# 9.11 (9.8)
# 9.15 (9.9)
import sys
import os
JUPYTER = 'ipykernel'
IPy_TERMINAL = 'IPython'
def get_ipython_info():
if JUPYTER in sys.modules:
return JUPYTER
elif IPy_TERMINAL in sys.modules:
return IPy_TERMINAL
return False
global ipython_info
ipython_info = get_ipython_info()
class SignatureFunction:
def __init__(self, values=None, counter=None, plot_title=''):
2020-11-05 15:10:12 +01:00
# counter of signature jumps
if counter is None:
counter = Counter()
values = values or []
2020-11-05 15:10:12 +01:00
for k, v in values:
counter[k] += v
2021-07-15 13:48:37 +02:00
counter = Counter({k : v for k, v in counter.items() if v != _sage_const_0 })
if any(k >= _sage_const_1 for k in counter.keys()):
2020-11-05 15:10:12 +01:00
msg = "Signature function is defined on the interval [0, 1)."
raise ValueError(msg)
2021-07-15 13:48:37 +02:00
counter[_sage_const_0 ] += _sage_const_0
counter[_sage_const_1 ] += _sage_const_0
2020-11-05 15:10:12 +01:00
self.jumps_counter = counter
self.plot_title = plot_title
def __rshift__(self, shift):
2020-11-05 15:10:12 +01:00
# A shift of the signature functions corresponds to the rotation.
counter = Counter({mod_one(k + shift) : v \
for k, v in self.jumps_counter.items()})
return SignatureFunction(counter=counter)
2020-11-05 15:10:12 +01:00
def __lshift__(self, shift):
return self.__rshift__(-shift)
def __neg__(self):
2020-11-05 15:10:12 +01:00
counter = Counter()
counter.subtract(self.jumps_counter)
return SignatureFunction(counter=counter)
def __add__(self, other):
2020-11-05 15:10:12 +01:00
counter = copy(self.jumps_counter)
counter.update(other.jumps_counter)
if self.plot_title and other.plot_title:
title = self.plot_title + " + " + other.plot_title
else:
title = self.plot_title or other.plot_title
return SignatureFunction(counter=counter, plot_title=title)
2020-11-05 15:10:12 +01:00
def __sub__(self, other):
counter = copy(self.jumps_counter)
counter.subtract(other.jumps_counter)
return SignatureFunction(counter=counter)
def __mul__(self, number):
# scalar multiplication
counter = Counter({k : number * v \
for k, v in self.jumps_counter.items()})
return SignatureFunction(counter=counter)
def __rmul__(self, number):
return(self.__mul__(number))
2020-11-05 15:10:12 +01:00
def __eq__(self, other):
return self.jumps_counter == other.jumps_counter
def __str__(self):
2020-11-05 15:10:12 +01:00
result = ''.join([str(jump_arg) + ": " + str(jump) + "\n"
for jump_arg, jump in sorted(self.jumps_counter.items())])
return result
def __repr__(self):
2020-11-05 15:10:12 +01:00
result = ''.join([str(jump_arg) + ": " + str(jump) + ", "
for jump_arg, jump in sorted(self.jumps_counter.items())])
2021-07-15 13:48:37 +02:00
return result[:-_sage_const_2 ] + "."
2020-11-05 15:10:12 +01:00
def __call__(self, arg):
# return the value of the signature function at the point arg, i.e.
# sum of all signature jumps that occur before arg
items = self.jumps_counter.items()
result = [jump for jump_arg, jump in items if jump_arg < mod_one(arg)]
2021-07-15 13:48:37 +02:00
return _sage_const_2 * sum(result) + self.jumps_counter[arg]
2020-11-05 15:10:12 +01:00
def double_cover(self):
# to read values for t^2
items = self.jumps_counter.items()
2021-07-15 13:48:37 +02:00
counter = Counter({(_sage_const_1 + k) / _sage_const_2 : v for k, v in items})
counter.update(Counter({k / _sage_const_2 : v for k, v in items}))
2020-11-05 15:10:12 +01:00
return SignatureFunction(counter=counter)
def square_root(self):
# to read values for t^(1/2)
counter = Counter()
for jump_arg, jump in self.jumps_counter.items():
2021-07-15 13:48:37 +02:00
if jump_arg < _sage_const_1 /_sage_const_2 :
counter[_sage_const_2 * jump_arg] = jump
2020-11-05 15:10:12 +01:00
return SignatureFunction(counter=counter)
def minus_square_root(self):
# to read values for t^(1/2)
items = self.jumps_counter.items()
2021-07-15 13:48:37 +02:00
counter = Counter({mod_one(_sage_const_2 * k) : v for k, v in items if k >= _sage_const_1 /_sage_const_2 })
2020-11-05 15:10:12 +01:00
return SignatureFunction(counter=counter)
def is_zero_everywhere(self):
return not any(self.jumps_counter.values())
def extremum(self, limit=math.inf):
2021-07-15 13:48:37 +02:00
max_point = (_sage_const_0 , _sage_const_0 )
current = _sage_const_0
2020-11-05 15:10:12 +01:00
items = sorted(self.jumps_counter.items())
for arg, jump in items:
2021-07-15 13:48:37 +02:00
current += _sage_const_2 * jump
2020-11-05 15:10:12 +01:00
assert current == self(arg) + jump
2021-07-15 13:48:37 +02:00
if abs(current) > abs(max_point[_sage_const_1 ]):
max_point = (arg, current)
if abs(current) > limit:
break
return max_point
2020-11-05 15:10:12 +01:00
def total_sign_jump(self):
# Total signature jump is the sum of all jumps.
2021-07-15 13:48:37 +02:00
return sum([j[_sage_const_1 ] for j in sorted(self.jumps_counter.items())])
2020-11-05 15:10:12 +01:00
def plot(self, *args, **kargs):
SignaturePloter.plot(self, *args, **kargs)
class SignaturePloter:
@classmethod
2020-11-10 17:20:02 +01:00
def plot_many(cls, *sf_list, save_path=None, title='', cols=None):
axes_num = len(sf_list)
2021-07-15 13:48:37 +02:00
if axes_num > _sage_const_36 :
sf_list = sf_list[_sage_const_36 ]
axes_num = _sage_const_36
msg = "To many functions for the plot were given. "
msg += "Only 36 can be plotted "
warnings.warn(msg)
# print war, set val in conf
2020-11-10 17:20:02 +01:00
cols = cols or ceil(sqrt(axes_num))
rows = ceil(axes_num/cols)
fig, axes_matrix = plt.subplots(rows, cols,
2020-11-10 17:20:02 +01:00
sharex='col', sharey='row',
2021-07-15 13:48:37 +02:00
gridspec_kw={'hspace': _sage_const_0 , 'wspace': _sage_const_0 },
2020-11-10 17:20:02 +01:00
# sharey=True,
# sharex=True,
)
for i, sf in enumerate(sf_list):
col = i % cols
row = (i - col)/cols
sf.plot(subplot=True,
ax=axes_matrix[row][col],
title=sf.plot_title)
2020-11-05 17:59:36 +01:00
fig.suptitle(title)
plt.tight_layout()
cls.show_and_save(save_path)
2020-11-05 17:59:36 +01:00
@classmethod
def plot_sum_of_two(cls, sf1, sf2, save_path=None, title=''):
2020-11-05 15:10:12 +01:00
sf = sf1 + sf2
2021-07-15 13:48:37 +02:00
fig, axes_matrix = plt.subplots(_sage_const_2 , _sage_const_2 , sharey=True, figsize=(_sage_const_10 ,_sage_const_5 ))
2020-11-05 15:10:12 +01:00
sf1.plot(subplot=True,
2021-07-15 13:48:37 +02:00
ax=axes_matrix[_sage_const_0 ][_sage_const_1 ])
2020-11-05 15:10:12 +01:00
sf2.plot(subplot=True,
2021-07-15 13:48:37 +02:00
ax=axes_matrix[_sage_const_1 ][_sage_const_0 ],
2020-11-05 15:10:12 +01:00
color='red',
linestyle='dotted')
2020-11-05 17:59:36 +01:00
sf.plot(subplot=True,
2021-07-15 13:48:37 +02:00
ax=axes_matrix[_sage_const_0 ][_sage_const_0 ],
2020-11-05 15:10:12 +01:00
color='black')
sf1.plot(subplot=True,
2021-07-15 13:48:37 +02:00
ax=axes_matrix[_sage_const_1 ][_sage_const_1 ],
alpha=_sage_const_0p3 )
2020-11-05 15:10:12 +01:00
sf2.plot(subplot=True,
2021-07-15 13:48:37 +02:00
ax=axes_matrix[_sage_const_1 ][_sage_const_1 ],
color='red', alpha=_sage_const_0p3 ,
2020-11-05 15:10:12 +01:00
linestyle='dotted')
2020-11-05 17:59:36 +01:00
sf.plot(subplot=True,
2021-07-15 13:48:37 +02:00
ax=axes_matrix[_sage_const_1 ][_sage_const_1 ],
2020-11-05 15:10:12 +01:00
color='black',
2021-07-15 13:48:37 +02:00
alpha=_sage_const_0p7 ,)
2020-11-05 15:10:12 +01:00
fig.suptitle(title)
plt.tight_layout()
cls.show_and_save(save_path)
2020-11-05 15:10:12 +01:00
@classmethod
def plot(cls, sf, subplot=False, ax=None,
save_path=None,
title='',
2021-07-15 13:48:37 +02:00
alpha=_sage_const_1 ,
2020-11-05 15:10:12 +01:00
color='blue',
2020-11-05 17:59:36 +01:00
linestyle='solid',
special_point=None,
special_label='',
extraticks=None,
2020-11-05 17:59:36 +01:00
ylabel=''):
2020-11-05 15:10:12 +01:00
if ax is None:
2021-07-15 13:48:37 +02:00
fig, ax = plt.subplots(_sage_const_1 , _sage_const_1 )
2020-11-05 15:10:12 +01:00
keys = sorted(sf.jumps_counter.keys())
2021-07-15 13:48:37 +02:00
y = [(sf(k) + sf.jumps_counter[k]) for k in keys[:-_sage_const_1 ]]
xmax = keys[_sage_const_1 :]
xmin = keys[:-_sage_const_1 ]
2020-11-05 15:10:12 +01:00
2020-11-05 17:59:36 +01:00
ax.set(ylabel=ylabel)
2020-11-05 15:10:12 +01:00
ax.set(title=title)
ax.hlines(y, xmin, xmax, color=color, linestyle=linestyle, alpha=alpha)
if special_point is not None:
arg, val = special_point
extraticks = extraticks or []
2021-07-15 13:48:37 +02:00
plt.xticks(list(plt.xticks()[_sage_const_0 ]) + extraticks)
ext = sf.extremum()[_sage_const_1 ]
ytext = ext/_sage_const_2 + _sage_const_1 /_sage_const_2
xtext = arg + _sage_const_1 /_sage_const_5
ax.annotate(special_label, xy=(arg, val), xytext=(xtext, ytext),
2021-07-15 13:48:37 +02:00
arrowprops=dict(facecolor='black', shrink=_sage_const_0p05 ,
alpha=_sage_const_0p7 , width=_sage_const_2 ),)
2020-11-05 15:10:12 +01:00
if subplot:
return ax
cls.show_and_save(save_path)
@staticmethod
def show_and_save(save_path):
if save_path is not None:
save_path = Path(save_path)
save_path = save_path.with_suffix('.png')
plt.savefig(save_path)
if ipython_info == JUPYTER:
plt.show()
2020-11-05 15:10:12 +01:00
elif True: # save_path is None:
plt.savefig('tmp.png')
plt.close()
image = Image.open('tmp.png')
image.show()
# msg = "For interactive shell set save_path."
# warnings.warn(msg)
@staticmethod
def step_function_data(sf):
2020-11-05 15:10:12 +01:00
# Transform the signature jump data to a format understandable
# by the plot function.
result = [(k, sf.sf(k) + sf.jumps_counter[k])
for k in sorted(sf.jumps_counter.keys())]
2020-11-05 15:10:12 +01:00
return result
@staticmethod
def tikz_plot(sf, save_as):
2021-07-15 13:48:37 +02:00
plt_sin = plot(sin(x), (x, _sage_const_0 , _sage_const_2 *pi))
2020-11-05 15:10:12 +01:00
# plt_sin.show()
plt_sin.save("MyPic.pdf")
return
# Draw the graph of the signature and transform it into TiKz.
# header of the LaTeX file
head = inspect.cleandoc(
r"""
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
""")
body = \
r"""
%A piecewise linear function is drawn over the interval.
\draw (5,0) -- (6,-4);
%The axes are drawn.
\draw[latex-latex] ($(0,{-4*(2/5)}) +(0pt,-12.5pt)$) --
($(0,{4*(2/5)}) +(0pt,12.5pt)$) node[above right]{$y$};
\draw[latex-latex] ($({-4*(2/5)},0) +(-12.5pt,0pt)$) --
($({12*(2/5)},0) +(12.5pt,0pt)$) node[below right]{$x$};
"""
tail = \
r"""
\end{tikzpicture}
\end{document}
"""
tikzpicture = re.sub(r' +', ' ', ''.join([head, body, tail]))
tikzpicture = re.sub(r'\n ', '\n', tikzpicture)
with open("tmp.tex", "w") as f:
f.write(tikzpicture)
data = self.step_function_data()
with open(save_as, "w") as f:
head = \
r"""
\documentclass[tikz]{{standalone}}
%\usepackage{{tikz}}
\usetikzlibrary{{datavisualization}}
\usetikzlibrary{{datavisualization.formats.functions}}
%\usetikzlibrary{{calc}}
\begin{{document}}
\begin{{tikzpicture}}
\datavisualization[scientific axes, visualize as smooth line,
x axis={{ticks={{none,major={{at={{, {arg0} " as \\( {val0} \\
%]
2021-07-15 13:48:37 +02:00
""".format(arg0=str(N(data[_sage_const_0 ][_sage_const_0 ] ,digits=_sage_const_4 )), val0=str(data[_sage_const_0 ][_sage_const_0 ]))
2020-11-05 15:10:12 +01:00
f.write(head)
# f.write(", " + str(N(data[0][0],digits=4)) + " as \\(" + \
# str(data[0][0]) + "\\)")
2021-07-15 13:48:37 +02:00
for jump_arg, jump in data[_sage_const_1 :_sage_const_3 ]:
f.write(", " + str(N(jump_arg,digits=_sage_const_4 )) +
2020-11-05 15:10:12 +01:00
" as \\(" + str(jump_arg) + "\\)")
f.write("}}}}\n")
f.write(" ]\n")
f.write("data [format=function]{\n")
f.write("var x : interval [0:1];\n")
f.write("func y = \\value x;\n")
f.write("};\n")
# close LaTeX enviroments
tail = \
r"""
%};
\end{tikzpicture}
\end{document}
"""
f.write(tail)
SignatureFunction.__doc__ = \
"""
This simple class encodes twisted and untwisted signature functions
of knots. Since the signature function is entirely encoded by its signature
jump, the class stores only information about signature jumps
in a dictionary self.jumps_counter.
The dictionary stores data of the signature jump as a key/values pair,
where the key is the argument at which the functions jumps
and value encodes the value of the jump. Remember that we treat
signature functions as defined on the interval [0,1).
"""
2021-07-15 13:48:37 +02:00