Skip to content

Instantly share code, notes, and snippets.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def transform_state_vector(rho, U):
N = len(U)
if rho.shape[-1] == N:
# rho is in Hilbert space
pass
elif rho.shape[-1] == N ** 2:
# rho is in Liouville space
U = np.kron(U, U)
else:
raise ValueError('basis transformation incompatible with '
import numpy as np
from numpy.lib.stride_tricks import as_strided
def broadcast_to(array, shape):
"""Expand a numpy.ndarray to a new shape according to broadcasting rules
"""
array = np.asarray(array)
# will raise ValueError if shapes incompatible
np.nditer((array,), itershape=shape)
@shoyer
shoyer / gist:9d06b7294a8d06981ec7
Last active August 29, 2015 14:13
numba performance with indexing
from numba import guvectorize, jit
import numpy as np
import pandas as pd
@guvectorize(['void(float64[:], int64[:], float64[:], float64[:])'],
'(x),(x),(y)->()')
def _grouped_sum_guvec_simple(values, labels, target, out):
for i in range(len(values)):
idx = labels[i]
target[idx] += values[i]
import inspect
import types
def injected(df, thunk):
"""Evaluate a thunk in the context of DataFrame
>>> df = pd.DataFrame({'x': [0, 1, 2]}, index=['a', 'b', 'c'])
>>> injected(df, lambda: x ** 2)
a 0
@shoyer
shoyer / xray_nd_groupby.py
Created February 19, 2015 23:15
xray nd-groupby
import functools
def nd_groupby_apply(ds, dims, func):
if isinstance(dims, str):
dims = [dims]
if len(dims) > 1:
func = functools.partial(nd_groupby_apply, dims=dims[1:], func=func)
return ds.groupby(dims[0]).apply(func)
def nd_group_over_apply(ds, dims, func):
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@shoyer
shoyer / gist:bfdda77549dcead3e996
Created April 15, 2015 18:41
xray groupby transform profiling
4926489 function calls (4835695 primitive calls) in 11.314 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
21928 1.839 0.000 4.441 0.000 slicing.py:202(slice_slices_and_integers)
4 1.521 0.380 1.610 0.402 {sum}
109869 1.494 0.000 1.494 0.000 {method 'update' of 'dict' objects}
131496 1.173 0.000 1.333 0.000 slicing.py:241(_slice_1d)
65748 0.624 0.000 1.551 0.000 slicing.py:544(new_blockdim)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jit(nopython=True)
def g0(x, y):
s = 0
for i in range(len(x)):
s += x[i] * y[i]
return s
@jit(nopython=True)
def g1(x, y):