'Sum the matrix elements over a given axis.
Parameters
axis : {-2, -1, 0, 1, None} optional
Axis along which the sum is computed. The default is to
compute the sum of all the matrix elements, returning a scalar
(i.e. `axis` = `None`).
dtype : dtype, optional
The type of the returned matrix and of the accumulator in which
the elements are summed.  The dtype of `a` is used by default
unless `a` has an integer dtype of less precision than the default
platform integer.  In that case, if `a` is signed then the platform
integer is used while if `a` is unsigned then an unsigned integer
of the same precision as the platform integer is used.
.. versionadded: 0.18.0
out : np.matrix, optional
Alternative output matrix in which to place the result. It must
have the same shape as the expected output, but the type of the
output values will be cast if necessary.
.. versionadded: 0.18.0
Returns
sum_along_axis : np.matrix
A matrix with the same shape as `self`, with the specified
axis removed.
See Also
np.matrix.sum : NumPy\'s implementation of \'sum\' for matrices'

def sum(self, axis=None, dtype=None, out=None):

    validateaxis(axis)
    (m, n) = self.shape
    res_dtype = get_sum_dtype(self.dtype)
    if (axis is None):
        return (self * np.asmatrix(np.ones((n, 1), dtype=res_dtype))).sum(dtype=dtype, out=out)
    if (axis < 0):
        axis += 2
    if (axis == 0):
        ret = (np.asmatrix(np.ones((1, m), dtype=res_dtype)) * self)
    else:
        ret = (self * np.asmatrix(np.ones((n, 1), dtype=res_dtype)))
    if ((out is not None) and (out.shape != ret.shape)):
        raise ValueError('dimensions    do    not    match')
    return ret.sum(axis=(), dtype=dtype, out=out)
