phydmslib.numutils module

Numerical routines for phydmslib.

phydmslib.numutils.broadcastGetCols()

Get specified columns from ndarray of square ndarrays.

This functions uses fast numpy broadcasting to get the columns from a long array of arrays.

Args:
m (numpy.ndarray, shape (r, n, n)

Array of matrices from which we get the columns

cols (numpy.ndarray, type int, length r)

All entries should be >= 0 and < n.

Returns:
mcols (numpy.ndarray, shape (r, n))

mcols[r] is equal to mcols[r][cols[r]]

>>> n = 2
>>> r = 3
>>> m = numpy.arange(r * n * n).reshape(r, n, n)
>>> cols = numpy.random.random_integers(0, n - 1, r)
>>> expected = numpy.array([m[i][:, cols[i]] for i in range(r)])
>>> numpy.allclose(expected, broadcastGetCols(m, cols))
True
phydmslib.numutils.broadcastMatrixMultiply()

Broadcast square matrix multiplication using blas for speed.

Args:
a (numpy.ndarray, shape (r, n, n))

First matrix.

b (numpy.ndarray, shape (r, n, n))

Second matrix.

alpha (float)

Multiply each entry in product by this (same meaning as for BLAS dgemm).

Returns:
ab (numpy.ndarray, shape (r, n, n))

ab[r] is the matrix product of a[r] with b[r].

>>> r = 3
>>> n = 2
>>> a = numpy.arange(r * n * n, dtype='float').reshape(r, n, n)
>>> b = numpy.arange(r * n * n, dtype='float').reshape(r, n, n)
>>> for i in range(r):
...   b[i][0][0] = 2
>>> ab = broadcastMatrixMultiply(a, b)
>>> expected = scipy.matmul(a, b)
>>> numpy.allclose(expected, ab)
True
phydmslib.numutils.broadcastMatrixVectorMultiply()

Broadcast matrix vector multiplication using blas for speed.

Args:
m (numpy.ndarray, shape (d1, d2, d2))

Array of square matrices to multiply.

v (numpy.ndarray, shape (d1, d2))

Array of vectors to multiply.

alpha (float)

Multiply each entry in product by this (same meaning as for BLAS dgemv).

Returns:
mv (numpy.ndarray, shape (d1, d2))

mv[r] is the matrix-vector product of m[r] with v[r] for 0 <= r <= d1.

>>> m = numpy.array([[[1., 2], [3, 4]], [[5, 6], [7, 8]], [[9, 8], [7, 6]]])
>>> v = numpy.array([[1., 2], [3, 4], [1, 3]])
>>> mv = numpy.ndarray(v.shape, dtype='int')
>>> for r in range(v.shape[0]):
...   for x in range(v.shape[1]):
...      mvrx = 0
...      for y in range(v.shape[1]):
...        mvrx += m[r][x][y] * v[r][y]
...      mv[r][x] = mvrx
>>> mv2 = broadcastMatrixVectorMultiply(m, v)
>>> mv.shape == mv2.shape
True
>>> numpy.allclose(mv, mv2)
True