def setUp(self):
        loops = 200
        blocks = 2
        decimals = 12

        self.loops = loops
        self.blocks = blocks
        self.decimals = decimals

        mat = np.matrix("""
                        -1.5 0.5 2 0 0;
                        -1   2  -1 0 0;
                        0   -1   2 -1 0;
                        0   0    -1 2 -1;
                        0   0   -1.5 0.5 2""").A
        diamat = todia(scipy.sparse.dia_matrix(mat))

        x = foldMatFor(diamat, 1).todense().A
        topx = x.copy()
        bottomx = x.copy()
        topx[-1,-2] = 0
        bottomx[0,1] = 0
        x, topx, bottomx = map(todia, [x, topx, bottomx])


        topblockx = scipy.sparse.block_diag([topx]*blocks)
        bottomblockx = scipy.sparse.block_diag([bottomx]*blocks)
        blockx = todia(scipy.sparse.block_diag([x]*blocks))
        self.blockxI = blockx.todense().I.A
        self.topblockxI = topblockx.todense().I.A
        self.bottomblockxI = bottomblockx.todense().I.A

        npt.assert_array_equal(todia(topblockx.dot(bottomblockx)).data, blockx.data)

        blockdiamat = todia(scipy.sparse.block_diag([diamat]*blocks))

        blocktridiamat = todia(blockx.dot(blockdiamat))
        topblocktridiamat = todia(topblockx.dot(blockdiamat))
        bottomblocktridiamat = todia(bottomblockx.dot(blockdiamat))

        self.blockdiamat = blockdiamat

        self.blockx = blockx
        self.topblockx = topblockx
        self.bottomblockx = bottomblockx

        self.blocktridiamat = blocktridiamat
        self.topblocktridiamat = topblocktridiamat
        self.bottomblocktridiamat = bottomblocktridiamat

        self.vec = np.random.random(mat.shape[1]*blocks)

        self.B = FD.BandedOperator((blockdiamat.data, (2, 1, 0, -1, -2)), inplace=False)
        self.B.blocks = blocks
    def setUp(self):
        k = 3.0
        nspots = 7
        spot_max = 1500.0
        spotdensity = 7.0  # infinity is linear?
        vec = utils.sinh_space(k, spot_max, spotdensity, nspots)
        self.vec = vec

        def coeff(high,low=None):
            if low is not None:
                high, low = low, high
            return np.linspace(0, 1, len(vec))[low:high]
        def fcoeff(i):
            return np.linspace(0, 1, len(vec))[i]
        def f0(x): return x*0.0
        def fx(x): return x+2.0

        data = np.ones((5,len(vec)))
        data[0][:2] = 0
        data[1][0] = 0
        data[3][-1] = 0
        data[4][-2:] = 0
        offsets = [2,1,0,-1,-2]
        res = np.ones_like(vec)
        oldB = FD.BandedOperator((data, offsets), res)
        self.oldB = oldB

        manualB = oldB.copy()
        # newB = oldB.copy()
        # vecB = oldB.copy()
        manualB.D.data[0][2:] *= coeff(len(vec)-2)
        manualB.D.data[1][1:] *= coeff(len(vec)-1)
        manualB.D.data[2] *= coeff(len(vec))
        manualB.D.data[3][:-1] *= coeff(1, len(vec))
        manualB.D.data[4][:-2] *= coeff(2, len(vec))
        manualB.R *= coeff(len(vec))

        self.manualB = manualB

        self.coeff = coeff
        self.fcoeff = fcoeff
        self.f0 = f0
        self.fx = fx
    def test_scale_no_dirichlet(self):
        no_nan = np.nan_to_num
        vec = self.vec
        def f0(x): return 0
        def fx(x): return x
        data = np.ones((5,len(vec)), dtype=float)
        data[0][:2] = 0
        data[1][0] = 0
        data[3][-1] = 0
        data[4][-2:] = 0
        offsets = [2,1,0,-1,-2]
        res = np.ones_like(vec)
        oldB = FD.BandedOperator((data, offsets), res)

        newB = oldB.copy()
        newB.scale(f0)
        # fp(newB.D.data)
        npt.assert_array_equal(0, no_nan(newB.D.data))

        manualB = oldB.copy()
        newB = oldB.copy()
        manualB.D.data[0][2:] *= np.arange(len(vec)-2)
        manualB.D.data[1][1:] *= np.arange(len(vec)-1)
        manualB.D.data[2] *= np.arange(len(vec))
        manualB.D.data[3][:-1] *= np.arange(1, len(vec))
        manualB.D.data[4][:-2] *= np.arange(2, len(vec))
        manualB.R *= np.arange(len(vec))
        newB.scale(fx)
        # print "manual"
        # fp(manualB.data)
        # print
        # print "new"
        # fp(newB.data)
        # print
        # print "manualR"
        # print manualB.R
        # print
        # print "newR"
        # print newB.R
        npt.assert_array_equal(manualB, newB)
    def test_scale_dirichlet(self):
        # no_nan = np.nan_to_num
        vec = self.vec
        flag = int(1)
        data = np.ones((5,len(vec)), dtype=int)*flag
        data[0][:2] = 0
        data[1][0] = 0
        data[3][-1] = 0
        data[4][-2:] = 0
        offsets = [2,1,0,-1,-2]
        res = np.ones_like(vec)

        for dchlet in itertools.product([1., None], repeat=2):
            oldB = FD.BandedOperator((data.copy(), offsets), res.copy())
            oldB.dirichlet = dchlet
            zeroB = oldB.copy()
            zeroB.scale(self.f0)

            manualzeroB = np.zeros_like(zeroB.D.data)
            if zeroB.dirichlet[0] is not None:
                manualzeroB[0, 2] = flag
                manualzeroB[1, 1] = flag
                manualzeroB[2, 0] = flag
            if zeroB.dirichlet[1] is not None:
                manualzeroB[2, -1] = flag
                manualzeroB[3, -2] = flag
                manualzeroB[4, -3] = flag

            # print "Dirichlet", dchlet
            # print
            # print "zeroB"
            # fp(zeroB.D.data)
            # fp(zeroB.R)
            # print
            # print "manualzeroB"
            # fp(manualzeroB)
            # fp(manualzeroB.R)
            # print

            manualB = oldB.copy()
            newB = oldB.copy()
            bottom = 0
            top = manualB.shape[0]
            if dchlet[0]:
                bottom += 1
            if dchlet[1]:
                top -= 1
            manualB.D.data[0][bottom+2:] *= np.arange(bottom, manualB.shape[0]-2)+2
            manualB.D.data[1][bottom+1:] *= np.arange(bottom, manualB.shape[0]-1)+2
            manualB.D.data[2][bottom:top] *= np.arange(bottom, top)+2
            manualB.D.data[3][:top-1] *= np.arange(1, top)+2
            manualB.D.data[4][:top-2] *= np.arange(2, top)+2
            manualB.R[bottom:top] *= np.arange(bottom, top)+2
            newB.scale(self.fx)
            # print "manual"
            # fp(manualB.D.data)
            # print
            # print "new"
            # fp(newB.D.data)
            # print
            # print "manualR"
            # print manualB.R
            # print
            # print "newR"
            # print newB.R
            npt.assert_array_equal(manualzeroB, zeroB.D.data)
            assert manualB == newB
    def test_block_vectorizedscale(self):
        no_nan = np.nan_to_num
        vec = self.vec
        oldB = self.oldB
        manualB = self.manualB
        flag = int(1)
        data = np.ones((5,len(vec)), dtype=float)*flag
        data[0][:2] = 0
        data[1][0] = 0
        data[3][-1] = 0
        data[4][-2:] = 0
        offsets = [2,1,0,-1,-2]
        res = np.ones_like(vec)

        blocks = 3

        blockvec = np.tile(vec, blocks)

        newB = oldB.copy()
        newB.scale(self.f0)
        vecB = oldB.copy()
        vecB.vectorized_scale(self.f0(vec))


        npt.assert_array_equal(0, no_nan(newB.D.data))
        npt.assert_array_equal(0, no_nan(vecB.D.data))

        for dchlet in itertools.product([1., None], repeat=2):
            oldB = FD.BandedOperator((data.copy(), offsets), res.copy())
            oldB.dirichlet = dchlet

            veczeroB = block_repeat(oldB, blocks)
            veczeroB.vectorized_scale(self.f0(blockvec))
            manualzeroB = np.zeros_like(oldB.D.data)
            if veczeroB.dirichlet[0] is not None:
                manualzeroB[0, 2] = flag
                manualzeroB[1, 1] = flag
                manualzeroB[2, 0] = flag
            if veczeroB.dirichlet[1] is not None:
                manualzeroB[2, -1] = flag
                manualzeroB[3, -2] = flag
                manualzeroB[4, -3] = flag
            manualzeroB = np.tile(manualzeroB, blocks)

            # print dchlet
            # print
            # print "veczeroB"
            # fp(veczeroB.D.data)
            # print
            # print "manualzeroB"
            # fp(manualzeroB)
            # print

            manualB = oldB.copy()
            bottom = 0
            top = last = manualB.shape[0]
            if dchlet[0]:
                bottom += 1
            if dchlet[1]:
                top -= 1
            manualB.D.data[0][bottom+2:]  *= vec[bottom : last-2]+2
            manualB.D.data[1][bottom+1:]  *= vec[bottom : last-1]+2
            manualB.D.data[2][bottom:top] *= vec[bottom : top]+2
            manualB.D.data[3][:top-1]     *= vec[1      : top]+2
            manualB.D.data[4][:top-2]     *= vec[2      : top]+2
            manualB.R[bottom:top]       *= vec[bottom:top]+2
            manualB = block_repeat(manualB, blocks)

            vecB = block_repeat(oldB, blocks)
            vecB.vectorized_scale(self.fx(blockvec))

            newB = block_repeat(oldB, blocks)
            newB.scale(lambda i: blockvec[i]+2)
            # print "vec"
            # fp(vec)
            # print
            # print "manual"
            # fp(manualB.D.data)
            # print
            # print "newB"
            # fp(newB.D.data)
            # print
            # print "vecB"
            # fp(vecB.D.data)
            # print
            # print "manualR"
            # print manualB.R
            # print
            # print "vecR"
            # print vecB.R
            # print
            # print "newR"
            # print newB.R
            # print
            # print "manual"
            # fp(manualB.D)
            # print
            # print "newB"
            # fp(newB.D)
            npt.assert_array_equal(manualzeroB, veczeroB.D.data)
            npt.assert_(newB == vecB)
            npt.assert_array_equal(manualB, newB)
            npt.assert_array_equal(manualB, vecB)

        newB = oldB.copy()
        newB.scale(self.fcoeff)
        vecB = oldB.copy()
        vecB.vectorized_scale(self.coeff(len(vec)))
        # print "new"
        # fp(newB.data)
        # print
        # print "vec"
        # fp(vecB.data)
        # print
        # print "newR"
        # print newB.R
        assert newB == vecB