def testFIR(self): smooth = np.array([0.25, 0.5, 0.25]) d = np.arange(10) filtered = fir(d, smooth, zero_edge=False) print d, '--', smooth, '(keep edge) -->', filtered self.assertTrue(np.all( filtered == d)) filtered = fir(d, smooth, zero_edge=True) print d, '--', smooth, '-->', filtered self.assertEquals(0, filtered[9]) d = np.zeros(10) d[5] = 10 filtered = fir(d, smooth) print d, '--', smooth, '-->', filtered self.assertEquals(5.0, filtered[5]) d = np.zeros(10) d[5:10] = np.ones(5) filtered = fir(d, smooth) print d, '--', smooth, '-->', filtered self.assertEquals(0.0, filtered[3]) self.assertEquals(0.25, filtered[4]) self.assertEquals(0.75, filtered[5]) self.assertEquals(1.0, filtered[6]) diff = np.array([-0.5, 0, 0.5]) filtered = fir(d, diff) print d, '--', diff, '-->', filtered
def sum(self, axis=None): """Sum the matrix over the given axis. If the axis is None, sum over both rows and columns, returning a scalar. """ # We use multiplication by an array of ones to achieve this. # For some sparse matrix formats more efficient methods are # possible -- these should override this function. m, n = self.shape if axis == 0: # sum over columns return np.asmatrix(np.ones((1, m), dtype=self.dtype)) * self elif axis == 1: # sum over rows return self * np.asmatrix(np.ones((n, 1), dtype=self.dtype)) elif axis is None: # sum over rows and columns return ( self * np.asmatrix(np.ones((n, 1), dtype=self.dtype)) ).sum() else: raise ValueError, "axis out of bounds"