\\ true if and only if (a,b,c) is reduced. {isreduced(a,b,c) = if(b^2-4*a*c>=0 || a<0, error("reduce: (a,b,c) must be positive definite.")); if(!(abs(b)<=a && a<=c), return(0)); if(abs(b)==a || a==c, return(b>=0)); return(1); } \\ reduces, printing out each step. returns the reduced form \\ and a matrix that transforms the input form to the reduced form. {reduce(a,b,c) = local(D, k, t, g); D=b^2-4*a*c; g=[1,0;0,1]; if(D>=0 || a<0, error("reduce: (a,b,c) must be positive definite.")); while(!isreduced(a,b,c), if(ca || -b==a, k = floor((a-b)/(2*a)); b = b+2*k*a; c = (b^2-D)/(4*a); g = g*[1,k;0,1]; print([a,b,c], " \t(2) with k=",k) ) ) ); return([a,b,c,g]) } /* Here is an example: ? reduce(458,214,25) [25, -214, 458] (1) [25, -14, 2] (2) with k=4 [2, 14, 25] (1) [2, 2, 1] (2) with k=-3 [1, -2, 2] (1) [1, 0, 1] (2) with k=1 %22 = [1, 0, 1, [3, 4; -13, -17]] */