]> www.ginac.de Git - cln.git/blobdiff - src/polynomial/elem/cl_UP_gen.h
Replace unused macro with cl_unused.
[cln.git] / src / polynomial / elem / cl_UP_gen.h
index c5970dda2e693a1dec770f9e4d7710b2f782f919..dc88ed1bc04658069a8c3624186e02ee402da515 100644 (file)
@@ -1,8 +1,10 @@
 // Univariate Polynomials over a general ring.
 
-#include "cl_SV_ringelt.h"
-#include "cl_integer.h"
-#include "cl_abort.h"
+#include "cln/SV_ringelt.h"
+#include "cln/integer.h"
+#include "cln/exception.h"
+
+namespace cln {
 
 // Assume a ring is a ring.
   inline cl_heap_ring* TheRing (const cl_ring& R)
@@ -26,11 +28,11 @@ static inline void gen_normalize (cl_heap_ring* R, cl_SV_ringelt& result, uintL
        }
 }
 
-static void gen_fprint (cl_heap_univpoly_ring* UPR, cl_ostream stream, const _cl_UP& x)
+static void gen_fprint (cl_heap_univpoly_ring* UPR, std::ostream& stream, const _cl_UP& x)
 {{
        DeclarePoly(cl_SV_ringelt,x);
        var cl_heap_ring* R = TheRing(UPR->basering());
-       var sintL xlen = x.length();
+       var sintL xlen = x.size();
        if (xlen == 0)
                fprint(stream, "0");
        else {
@@ -54,19 +56,19 @@ static void gen_fprint (cl_heap_univpoly_ring* UPR, cl_ostream stream, const _cl
        }
 }}
 
-static cl_boolean gen_equal (cl_heap_univpoly_ring* UPR, const _cl_UP& x, const _cl_UP& y)
+static bool gen_equal (cl_heap_univpoly_ring* UPR, const _cl_UP& x, const _cl_UP& y)
 {{
        DeclarePoly(cl_SV_ringelt,x);
        DeclarePoly(cl_SV_ringelt,y);
        var cl_heap_ring* R = TheRing(UPR->basering());
-       var sintL xlen = x.length();
-       var sintL ylen = y.length();
+       var sintL xlen = x.size();
+       var sintL ylen = y.size();
        if (!(xlen == ylen))
-               return cl_false;
+               return false;
        for (var sintL i = xlen-1; i >= 0; i--)
                if (!R->_equal(x[i],y[i]))
-                       return cl_false;
-       return cl_true;
+                       return false;
+       return true;
 }}
 
 static const _cl_UP gen_zero (cl_heap_univpoly_ring* UPR)
@@ -74,15 +76,15 @@ static const _cl_UP gen_zero (cl_heap_univpoly_ring* UPR)
        return _cl_UP(UPR, cl_null_SV_ringelt);
 }
 
-static cl_boolean gen_zerop (cl_heap_univpoly_ring* UPR, const _cl_UP& x)
+static bool gen_zerop (cl_heap_univpoly_ring* UPR, const _cl_UP& x)
 {
-       unused UPR;
+       cl_unused UPR;
  {     DeclarePoly(cl_SV_ringelt,x);
-       var sintL xlen = x.length();
+       var sintL xlen = x.size();
        if (xlen == 0)
-               return cl_true;
+               return true;
        else
-               return cl_false;
+               return false;
 }}
 
 static const _cl_UP gen_plus (cl_heap_univpoly_ring* UPR, const _cl_UP& x, const _cl_UP& y)
@@ -90,8 +92,8 @@ static const _cl_UP gen_plus (cl_heap_univpoly_ring* UPR, const _cl_UP& x, const
        DeclarePoly(cl_SV_ringelt,x);
        DeclarePoly(cl_SV_ringelt,y);
        var cl_heap_ring* R = TheRing(UPR->basering());
-       var sintL xlen = x.length();
-       var sintL ylen = y.length();
+       var sintL xlen = x.size();
+       var sintL ylen = y.size();
        if (xlen == 0)
                return _cl_UP(UPR, y);
        if (ylen == 0)
@@ -129,17 +131,36 @@ static const _cl_UP gen_plus (cl_heap_univpoly_ring* UPR, const _cl_UP& x, const
        return _cl_UP(UPR, cl_null_SV_ringelt);
 }}
 
