Skip to content

Instantly share code, notes, and snippets.

@TheBB
Created November 9, 2017 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheBB/ccf771da692bd10e5a2d40a633a9f3ac to your computer and use it in GitHub Desktop.
Save TheBB/ccf771da692bd10e5a2d40a633a9f3ac to your computer and use it in GitHub Desktop.
import numpy as np
import timeit
from nutils import core, log, function as fn, plot, _, mesh
def one(domain, basis, z):
mx = basis[:,_,_] * basis[_,:,_] * basis[_,_,:]
mx = (mx[:,_,:,_,:,_] * z[:,:,_,_,_,_] * z[_,_,:,:,_,_] * z[_,_,_,_,:,:]).sum([0, 2, 4])
return domain.integrate(mx, ischeme='gauss9')
def two(domain, basis, z):
basis = fn.matmat(basis, z)
mx = basis[:,_,_] * basis[_,:,_] * basis[_,_,:]
return domain.integrate(mx, ischeme='gauss9')
def three(domain, basis, z):
mx = fn.ravel(basis[:,_,_] * basis[_,:,_] * basis[_,_,:], 1)
mx = domain.integrate(mx, ischeme='gauss9').core
nb, nr = len(basis), z.shape[1]
mx = mx.T.dot(z).T.reshape((nr, nb, nb))
mx = mx.dot(z)
return mx.transpose((0,2,1)).dot(z).transpose((0,1,2))
if __name__ == '__main__':
pts = np.linspace(0, 1, 31)
domain, geom = mesh.rectilinear([pts,pts])
basis = domain.basis('spline', degree=1)
z = np.random.random((len(basis), 15))
log.user(timeit.timeit(
'one(domain,basis,z)', number=1,
globals={'one': one, 'domain': domain, 'geom': geom, 'basis': basis, 'z': z}
))
log.user(timeit.timeit(
'two(domain,basis,z)', number=1,
globals={'two': two, 'domain': domain, 'geom': geom, 'basis': basis, 'z': z}
))
log.user(timeit.timeit(
'three(domain,basis,z)', number=1,
globals={'three': three, 'domain': domain, 'geom': geom, 'basis': basis, 'z': z}
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment