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 121 122 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
// cubic.cc: implementation of integer cubic class
//////////////////////////////////////////////////////////////////////////
//
// Copyright 1990-2012 John Cremona
//
// This file is part of the eclib package.
//
// eclib is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// eclib 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 General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with eclib; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
//////////////////////////////////////////////////////////////////////////
//
#include <eclib/marith.h>
#include <eclib/unimod.h>
#include <eclib/cubic.h>
#include <eclib/realroots.h>
void cubic::init()
{
coeffs = new bigint[4];
}
cubic::~cubic()
{
delete [] coeffs;
}
void cubic::transform(const unimod& m)
{
bigint m112=sqr(m(1,1)); bigint m113=m112*m(1,1);
bigint m212=sqr(m(2,1)); bigint m213=m212*m(2,1);
bigint m222=sqr(m(2,2)); bigint m223=m222*m(2,2);
bigint m122=sqr(m(1,2)); bigint m123=m122*m(1,2);
coeffs[0] = m113*a() + m(2,1)*m112*b() + m212*m(1,1)*c() + m213*d();
coeffs[1] = m123*a() + m(2,2)*m122*b() + m222*m(1,2)*c() + m223*d();
coeffs[3] = 3*m(1,2)*m112*a() + (m(2,2)*m112 + 2*m(2,1)*m(1,2)*m(1,1))*b()
+ (2*m(2,2)*m(2,1)*m(1,1) + m212*m(1,2))*c() + 3*m(2,2)*m212*d();
coeffs[2] = 3*m122*m(1,1)*a() + (2*m(2,2)*m(1,2)*m(1,1) + m(2,1)*m122)*b()
+ (m222*m(1,1) + 2*m(2,2)*m(2,1)*m(1,2))*c() + 3*m222*m(2,1)*d();
}
void cubic::x_shift(const bigint& e, unimod& m)
{
coeffs[3] += e*(c()+e*( b()+ e*a()));
coeffs[2] += e*(2*b()+3*e*a());
coeffs[1] += 3*e*a();
m.x_shift(e);
}
void cubic::y_shift(const bigint& e, unimod& m)
{
coeffs[0] += e*(b()+e*( c()+ e*d()));
coeffs[1] += e*(2*c()+3*e*d());
coeffs[2] += 3*e*d();
m.y_shift(e);
}
void cubic::invert(unimod& m)
{
swap(coeffs[0],coeffs[3]); ::negate(coeffs[0]);
swap(coeffs[1],coeffs[2]); ::negate(coeffs[2]);
m.invert();
}
bigint cubic::j_c1() const
{
bigint b2=sqr(b());
bigint b3=b()*b2;
bigint b4=b()*b3;
bigint b5=b()*b4;
bigint b6=b()*b5;
bigint a2=sqr(a());
bigint a3=a()*a2;
bigint a4=a()*a3;
bigint c2=sqr(c());
bigint c3=c()*c2;
bigint c4=c()*c3;
bigint c5=c()*c4;
bigint c6=c()*c5;
bigint d2=sqr(d());
bigint d3=d()*d2;
bigint d4=d()*d3;
bigint ac=a()*c(), bd=b()*d();
return - 108*b3*a2*d() - 3*b4*c2 + 54*a2*c4 + 18*b5*d() + 243*a2*d2*b2 -
54*b3*ac*d() - 162*bd*c2*a2 - 54*a3*c3 + 486*a3*bd*c() + 3*c4*b2 -
18*c5*a() + 54*c3*a()*bd - 243*d2*a2*c2 + 162*d2*ac*b2 + 2*c6 -
729*a4*d2 - 2*b6 + 18*b4*ac - 27*a2*b2*c2 + 729*d4*a2 + 54*b3*d3 +
108*c3*d2*a() - 18*c4*bd + 27*d2*c2*b2 - 486*d3*ac*b() - 54*d2*b4;
}
bigint cubic::j_c2() const
{
bigint b2=sqr(b());
bigint b3=b()*b2;
bigint b4=b()*b3;
bigint b5=b()*b4;
bigint b6=b()*b5;
bigint a2=sqr(a());
bigint a3=a()*a2;
bigint a4=a()*a3;
bigint c2=sqr(c());
bigint c3=c()*c2;
bigint c4=c()*c3;
bigint c5=c()*c4;
bigint c6=c()*c5;
bigint d2=sqr(d());
bigint d3=d()*d2;
bigint d4=d()*d3;
bigint ac=a()*c(), bd=b()*d();
return - 108*b3*a2*d() + 12*b4*c2 - 216*a2*c4 - 72*b5*d() - 486*a3*c2*d()
+ 270*a2*c3*b() - 90*b3*c2*a() - 972*a2*d2*b2 + 216*b3*ac*d() +
648*bd*c2*a2 - 54*a3*c3 + 486*a3*bd*c() - 16*c3*b3 + 216*d2*b3*a() +
72*d()*b4*c() + 72*c4*b()*a() + 216*d()*c3*a2 - 432*d()*b2*a()*c2
- 729*a4*d2 - 2*b6
+ 18*b4*ac - 27*a2*b2*c2 + 6*b5*c() - 648*b2*c()*a2*d()
+ 162*a()*d()*b4 + 1458*a3*d2*b();
}
bigint cubic::j_c3() const
{
bigint a = coeffs[0], b=coeffs[1], c=coeffs[2], d=coeffs[3];
bigint b2=b*b;
bigint b3=b*b2;
bigint b4=b*b3;
bigint b5=b*b4;
bigint b6=b*b5;
bigint a2=a*a;
bigint a3=a*a2;
bigint a4=a*a3;
bigint c2=c*c;
bigint c3=c*c2;
bigint c4=c*c3;
bigint c5=c*c4;
bigint c6=c*c5;
bigint d2=d*d;
bigint d3=d*d2;
bigint d4=d*d3;
return 108*b3*a2*d - 12*b4*c2 + 216*a2*c4 + 72*b5*d - 486*a3*c2*d +
270*a2*c3*b - 90*b3*c2*a + 972*a2*d2*b2 - 216*b3*c*a*d - 648*b*c2*a2*d
+ 54*a3*c3 - 486*a3*d*c*b - 16*c3*b3 + 216*d2*b3*a + 72*d*b4*c +
72*c4*b*a + 216*d*c3*a2 - 432*d*b2*a*c2 + 729*a4*d2 + 2*b6 - 18*b4*a*c
+ 27*a2*b2*c2 + 6*b5*c - 648*b2*c*a2*d + 162*a*d*b4 + 1458*a3*d2*b;
}
//#define DEBUG
bigcomplex cubic::hess_root() const
{
bigfloat discr = I2bigfloat(disc());
if(!is_positive(disc()))
{
cout<<"Error: hess_root called with negative dicriminant!\n";
return to_bigfloat(0);
}
bigfloat P = I2bigfloat(p_semi());
bigfloat Q = I2bigfloat(q_semi());
bigfloat delta = sqrt(3*discr);
bigcomplex gamma(-Q,delta); gamma/=(2*P);
return gamma;
}
void cubic::hess_reduce(unimod& m)
{
int s=1; bigint k;
m.reset();
while(s)
{
s=0;
k = roundover(-q_semi(),2*p_semi());
if(!is_zero(k))
{
s=1; x_shift(k,m);
#ifdef DEBUG
cout << "Shift by " << k << ": " << (*this) << endl;
#endif
}
if(p_semi()>r_semi())
{
s=1; invert(m);
#ifdef DEBUG
cout << "invert: " << (*this) << endl;
#endif
}
}
if(a()<0) {::negate(coeffs[0]); ::negate(coeffs[2]);}
}
void cubic::mathews_reduce(unimod& m)
{
int s=1; bigint k; bigfloat alpha;
m.reset();
while(s)
{
s=0;
if(mat_c1()<0)
{
s=1; invert(m);
#ifdef DEBUG
cout << "invert: " << (*this) << endl;
#endif
}
alpha = real_root();
k = Iround(-alpha/2 - I2bigfloat(b())/I2bigfloat(2*a()));
x_shift(k,m);
#ifdef DEBUG
cout << "Shift by "<<k<<": "<<(*this)<<endl;
#endif
bigint plus1, minus1; plus1=1; minus1=-1;
while(mat_c2()>0)
{
s=1; x_shift(plus1,m);
#ifdef DEBUG
cout << "Shift by +1: "<<(*this)<<endl;
#endif
}
while(mat_c3()<0)
{
s=1; x_shift(minus1,m);
#ifdef DEBUG
cout << "Shift by -1: "<<(*this)<<endl;
#endif
}
}
if(a()<0) {::negate(coeffs[0]); ::negate(coeffs[2]);}
}
void cubic::jc_reduce(unimod& m)
{
int s=1; bigint k, jc2, jc3;
bigint plus1, minus1; plus1=1; minus1=-1;
m.reset();
while(s)
{
s=0;
if(j_c1()<0)
{
s=1; invert(m);
#ifdef DEBUG
cout << "invert: " << (*this) << endl;
#endif
}
bigfloat alpha = real_root();
bigfloat ra = I2bigfloat(a());
bigfloat rb = I2bigfloat(b());
bigfloat rc = I2bigfloat(c());
bigfloat h0 = (9*ra*ra*alpha + 6*ra*rb)*alpha + 6*ra*rc-rb*rb;
bigfloat h1 = 6*(ra*rb*alpha + (rb*rb-ra*rc))*alpha + 2*rb*rc;
k = Iround(-h1/(2*h0));
x_shift(k,m);
#ifdef DEBUG
cout << "Shift by "<<k<<": "<<(*this)<<endl;
#endif
while(j_c2()>0)
{
s=1; x_shift(minus1,m);
#ifdef DEBUG
cout << "Shift by -1: "<<(*this)<<endl;
#endif
}
while(j_c3()<0)
{
s=1; x_shift(plus1,m);
#ifdef DEBUG
cout << "Shift by +1: "<<(*this)<<endl;
#endif
}
}
if(a()<0) {::negate(coeffs[0]); ::negate(coeffs[1]);}
}
// Just shifts x:
bigint cubic::shift_reduce()
{
unimod m; bigint k;
if(is_positive(disc()))
{
k = roundover(-q_semi(),2*p_semi());
}
else
{
bigfloat alpha = real_root();
bigfloat ra = I2bigfloat(a());
bigfloat rb = I2bigfloat(b());
bigfloat rc = I2bigfloat(c());
bigfloat h0 = (9*ra*ra*alpha + 6*ra*rb)*alpha + 6*ra*rc-rb*rb;
bigfloat h1 = 6*(ra*rb*alpha + (rb*rb-ra*rc))*alpha + 2*rb*rc;
k = Iround(-h1/(2*h0));
}
x_shift(k,m);
return k;
}
bigfloat cubic::real_root() const
{
bigfloat discr = I2bigfloat(disc());
if(discr>=0)
{
cout<<"Error: real_root called with positive dicriminant!\n";
return to_bigfloat(0);
}
bigfloat P = I2bigfloat(p_semi());
bigfloat Q = I2bigfloat(q_semi());
bigfloat A = I2bigfloat(a());
if(is_zero(P))
{
bigfloat Q = I2bigfloat(q_semi());
bigfloat R = I2bigfloat(r_semi())/Q;
bigfloat eta3 = I2bigfloat(d())/A - (I2bigfloat(c())*R)/(3*A);
bigfloat eta = cube_root(eta3);
bigfloat alpha = -eta - R;
return alpha;
}
bigfloat U = I2bigfloat(u_semi());
bigfloat delta = sqrt(-3*discr);
bigfloat gamma1 = (-Q+delta)/(2*P); // roots of Hessian
bigfloat gamma2 = (-Q-delta)/(2*P); //
bigfloat eta3 = (U-3*A*delta)/(U+3*A*delta);
bigfloat eta = cube_root(eta3);
bigfloat alpha = (eta*gamma1-gamma2)/(eta-1);
return alpha;
}
|