Automatically generated Python scripts and Jupyter notebook¶
The library MoebInv can create annotatted Python scripts from existing figures--either created in C++ code or by mouse clicks in GUI. The next section contains Jupyter notebook produced by p2j (Python to Jupyter) from the automatically generated Python script. This script is included in the last section of this notebook as well.
Such scripts are produced by calls to figure::write_python_script()
or its Python wrapper like this:
F.write_python_script("file-name.py")
To evaluate the notebook we need to load libraries and optional magic extensions.
from figure import *
Figure: This is an illustration of the Nine point theorem.
We define new figure with the following point and cycle metrics:
MyFig = figure(matrix([[-1, 0], [0, -1]]),\
matrix([[-1, 0], [0, -1]]))
The figure description¶
MyFig.info_write(\
"""
This is an illustration of the Nine point theorem.
For the triangle ABC defined by three given points A, B and C we construct:
* the feets of altitudes A_h, B_h and C_h;
* the midpoints of sides A_m, B_m and C_m;
* the midpoints A_d, B_d and C_d between the respective vertices and the point O of intersection of the altitudes.
All these nine points belong to the Euler circle p. Furthermore, p is tangent to three excircles v_a, v_b and v_c.
A user may play with this figure by moving points A, B and C around. It is even more intriguing to change the metrics of point and cycle spaces from the default elliptic to hyperbolic and parabolic!
""")
Cycles in generation 0
The following cycles are proper point(s)
C = MyFig.add_point([-1/5, -3/2], "C")
B = MyFig.add_point([1, 0], "B")
A = MyFig.add_point([-1, 0], "A")
The following cycle is defined by coefficients
N = MyFig.add_cycle(cycle_data(0, matrix([[0, 0]]), 1), "N")
Subfigure: Midpoint in cycle metric.
We will use the following subfigure
MyFigSF1 = figure(matrix([[-1, 0], [0, -1]]),\
matrix([[-1, 0], [0, -1]])).freeze()
MyFigSF1.info_write(\
"""
Midpoint in cycle metric.
For two points provided the figure returns their midpoint.
For three given points A, B and N the subfigure produces
such a point C on the cycle passing A, B and N,
such that the cross-ratio of A, B, C, N is equal to 1.
If N is infinity (the default value, if it is not supplied),
then C is the midpoint between A and B as said above.
More generally, for any two cycles A and B for N=infty
figures returns the point between centres A and B
which has equal tangential distance to A and B.
.
To use this subfigure provide cycles A, B and N in this order.
""")
variable002SF1 = MyFigSF1.add_cycle(cycle_data(0, matrix([[0, 0]]), 1), "variable002")
variable000SF1 = MyFigSF1.add_cycle(cycle_data(1, matrix([[1, 0]]), 1), "variable000")
variable001SF1 = MyFigSF1.add_cycle(cycle_data(1, matrix([[0, 0]]), 0), "variable001")
v4SF1 = MyFigSF1.add_cycle_rel([cycle_relation(variable000SF1, "orthogonal", True), cycle_relation(variable001SF1, "orthogonal", True), cycle_relation(variable002SF1, "orthogonal", True)], "v4")
v5SF1 = MyFigSF1.add_cycle_rel([cycle_relation(variable000SF1, "orthogonal", True), cycle_relation(variable001SF1, "orthogonal", True), cycle_relation(v4SF1, "orthogonal", True)], "v5")
v6SF1 = MyFigSF1.add_cycle_rel([cycle_relation(variable002SF1, "orthogonal", True), cycle_relation(v4SF1, "orthogonal", True), cycle_relation(v5SF1, "orthogonal", True)], "v6")
resultSF1 = realsymbol("result")
MyFigSF1.add_cycle_rel([cycle_relation(v4SF1, "orthogonal", True), cycle_relation(v6SF1, "orthogonal", True), cycle_relation(resultSF1, "orthogonal", False), cycle_relation(variable002SF1, "adifferent", True)], resultSF1)
Cycles in generation 1
The following cycles are defined by the above subfigure(s)
C_m = MyFig.add_subfigure(MyFigSF1, [A, B, N], "C_m")
MyFig.set_asy_style(C_m, "\nNE")
A_m = MyFig.add_subfigure(MyFigSF1, [B, C, N], "A_m")
MyFig.set_asy_style(A_m, "\nE")
B_m = MyFig.add_subfigure(MyFigSF1, [C, A, N], "B_m")
MyFig.set_asy_style(B_m, "\nNE")
The following cycles are defined by cycle relations
b = MyFig.add_cycle_rel([cycle_relation(A, "orthogonal", True), cycle_relation(C, "orthogonal", True), cycle_relation(N, "orthogonal", True)], "b")
MyFig.set_asy_style(b, "rgb(0,0,.8)+1")
a = MyFig.add_cycle_rel([cycle_relation(B, "orthogonal", True), cycle_relation(C, "orthogonal", True), cycle_relation(N, "orthogonal", True)], "a")
MyFig.set_asy_style(a, "rgb(0,0,.8)+1")
c = MyFig.add_cycle_rel([cycle_relation(A, "orthogonal", True), cycle_relation(B, "orthogonal", True), cycle_relation(N, "orthogonal", True)], "c")
MyFig.set_asy_style(c, "\nNE")
Cycles in generation 2
The following cycles are defined by cycle relations
h_b = MyFig.add_cycle_rel([cycle_relation(B, "orthogonal", True), cycle_relation(N, "orthogonal", True), cycle_relation(b, "orthogonal", True)], "h_b")
MyFig.set_asy_style(h_b, "dashed")
v_a = MyFig.add_cycle_rel([cycle_relation(a, "tangent_o", True), cycle_relation(b, "tangent_i", True), cycle_relation(c, "tangent_i", True)], "v_a")
MyFig.set_asy_style(v_a, "rgb(0.8,0,0)+.5\nSE")
v_c = MyFig.add_cycle_rel([cycle_relation(a, "tangent_i", True), cycle_relation(b, "tangent_i", True), cycle_relation(c, "tangent_o", True)], "v_c")
MyFig.set_asy_style(v_c, "rgb(0.8,0,0)+.5")
h_c = MyFig.add_cycle_rel([cycle_relation(C, "orthogonal", True), cycle_relation(N, "orthogonal", True), cycle_relation(c, "orthogonal", True)], "h_c")
MyFig.set_asy_style(h_c, "dashed")
v_b = MyFig.add_cycle_rel([cycle_relation(a, "tangent_i", True), cycle_relation(b, "tangent_o", True), cycle_relation(c, "tangent_i", True)], "v_b")
MyFig.set_asy_style(v_b, "rgb(0.8,0,0)+.5")
h_a = MyFig.add_cycle_rel([cycle_relation(A, "orthogonal", True), cycle_relation(N, "orthogonal", True), cycle_relation(a, "orthogonal", True)], "h_a")
MyFig.set_asy_style(h_a, "dashed")
Cycles in generation 3
The following cycles are defined by cycle relations
A_h = realsymbol("A_h")
MyFig.add_cycle_rel([cycle_relation(a, "orthogonal", True), cycle_relation(h_a, "orthogonal", True), cycle_relation(A_h, "orthogonal", True), cycle_relation(N, "adifferent", True)], A_h)
O = realsymbol("O")
MyFig.add_cycle_rel([cycle_relation(h_a, "orthogonal", True), cycle_relation(h_b, "orthogonal", True), cycle_relation(O, "orthogonal", True), cycle_relation(N, "adifferent", True)], O)
B_h = realsymbol("B_h")
MyFig.add_cycle_rel([cycle_relation(b, "orthogonal", True), cycle_relation(h_b, "orthogonal", True), cycle_relation(N, "adifferent", True), cycle_relation(B_h, "orthogonal", True)], B_h)
MyFig.set_asy_style(B_h, "\nS")
C_h = realsymbol("C_h")
MyFig.add_cycle_rel([cycle_relation(N, "adifferent", True), cycle_relation(c, "orthogonal", True), cycle_relation(h_c, "orthogonal", True), cycle_relation(C_h, "orthogonal", True)], C_h)
MyFig.set_asy_style(C_h, "\nNW")
Cycles in generation 4
The following cycles are defined by the above subfigure(s)
A_d = MyFig.add_subfigure(MyFigSF1, [O, A, N], "A_d")
MyFig.set_asy_style(A_d, "\nE")
C_d = MyFig.add_subfigure(MyFigSF1, [C, O, N], "C_d")
B_d = MyFig.add_subfigure(MyFigSF1, [B, O, N], "B_d")
MyFig.set_asy_style(B_d, "\nNE")
The following cycle is defined by cycle relations
p = MyFig.add_cycle_rel([cycle_relation(A_h, "orthogonal", True), cycle_relation(B_h, "orthogonal", True), cycle_relation(C_h, "orthogonal", True)], "p")
MyFig.set_asy_style(p, "rgb(0,.8,0)+1\nSW")
The automatically generated Python script¶
The above notebook is produced by p2j (Python to Jupyter) utility from the Python script provided below. It is included ``as-is'' after its generaion by the library.
# ## Figure: This is an illustration of the Nine point theorem.
# We define new figure with the following point and cycle metrics:
MyFig = figure(matrix([[-1, 0], [0, -1]]),\
matrix([[-1, 0], [0, -1]]))
# ### The figure description
MyFig.info_write(\
"""
This is an illustration of the Nine point theorem.
For the triangle ABC defined by three given points A, B and C we construct:
* the feets of altitudes A_h, B_h and C_h;
* the midpoints of sides A_m, B_m and C_m;
* the midpoints A_d, B_d and C_d between the respective vertices and the point O of intersection of the altitudes.
All these nine points belong to the Euler circle p. Furthermore, p is tangent to three excircles v_a, v_b and v_c.
A user may play with this figure by moving points A, B and C around. It is even more intriguing to change the metrics of point and cycle spaces from the default elliptic to hyperbolic and parabolic!
""")
# ## Cycles in generation 0
# The following cycles are proper point(s)
C = MyFig.add_point([-1/5, -3/2], "C")
B = MyFig.add_point([1, 0], "B")
A = MyFig.add_point([-1, 0], "A")
# The following cycle is defined by coefficients
N = MyFig.add_cycle(cycle_data(0, matrix([[0, 0]]), 1), "N")
# ### Subfigure: Midpoint in cycle metric.
# We will use the following subfigure
MyFigSF1 = figure(matrix([[-1, 0], [0, -1]]),\
matrix([[-1, 0], [0, -1]])).freeze()
MyFigSF1.info_write(\
"""
Midpoint in cycle metric.
For two points provided the figure returns their midpoint.
For three given points A, B and N the subfigure produces
such a point C on the cycle passing A, B and N,
such that the cross-ratio of A, B, C, N is equal to 1.
If N is infinity (the default value, if it is not supplied),
then C is the midpoint between A and B as said above.
More generally, for any two cycles A and B for N=infty
figures returns the point between centres A and B
which has equal tangential distance to A and B.
.
To use this subfigure provide cycles A, B and N in this order.
""")
variable002SF1 = MyFigSF1.add_cycle(cycle_data(0, matrix([[0, 0]]), 1), "variable002")
variable000SF1 = MyFigSF1.add_cycle(cycle_data(1, matrix([[1, 0]]), 1), "variable000")
variable001SF1 = MyFigSF1.add_cycle(cycle_data(1, matrix([[0, 0]]), 0), "variable001")
v4SF1 = MyFigSF1.add_cycle_rel([cycle_relation(variable000SF1, "orthogonal", True), cycle_relation(variable001SF1, "orthogonal", True), cycle_relation(variable002SF1, "orthogonal", True)], "v4")
v5SF1 = MyFigSF1.add_cycle_rel([cycle_relation(variable000SF1, "orthogonal", True), cycle_relation(variable001SF1, "orthogonal", True), cycle_relation(v4SF1, "orthogonal", True)], "v5")
v6SF1 = MyFigSF1.add_cycle_rel([cycle_relation(variable002SF1, "orthogonal", True), cycle_relation(v4SF1, "orthogonal", True), cycle_relation(v5SF1, "orthogonal", True)], "v6")
resultSF1 = realsymbol("result")
MyFigSF1.add_cycle_rel([cycle_relation(v4SF1, "orthogonal", True), cycle_relation(v6SF1, "orthogonal", True), cycle_relation(resultSF1, "orthogonal", False), cycle_relation(variable002SF1, "adifferent", True)], resultSF1)
# ## Cycles in generation 1
# The following cycles are defined by the above subfigure(s)
C_m = MyFig.add_subfigure(MyFigSF1, [A, B, N], "C_m")
MyFig.set_asy_style(C_m, "\nNE")
A_m = MyFig.add_subfigure(MyFigSF1, [B, C, N], "A_m")
MyFig.set_asy_style(A_m, "\nE")
B_m = MyFig.add_subfigure(MyFigSF1, [C, A, N], "B_m")
MyFig.set_asy_style(B_m, "\nNE")
# The following cycles are defined by cycle relations
b = MyFig.add_cycle_rel([cycle_relation(A, "orthogonal", True), cycle_relation(C, "orthogonal", True), cycle_relation(N, "orthogonal", True)], "b")
MyFig.set_asy_style(b, "rgb(0,0,.8)+1")
a = MyFig.add_cycle_rel([cycle_relation(B, "orthogonal", True), cycle_relation(C, "orthogonal", True), cycle_relation(N, "orthogonal", True)], "a")
MyFig.set_asy_style(a, "rgb(0,0,.8)+1")
c = MyFig.add_cycle_rel([cycle_relation(A, "orthogonal", True), cycle_relation(B, "orthogonal", True), cycle_relation(N, "orthogonal", True)], "c")
MyFig.set_asy_style(c, "\nNE")
# ## Cycles in generation 2
# The following cycles are defined by cycle relations
h_b = MyFig.add_cycle_rel([cycle_relation(B, "orthogonal", True), cycle_relation(N, "orthogonal", True), cycle_relation(b, "orthogonal", True)], "h_b")
MyFig.set_asy_style(h_b, "dashed")
v_a = MyFig.add_cycle_rel([cycle_relation(a, "tangent_o", True), cycle_relation(b, "tangent_i", True), cycle_relation(c, "tangent_i", True)], "v_a")
MyFig.set_asy_style(v_a, "rgb(0.8,0,0)+.5\nSE")
v_c = MyFig.add_cycle_rel([cycle_relation(a, "tangent_i", True), cycle_relation(b, "tangent_i", True), cycle_relation(c, "tangent_o", True)], "v_c")
MyFig.set_asy_style(v_c, "rgb(0.8,0,0)+.5")
h_c = MyFig.add_cycle_rel([cycle_relation(C, "orthogonal", True), cycle_relation(N, "orthogonal", True), cycle_relation(c, "orthogonal", True)], "h_c")
MyFig.set_asy_style(h_c, "dashed")
v_b = MyFig.add_cycle_rel([cycle_relation(a, "tangent_i", True), cycle_relation(b, "tangent_o", True), cycle_relation(c, "tangent_i", True)], "v_b")
MyFig.set_asy_style(v_b, "rgb(0.8,0,0)+.5")
h_a = MyFig.add_cycle_rel([cycle_relation(A, "orthogonal", True), cycle_relation(N, "orthogonal", True), cycle_relation(a, "orthogonal", True)], "h_a")
MyFig.set_asy_style(h_a, "dashed")
# ## Cycles in generation 3
# The following cycles are defined by cycle relations
A_h = realsymbol("A_h")
MyFig.add_cycle_rel([cycle_relation(a, "orthogonal", True), cycle_relation(h_a, "orthogonal", True), cycle_relation(A_h, "orthogonal", True), cycle_relation(N, "adifferent", True)], A_h)
O = realsymbol("O")
MyFig.add_cycle_rel([cycle_relation(h_a, "orthogonal", True), cycle_relation(h_b, "orthogonal", True), cycle_relation(O, "orthogonal", True), cycle_relation(N, "adifferent", True)], O)
B_h = realsymbol("B_h")
MyFig.add_cycle_rel([cycle_relation(b, "orthogonal", True), cycle_relation(h_b, "orthogonal", True), cycle_relation(N, "adifferent", True), cycle_relation(B_h, "orthogonal", True)], B_h)
MyFig.set_asy_style(B_h, "\nS")
C_h = realsymbol("C_h")
MyFig.add_cycle_rel([cycle_relation(N, "adifferent", True), cycle_relation(c, "orthogonal", True), cycle_relation(h_c, "orthogonal", True), cycle_relation(C_h, "orthogonal", True)], C_h)
MyFig.set_asy_style(C_h, "\nNW")
# ## Cycles in generation 4
# The following cycles are defined by the above subfigure(s)
A_d = MyFig.add_subfigure(MyFigSF1, [O, A, N], "A_d")
MyFig.set_asy_style(A_d, "\nE")
C_d = MyFig.add_subfigure(MyFigSF1, [C, O, N], "C_d")
B_d = MyFig.add_subfigure(MyFigSF1, [B, O, N], "B_d")
MyFig.set_asy_style(B_d, "\nNE")
# The following cycle is defined by cycle relations
p = MyFig.add_cycle_rel([cycle_relation(A_h, "orthogonal", True), cycle_relation(B_h, "orthogonal", True), cycle_relation(C_h, "orthogonal", True)], "p")
MyFig.set_asy_style(p, "rgb(0,.8,0)+1\nSW")
Finally, we visualise the produced figure to compare it with the original one from the notebook:
%load_ext figure.figure_ipython
%asy_write MyFig, 600, -1.75, 1.75, -2, 1.5, "test-9p"