'Test the mul method.'

def test_mul(self):

    n = 2
    ones = np.mat(np.ones((n, n)))
    x = Variable(n, n)
    A = np.matrix('1    2;    3    4')
    expr = (A * x).canonical_form[0]
    val_dict = {x.id: ones}
    result = mul(expr, val_dict)
    assert (result == (A * ones)).all()
    result_dict = tmul(expr, result)
    assert (result_dict[x.id] == ((A.T * A) * ones)).all()
    t = Variable()
    A = np.matrix('1    2;    3    4')
    expr = (A * t).canonical_form[0]
    val_dict = {t.id: 2}
    result = mul(expr, val_dict)
    assert (result == (A * 2)).all()
    result_dict = tmul(expr, result)
    total = 0
    for i in range(A.shape[0]):
        for j in range(A.shape[1]):
            total += (A[(i, j)] * result[(i, j)])
    assert (result_dict[t.id] == total)
    y = Variable(n, n)
    expr = (y + (A * x)).canonical_form[0]
    val_dict = {x.id: np.ones((n, n)), y.id: np.ones((n, n))}
    result = mul(expr, val_dict)
    assert (result == ((A * ones) + ones)).all()
    result_dict = tmul(expr, result)
    assert (result_dict[y.id] == result).all()
    assert (result_dict[x.id] == (A.T * result)).all()
    val_dict = {x.id: A, y.id: A}
    expr = (x[:, 0] + y[:, 1]).canonical_form[0]
    result = mul(expr, val_dict)
    assert (result == (A[:, 0] + A[:, 1])).all()
    result_dict = tmul(expr, result)
    mat = ones
    mat[:, 0] = result
    mat[:, 1] = 0
    assert (result_dict[x.id] == mat).all()
    val_dict = {x.id: A}
    expr = (- x).canonical_form[0]
    result = mul(expr, val_dict)
    assert (result == (- A)).all()
    result_dict = tmul(expr, result)
    assert (result_dict[x.id] == A).all()
    expr = x.T.canonical_form[0]
    val_dict = {x.id: A}
    result = mul(expr, val_dict)
    assert (result == A.T).all()
    result_dict = tmul(expr, result)
    assert (result_dict[x.id] == A).all()
    x = Variable(3)
    f = np.matrix(np.array([1, 2, 3])).T
    g = np.array([0, 1, 0.5])
    f_conv_g = np.array([0.0, 1.0, 2.5, 4.0, 1.5])
    expr = conv(f, x).canonical_form[0]
    val_dict = {x.id: g}
    result = mul(expr, val_dict)
    self.assertItemsAlmostEqual(result, f_conv_g)
    value = np.array(range(5))
    result_dict = tmul(expr, value)
    toep = LA.toeplitz(np.array([1, 0, 0]), np.array([1, 2, 3, 0, 0]))
    x_val = toep.dot(value)
    self.assertItemsAlmostEqual(result_dict[x.id], x_val)
