def add_diagonal(self, val, target=None):
    """
    Add val to the diagonal of self. If a target
    is provided, it is used to store the result instead of self.
    """

    if not target:
      target = self

    assert self.shape[0] == self.shape[1], 'self must be a square matrix'
    if isinstance(val, EigenMatrix):
      err_code = _eigenmat.add_diagonal(self.p_mat, val.p_mat, target.p_mat)
    elif isinstance(val, (int, float)):
      err_code = _eigenmat.add_diagonal_scalar(self.p_mat, ct.c_float(val), target.p_mat)
    else:
      raise ValueError, "Value must be of type EigenMatrix, int, or float."

    if err_code:
      raise generate_exception(err_code)

    return target

    Add val to the diagonal of self. If a target
    is provided, it is used to store the result instead of self.
    
eigenmat/eigenmat.py