From a5ed7b3c096e94c899995c348223fed1fe448c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horva=CC=81t?= Date: Fri, 28 Jun 2024 16:42:07 +0000 Subject: [PATCH] feat: mean_degree() --- src/_igraph/graphobject.c | 31 +++++++++++++++++++++++++++++++ tests/test_structural.py | 7 +++++++ 2 files changed, 38 insertions(+) diff --git a/src/_igraph/graphobject.c b/src/_igraph/graphobject.c index 31049e887..f97c2a2f5 100644 --- a/src/_igraph/graphobject.c +++ b/src/_igraph/graphobject.c @@ -962,6 +962,29 @@ PyObject *igraphmodule_Graph_density(igraphmodule_GraphObject * self, return igraphmodule_real_t_to_PyObject(res, IGRAPHMODULE_TYPE_FLOAT); } +/** \ingroup python_interface_graph + * \brief Calculates the mean degree + * \return the mean degree + * \sa igraph_mean_degree + */ +PyObject *igraphmodule_Graph_mean_degree(igraphmodule_GraphObject * self, + PyObject * args, PyObject * kwds) +{ + char *kwlist[] = { "loops", NULL }; + igraph_real_t res; + PyObject *loops = Py_True; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &loops)) + return NULL; + + if (igraph_mean_degree(&self->g, &res, PyObject_IsTrue(loops))) { + igraphmodule_handle_igraph_error(); + return NULL; + } + + return igraphmodule_real_t_to_PyObject(res, IGRAPHMODULE_TYPE_FLOAT); +} + /** \ingroup python_interface_graph * \brief The maximum degree of some vertices in an \c igraph.Graph * \return the maxium degree as a Python object @@ -15075,6 +15098,14 @@ struct PyMethodDef igraphmodule_Graph_methods[] = { " assumes that there can't be any loops.\n" "@return: the density of the graph."}, + /* interface to igraph_mean_degree */ + {"mean_degree", (PyCFunction) igraphmodule_Graph_mean_degree, + METH_VARARGS | METH_KEYWORDS, + "mean_degree(loops=True)\n--\n\n" + "Calculates the mean degree of the graph.\n\n" + "@param loops: whether to consider self-loops during the calculation\n" + "@return: the mean degree of the graph."}, + /* interfaces to igraph_diameter */ {"diameter", (PyCFunction) igraphmodule_Graph_diameter, METH_VARARGS | METH_KEYWORDS, diff --git a/tests/test_structural.py b/tests/test_structural.py index bfda0f2e5..b9d90a7dd 100644 --- a/tests/test_structural.py +++ b/tests/test_structural.py @@ -24,6 +24,13 @@ def testDensity(self): self.assertAlmostEqual(7 / 16, self.gdir.density(True), places=5) self.assertAlmostEqual(1 / 7, self.tree.density(), places=5) + def testMeanDegree(self): + self.assertEqual(9.0, self.gfull.mean_degree()) + self.assertEqual(0.0, self.gempty.mean_degree()) + self.assertEqual(2.5, self.g.mean_degree()) + self.assertEqual(7 / 4, self.gdir.mean_degree()) + self.assertAlmostEqual(13 / 7, self.tree.mean_degree(), places=5) + def testDiameter(self): self.assertTrue(self.gfull.diameter() == 1) self.assertTrue(self.gempty.diameter(unconn=False) == inf)