+static const _cl_UP gen_uminus (cl_heap_univpoly_ring* UPR, const _cl_UP& x)
+{{
+       DeclarePoly(cl_SV_ringelt,x);
+       var cl_heap_ring* R = TheRing(UPR->basering());
+       var sintL xlen = x.size();
+       if (xlen == 0)
+               return _cl_UP(UPR, x);
+       // Now xlen > 0.
+       // Negate. No normalization necessary, since the degree doesn't change.
+       var sintL i = xlen-1;
+       var _cl_ring_element hicoeff = R->_uminus(x[i]);
+       if (R->_zerop(hicoeff)) throw runtime_exception();
+       var cl_SV_ringelt result = cl_SV_ringelt(cl_make_heap_SV_ringelt_uninit(xlen));
+       init1(_cl_ring_element, result[i]) (hicoeff);
+       for (i-- ; i >= 0; i--)
+               init1(_cl_ring_element, result[i]) (R->_uminus(x[i]));
+       return _cl_UP(UPR, result);
+}}
+
 static const _cl_UP gen_minus (cl_heap_univpoly_ring* UPR, const _cl_UP& x, const _cl_UP& y)
 {{
        DeclarePoly(cl_SV_ringelt,x);
        DeclarePoly(cl_SV_ringelt,y);
        var cl_heap_ring* R = TheRing(UPR->basering());
-       var sintL xlen = x.length();
-       var sintL ylen = y.length();
-       if (xlen == 0)
-               return _cl_UP(UPR, y);
+       var sintL xlen = x.size();
+       var sintL ylen = y.size();
        if (ylen == 0)
                return _cl_UP(UPR, x);
+       if (xlen == 0)
+               return gen_uminus(UPR,_cl_UP(UPR, y));
        // Now xlen > 0, ylen > 0.
        if (xlen > ylen) {
                var cl_SV_ringelt result = cl_SV_ringelt(cl_make_heap_SV_ringelt_uninit(xlen));
@@ -173,25 +194,6 @@ static const _cl_UP gen_minus (cl_heap_univpoly_ring* UPR, const _cl_UP& x, cons
        return _cl_UP(UPR, cl_null_SV_ringelt);
 }}
 
-static const _cl_UP gen_uminus (cl_heap_univpoly_ring* UPR, const _cl_UP& x)
-{{
-       DeclarePoly(cl_SV_ringelt,x);
-       var cl_heap_ring* R = TheRing(UPR->basering());
-       var sintL xlen = x.length();
-       if (xlen == 0)
-               return _cl_UP(UPR, x);
-       // Now xlen > 0.
-       // Negate. No normalization necessary, since the degree doesn't change.
-       var sintL i = xlen-1;
-       var _cl_ring_element hicoeff = R->_uminus(x[i]);
-       if (R->_zerop(hicoeff)) cl_abort();
-       var cl_SV_ringelt result = cl_SV_ringelt(cl_make_heap_SV_ringelt_uninit(xlen));
-       init1(_cl_ring_element, result[i]) (hicoeff);
-       for (i-- ; i >= 0; i--)
-               init1(_cl_ring_element, result[i]) (R->_uminus(x[i]));
-       return _cl_UP(UPR, result);
-}}
-
 static const _cl_UP gen_one (cl_heap_univpoly_ring* UPR)
 {
        var cl_heap_ring* R = TheRing(UPR->basering());
@@ -213,8 +215,8 @@ static const _cl_UP gen_mul (cl_heap_univpoly_ring* UPR, const _cl_UP& x, const
        DeclarePoly(cl_SV_ringelt,x);
        DeclarePoly(cl_SV_ringelt,y);
        var cl_heap_ring* R = TheRing(UPR->basering());
-       var sintL xlen = x.length();
-       var sintL ylen = y.length();
+       var sintL xlen = x.size();
+       var sintL ylen = y.size();
        if (xlen == 0)
                return _cl_UP(UPR, x);
        if (ylen == 0)
@@ -251,7 +253,7 @@ static const _cl_UP gen_mul (cl_heap_univpoly_ring* UPR, const _cl_UP& x, const
        }
        // Normalize (not necessary in integral domains).
        //gen_normalize(R,result,len);
-       if (R->_zerop(result[len-1])) cl_abort();
+       if (R->_zerop(result[len-1])) throw runtime_exception();
        return _cl_UP(UPR, result);
 }}
 
@@ -259,7 +261,7 @@ static const _cl_UP gen_square (cl_heap_univpoly_ring* UPR, const _cl_UP& x)
 {{
        DeclarePoly(cl_SV_ringelt,x);
        var cl_heap_ring* R = TheRing(UPR->basering());
-       var sintL xlen = x.length();
+       var sintL xlen = x.size();
        if (xlen == 0)
                return cl_UP(UPR, x);
        var sintL len = 2*xlen-1;
@@ -290,7 +292,7 @@ static const _cl_UP gen_square (cl_heap_univpoly_ring* UPR, const _cl_UP& x)
        init1(_cl_ring_element, result[0]) (R->_square(x[0]));
        // Normalize (not necessary in integral domains).
        //gen_normalize(R,result,len);
-       if (R->_zerop(result[len-1])) cl_abort();
+       if (R->_zerop(result[len-1])) throw runtime_exception();
        return _cl_UP(UPR, result);
 }}
 
@@ -310,11 +312,11 @@ static const _cl_UP gen_exptpos (cl_heap_univpoly_ring* UPR, const _cl_UP& x, co
 
 static const _cl_UP gen_scalmul (cl_heap_univpoly_ring* UPR, const cl_ring_element& x, const _cl_UP& y)
 {
-       if (!(UPR->basering() == x.ring())) cl_abort();
+       if (!(UPR->basering() == x.ring())) throw runtime_exception();
  {
        DeclarePoly(cl_SV_ringelt,y);
        var cl_heap_ring* R = TheRing(UPR->basering());
-       var sintL ylen = y.length();
+       var sintL ylen = y.size();
        if (ylen == 0)
                return _cl_UP(UPR, y);
        if (R->zerop(x))
@@ -324,20 +326,31 @@ static const _cl_UP gen_scalmul (cl_heap_univpoly_ring* UPR, const cl_ring_eleme
                init1(_cl_ring_element, result[i]) (R->_mul(x,y[i]));
        // Normalize (not necessary in integral domains).
        //gen_normalize(R,result,ylen);
-       if (R->_zerop(result[ylen-1])) cl_abort();
+       if (R->_zerop(result[ylen-1])) throw runtime_exception();
        return _cl_UP(UPR, result);
 }}
 
 static sintL gen_degree (cl_heap_univpoly_ring* UPR, const _cl_UP& x)
 {
-       unused UPR;
+       cl_unused UPR;
  {     DeclarePoly(cl_SV_ringelt,x);
-       return (sintL) x.length() - 1;
+       return (sintL) x.size() - 1;
+}}
+
+static sintL gen_ldegree (cl_heap_univpoly_ring* UPR, const _cl_UP& x)
+{{     DeclarePoly(cl_SV_ringelt,x);
+       var cl_heap_ring* R = TheRing(UPR->basering());
+       var sintL xlen = x.size();
+       for (sintL i = 0; i < xlen; i++) {
+               if (!R->_zerop(x[i]))
+                       return i;
+       }
+       return -1;
 }}
 
 static const _cl_UP gen_monomial (cl_heap_univpoly_ring* UPR, const cl_ring_element& x, uintL e)
 {
-       if (!(UPR->basering() == x.ring())) cl_abort();
+       if (!(UPR->basering() == x.ring())) throw runtime_exception();
        var cl_heap_ring* R = TheRing(UPR->basering());
        if (R->_zerop(x))
                return _cl_UP(UPR, cl_null_SV_ringelt);
@@ -353,7 +366,7 @@ static const cl_ring_element gen_coeff (cl_heap_univpoly_ring* UPR, const _cl_UP
 {{
        DeclarePoly(cl_SV_ringelt,x);
        var cl_heap_ring* R = TheRing(UPR->basering());
-       if (index < x.length())
+       if (index < x.size())
                return cl_ring_element(R, x[index]);
        else
                return R->zero();
@@ -372,8 +385,8 @@ static const _cl_UP gen_create (cl_heap_univpoly_ring* UPR, sintL deg)
 static void gen_set_coeff (cl_heap_univpoly_ring* UPR, _cl_UP& x, uintL index, const cl_ring_element& y)
 {{
        DeclareMutablePoly(cl_SV_ringelt,x);
-       if (!(UPR->basering() == y.ring())) cl_abort();
-       if (!(index < x.length())) cl_abort();
+       if (!(UPR->basering() == y.ring())) throw runtime_exception();
+       if (!(index < x.size())) throw runtime_exception();
        x[index] = y;
 }}
 
@@ -381,7 +394,7 @@ static void gen_finalize (cl_heap_univpoly_ring* UPR, _cl_UP& x)
 {{
        DeclareMutablePoly(cl_SV_ringelt,x); // NB: x is modified by reference!
        var cl_heap_ring* R = TheRing(UPR->basering());
-       var uintL len = x.length();
+       var uintL len = x.size();
        if (len > 0)
                gen_normalize(R,x,len);
 }}
@@ -394,8 +407,8 @@ static const cl_ring_element gen_eval (cl_heap_univpoly_ring* UPR, const _cl_UP&
        // Else compute (...(x[len-1]*y+x[len-2])*y ...)*y + x[0].
        DeclarePoly(cl_SV_ringelt,x);
        var cl_heap_ring* R = TheRing(UPR->basering());
-       if (!(y.ring() == R)) cl_abort();
-       var uintL len = x.length();
+       if (!(y.ring() == R)) throw runtime_exception();
+       var uintL len = x.size();
        if (len==0)
                return R->zero();
        if (R->_zerop(y))
@@ -434,6 +447,7 @@ static cl_univpoly_modulops gen_modulops = {
 
 static cl_univpoly_polyops gen_polyops = {
        gen_degree,
+       gen_ldegree,
        gen_monomial,
        gen_coeff,
        gen_create,
@@ -446,6 +460,26 @@ class cl_heap_gen_univpoly_ring : public cl_heap_univpoly_ring {
        SUBCLASS_cl_heap_univpoly_ring()
 public:
        // Constructor.
-       cl_heap_gen_univpoly_ring (const cl_ring& r)
-               : cl_heap_univpoly_ring (r, &gen_setops, &gen_addops, &gen_mulops, &gen_modulops, &gen_polyops) {}
+       cl_heap_gen_univpoly_ring (const cl_ring& r);
+       // Destructor
+       ~cl_heap_gen_univpoly_ring () {}
+};
+
+static void cl_heap_gen_univpoly_ring_destructor (cl_heap* pointer)
+{
+       (*(cl_heap_gen_univpoly_ring*)pointer).~cl_heap_gen_univpoly_ring();
+}
+
+cl_class cl_class_gen_univpoly_ring = {
+       cl_heap_gen_univpoly_ring_destructor,
+       cl_class_flags_univpoly_ring
 };
+
+// Constructor.
+inline cl_heap_gen_univpoly_ring::cl_heap_gen_univpoly_ring (const cl_ring& r)
+       : cl_heap_univpoly_ring (r, &gen_setops, &gen_addops, &gen_mulops, &gen_modulops, &gen_polyops)
+{
+       type = &cl_class_gen_univpoly_ring;
+}
+
+}  // namespace cln