(A, B)
Uses Lagrange's method to find a non trivial solution to
    `w^2 = Ax^2 + By^2`.

    Here, `A \\neq 0` and `B \\neq 0` and `A` and `B` are square free. Output a
    tuple `(w_0, x_0, y_0)` which is a solution to the above equation.

    Examples
    ========

    >>> from sympy.solvers.diophantine import ldescent
    >>> ldescent(1, 1) # w^2 = x^2 + y^2
    (1, 1, 0)
    >>> ldescent(4, -7) # w^2 = 4x^2 - 7y^2
    (2, -1, 0)

    This means that `x = -1, y = 0` and `w = 2` is a solution to the equation
    `w^2 = 4x^2 - 7y^2`

    >>> ldescent(5, -1) # w^2 = 5x^2 - y^2
    (2, 1, -1)

    References
    ==========

    .. [1] The algorithmic resolution of Diophantine equations, Nigel P. Smart,
           London Mathematical Society Student Texts 41, Cambridge University
           Press, Cambridge, 1998.
    .. [2] Efficient Solution of Rational Conices, J. E. Cremona and D. Rusin,
           Mathematics of Computation, Volume 00, Number 0.
Uses Lagrange's method to find a non trivial solution to
    `w^2 = Ax^2 + By^2`.
Uses Lagrange s method to find a non trivial solution to w^2 = Ax^2 + By^2 .
def ldescent(A, B):
    """
    Uses Lagrange's method to find a non trivial solution to
    `w^2 = Ax^2 + By^2`.

    Here, `A \\neq 0` and `B \\neq 0` and `A` and `B` are square free. Output a
    tuple `(w_0, x_0, y_0)` which is a solution to the above equation.

    Examples
    ========

    >>> from sympy.solvers.diophantine import ldescent
    >>> ldescent(1, 1) # w^2 = x^2 + y^2
    (1, 1, 0)
    >>> ldescent(4, -7) # w^2 = 4x^2 - 7y^2
    (2, -1, 0)

    This means that `x = -1, y = 0` and `w = 2` is a solution to the equation
    `w^2 = 4x^2 - 7y^2`

    >>> ldescent(5, -1) # w^2 = 5x^2 - y^2
    (2, 1, -1)

    References
    ==========

    .. [1] The algorithmic resolution of Diophantine equations, Nigel P. Smart,
           London Mathematical Society Student Texts 41, Cambridge University
           Press, Cambridge, 1998.
    .. [2] Efficient Solution of Rational Conices, J. E. Cremona and D. Rusin,
           Mathematics of Computation, Volume 00, Number 0.
    """
    if abs(A) > abs(B):
        w, y, x = ldescent(B, A)
        return w, x, y

    if A == 1:
        return (S.One, S.One, 0)

    if B == 1:
        return (S.One, 0, S.One)

    r = sqrt_mod(A, B)

    Q = (r**2 - A) // B

    if Q == 0:
        B_0 = 1
        d = 0
    else:
        div = divisors(Q)
        B_0 = None

        for i in div:
            if isinstance(sqrt(abs(Q) // i), Integer):
                B_0, d = sign(Q)*i, sqrt(abs(Q) // i)
                break

    if B_0 != None:
        W, X, Y = ldescent(A, B_0)
        return simplified((-A*X + r*W), (r*X - W), Y*(B_0*d))
def ldescent ( A , B ) : if abs ( A ) > abs ( B ) : w , y , x = ldescent ( B , A ) return w , x , y if A == 1 : return ( S . One , S . One , 0 ) if B == 1 : return ( S . One , 0 , S . One ) r = sqrt_mod ( A , B ) Q = ( r ** 2 - A ) // B if Q == 0 : B_0 = 1 d = 0 else : div = divisors ( Q ) B_0 = None for i in div : if isinstance ( sqrt ( abs ( Q ) // i ) , Integer ) : B_0 , d = sign ( Q ) * i , sqrt ( abs ( Q ) // i ) break if B_0 != None : W , X , Y = ldescent ( A , B_0 ) return simplified ( ( - A * X + r * W ) , ( r * X - W ) , Y * ( B_0 * d ) )