cython

How is numpy multi_dot slower than numpy.dot?

泪湿孤枕 提交于 2019-12-31 05:19:06
问题 I'm trying to optimize some code that performs lots of sequential matrix operations. I figured numpy.linalg.multi_dot (docs here) would perform all the operations in C or BLAS and thus it would be way faster than going something like arr1.dot(arr2).dot(arr3) and so on. I was really surprised running this code on a notebook: v1 = np.random.rand(2,2) v2 = np.random.rand(2,2) %%timeit ​ v1.dot(v2.dot(v1.dot(v2))) The slowest run took 9.01 times longer than the fastest. This could mean that an

Why Cython forces declaration of locals at the beginning of a function

一世执手 提交于 2019-12-31 03:54:08
问题 This was asked as a comment in Cython - copy constructors. The following code doesn't compile in Cython: def bar(int i): if i == 0: return i else: cdef int j j = i+1 return j whereas this one is perfectly correct: def foo(int i): cdef int j if i == 0: return i else: j = i+1 return j The question is: why does Cython forces to declare j at the beginning of the function and not in the else block ? 回答1: The reason is scoping rule in Python vs C/C++. Cython is trying to get the better of both

Cython: Avoid copy through std::move not working

自古美人都是妖i 提交于 2019-12-31 03:32:29
问题 Problem I have a very large std::vector that gets returned from a C++ function, let's call it getVector() . Now I want to wrap that function in Cython: cdef extern from "XY.h": cdef cppclass _XY: vector[double] getVector() except + cdef class XY: cdef _XY _this ... def getVector(): return self._this.getVector() As I want to avoid copying this large vector, I would like to make use of std::move. Like this: cdef extern from "<utility>" namespace "std": vector[double] move(vector[double]) #

Can you cimport an .so file?

拟墨画扇 提交于 2019-12-31 02:50:11
问题 I have an .so file called tissue-classifier.cpython-37m-x86_64-linux-gnu.so from an external library that I want to import so that I can extend it in one of my local classes. Since I am extending a class, I need to import it as an extension type using cimport and I am wondering if this is even possible. If I use a normal import statement then I will be left with a Python compiled version which cannot be used to extend a cdef class in my current file. When I try to cimport the tissue

Cython: templates in python class wrappers

情到浓时终转凉″ 提交于 2019-12-30 17:23:12
问题 Question Is there a way to create a Python wrapper for Cython-wrapped C++ class with templates? (i.e. do exactly what is show here but with templates: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#create-cython-wrapper-class). I know about the fused types workaround (https://groups.google.com/forum/#!topic/cython-users/qQpMo3hGQqI) but that doesn't allow you to instatiate classes like vector<vector<int>> : the fused types have, quite unsurprisingly, no notion of recursion.

How to call time from time.h with Cython?

孤街醉人 提交于 2019-12-30 12:13:16
问题 I am trying to load time.h directly with Cython instead of Python's import time but it doesn't work. All I get is an error Call with wrong number of arguments (expected 1, got 0) with the following code cdef extern from "time.h" nogil: ctypedef int time_t time_t time(time_t*) def test(): cdef int ts ts = time() return ts and Cannot assign type 'long' to 'time_t *' with the following code cdef extern from "time.h" nogil: ctypedef int time_t time_t time(time_t*) def test(): cdef int ts ts =

Cythonize list of all splits of a string

北慕城南 提交于 2019-12-30 10:12:03
问题 I'm trying to speed up a piece of code that generates all possible splits of a string. splits('foo') -> [('f', 'oo'), ('fo', 'o'), ('foo', '')] The code for this in python is very simple: def splits(text): return [(text[:i + 1], text[i + 1:]) for i in range(len(text))] Is there a way to speed this up via cython or some other means? For context, the greater purpose of this code is to find the split of a string with the highest probability. 回答1: This isn't the sort of problem that Cython tends

Using Opencv Cuda functions from python

霸气de小男生 提交于 2019-12-30 10:04:46
问题 For one of my course projects, I need to use the OpenCVs GPU libraries. I am working on an existing code where OpenCV python is used and my work is to find a way to access the OpenCV Cuda libraries as right now there are no accessible Python bindings to OpenCV's various CUDA modules. Two of the functions that i extremely need right now are cuda::warpPerspective and cv::cuda::DescriptorMatcher::knnMatch() . i tried to implement the warpPerspective by following what @ostrumvulpes suggested in

Specify cython output file

梦想与她 提交于 2019-12-30 07:53:27
问题 It seems that by default setup from distutils.core with cmdclass set to build_ext, compiles a cpp or c file in the current working directory. Is there a way to determine where the generated c code is written to? Otherwise, a repository will be littered with generated code. For example this file setup.py will write a file example.c to the current working directory: from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("example.pyx")) 回答1: You can

Specify cython output file

大城市里の小女人 提交于 2019-12-30 07:53:01
问题 It seems that by default setup from distutils.core with cmdclass set to build_ext, compiles a cpp or c file in the current working directory. Is there a way to determine where the generated c code is written to? Otherwise, a repository will be littered with generated code. For example this file setup.py will write a file example.c to the current working directory: from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("example.pyx")) 回答1: You can