import copy


def multiply_variable(self, c, i, in_place = False):
    """
    Replace the variables `x_i` by `c*x_i` in the quadratic form
    (replacing the original form if the in_place flag is True).

    Here `c` must be an element of the base_ring defining the
    quadratic form.

    INPUT:

        `c` -- an element of Q.base_ring()

        `i` -- an integer >= 0

    OUTPUT:

        a QuadraticForm (by default, otherwise none)

    EXAMPLES::

        sage: Q = DiagonalQuadraticForm(ZZ, [1,9,5,7])
        sage: Q.multiply_variable(5,0)
        Quadratic form in 4 variables over Integer Ring with coefficients:
        [ 25 0 0 0 ]
        [ * 9 0 0 ]
        [ * * 5 0 ]
        [ * * * 7 ]

    """
    if (in_place == False):
        Q = copy.deepcopy(self)
        Q.__init__(self.base_ring(), self.dim(), self.coefficients())
        Q.multiply_variable(c,i,in_place=True)
        return Q

    else:
        ## Stretch the diagonal element
        tmp = c * c * self[i,i]
        self[i,i] = tmp

        ## Switch off-diagonal elements
        for k in range(self.dim()):
            if (k != i):
                tmp = c * self[k,i]
                self[k,i] = tmp