(w, a, b)
return c[0]*w + b*c[1], c[0]
Returns a reduced solution `(x, z)` to the congruence
    `X^2 - aZ^2 \equiv 0 \ (mod \ b)` so that `x^2 + |a|z^2` is minimal.

    Details
    =======

    Here ``w`` is a solution of the congruence `x^2 \equiv a \ (mod \ b)`

    References
    ==========

    .. [1] Gaussian lattice Reduction [online]. Available:
        http://home.ie.cuhk.edu.hk/~wkshum/wordpress/?p=404
    .. [2] Efficient Solution of Rational Conices, J. E. Cremona and D. Rusin,
        Mathematics of Computation, Volume 00, Number 0.
Returns a reduced solution `(x, z)` to the congruence
    `X^2 - aZ^2 \equiv 0 \ (mod \ b)` so that `x^2 + |a|z^2` is minimal.
Returns a reduced solution ( x z ) to the congruence X^2 - aZ^2 \ equiv 0 \ ( mod \ b ) so that x^2 + |a|z^2 is minimal .
def gaussian_reduce(w, a, b):
    """
    Returns a reduced solution `(x, z)` to the congruence
    `X^2 - aZ^2 \equiv 0 \ (mod \ b)` so that `x^2 + |a|z^2` is minimal.

    Details
    =======

    Here ``w`` is a solution of the congruence `x^2 \equiv a \ (mod \ b)`

    References
    ==========

    .. [1] Gaussian lattice Reduction [online]. Available:
        http://home.ie.cuhk.edu.hk/~wkshum/wordpress/?p=404
    .. [2] Efficient Solution of Rational Conices, J. E. Cremona and D. Rusin,
        Mathematics of Computation, Volume 00, Number 0.
    """
    u = (0, 1)
    v = (1, 0)

    if dot(u, v, w, a, b) < 0:
        v = (-v[0], -v[1])

    if norm(u, w, a, b) < norm(v, w, a, b):
        u, v = v, u

    while norm(u, w, a, b) > norm(v, w, a, b):
        k = dot(u, v, w, a, b) // dot(v, v, w, a, b)
        u, v = v, (u[0]- k*v[0], u[1]- k*v[1])

    u, v = v, u

    if dot(u, v, w, a, b) < dot(v, v, w, a, b)/2 or norm((u[0]-v[0], u[1]-v[1]), w, a, b) > norm(v, w, a, b):
        c = v
    else:
        c = (u[0] - v[0], u[1] - v[1])

    return c[0]*w + b*c[1], c[0]
def gaussian_reduce ( w , a , b ) : u = ( 0 , 1 ) v = ( 1 , 0 ) if dot ( u , v , w , a , b ) < 0 : v = ( - v [ 0 ] , - v [ 1 ] ) if norm ( u , w , a , b ) < norm ( v , w , a , b ) : u , v = v , u while norm ( u , w , a , b ) > norm ( v , w , a , b ) : k = dot ( u , v , w , a , b ) // dot ( v , v , w , a , b ) u , v = v , ( u [ 0 ] - k * v [ 0 ] , u [ 1 ] - k * v [ 1 ] ) u , v = v , u if dot ( u , v , w , a , b ) < dot ( v , v , w , a , b ) / 2 or norm ( ( u [ 0 ] - v [ 0 ] , u [ 1 ] - v [ 1 ] ) , w , a , b ) > norm ( v , w , a , b ) : c = v else : c = ( u [ 0 ] - v [ 0 ] , u [ 1 ] - v [ 1 ] ) return c [ 0 ] * w + b * c [ 1 ] , c [ 0 ]