Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168733
Image: ubuntu2004
G = Graph(graphs.GridGraph([50,50]), implementation='c_graph', sparse=True)
%cython from sage.graphs.graph import Graph from sage.all import graphs from sage.graphs.base.sparse_graph cimport SparseGraph def iterate1(G): cdef SparseGraph SG = <SparseGraph> G._backend._cg n=G.order() #cdef int i #cdef list nbrs nbrs=[] for i in range(n): nbrs.append(SG.in_neighbors(i)) return nbrs
timeit('a=iterate1(G)')
5 loops, best of 3: 71.8 ms per loop
%cython from sage.graphs.graph import Graph from sage.all import graphs from sage.graphs.base.sparse_graph cimport SparseGraph def iterate2(G): cdef SparseGraph SG = <SparseGraph> G._backend._cg cdef int i cdef int n = G.order() cdef list nbrs c=1 cdef int *neighbors = <int *> sage_malloc(size * sizeof(int)) if not neighbors: raise RuntimeError("Failure allocating memory.") num_nbrs = self.out_neighbors_unsafe(u, neighbors, size) output = [neighbors[i] for i from 0 <= i < num_nbrs] sage_free(neighbors) for i in range(n): c+=len(SG.in_neighbors_unsafe(i)) return c
Traceback (most recent call last): File "", line 1, in <module> File "/sagenb/sage_install/sage-4.7/devel/rkirov-flask/sagenb/misc/support.py", line 519, in cython_import_all create_local_c_file=create_local_c_file) File "/sagenb/sage_install/sage-4.7/devel/rkirov-flask/sagenb/misc/support.py", line 496, in cython_import create_local_c_file=create_local_c_file) File "/sagenb/sage_install/sage-4.7/local/lib/python2.6/site-packages/sage/misc/cython.py", line 367, in cython raise RuntimeError, "Error converting %s to C:\n%s\n%s"%(filename, log, err) RuntimeError: Error converting /sagenb/servers/sage_notebook-sagenb.sagenb/home/jason3/456/code/sage60.spyx to C: Error compiling Cython file: ------------------------------------------------------------ ... cdef int n = G.order() cdef list nbrs c=1 for i in range(n): c+=len(SG.in_neighbors_unsafe(i)) ^ ------------------------------------------------------------ _sagenb_servers_sage_notebook_sagenb_sagenb_home_jason3_456_code_sage60_spyx_0.pyx:19:37: Call with wrong number of arguments (expected 4, got 2)
timeit('a=iterate2(G)')
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_48.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dGltZWl0KCdhPWl0ZXJhdGUyKEcpJyk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmpo_UQ3S/___code___.py", line 2, in <module> exec compile(u"timeit('a=iterate2(G)')" + '\n', '', 'single') File "", line 1, in <module> File "sage_timeit_class.pyx", line 82, in sage.misc.sage_timeit_class.SageTimeit.__call__ (sage/misc/sage_timeit_class.c:744) File "sage_timeit_class.pyx", line 59, in sage.misc.sage_timeit_class.SageTimeit.eval (sage/misc/sage_timeit_class.c:605) File "/sagenb/sage_install/sage-4.7/local/lib/python2.6/site-packages/sage/misc/sage_timeit.py", line 181, in sage_timeit if timer.timeit(number) >= 0.2: File "/sagenb/sage_install/sage-4.7/local/lib/python/timeit.py", line 193, in timeit timing = self.inner(it, self.timer) File "<magic-timeit>", line 6, in inner File "_sagenb_servers_sage_notebook_sagenb_sagenb_home_jason3_456_code_sage57_spyx_0.pyx", line 19, in _sagenb_servers_sage_notebook_sagenb_sagenb_home_jason3_456_code_sage57_spyx_0.iterate2 (_sagenb_servers_sage_notebook_sagenb_sagenb_home_jason3_456_code_sage57_spyx_0.c:696) c+=len(SG.neighbors_unsafe(i)) AttributeError: 'sage.graphs.base.sparse_graph.SparseGraph' object has no attribute 'neighbors_unsafe'
def iterate_py(G): nbrs=[] for i in G.vertices(): nbrs.append(G.neighbors(i)) return nbrs
timeit('a=iterate_py(G)')
5 loops, best of 3: 46.4 ms per loop