<jupyter_start><jupyter_text>Python  Julia  Bridge<jupyter_code>%load_ext julia.magic 
julia_version = %julia VERSION
julia_version
%%julia
@pyimport matplotlib.pyplot as plt
@pyimport numpy as np
%%julia
                                        # Note how we mix numpy and julia:
t = linspace(0, 2*pi,1000);             # use the julia `linspace` and `pi`
s = sin(3*t + 4*np.cos(2*t));           # use the numpy cosine and julia sine
fig = plt.gcf()                         # **** WATCH THIS VARIABLE ****
plt.plot(t, s, color="red", linewidth=2.0, linestyle="--", label="sin(3t+4.cos(2t))")
import numpy as np
fig = %julia fig
x = np.arange(0, 6, 0.01)
fig.axes[0].plot(x, np.sin(x), '--', label='sin')
fig.axes[0].set_title('A weird Julia function and Fib')
fig.axes[0].legend()

fig
from __future__ import print_function


# julia fib function
jlfib = %julia _fib(n, pyfib) = n <= 2 ? 1 : pyfib(n-1, _fib) + pyfib(n-2, _fib)


def pyfib(n, _fib):
    """
    Python fib function
    """
    #print('(P', end='')
    if n <= 2:
         r = 1
    else:
        #print('(J', end='')
        # here we tell julia (_fib) to recurse using Python
        r =  _fib(n-1, pyfib) + _fib(n-2, pyfib)
        #print(')',end='')
    #print(')',end='')
    return r

fibonacci = lambda x: pyfib(x, jlfib)

fibonacci(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1672890216579537, 'fraction_numerical': 0.02389843166542196, 'mean_word_length': 2.7430167597765363, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '337547', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 563, 'n_tokens_neox': 523, 'n_words': 135, 'path': 'examples/Notebook/Julia and Python Bridge.ipynb', 'repo_name': '22sangho/ipython-in-depth', 'size': '1339'}