algos.cc 8.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/* Common header file for the rrspace software

This file is part of the rrspace project.

This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 51
Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#include "algos.h"
#include <utility>
#include <vector>
#include "ZZ_pXResultant.h"

using namespace NTL;
using namespace std;

size_t 
GetInterpolationDegree(size_t deg_curve, size_t deg_div) {
  if ((deg_curve + 1)*deg_curve > 2*deg_div)
    return floor((1+sqrt(1+8*deg_div)-1)/2);
  return floor((double)deg_div/(double)deg_curve+((double)deg_curve-1)/2);
}

// Returns a vector such that the ind-th element is the pair (i, j) which
// corresponds to the ind-th monomial X^i Y^j such that j < deg_curve
vector<pair<size_t, size_t> >
BuildMonomialVector(size_t deg_curve, size_t deg_max) {
  vector<pair<size_t, size_t> > res;
  for (size_t d = 0; d <= deg_max; ++d)
    for (size_t i = 0; i <= min(d, deg_curve - 1); ++i) 
      res.push_back(pair<size_t, size_t>(d-i, i));
  return res;
}

mat_ZZ_p
BuildInterpolationMatrixInDegree(const EffectiveDivisor& D, size_t deg) {
  vector<ZZ_pX> powers_of_g;
  vector<ZZ_pX> powers_of_x;

  powers_of_g.push_back(ZZ_pX(1));
  powers_of_x.push_back(ZZ_pX(1));

  ZZ_pXModulus modf(D.get_f());

  ZZ_pX x_pol(1);
  x_pol <<= 1;

  for (size_t i = 0; i <= min(deg, D.curve().degree()-1); ++i) {
    ZZ_pX tmp;
    MulMod(tmp, powers_of_g.back(), D.get_g(), modf);
    powers_of_g.push_back(tmp);
  }
 
  for (size_t i = 0; i <= deg; ++i) {
    ZZ_pX tmp;
    MulMod(tmp, powers_of_x.back(), x_pol % modf, modf);
    powers_of_x.push_back(tmp);
  }

  auto vec_mons = BuildMonomialVector(D.curve().degree(), deg);
  Vec<Vec<ZZ_p> > vec_vec_res;
  Vec<ZZ_p> tmp_row;
  ZZ_pX tmp_pol;

  for (const auto& p : vec_mons) {
    tmp_row.SetLength(0);
    MulMod(tmp_pol, powers_of_x[p.first], powers_of_g[p.second], modf);
    for (size_t i = 0; i < D.degree(); ++i)
      tmp_row.append(coeff(tmp_pol, i));
    vec_vec_res.append(tmp_row);
  }
  
  mat_ZZ_p res;
  res.SetDims(vec_mons.size(), D.degree());
  MakeMatrix(res, vec_vec_res);

  return res;
}

BivPol 
Interpolate(const EffectiveDivisor& D) {
  size_t deg_res = GetInterpolationDegree(D.curve().degree(), D.degree());
  mat_ZZ_p M = BuildInterpolationMatrixInDegree(D, deg_res);
  mat_ZZ_p kerM;
  kernel(kerM, M);
  mat_ZZ_p randmat;
  randmat.SetDims(1, kerM.NumRows());
  for (size_t i = 0; i < (size_t)kerM.NumRows(); ++i)
    randmat.put(0, i, random_ZZ_p());
  mul(kerM, randmat, kerM);
  vector<ZZ_pX> interp_pol;
  auto vec_mons = BuildMonomialVector(D.curve().degree(), deg_res);
  interp_pol.insert(interp_pol.begin(),
                    min(D.curve().degree(), deg_res + 1),
                    ZZ_pX(0));
  for (size_t i = 0; i < vec_mons.size(); ++i) {
    pair<size_t, size_t> p = vec_mons[i];
    interp_pol[p.second] += kerM.get(0, i)* (ZZ_pX(1) << p.first);
  }
  while (IsZero(interp_pol.back())) 
    interp_pol.pop_back();
  interp_pol.shrink_to_fit();
  BivPol res(interp_pol);
  assert(!res.IsZero());
  return res;
}

EffectiveDivisor
121 122
PrincipalDivisor(const Curve& C, const BivPol& h, const EffectiveDivisor& E) {
  assert(E.curve() == C);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  size_t deg_res = h.degree() * C.degree(); // Bézout bound
  vec_ZZ_p eval_pts;
  for (long i = 0; i <= (long)deg_res; ++i)
    eval_pts.append(ZZ_p(i));
  vec_vec_ZZ_p eval_C, eval_h;
  vec_ZZ_p eval_res, eval_subres0, eval_subres1;
  for (size_t i = 0; i <= C.degree(); ++i)
    eval_C.append(eval(C.coeff(i), eval_pts));
  for (size_t i = 0; i <= h.degree_y(); ++i)
    eval_h.append(eval(h.coeff(i), eval_pts));

  for (size_t i = 0; i <= deg_res; ++i) {
    ZZ_pX C_tmp, h_tmp, subres_tmp;
    ZZ_p res_tmp;

    for (size_t j = 0; j <= C.degree(); ++j)
      C_tmp += eval_C[j][i] * (ZZ_pX(1) << j);
    for (size_t j = 0; j <= h.degree_y(); ++j)
      h_tmp += eval_h[j][i] * (ZZ_pX(1) << j);
    
    resultantWithSubRes(res_tmp, subres_tmp, C_tmp, h_tmp);
    assert(deg(subres_tmp) == 1);
    eval_res.append(res_tmp);
    eval_subres0.append(ConstTerm(subres_tmp));
    eval_subres1.append(LeadCoeff(subres_tmp));
  }

  ZZ_pX res     = interpolate(eval_pts, eval_res);
  ZZ_pX subres0 = interpolate(eval_pts, eval_subres0);
  ZZ_pX subres1 = interpolate(eval_pts, eval_subres1);

154
  return PositiveDifference(EffectiveDivisor(C, res, -MulMod(InvMod(subres1, res), subres0, res)), E);
155 156 157 158 159 160 161 162 163 164
}

EffectiveDivisor
PositiveDifference(const EffectiveDivisor& D1, const EffectiveDivisor& D2) {
  assert(D1.curve() == D2.curve());
  ZZ_pX f;
  divide(f, D1.get_f(), GCD(D1.get_f(), D2.get_f()));
  return EffectiveDivisor(D1.curve(), f/LeadCoeff(f), D1.get_g() % f);
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
// This simple sum only works if D1 and D2 do not have any point with the same
// x-coordinate in their support
EffectiveDivisor
SimpleSum(const EffectiveDivisor& D1, const EffectiveDivisor& D2) {
  assert(D1.curve() == D2.curve());
  if (deg(D1.get_f()) == 0) 
    return D2;
  if (deg(D2.get_f()) == 0) 
    return D1;
  NTL::ZZ_pX gcd, a1, a2;
  XGCD(gcd, a1, a2, D1.get_f(), D2.get_f());
  assert(deg(gcd) == 0);

  ZZ_pX new_f = D1.get_f()*D2.get_f();
  ZZ_pX new_g = (D1.get_g()*a2*D2.get_f() +
                 D2.get_g()*a1*D1.get_f()) % new_f;
  assert(new_g % D1.get_f() == D1.get_g());
  assert(new_g % D2.get_f() == D2.get_g());
  return EffectiveDivisor(D1.curve(), new_f, new_g);
}

// This adding function works only if all points in the support are
// non-singular
188 189 190 191 192 193 194
EffectiveDivisor
Sum(const EffectiveDivisor& D1, const EffectiveDivisor& D2) {
  assert(D1.curve() == D2.curve());
  if (deg(D1.get_f()) == 0) 
    return D2;
  if (deg(D2.get_f()) == 0) 
    return D1;
195 196 197 198
  NTL::ZZ_pX gcd, a1, a2;
  XGCD(gcd, a1, a2, D1.get_f(), D2.get_f());
  assert((D1.get_g() % gcd) == (D2.get_g() % gcd));

199
  ZZ_pX new_f = D1.get_f()*D2.get_f();
200 201
  ZZ_pX new_g = (D1.get_g()*a2*(D2.get_f()/gcd) +
                 D2.get_g()*a1*(D1.get_f()/gcd)) % (new_f/gcd);
202

203
  new_g = NewtonHenselStep(*D1.curve().get_pdefpol(), new_g, new_f/gcd);
204
  new_g = new_g % new_f;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  assert(new_g % D1.get_f() == D1.get_g());
  assert(new_g % D2.get_f() == D2.get_g());
  return EffectiveDivisor(D1.curve(), new_f, new_g);
}

EffectiveDivisor
MultiplyByInt(const EffectiveDivisor& D, unsigned int k) {
  ZZ_pX _f = D.get_f();
  ZZ_pX _g = D.get_g();
 
  assert(IsZero(D.curve().get_pdefpol()->mod_eval(_g, _f)));

  // Case where D is the zero divisor
  if (deg(_f) == 0) 
    return D;

  int nbsteps;
  frexp((double)k, &nbsteps);
  // TODO: do all computations mod _f
  _g = NewtonHensel(*D.curve().get_pdefpol(), _g, _f, nbsteps);
  power(_f, _f, k);
  return EffectiveDivisor(D.curve(), _f, _g % _f);
}

Divisor
Sum(const Divisor& D1, const Divisor& D2) {
  return Divisor(Sum(D1.get_pos(), D2.get_pos()),
                 Sum(D1.get_neg(), D2.get_neg()));
}

Divisor
MultiplyByInt(const Divisor& D, unsigned int k) {
  return Divisor(MultiplyByInt(D.get_pos(), k), MultiplyByInt(D.get_neg(), k));
}

vector<BivPol>
EffectiveBasisRRinDegree(const EffectiveDivisor& D, size_t deg) {
  vector<BivPol> res;
  mat_ZZ_p M = BuildInterpolationMatrixInDegree(D, deg);
  mat_ZZ_p kerM;
  kernel(kerM, M);
  for (long k = 0; k < kerM.NumRows(); ++k) {
    vector<ZZ_pX> interp_pol;
    auto vec_mons = BuildMonomialVector(D.curve().degree(), deg);
    interp_pol.insert(interp_pol.begin(),
        min(D.curve().degree(), deg + 1),
        ZZ_pX(0));
    for (size_t i = 0; i < vec_mons.size(); ++i) {
      pair<size_t, size_t> p = vec_mons[i];
254
      interp_pol[p.second] += kerM.get(k, i)*(ZZ_pX(1) << p.first);
255 256 257 258 259 260 261 262 263 264
    }
    while (IsZero(interp_pol.back())) 
      interp_pol.pop_back();
    interp_pol.shrink_to_fit();
    res.push_back(BivPol(interp_pol));
  }
  return res;
}

RRspace
265
RiemannRochBasis(const Divisor& D, const EffectiveDivisor& E) {
266 267
  assert(D.curve().get_pdefpol()->mod_eval(D.get_pos().get_g(), D.get_pos().get_f()) == 0);
  assert(D.curve().get_pdefpol()->mod_eval(D.get_neg().get_g(), D.get_neg().get_f()) == 0);
268 269 270
  assert(D.curve().get_pdefpol()->mod_eval(E.get_g(), E.get_f()) == 0);
  BivPol h = Interpolate(SimpleSum(D.get_pos(), E));
  EffectiveDivisor Dp = PrincipalDivisor(D.curve(), h, E);
271
  EffectiveDivisor Dp2 = PositiveDifference(Dp, D.get_pos());
272 273
  EffectiveDivisor Dp3 = PositiveDifference(Dp2, E);
  EffectiveDivisor newDneg = SimpleSum(Sum(Dp3, D.get_neg()), E);
274 275
  return RRspace(h, EffectiveBasisRRinDegree(newDneg, h.degree()), newDneg);
}