Skip to content

Instantly share code, notes, and snippets.

@jdfr
jdfr / README.md
Created May 1, 2015 08:05
Cython: using C++ templates inside "with nogil" sections

Let's suppose you have implemented a templated function with two template arguments fun<A,B> in the files cside.cpp and cside.hpp. Now, you want to use it in cython, so you declare it:

cdef extern from "cside.hpp" nogil:
  void fun[A,B](A *arg1, B *arg2)

and you can use it:

@jdfr
jdfr / README.md
Last active August 29, 2015 14:20
Numpy's PyArray_New for dummies

When it comes to usign the Numpy C API to create arrays, there are plenty of resources to use the simpler functions, such as the PyArray_SimpleNewXXX() series. To use the most generic ones, you are supposed to be fairly well versed in Numpy. Unfortunately, there are functions of intermediate complexity, and resources for these are scarce.

Today, I had to create a Numpy array as a view (without copying data) of some fields in a vector of structs. Clearly, not a job for the PyArray_SimpleNewXXX() functions,

@jdfr
jdfr / bigperm.py
Created April 26, 2015 15:15
Algorithmic pseudo-random permutations for large sequences
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License v2 as published by
#the Free Software Foundation.
"""Algorithmic pseudo-random permutations for large sequences.
Module to generate random, arbitrarily large permutations. Random permutations
can be generated in Python with random.shuffle or numpy.random.shuffle:
import random