Skip to content

Commit 3c616bf

Browse files
committed
add example of custom c++ function
1 parent ebe48c1 commit 3c616bf

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

docs/examples/cpp-interaction.ipynb

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,36 @@
235235
{
236236
"cell_type": "code",
237237
"execution_count": 5,
238+
"id": "sharing-sugar",
239+
"metadata": {
240+
"collapsed": false,
241+
"execution": {
242+
"iopub.execute_input": "2021-09-14T13:18:50.317866Z",
243+
"iopub.status.busy": "2021-09-14T13:18:50.317492Z",
244+
"iopub.status.idle": "2021-09-14T13:18:50.319354Z",
245+
"shell.execute_reply": "2021-09-14T13:18:50.318988Z"
246+
},
247+
"pycharm": {
248+
"name": "#%%\n"
249+
}
250+
},
251+
"outputs": [],
252+
"source": [
253+
"%%cppdef\n",
254+
"\n",
255+
"// implemented in C++ for efficiency\n",
256+
"int avg_width(const ogdf::GraphAttributes &GA) {\n",
257+
" int sum = 0;\n",
258+
" for (auto n : GA.constGraph().nodes) {\n",
259+
" sum += GA.width(n);\n",
260+
" }\n",
261+
" return sum / GA.constGraph().numberOfNodes();\n",
262+
"}"
263+
]
264+
},
265+
{
266+
"cell_type": "code",
267+
"execution_count": 6,
238268
"id": "brutal-egyptian",
239269
"metadata": {
240270
"execution": {
@@ -249,7 +279,8 @@
249279
"name": "stdout",
250280
"output_type": "stream",
251281
"text": [
252-
"The node widths are 20 20 20 20 40 20 20 20 20 20 20 20 20 20 20 20\n"
282+
"The node widths are 20 20 20 20 40 20 20 20 20 20 20 20 20 20 20 20\n",
283+
"The average width is 22\n"
253284
]
254285
},
255286
{
@@ -273,13 +304,14 @@
273304
],
274305
"source": [
275306
"print(\"The node widths are\", GA.width())\n",
307+
"print(\"The average width is\", cpp.avg_width(GA)) # call your own C++ functions from python\n",
276308
"\n",
277309
"dict(zip(G.nodes, GA.width()))"
278310
]
279311
},
280312
{
281313
"cell_type": "code",
282-
"execution_count": 6,
314+
"execution_count": 7,
283315
"id": "covered-effect",
284316
"metadata": {
285317
"execution": {
@@ -411,7 +443,7 @@
411443
},
412444
{
413445
"cell_type": "code",
414-
"execution_count": 7,
446+
"execution_count": 8,
415447
"id": "wrapped-mexican",
416448
"metadata": {
417449
"execution": {

0 commit comments

Comments
 (0)