[PYTHON] Jupyter Tips 2

Continuation of Jupyter's trick

Calculate arithmetic progression or geometric progression with Jupyter

As a preparation, first do the following:

jupyter_notebook


import re, IPython.core.getipython
from math import log
class NumberSequence:
    def __init__(self, s):
        ss = [t.replace(' ','') for t in re.sub('(.)-', r'\1+-', s).split('+')]
        assert len(ss) == 3 or (len(ss) > 3 and ss[3] == '...')
        tt = [int(i) for i in ss[:3]]
        d, e = tt[1] - tt[0], tt[1]//(tt[0] if tt[0] else 1)
        isratio = tt[2]/(tt[1] if tt[1] else 1) == e
        assert isratio or tt[2]-tt[1] == d
        ssum, self.value = r'\sum_{i=0}', None
        if len(ss) > 4:
            last = int(ss[-1])
            if isratio:
                n = int(round(log(last / tt[0], abs(e)))) + 1
                assert last == tt[0]*e**(n-1)
                self.value = sum(tt[0]*e**i for i in range(n))
            else:
                n = (last-tt[0]) // d + 1
                assert last == tt[0]+d*(n-1)
                self.value = sum(tt[0]+d*i for i in range(n))
            ssum += '^{%d}'%(n-1)
        if isratio:
            self.form = '$%s{%s%d^i}$'%(ssum, '' if tt[0]==1 else r'%s \times '%tt[1], e)
        else:
            self.form = r'$%s{%s%s i}$'%(ssum, '' if tt[0]==0 else '%s + '%tt[0],
                '' if d==1 else '%d \times'%d)
    def _repr_html_(self):
        if self.value is not None:
            print(self.value)
        return self.form
def S_impl(s):
    return NumberSequence(s)
ip = IPython.core.getipython.get_ipython()
if ip:
    ip.register_magic_function(S_impl, magic_name='S')

Try to run

jupyter_notebook


%S 1+4+7+...

\sum_{i=0}{1 + 3 \times i}

The calculation formula is displayed.


jupyter_notebook


%S 1+4+7+...+19
>>>
70

\sum_{i=0}^{6}{1 + 3 \times i}

With the last term, the sum and formula are displayed.


jupyter_notebook


%S 1-3+9+...

\sum_{i=0}{-3^i}


You can omit the "%" of "% S".

jupyter_notebook


S 1 - 3 + 9 + ... + 81
>>>
61

\sum_{i=0}^{4}{-3^i}

jupyter_notebook


S {'+'.join(['0','1','2','...','100'])}
>>>
5050

\sum_{i=0}^{100}{ i}

You can eval it first by enclosing it in "{}".

that's all

Recommended Posts

Jupyter Tips 4
Jupyter Tips 5
Jupyter Tips 3
Jupyter Tips 2
Jupyter begins
Jupyter nbextensions
numpy tips
python tips
Scapy Tips
Jupyter Notebook: 4 banal tips and tricks
Jupyter installation
Jupyter tricks
Python Tips
Python tips
Python Conda Tips
Jupyter installation error
Jupyter Notebook memo
Introducing Jupyter Notebook
Jupyter study notes_006
Jupyter Lab begins
Jupyter process management
Django's personal tips
Python debugging tips
Python click tips
Powerful Jupyter Notebook
[pandas] GroupBy Tips
Unexpectedly (?) Python tips
Golang on jupyter
Tensorflow personal tips
Bash in Jupyter
Django Template Tips
Jupyter on AWS
fabric Writing Tips
Jupyter Study Note_002
Jupyter notebook password
Build Jupyter Hub
Jupyter Notebook memo
Jupyter study notes_008
LTTng Tracing Tips
Jupyter study notes_004
jupyter qtconsole config
Jupyter study notes_001
Jupyter Study Note_003
Jupyter Study Note_007
Choregraphe Tips Collection
Jupyter Study Note_005
[Tips] Save / copy the graph displayed in Jupyter Lab