Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New cfunc API #167

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/notebooks/NeuralODEs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@
"outputs": [],
"source": [
"# Assemble the r.h.s. for scipy-integration\n",
"rhs = hy.make_cfunc(fn = [it[1] for it in dyn], compact_mode=True, vars = state + list(phi.flatten())+ list(varphi.flatten()))"
"rhs = hy.cfunc(fn = [it[1] for it in dyn], compact_mode=True, vars = state + list(phi.flatten())+ list(varphi.flatten()))"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
"\n",
"# Introduce a compiled function\n",
"# for the evaluation of the Hamiltonian.\n",
"H_cf = hy.make_cfunc([H], vars=[x, y, lx, ly, u])\n",
"H_cf = hy.cfunc([H], vars=[x, y, lx, ly, u])\n",
"\n",
"def _hamiltonian(x,y,lx,ly,p):\n",
" sw_v = switching_function(x,y,lx,ly,p)\n",
Expand Down
2 changes: 1 addition & 1 deletion doc/notebooks/Outer Solar System.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"metadata": {},
"outputs": [],
"source": [
"en_cf = hy.make_cfunc([hy.model.nbody_energy(6, masses = masses, Gconst = G)],\n",
"en_cf = hy.cfunc([hy.model.nbody_energy(6, masses = masses, Gconst = G)],\n",
" # NOTE: the variables for the compiled function\n",
" # are taken from the definition of the ODE system.\n",
" vars=[_[0] for _ in sys])"
Expand Down
4 changes: 2 additions & 2 deletions doc/notebooks/Periodic orbits in the CR3BP.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"source": [
"# Introduce a compiled function for the evaluation\n",
"# of the dynamics equation for px.\n",
"px_dyn_cf = hy.make_cfunc([f[3]], vars=x)\n",
"px_dyn_cf = hy.cfunc([f[3]], vars=x)\n",
"\n",
"def compute_L_points(mu):\n",
" \"\"\"Computes The exact position of the Lagrangian points. To do so it finds the zeros of the\n",
Expand Down Expand Up @@ -651,7 +651,7 @@
"source": [
"# Introduce a compiled function for the evaluation\n",
"# of the dynamics equations.\n",
"dyn_cf = hy.make_cfunc(f, vars=x)\n",
"dyn_cf = hy.cfunc(f, vars=x)\n",
"\n",
"def corrector(ta, x0):\n",
" \"\"\"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@
"f = np.array(f)\n",
"\n",
"# We compile the r.h.s. of the dynamics into compiled functions as to later be able to evaluate their numerical values magnitude.\n",
"cf_f = hy.make_cfunc(f, vars=x)\n",
"cf_px = hy.make_cfunc([f[3]], vars=[x[0],x[1],x[2],x[4]]) # x,y,z,py"
"cf_f = hy.cfunc(f, vars=x)\n",
"cf_px = hy.cfunc([f[3]], vars=[x[0],x[1],x[2],x[4]]) # x,y,z,py"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion doc/notebooks/arbitrary_precision.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@
"sym_func = x**2-y**2\n",
"\n",
"# Compile it.\n",
"cf = hy.make_cfunc([sym_func], [x, y],\n",
"cf = hy.cfunc([sym_func], [x, y],\n",
" fp_type=real, prec=prec)"
]
},
Expand Down
18 changes: 9 additions & 9 deletions doc/notebooks/compiled_functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"id": "68861247-8ac9-42a3-bf58-b893282ccdf7",
"metadata": {},
"source": [
"We can now proceed to JIT compile ``sym_func`` via the ``make_cfunc()`` function. ``make_cfunc()`` takes two mandatory input arguments:\n",
"We can now proceed to JIT compile ``sym_func`` via the ``cfunc()`` function. ``cfunc()`` takes two mandatory input arguments:\n",
"\n",
"- the list of symbolic expressions representing the outputs of the vector function, and\n",
"- the list of symbolic variables representing the inputs of the function.\n",
Expand All @@ -72,15 +72,15 @@
"metadata": {},
"outputs": [],
"source": [
"cf = hy.make_cfunc([sym_func], [x, y])"
"cf = hy.cfunc([sym_func], [x, y])"
]
},
{
"cell_type": "markdown",
"id": "8df3f92c-49b2-459d-bb0a-33e2e000812c",
"metadata": {},
"source": [
"The value returned by ``make_cfunc()`` is a callable function object which accepts as input a NumPy array representing the values to use in the evaluation of ``sym_func``:"
"The value returned by ``cfunc()`` is a callable function object which accepts as input a NumPy array representing the values to use in the evaluation of ``sym_func``:"
]
},
{
Expand Down Expand Up @@ -163,7 +163,7 @@
}
],
"source": [
"cf2 = hy.make_cfunc([sym_func], vars=[y,x])\n",
"cf2 = hy.cfunc([sym_func], vars=[y,x])\n",
"\n",
"# Evaluate for x=1 and y=5.\n",
"cf2([5,1])"
Expand Down Expand Up @@ -205,9 +205,9 @@
"id": "43f895e7-2da5-440d-84f8-1932d5a9d1fc",
"metadata": {},
"source": [
"``make_cfunc()`` accepts several additional keyword arguments, the most important of which is the boolean flag ``compact_mode`` (defaulting to ``False``). Similarly to the [adaptive Taylor integrators](<./Customising the adaptive integrator.ipynb>), you should enable ``compact_mode`` if you want to compile extremely large symbolic expressions that result in excessively long compilation times. The downside of ``compact_mode`` is a slight performance degradation due to the different code generation model adopted during the JIT compilation process.\n",
"``cfunc()`` accepts several additional keyword arguments, the most important of which is the boolean flag ``compact_mode`` (defaulting to ``False``). Similarly to the [adaptive Taylor integrators](<./Customising the adaptive integrator.ipynb>), you should enable ``compact_mode`` if you want to compile extremely large symbolic expressions that result in excessively long compilation times. The downside of ``compact_mode`` is a slight performance degradation due to the different code generation model adopted during the JIT compilation process.\n",
"\n",
"The function object returned by ``make_cfunc()`` also accepts several optional keyword arguments. It is possible, for instance, to pass as ``outputs`` argument a pre-allocated NumPy array into which the result of the evaluation will be written. This is useful to avoid the overhead of allocating new memory for the return value, if such memory is already available:"
"The function object returned by ``cfunc()`` also accepts several optional keyword arguments. It is possible, for instance, to pass as ``outputs`` argument a pre-allocated NumPy array into which the result of the evaluation will be written. This is useful to avoid the overhead of allocating new memory for the return value, if such memory is already available:"
]
},
{
Expand Down Expand Up @@ -273,7 +273,7 @@
"sym_func_par = hy.par[0]*x**2-hy.par[1]*y**2+hy.par[2]\n",
"\n",
"# Compile it.\n",
"cf_par = hy.make_cfunc([sym_func_par], [x, y])\n",
"cf_par = hy.cfunc([sym_func_par], [x, y])\n",
"\n",
"# Evaluate, specifying the parameter values\n",
"cf_par([1,5], pars=[-1, -2, -3])"
Expand Down Expand Up @@ -315,7 +315,7 @@
"sym_func_tm = x**2-y**2+hy.time\n",
"\n",
"# Compile it.\n",
"cf_tm = hy.make_cfunc([sym_func_tm], [x, y])\n",
"cf_tm = hy.cfunc([sym_func_tm], [x, y])\n",
"\n",
"# Evaluate for x=1, y=5 and time=6.\n",
"cf_tm([1,5], time=6)"
Expand Down Expand Up @@ -538,7 +538,7 @@
"metadata": {},
"outputs": [],
"source": [
"Ham_cf = hy.make_cfunc([Ham_sym], vars=[px,py,pz,x,y,z])"
"Ham_cf = hy.cfunc([Ham_sym], vars=[px,py,pz,x,y,z])"
]
},
{
Expand Down
38 changes: 23 additions & 15 deletions doc/notebooks/computing_derivatives.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@
"execution_count": 2,
"id": "3d51a390-e9fa-4cad-8bdc-358b5ea3cf53",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2024-02-12 07:49:15.252] [heyoka] [info] heyoka logger initialised\n"
]
}
],
"source": [
"dt = hy.diff_tensors([x + hy.cos(y*z), hy.exp(x-z) + hy.log(y)],\n",
" diff_args=[x, y, z], diff_order=1)"
Expand Down Expand Up @@ -472,7 +480,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"id": "4cab33d2-891e-43f8-93f6-dbcd66a369ba",
"metadata": {},
"outputs": [
Expand All @@ -489,14 +497,14 @@
" (u_0 * u_1 * u_2 * u_3 * u_4 * u_5 * u_7)]"
]
},
"execution_count": 15,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"grad_diff_cf = hy.make_cfunc(grad_diff, sym_vars)\n",
"grad_diff_cf.decomposition[8:-8]"
"grad_diff_cf = hy.cfunc(grad_diff, sym_vars)\n",
"grad_diff_cf.dc[8:-8]"
]
},
{
Expand All @@ -509,7 +517,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"id": "5d91bb57-227d-460f-8827-f01444399075",
"metadata": {},
"outputs": [
Expand All @@ -526,7 +534,7 @@
" {({x_7} * {({(x_5 * x_6)} * {((x_1 * x_2) * (x_3 * x_4))})})}]"
]
},
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -549,7 +557,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"id": "36e7c1ef-2b45-4a5d-a1ae-7272f26ce4c0",
"metadata": {},
"outputs": [
Expand All @@ -576,14 +584,14 @@
" (u_2 * u_17)]"
]
},
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"grad_diff_tensors_cf = hy.make_cfunc(grad_diff_tensors, sym_vars)\n",
"grad_diff_tensors_cf.decomposition[8:-8]"
"grad_diff_tensors_cf = hy.cfunc(grad_diff_tensors, sym_vars)\n",
"grad_diff_tensors_cf.dc[8:-8]"
]
},
{
Expand All @@ -596,7 +604,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"id": "4b719891-0d23-4e4e-b8e0-49ff0aea97bb",
"metadata": {},
"outputs": [
Expand All @@ -621,8 +629,8 @@
" grad_diff_tensors = [t[1] for t in hy.diff_tensors([sp_func],\n",
" diff_args=hy.diff_args.vars,\n",
" diff_order=1).get_derivatives(1)]\n",
" grad_diff_tensors_cf = hy.make_cfunc(grad_diff_tensors, sym_vars)\n",
" nops.append(len(grad_diff_tensors_cf.decomposition) - 2*nvars)\n",
" grad_diff_tensors_cf = hy.cfunc(grad_diff_tensors, sym_vars)\n",
" nops.append(len(grad_diff_tensors_cf.dc) - 2*nvars)\n",
"\n",
"%matplotlib inline\n",
"from matplotlib.pylab import plt\n",
Expand Down Expand Up @@ -672,7 +680,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion doc/notebooks/elp2000.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
" thresh=thr)\n",
"\n",
" # Compile the function for the evaluation of moon_x/y/z.\n",
" moon_cf = hy.make_cfunc([moon_x, moon_y, moon_z], [tm], compact_mode=True)\n",
" moon_cf = hy.cfunc([moon_x, moon_y, moon_z], [tm], compact_mode=True)\n",
" \n",
" # Run the evaluation.\n",
" elp2000_states = moon_cf(dates.reshape((1,-1)))\n",
Expand Down
28 changes: 18 additions & 10 deletions doc/notebooks/ex_system_revisited.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,17 @@
"execution_count": 17,
"id": "0312a937-a53b-4dc4-8570-274602e796f2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2024-02-12 07:51:33.090] [heyoka] [info] heyoka logger initialised\n"
]
}
],
"source": [
"f_cf = hy.make_cfunc([x+y+1., x+y+z], [x, y, z])"
"f_cf = hy.cfunc([x+y+1., x+y+z], [x, y, z])"
]
},
{
Expand Down Expand Up @@ -528,7 +536,7 @@
}
],
"source": [
"f_cf.decomposition"
"f_cf.dc"
]
},
{
Expand Down Expand Up @@ -608,7 +616,7 @@
"metadata": {},
"outputs": [],
"source": [
"f_cf_fix = hy.make_cfunc([hy.fix(x+y)+1., hy.fix(x+y)+z],\n",
"f_cf_fix = hy.cfunc([hy.fix(x+y)+1., hy.fix(x+y)+z],\n",
" [x, y, z])"
]
},
Expand Down Expand Up @@ -638,7 +646,7 @@
}
],
"source": [
"f_cf_fix.decomposition"
"f_cf_fix.dc"
]
},
{
Expand Down Expand Up @@ -711,8 +719,8 @@
}
],
"source": [
"g_cf = hy.make_cfunc([2.0 * (x + y + z)], [x, y, z])\n",
"g_cf.decomposition"
"g_cf = hy.cfunc([2.0 * (x + y + z)], [x, y, z])\n",
"g_cf.dc"
]
},
{
Expand Down Expand Up @@ -741,9 +749,9 @@
}
],
"source": [
"g_cf_fix = hy.make_cfunc([2.0 * hy.fix(x + y + z)],\n",
"g_cf_fix = hy.cfunc([2.0 * hy.fix(x + y + z)],\n",
" [x, y, z])\n",
"g_cf_fix.decomposition"
"g_cf_fix.dc"
]
},
{
Expand Down Expand Up @@ -879,7 +887,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions doc/notebooks/ffnn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
}
],
"source": [
"cf = hy.make_cfunc(ffnn, [x, y])\n",
"cf = hy.cfunc(ffnn, [x, y])\n",
"print(cf)"
]
},
Expand Down Expand Up @@ -251,7 +251,7 @@
}
],
"source": [
"cf = hy.make_cfunc(ffnn, [x, y])\n",
"cf = hy.cfunc(ffnn, [x, y])\n",
"print(cf)"
]
},
Expand Down
4 changes: 2 additions & 2 deletions doc/notebooks/lagrangian_propagator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
"metadata": {},
"outputs": [],
"source": [
"cf = hy.make_cfunc(pos_vel,\n",
"cf = hy.cfunc(pos_vel,\n",
" # Specify the order in which the input\n",
" # variables are passed to the compiled\n",
" # function.\n",
Expand Down Expand Up @@ -373,7 +373,7 @@
"metadata": {},
"outputs": [],
"source": [
"cf_stm = hy.make_cfunc(jac.flatten(),\n",
"cf_stm = hy.cfunc(jac.flatten(),\n",
" # Specify the order in which the input\n",
" # variables are passed to the compiled\n",
" # function.\n",
Expand Down
Loading