My Project
Loading...
Searching...
No Matches
Macros | Functions
bigintmat.cc File Reference
#include "misc/auxiliary.h"
#include "coeffs/bigintmat.h"
#include "misc/intvec.h"
#include "coeffs/rmodulon.h"
#include <cmath>

Go to the source code of this file.

Macros

#define swap(_i, _j)
 
#define MIN(a, b)   (a < b ? a : b)
 

Functions

static coeffs numbercoeffs (number n, coeffs c)
 create Z/nA of type n_Zn
 
bool operator== (const bigintmat &lhr, const bigintmat &rhr)
 
bool operator!= (const bigintmat &lhr, const bigintmat &rhr)
 
bigintmatbimAdd (bigintmat *a, bigintmat *b)
 Matrix-Add/-Sub/-Mult so oder mit operator+/-/* ? @Note: NULL as a result means an error (non-compatible matrices?)
 
bigintmatbimAdd (bigintmat *a, long b)
 
bigintmatbimSub (bigintmat *a, bigintmat *b)
 
bigintmatbimSub (bigintmat *a, long b)
 
bigintmatbimMult (bigintmat *a, bigintmat *b)
 
bigintmatbimMult (bigintmat *a, long b)
 
bigintmatbimMult (bigintmat *a, number b, const coeffs cf)
 
intvecbim2iv (bigintmat *b)
 
bigintmativ2bim (intvec *b, const coeffs C)
 
bigintmatbimCopy (const bigintmat *b)
 same as copy constructor - apart from it being able to accept NULL as input
 
static int intArrSum (int *a, int length)
 
static int findLongest (int *a, int length)
 
static int getShorter (int *a, int l, int j, int cols, int rows)
 
bigintmatbimChangeCoeff (bigintmat *a, coeffs cnew)
 Liefert Kopier von Matrix a zurück, mit coeffs cnew statt den ursprünglichen.
 
void bimMult (bigintmat *a, bigintmat *b, bigintmat *c)
 Multipliziert Matrix a und b und speichert Ergebnis in c.
 
static void reduce_mod_howell (bigintmat *A, bigintmat *b, bigintmat *eps, bigintmat *x)
 
static bigintmatprependIdentity (bigintmat *A)
 
static number bimFarey (bigintmat *A, number N, bigintmat *L)
 
static number solveAx_dixon (bigintmat *A, bigintmat *B, bigintmat *x, bigintmat *kern)
 
static number solveAx_howell (bigintmat *A, bigintmat *b, bigintmat *x, bigintmat *kern)
 
number solveAx (bigintmat *A, bigintmat *b, bigintmat *x)
 solve Ax=b*d. x needs to be pre-allocated to the same number of columns as b. the minimal denominator d is returned. Currently available for Z, Q and Z/nZ (and possibly for all fields: d=1 there) Beware that the internal functions can find the kernel as well - but the interface is lacking.
 
void diagonalForm (bigintmat *A, bigintmat **S, bigintmat **T)
 
int kernbase (bigintmat *a, bigintmat *c, number p, coeffs q)
 a basis for the nullspace of a mod p: only used internally in Round2. Don't use it.
 
bool nCoeffs_are_equal (coeffs r, coeffs s)
 

Macro Definition Documentation

◆ MIN

#define MIN (   a,
  b 
)    (a < b ? a : b)

◆ swap

#define swap (   _i,
  _j 
)
Value:
int __i = (_i), __j=(_j); \
number c = v[__i]; \
v[__i] = v[__j]; \
v[__j] = c \
const Variable & v
< [in] a sqrfree bivariate poly
Definition facBivar.h:39

Function Documentation

◆ bim2iv()

intvec * bim2iv ( bigintmat b)

Definition at line 339 of file bigintmat.cc.

340{
341 intvec * iv = new intvec(b->rows(), b->cols(), 0);
342 for (int i=0; i<(b->rows())*(b->cols()); i++)
343 (*iv)[i] = n_Int((*b)[i], b->basecoeffs()); // Geht das so?
344 return iv;
345}
int i
Definition cfEzgcd.cc:132
CanonicalForm b
Definition cfModGcd.cc:4111
static FORCE_INLINE long n_Int(number &n, const coeffs r)
conversion of n to an int; 0 if not possible in Z/pZ: the representing int lying in (-p/2 ....
Definition coeffs.h:548

◆ bimAdd() [1/2]

bigintmat * bimAdd ( bigintmat a,
bigintmat b 
)

Matrix-Add/-Sub/-Mult so oder mit operator+/-/* ? @Note: NULL as a result means an error (non-compatible matrices?)

Definition at line 180 of file bigintmat.cc.

181{
182 if (a->cols() != b->cols()) return NULL;
183 if (a->rows() != b->rows()) return NULL;
184 if (a->basecoeffs() != b->basecoeffs()) { return NULL; }
185
186 const coeffs basecoeffs = a->basecoeffs();
187
188 int i;
189
190 bigintmat * bim = new bigintmat(a->rows(), a->cols(), basecoeffs);
191
192 for (i=a->rows()*a->cols()-1;i>=0; i--)
193 bim->rawset(i, n_Add((*a)[i], (*b)[i], basecoeffs), basecoeffs);
194
195 return bim;
196}
Matrices of numbers.
Definition bigintmat.h:51
int cols() const
Definition bigintmat.h:144
int rows() const
Definition bigintmat.h:145
void rawset(int i, number n, const coeffs C=NULL)
replace an entry with the given number n (only delete old). NOTE: starts at [0]. Should be named set_...
Definition bigintmat.h:196
coeffs basecoeffs() const
Definition bigintmat.h:146
static FORCE_INLINE number n_Add(number a, number b, const coeffs r)
return the sum of 'a' and 'b', i.e., a+b
Definition coeffs.h:651
The main handler for Singular numbers which are suitable for Singular polynomials.
#define NULL
Definition omList.c:12

◆ bimAdd() [2/2]

bigintmat * bimAdd ( bigintmat a,
long  b 
)

Definition at line 197 of file bigintmat.cc.

198{
199
200 const int mn = si_min(a->rows(),a->cols());
201
202 const coeffs basecoeffs = a->basecoeffs();
203 number bb=n_Init(b,basecoeffs);
204
205 int i;
206
207 bigintmat * bim = new bigintmat(a->rows(),a->cols() , basecoeffs);
208
209 for (i=1; i<=mn; i++)
210 BIMATELEM(*bim,i,i)=n_Add(BIMATELEM(*a,i,i), bb, basecoeffs);
211
212 n_Delete(&bb,basecoeffs);
213 return bim;
214}
static int si_min(const int a, const int b)
Definition auxiliary.h:125
#define BIMATELEM(M, I, J)
Definition bigintmat.h:133
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition coeffs.h:459
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition coeffs.h:539

◆ bimChangeCoeff()

bigintmat * bimChangeCoeff ( bigintmat a,
coeffs  cnew 
)

Liefert Kopier von Matrix a zurück, mit coeffs cnew statt den ursprünglichen.

Definition at line 1796 of file bigintmat.cc.

1797{
1798 coeffs cold = a->basecoeffs();
1799 bigintmat *b = new bigintmat(a->rows(), a->cols(), cnew);
1800 // Erzeugt Karte von alten coeffs nach neuen
1802 number t1;
1803 number t2;
1804 // apply map to all entries.
1805 for (int i=1; i<=a->rows(); i++)
1806 {
1807 for (int j=1; j<=a->cols(); j++)
1808 {
1809 t1 = a->get(i, j);
1810 t2 = f(t1, cold, cnew);
1811 b->set(i, j, t2);
1812 n_Delete(&t1, cold);
1813 n_Delete(&t2, cnew);
1814 }
1815 }
1816 return b;
1817}
FILE * f
Definition checklibs.c:9
number get(int i, int j) const
get a copy of an entry. NOTE: starts at [1,1]
Definition bigintmat.cc:117
static FORCE_INLINE nMapFunc n_SetMap(const coeffs src, const coeffs dst)
set the mapping function pointers for translating numbers from src to dst
Definition coeffs.h:701
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
Definition coeffs.h:80
int j
Definition facHensel.cc:110

◆ bimCopy()

bigintmat * bimCopy ( const bigintmat b)

same as copy constructor - apart from it being able to accept NULL as input

Definition at line 403 of file bigintmat.cc.

404{
405 if (b == NULL)
406 return NULL;
407
408 return new bigintmat(b);
409}

◆ bimFarey()

static number bimFarey ( bigintmat A,
number  N,
bigintmat L 
)
static

Definition at line 2037 of file bigintmat.cc.

2038{
2039 coeffs Z = A->basecoeffs(),
2040 Q = nInitChar(n_Q, 0);
2041 number den = n_Init(1, Z);
2042 nMapFunc f = n_SetMap(Q, Z);
2043
2044 for(int i=1; i<= A->rows(); i++)
2045 {
2046 for(int j=1; j<= A->cols(); j++)
2047 {
2048 number ad = n_Mult(den, A->view(i, j), Z);
2049 number re = n_IntMod(ad, N, Z);
2050 n_Delete(&ad, Z);
2051 number q = n_Farey(re, N, Z);
2052 n_Delete(&re, Z);
2053 if (!q)
2054 {
2055 n_Delete(&ad, Z);
2056 n_Delete(&den, Z);
2057 return NULL;
2058 }
2059
2060 number d = n_GetDenom(q, Q),
2061 n = n_GetNumerator(q, Q);
2062
2063 n_Delete(&q, Q);
2064 n_Delete(&ad, Z);
2065 number dz = f(d, Q, Z),
2066 nz = f(n, Q, Z);
2067 n_Delete(&d, Q);
2068 n_Delete(&n, Q);
2069
2070 if (!n_IsOne(dz, Z))
2071 {
2072 L->skalmult(dz, Z);
2073 n_InpMult(den, dz, Z);
2074#if 0
2075 PrintS("den increasing to ");
2076 n_Print(den, Z);
2077 PrintLn();
2078#endif
2079 }
2080 n_Delete(&dz, Z);
2081 L->rawset(i, j, nz);
2082 }
2083 }
2084
2085 nKillChar(Q);
2086 PrintS("bimFarey worked\n");
2087#if 0
2088 L->Print();
2089 PrintS("\n * 1/");
2090 n_Print(den, Z);
2091 PrintLn();
2092#endif
2093 return den;
2094}
CanonicalForm den(const CanonicalForm &f)
const CanonicalForm CFMap CFMap & N
Definition cfEzgcd.cc:56
void Print()
IO: simply prints the matrix to the current output (screen?)
Definition bigintmat.cc:437
bool skalmult(number b, coeffs c)
Multipliziert zur Matrix den Skalar b hinzu.
Definition bigintmat.cc:932
static FORCE_INLINE number n_Mult(number a, number b, const coeffs r)
return the product of 'a' and 'b', i.e., a*b
Definition coeffs.h:637
static FORCE_INLINE number n_GetDenom(number &n, const coeffs r)
return the denominator of n (if elements of r are by nature not fractional, result is 1)
Definition coeffs.h:604
@ n_Q
rational (GMP) numbers
Definition coeffs.h:30
void n_Print(number &a, const coeffs r)
print a number (BEWARE of string buffers!) mostly for debugging
Definition numbers.cc:655
static FORCE_INLINE number n_Farey(number a, number b, const coeffs r)
Definition coeffs.h:760
coeffs nInitChar(n_coeffType t, void *parameter)
one-time initialisations for new coeffs in case of an error return NULL
Definition numbers.cc:406
static FORCE_INLINE number n_IntMod(number a, number b, const coeffs r)
for r a field, return n_Init(0,r) always: n_Div(a,b,r)*b+n_IntMod(a,b,r)==a n_IntMod(a,...
Definition coeffs.h:629
static FORCE_INLINE void n_InpMult(number &a, number b, const coeffs r)
multiplication of 'a' and 'b'; replacement of 'a' by the product a*b
Definition coeffs.h:642
static FORCE_INLINE number n_GetNumerator(number &n, const coeffs r)
return the numerator of n (if elements of r are by nature not fractional, result is n)
Definition coeffs.h:609
void nKillChar(coeffs r)
undo all initialisations
Definition numbers.cc:556
static FORCE_INLINE BOOLEAN n_IsOne(number n, const coeffs r)
TRUE iff 'n' represents the one element.
Definition coeffs.h:472
void PrintS(const char *s)
Definition reporter.cc:284
void PrintLn()
Definition reporter.cc:310
#define A
Definition sirandom.c:24
#define Q
Definition sirandom.c:26

◆ bimMult() [1/4]

bigintmat * bimMult ( bigintmat a,
bigintmat b 
)

Definition at line 253 of file bigintmat.cc.

254{
255 const int ca = a->cols();
256 const int cb = b->cols();
257
258 const int ra = a->rows();
259 const int rb = b->rows();
260
261 if (ca != rb)
262 {
263#ifndef SING_NDEBUG
264 Werror("wrong bigintmat sizes at multiplication a * b: acols: %d != brows: %d\n", ca, rb);
265#endif
266 return NULL;
267 }
268
269 assume (ca == rb);
270
271 if (a->basecoeffs() != b->basecoeffs()) { return NULL; }
272
273 const coeffs basecoeffs = a->basecoeffs();
274
275 int i, j, k;
276
277 number sum;
278
279 bigintmat * bim = new bigintmat(ra, cb, basecoeffs);
280
281 for (i=1; i<=ra; i++)
282 for (j=1; j<=cb; j++)
283 {
284 sum = n_Init(0, basecoeffs);
285
286 for (k=1; k<=ca; k++)
287 {
288 number prod = n_Mult( BIMATELEM(*a, i, k), BIMATELEM(*b, k, j), basecoeffs);
289
290 n_InpAdd(sum, prod, basecoeffs);
291
292 n_Delete(&prod, basecoeffs);
293 }
294 bim->rawset(i, j, sum, basecoeffs);
295 }
296 return bim;
297}
int k
Definition cfEzgcd.cc:99
static FORCE_INLINE void n_InpAdd(number &a, number b, const coeffs r)
addition of 'a' and 'b'; replacement of 'a' by the sum a+b
Definition coeffs.h:647
fq_nmod_poly_t prod
Definition facHensel.cc:100
#define assume(x)
Definition mod2.h:389
void Werror(const char *fmt,...)
Definition reporter.cc:189

◆ bimMult() [2/4]

void bimMult ( bigintmat a,
bigintmat b,
bigintmat c 
)

Multipliziert Matrix a und b und speichert Ergebnis in c.

Definition at line 1921 of file bigintmat.cc.

1922{
1923 if (!nCoeffs_are_equal(a->basecoeffs(), b->basecoeffs()))
1924 {
1925 WerrorS("Error in bimMult. Coeffs do not agree!");
1926 return;
1927 }
1928 if ((a->rows() != c->rows()) || (b->cols() != c->cols()) || (a->cols() != b->rows()))
1929 {
1930 WerrorS("Error in bimMult. Dimensions do not agree!");
1931 return;
1932 }
1933 bigintmat *tmp = bimMult(a, b);
1934 c->copy(tmp);
1935
1936 delete tmp;
1937}
bool nCoeffs_are_equal(coeffs r, coeffs s)
bigintmat * bimMult(bigintmat *a, bigintmat *b)
Definition bigintmat.cc:253
bool copy(bigintmat *b)
Kopiert Einträge von b auf Bigintmat.
void WerrorS(const char *s)
Definition feFopen.cc:24

◆ bimMult() [3/4]

bigintmat * bimMult ( bigintmat a,
long  b 
)

Definition at line 299 of file bigintmat.cc.

300{
301
302 const int mn = a->rows()*a->cols();
303
304 const coeffs basecoeffs = a->basecoeffs();
305 number bb=n_Init(b,basecoeffs);
306
307 int i;
308
309 bigintmat * bim = new bigintmat(a->rows(),a->cols() , basecoeffs);
310
311 for (i=0; i<mn; i++)
312 bim->rawset(i, n_Mult((*a)[i], bb, basecoeffs), basecoeffs);
313
314 n_Delete(&bb,basecoeffs);
315 return bim;
316}

◆ bimMult() [4/4]

bigintmat * bimMult ( bigintmat a,
number  b,
const coeffs  cf 
)

Definition at line 318 of file bigintmat.cc.

319{
320 if (cf!=a->basecoeffs()) return NULL;
321
322 const int mn = a->rows()*a->cols();
323
324 const coeffs basecoeffs = a->basecoeffs();
325
326 int i;
327
328 bigintmat * bim = new bigintmat(a->rows(),a->cols() , basecoeffs);
329
330 for (i=0; i<mn; i++)
331 bim->rawset(i, n_Mult((*a)[i], b, basecoeffs), basecoeffs);
332
333 return bim;
334}
CanonicalForm cf
Definition cfModGcd.cc:4091

◆ bimSub() [1/2]

bigintmat * bimSub ( bigintmat a,
bigintmat b 
)

Definition at line 216 of file bigintmat.cc.

217{
218 if (a->cols() != b->cols()) return NULL;
219 if (a->rows() != b->rows()) return NULL;
220 if (a->basecoeffs() != b->basecoeffs()) { return NULL; }
221
222 const coeffs basecoeffs = a->basecoeffs();
223
224 int i;
225
226 bigintmat * bim = new bigintmat(a->rows(), a->cols(), basecoeffs);
227
228 for (i=a->rows()*a->cols()-1;i>=0; i--)
229 bim->rawset(i, n_Sub((*a)[i], (*b)[i], basecoeffs), basecoeffs);
230
231 return bim;
232}
static FORCE_INLINE number n_Sub(number a, number b, const coeffs r)
return the difference of 'a' and 'b', i.e., a-b
Definition coeffs.h:656

◆ bimSub() [2/2]

bigintmat * bimSub ( bigintmat a,
long  b 
)

Definition at line 234 of file bigintmat.cc.

235{
236 const int mn = si_min(a->rows(),a->cols());
237
238 const coeffs basecoeffs = a->basecoeffs();
239 number bb=n_Init(b,basecoeffs);
240
241 int i;
242
243 bigintmat * bim = new bigintmat(a->rows(),a->cols() , basecoeffs);
244
245 for (i=1; i<=mn; i++)
246 BIMATELEM(*bim,i,i)=n_Sub(BIMATELEM(*a,i,i), bb, basecoeffs);
247
248 n_Delete(&bb,basecoeffs);
249 return bim;
250}

◆ diagonalForm()

void diagonalForm ( bigintmat A,
bigintmat **  S,
bigintmat **  T 
)

Definition at line 2460 of file bigintmat.cc.

2461{
2462 bigintmat * t, *s, *a=A;
2463 coeffs R = a->basecoeffs();
2464 if (T)
2465 {
2466 *T = new bigintmat(a->cols(), a->cols(), R),
2467 (*T)->one();
2468 t = new bigintmat(*T);
2469 }
2470 else
2471 {
2472 t = *T;
2473 }
2474
2475 if (S)
2476 {
2477 *S = new bigintmat(a->rows(), a->rows(), R);
2478 (*S)->one();
2479 s = new bigintmat(*S);
2480 }
2481 else
2482 {
2483 s = *S;
2484 }
2485
2486 int flip=0;
2487 do
2488 {
2489 bigintmat * x, *X;
2490 if (flip)
2491 {
2492 x = s;
2493 X = *S;
2494 }
2495 else
2496 {
2497 x = t;
2498 X = *T;
2499 }
2500
2501 if (x)
2502 {
2503 x->one();
2504 bigintmat * r = new bigintmat(a->rows()+a->cols(), a->cols(), R);
2505 bigintmat * rw = new bigintmat(1, a->cols(), R);
2506 for(int i=0; i<a->cols(); i++)
2507 {
2508 x->getrow(i+1, rw);
2509 r->setrow(i+1, rw);
2510 }
2511 for (int i=0; i<a->rows(); i++)
2512 {
2513 a->getrow(i+1, rw);
2514 r->setrow(i+a->cols()+1, rw);
2515 }
2516 r->hnf();
2517 for(int i=0; i<a->cols(); i++)
2518 {
2519 r->getrow(i+1, rw);
2520 x->setrow(i+1, rw);
2521 }
2522 for(int i=0; i<a->rows(); i++)
2523 {
2524 r->getrow(i+a->cols()+1, rw);
2525 a->setrow(i+1, rw);
2526 }
2527 delete rw;
2528 delete r;
2529
2530#if 0
2531 Print("X: %ld\n", X);
2532 X->Print();
2533 Print("\nx: %ld\n", x);
2534 x->Print();
2535#endif
2536 bimMult(X, x, X);
2537#if 0
2538 Print("\n2:X: %ld %ld %ld\n", X, *S, *T);
2539 X->Print();
2540 Print("\n2:x: %ld\n", x);
2541 x->Print();
2542 PrintLn();
2543#endif
2544 }
2545 else
2546 {
2547 a->hnf();
2548 }
2549
2550 int diag = 1;
2551 for(int i=a->rows(); diag && i>0; i--)
2552 {
2553 for(int j=a->cols(); j>0; j--)
2554 {
2555 if ((a->rows()-i)!=(a->cols()-j) && !n_IsZero(a->view(i, j), R))
2556 {
2557 diag = 0;
2558 break;
2559 }
2560 }
2561 }
2562#if 0
2563 PrintS("Diag ? %d\n", diag);
2564 a->Print();
2565 PrintLn();
2566#endif
2567 if (diag) break;
2568
2569 a = a->transpose(); // leaks - I need to write inpTranspose
2570 flip = 1-flip;
2571 } while (1);
2572 if (flip)
2573 a = a->transpose();
2574
2575 if (S) *S = (*S)->transpose();
2576 if (s) delete s;
2577 if (t) delete t;
2578 A->copy(a);
2579}
Variable x
Definition cfModGcd.cc:4090
void hnf()
transforms INPLACE to HNF
bigintmat * transpose()
Definition bigintmat.cc:35
void setrow(int i, bigintmat *m)
Setzt i-te Zeile gleich übergebenem Vektor (Matrix) m.
Definition bigintmat.cc:854
number view(int i, int j) const
view an entry an entry. NOTE: starts at [1,1]
Definition bigintmat.cc:125
void one()
Macht Matrix (Falls quadratisch) zu Einheitsmatrix.
void getrow(int i, bigintmat *a)
Schreibt i-te Zeile in Vektor (Matrix) a.
Definition bigintmat.cc:785
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff 'n' represents the zero element.
Definition coeffs.h:468
#define Print
Definition emacs.cc:80
const CanonicalForm int s
Definition facAbsFact.cc:51
std::pair< ideal, ring > flip(const ideal I, const ring r, const gfan::ZVector interiorPoint, const gfan::ZVector facetNormal, const gfan::ZVector adjustedInteriorPoint, const gfan::ZVector adjustedFacetNormal)
Definition flip.cc:17
STATIC_VAR jList * T
Definition janet.cc:30
#define R
Definition sirandom.c:27

◆ findLongest()

static int findLongest ( int a,
int  length 
)
static

Definition at line 531 of file bigintmat.cc.

532{
533 int l = 0;
534 int index;
535 for (int i=0; i<length; i++)
536 {
537 if (a[i] > l)
538 {
539 l = a[i];
540 index = i;
541 }
542 }
543 return index;
544}
int l
Definition cfEzgcd.cc:100
static BOOLEAN length(leftv result, leftv arg)
Definition interval.cc:257
static int index(p_Length length, p_Ord ord)

◆ getShorter()

static int getShorter ( int a,
int  l,
int  j,
int  cols,
int  rows 
)
static

Definition at line 546 of file bigintmat.cc.

547{
548 int sndlong = 0;
549 int min;
550 for (int i=0; i<rows; i++)
551 {
552 int index = cols*i+j;
553 if ((a[index] > sndlong) && (a[index] < l))
554 {
555 min = floor(log10((double)cols))+floor(log10((double)rows))+5;
556 if ((a[index] < min) && (min < l))
557 sndlong = min;
558 else
559 sndlong = a[index];
560 }
561 }
562 if (sndlong == 0)
563 {
564 min = floor(log10((double)cols))+floor(log10((double)rows))+5;
565 if (min < l)
566 sndlong = min;
567 else
568 sndlong = 1;
569 }
570 return sndlong;
571}
static int min(int a, int b)
Definition fast_mult.cc:268
const signed long floor(const ampf< Precision > &x)
Definition amp.h:773
const ampf< Precision > log10(const ampf< Precision > &x)
Definition amp.h:1022

◆ intArrSum()

static int intArrSum ( int a,
int  length 
)
static

Definition at line 523 of file bigintmat.cc.

524{
525 int sum = 0;
526 for (int i=0; i<length; i++)
527 sum += a[i];
528 return sum;
529}

◆ iv2bim()

bigintmat * iv2bim ( intvec b,
const coeffs  C 
)

Definition at line 347 of file bigintmat.cc.

348{
349 const int l = (b->rows())*(b->cols());
350 bigintmat * bim = new bigintmat(b->rows(), b->cols(), C);
351
352 for (int i=0; i < l; i++)
353 bim->rawset(i, n_Init((*b)[i], C), C);
354
355 return bim;
356}

◆ kernbase()

int kernbase ( bigintmat a,
bigintmat c,
number  p,
coeffs  q 
)

a basis for the nullspace of a mod p: only used internally in Round2. Don't use it.

Definition at line 2584 of file bigintmat.cc.

2585{
2586#if 0
2587 PrintS("Kernel of ");
2588 a->Print();
2589 PrintS(" modulo ");
2590 n_Print(p, q);
2591 PrintLn();
2592#endif
2593
2594 coeffs coe = numbercoeffs(p, q);
2595 bigintmat *m = bimChangeCoeff(a, coe), *U, *V;
2596 diagonalForm(m, &U, &V);
2597#if 0
2598 PrintS("\ndiag form: ");
2599 m->Print();
2600 PrintS("\nU:\n");
2601 U->Print();
2602 PrintS("\nV:\n");
2603 V->Print();
2604 PrintLn();
2605#endif
2606
2607 int rg = 0;
2608#undef MIN
2609#define MIN(a,b) (a < b ? a : b)
2610 for(rg=0; rg<MIN(m->rows(), m->cols()) && !n_IsZero(m->view(m->rows()-rg,m->cols()-rg), coe); rg++);
2611
2612 bigintmat * k = new bigintmat(m->cols(), m->rows(), coe);
2613 for(int i=0; i<rg; i++)
2614 {
2615 number A = n_Ann(m->view(m->rows()-i, m->cols()-i), coe);
2616 k->set(m->cols()-i, i+1, A);
2617 n_Delete(&A, coe);
2618 }
2619 for(int i=rg; i<m->cols(); i++)
2620 {
2621 k->set(m->cols()-i, i+1-rg, n_Init(1, coe));
2622 }
2623 bimMult(V, k, k);
2624 c->copy(bimChangeCoeff(k, q));
2625 return c->cols();
2626}
bigintmat * bimChangeCoeff(bigintmat *a, coeffs cnew)
Liefert Kopier von Matrix a zurück, mit coeffs cnew statt den ursprünglichen.
#define MIN(a, b)
void diagonalForm(bigintmat *A, bigintmat **S, bigintmat **T)
static coeffs numbercoeffs(number n, coeffs c)
create Z/nA of type n_Zn
Definition bigintmat.cc:20
int m
Definition cfEzgcd.cc:128
int p
Definition cfModGcd.cc:4086
static FORCE_INLINE number n_Ann(number a, const coeffs r)
if r is a ring with zero divisors, return an annihilator!=0 of b otherwise return NULL
Definition coeffs.h:680

◆ nCoeffs_are_equal()

bool nCoeffs_are_equal ( coeffs  r,
coeffs  s 
)

Definition at line 2628 of file bigintmat.cc.

2629{
2630 if ((r == NULL) || (s == NULL))
2631 return false;
2632 if (r == s)
2633 return true;
2634 if ((getCoeffType(r)==n_Z) && (getCoeffType(s)==n_Z))
2635 return true;
2636 if ((getCoeffType(r)==n_Zp) && (getCoeffType(s)==n_Zp))
2637 {
2638 if (r->ch == s->ch)
2639 return true;
2640 else
2641 return false;
2642 }
2643 // n_Zn stimmt wahrscheinlich noch nicht
2644 if ((getCoeffType(r)==n_Zn) && (getCoeffType(s)==n_Zn))
2645 {
2646 if (r->ch == s->ch)
2647 return true;
2648 else
2649 return false;
2650 }
2651 if ((getCoeffType(r)==n_Q) && (getCoeffType(s)==n_Q))
2652 return true;
2653 // FALL n_Zn FEHLT NOCH!
2654 //if ((getCoeffType(r)==n_Zn) && (getCoeffType(s)==n_Zn))
2655 return false;
2656}
@ n_Zn
only used if HAVE_RINGS is defined
Definition coeffs.h:44
@ n_Zp
\F{p < 2^31}
Definition coeffs.h:29
@ n_Z
only used if HAVE_RINGS is defined
Definition coeffs.h:43
static FORCE_INLINE n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
Definition coeffs.h:429

◆ numbercoeffs()

static coeffs numbercoeffs ( number  n,
coeffs  c 
)
static

create Z/nA of type n_Zn

Definition at line 20 of file bigintmat.cc.

21{
22 mpz_t p;
23 number2mpz(n, c, p);
24 ZnmInfo *pp = new ZnmInfo;
25 pp->base = p;
26 pp->exp = 1;
27 coeffs nc = nInitChar(n_Zn, (void*)pp);
28 mpz_clear(p);
29 delete pp;
30 return nc;
31}
CanonicalForm FACTORY_PUBLIC pp(const CanonicalForm &)
CanonicalForm pp ( const CanonicalForm & f )
Definition cf_gcd.cc:676
static FORCE_INLINE void number2mpz(number n, coeffs c, mpz_t m)
Definition coeffs.h:980

◆ operator!=()

bool operator!= ( const bigintmat lhr,
const bigintmat rhr 
)

Definition at line 174 of file bigintmat.cc.

175{
176 return !(lhr==rhr);
177}

◆ operator==()

bool operator== ( const bigintmat lhr,
const bigintmat rhr 
)

Definition at line 157 of file bigintmat.cc.

158{
159 if (&lhr == &rhr) { return true; }
160 if (lhr.cols() != rhr.cols()) { return false; }
161 if (lhr.rows() != rhr.rows()) { return false; }
162 if (lhr.basecoeffs() != rhr.basecoeffs()) { return false; }
163
164 const int l = (lhr.rows())*(lhr.cols());
165
166 for (int i=0; i < l; i++)
167 {
168 if (!n_Equal(lhr[i], rhr[i], lhr.basecoeffs())) { return false; }
169 }
170
171 return true;
172}
static FORCE_INLINE BOOLEAN n_Equal(number a, number b, const coeffs r)
TRUE iff 'a' and 'b' represent the same number; they may have different representations.
Definition coeffs.h:464

◆ prependIdentity()

static bigintmat * prependIdentity ( bigintmat A)
static

Definition at line 2025 of file bigintmat.cc.

2026{
2027 coeffs R = A->basecoeffs();
2028 bigintmat *m = new bigintmat(A->rows()+A->cols(), A->cols(), R);
2029 m->copySubmatInto(A, 1, 1, A->rows(), A->cols(), A->cols()+1, 1);
2030 number one = n_Init(1, R);
2031 for(int i=1; i<= A->cols(); i++)
2032 m->set(i,i,one);
2033 n_Delete(&one, R);
2034 return m;
2035}

◆ reduce_mod_howell()

static void reduce_mod_howell ( bigintmat A,
bigintmat b,
bigintmat eps,
bigintmat x 
)
static

Definition at line 1939 of file bigintmat.cc.

1940{
1941 //write b = Ax + eps where eps is "small" in the sense of bounded by the
1942 //pivot entries in H. H does not need to be Howell (or HNF) but need
1943 //to be triagonal in the same direction.
1944 //b can have multiple columns.
1945#if 0
1946 PrintS("reduce_mod_howell: A:\n");
1947 A->Print();
1948 PrintS("\nb:\n");
1949 b->Print();
1950#endif
1951
1952 coeffs R = A->basecoeffs();
1953 assume(x->basecoeffs() == R);
1954 assume(b->basecoeffs() == R);
1955 assume(eps->basecoeffs() == R);
1956 if (!A->cols())
1957 {
1958 x->zero();
1959 eps->copy(b);
1960
1961#if 0
1962 PrintS("\nx:\n");
1963 x->Print();
1964 PrintS("\neps:\n");
1965 eps->Print();
1966 PrintS("\n****************************************\n");
1967#endif
1968 return;
1969 }
1970
1971 bigintmat * B = new bigintmat(b->rows(), 1, R);
1972 for(int i=1; i<= b->cols(); i++)
1973 {
1974 int A_col = A->cols();
1975 b->getcol(i, B);
1976 for(int j = B->rows(); j>0; j--)
1977 {
1978 number Ai = A->view(A->rows() - B->rows() + j, A_col);
1979 if (n_IsZero(Ai, R) &&
1980 n_IsZero(B->view(j, 1), R))
1981 {
1982 continue; //all is fine: 0*x = 0
1983 }
1984 else if (n_IsZero(B->view(j, 1), R))
1985 {
1986 x->rawset(x->rows() - B->rows() + j, i, n_Init(0, R));
1987 A_col--;
1988 }
1989 else if (n_IsZero(Ai, R))
1990 {
1991 A_col--;
1992 }
1993 else
1994 {
1995 // "solve" ax=b, possibly enlarging d
1996 number Bj = B->view(j, 1);
1997 number q = n_Div(Bj, Ai, R);
1998 x->rawset(x->rows() - B->rows() + j, i, q);
1999 for(int k=j; k>B->rows() - A->rows(); k--)
2000 {
2001 //B[k] = B[k] - x[k]A[k][j]
2002 number s = n_Mult(q, A->view(A->rows() - B->rows() + k, A_col), R);
2003 B->rawset(k, 1, n_Sub(B->view(k, 1), s, R));
2004 n_Delete(&s, R);
2005 }
2006 A_col--;
2007 }
2008 if (!A_col)
2009 {
2010 break;
2011 }
2012 }
2013 eps->setcol(i, B);
2014 }
2015 delete B;
2016#if 0
2017 PrintS("\nx:\n");
2018 x->Print();
2019 PrintS("\neps:\n");
2020 eps->Print();
2021 PrintS("\n****************************************\n");
2022#endif
2023}
static FORCE_INLINE number n_Div(number a, number b, const coeffs r)
return the quotient of 'a' and 'b', i.e., a/b; raises an error if 'b' is not invertible in r exceptio...
Definition coeffs.h:616
b *CanonicalForm B
Definition facBivar.cc:52

◆ solveAx()

number solveAx ( bigintmat A,
bigintmat b,
bigintmat x 
)

solve Ax=b*d. x needs to be pre-allocated to the same number of columns as b. the minimal denominator d is returned. Currently available for Z, Q and Z/nZ (and possibly for all fields: d=1 there) Beware that the internal functions can find the kernel as well - but the interface is lacking.

Definition at line 2417 of file bigintmat.cc.

2418{
2419#if 0
2420 PrintS("Solve Ax=b for A=\n");
2421 A->Print();
2422 PrintS("\nb = \n");
2423 b->Print();
2424 PrintS("\nx = \n");
2425 x->Print();
2426 PrintLn();
2427#endif
2428
2429 coeffs R = A->basecoeffs();
2430 assume (R == b->basecoeffs());
2431 assume (R == x->basecoeffs());
2432 assume ((x->cols() == b->cols()) && (x->rows() == A->cols()) && (A->rows() == b->rows()));
2433
2434 switch (getCoeffType(R))
2435 {
2436 case n_Z:
2437 return solveAx_dixon(A, b, x, NULL);
2438 case n_Zn:
2439 case n_Znm:
2440 case n_Z2m:
2441 return solveAx_howell(A, b, x, NULL);
2442 case n_Zp:
2443 case n_Q:
2444 case n_GF:
2445 case n_algExt:
2446 case n_transExt:
2447 WarnS("have field, should use Gauss or better");
2448 break;
2449 default:
2450 if (R->cfXExtGcd && R->cfAnn)
2451 { //assume it's Euclidean
2452 return solveAx_howell(A, b, x, NULL);
2453 }
2454 WerrorS("have no solve algorithm");
2455 break;
2456 }
2457 return NULL;
2458}
static number solveAx_dixon(bigintmat *A, bigintmat *B, bigintmat *x, bigintmat *kern)
static number solveAx_howell(bigintmat *A, bigintmat *b, bigintmat *x, bigintmat *kern)
@ n_GF
\GF{p^n < 2^16}
Definition coeffs.h:32
@ n_Znm
only used if HAVE_RINGS is defined
Definition coeffs.h:45
@ n_algExt
used for all algebraic extensions, i.e., the top-most extension in an extension tower is algebraic
Definition coeffs.h:35
@ n_Z2m
only used if HAVE_RINGS is defined
Definition coeffs.h:46
@ n_transExt
used for all transcendental extensions, i.e., the top-most extension in an extension tower is transce...
Definition coeffs.h:38
#define WarnS
Definition emacs.cc:78

◆ solveAx_dixon()

static number solveAx_dixon ( bigintmat A,
bigintmat B,
bigintmat x,
bigintmat kern 
)
static

Definition at line 2096 of file bigintmat.cc.

2096 {
2097 coeffs R = A->basecoeffs();
2098
2099 assume(getCoeffType(R) == n_Z);
2100
2101 number p = n_Init(536870909, R); // PreviousPrime(2^29); not clever
2102 coeffs Rp = numbercoeffs(p, R); // R/pR
2104 *m = prependIdentity(Ap),
2105 *Tp, *Hp;
2106 delete Ap;
2107
2108 m->howell();
2109 Hp = new bigintmat(A->rows(), A->cols(), Rp);
2110 Hp->copySubmatInto(m, A->cols()+1, 1, A->rows(), A->cols(), 1, 1);
2111 Tp = new bigintmat(A->cols(), A->cols(), Rp);
2112 Tp->copySubmatInto(m, 1, 1, A->cols(), A->cols(), 1, 1);
2113
2114 int i, j;
2115
2116 for(i=1; i<= A->cols(); i++)
2117 {
2118 for(j=m->rows(); j>A->cols(); j--)
2119 {
2120 if (!n_IsZero(m->view(j, i), Rp)) break;
2121 }
2122 if (j>A->cols()) break;
2123 }
2124// Print("Found nullity (kern dim) of %d\n", i-1);
2125 bigintmat * kp = new bigintmat(A->cols(), i-1, Rp);
2126 kp->copySubmatInto(Tp, 1, 1, A->cols(), i-1, 1, 1);
2127 kp->howell();
2128
2129 delete m;
2130
2131 //Hp is the mod-p howell form
2132 //Tp the transformation, mod p
2133 //kp a basis for the kernel, in howell form, mod p
2134
2135 bigintmat * eps_p = new bigintmat(B->rows(), B->cols(), Rp),
2136 * x_p = new bigintmat(A->cols(), B->cols(), Rp),
2137 * fps_p = new bigintmat(kp->cols(), B->cols(), Rp);
2138
2139 //initial solution
2140
2141 number zero = n_Init(0, R);
2142 x->skalmult(zero, R);
2143 n_Delete(&zero, R);
2144
2145 bigintmat * b = new bigintmat(B);
2146 number pp = n_Init(1, R);
2147 i = 1;
2148 do
2149 {
2150 bigintmat * b_p = bimChangeCoeff(b, Rp), * s;
2151 bigintmat * t1, *t2;
2153 delete b_p;
2154 if (!eps_p->isZero())
2155 {
2156 PrintS("no solution, since no modular solution\n");
2157
2158 delete eps_p;
2159 delete x_p;
2160 delete Hp;
2161 delete kp;
2162 delete Tp;
2163 delete b;
2164 n_Delete(&pp, R);
2165 n_Delete(&p, R);
2166 nKillChar(Rp);
2167
2168 return NULL;
2169 }
2170 t1 = bimMult(Tp, x_p);
2171 delete x_p;
2172 x_p = t1;
2173 reduce_mod_howell(kp, x_p, x_p, fps_p); //we're not all interested in fps_p
2174 s = bimChangeCoeff(x_p, R);
2175 t1 = bimMult(A, s);
2176 t2 = bimSub(b, t1);
2177 t2->skaldiv(p);
2178 delete b;
2179 delete t1;
2180 b = t2;
2181 s->skalmult(pp, R);
2182 t1 = bimAdd(x, s);
2183 delete s;
2184 x->swapMatrix(t1);
2185 delete t1;
2186
2187 if(kern && i==1)
2188 {
2190 t1 = bimMult(A, ker);
2191 t1->skaldiv(p);
2192 t1->skalmult(n_Init(-1, R), R);
2193 b->appendCol(t1);
2194 delete t1;
2195 x->appendCol(ker);
2196 delete ker;
2197 x_p->extendCols(kp->cols());
2198 eps_p->extendCols(kp->cols());
2199 fps_p->extendCols(kp->cols());
2200 }
2201
2202 n_InpMult(pp, p, R);
2203
2204 if (b->isZero())
2205 {
2206 //exact solution found, stop
2207 delete eps_p;
2208 delete fps_p;
2209 delete x_p;
2210 delete Hp;
2211 delete kp;
2212 delete Tp;
2213 delete b;
2214 n_Delete(&pp, R);
2215 n_Delete(&p, R);
2216 nKillChar(Rp);
2217
2218 return n_Init(1, R);
2219 }
2220 else
2221 {
2222 bigintmat *y = new bigintmat(x->rows(), x->cols(), R);
2223 number d = bimFarey(x, pp, y);
2224 if (d)
2225 {
2226 bigintmat *c = bimMult(A, y);
2227 bigintmat *bd = new bigintmat(B);
2228 bd->skalmult(d, R);
2229 if (kern)
2230 {
2231 bd->extendCols(kp->cols());
2232 }
2233 if (*c == *bd)
2234 {
2235 x->swapMatrix(y);
2236 delete y;
2237 delete c;
2238 if (kern)
2239 {
2240 y = new bigintmat(x->rows(), B->cols(), R);
2241 c = new bigintmat(x->rows(), kp->cols(), R);
2242 x->splitcol(y, c);
2243 x->swapMatrix(y);
2244 delete y;
2245 kern->swapMatrix(c);
2246 delete c;
2247 }
2248
2249 delete bd;
2250
2251 delete eps_p;
2252 delete fps_p;
2253 delete x_p;
2254 delete Hp;
2255 delete kp;
2256 delete Tp;
2257 delete b;
2258 n_Delete(&pp, R);
2259 n_Delete(&p, R);
2260 nKillChar(Rp);
2261
2262 return d;
2263 }
2264 delete c;
2265 delete bd;
2266 n_Delete(&d, R);
2267 }
2268 delete y;
2269 }
2270 i++;
2271 } while (1);
2272 delete eps_p;
2273 delete fps_p;
2274 delete x_p;
2275 delete Hp;
2276 delete kp;
2277 delete Tp;
2278 n_Delete(&pp, R);
2279 n_Delete(&p, R);
2280 nKillChar(Rp);
2281 return NULL;
2282}
static void reduce_mod_howell(bigintmat *A, bigintmat *b, bigintmat *eps, bigintmat *x)
static bigintmat * prependIdentity(bigintmat *A)
bigintmat * bimSub(bigintmat *a, bigintmat *b)
Definition bigintmat.cc:216
static number bimFarey(bigintmat *A, number N, bigintmat *L)
bigintmat * bimAdd(bigintmat *a, bigintmat *b)
Matrix-Add/-Sub/-Mult so oder mit operator+/-/* ? @Note: NULL as a result means an error (non-compati...
Definition bigintmat.cc:180
CF_NO_INLINE bool isZero() const
void skaldiv(number b)
Macht Ganzzahldivision aller Matrixeinträge mit b.
const CanonicalForm int const CFList const Variable & y
Definition facAbsFact.cc:53

◆ solveAx_howell()

static number solveAx_howell ( bigintmat A,
bigintmat b,
bigintmat x,
bigintmat kern 
)
static

Definition at line 2285 of file bigintmat.cc.

2286{
2287 // try to solve Ax=b, more precisely, find
2288 // number d
2289 // bigintmat x
2290 // sth. Ax=db
2291 // where d is small-ish (divides the determinant of A if this makes sense)
2292 // return 0 if there is no solution.
2293 //
2294 // if kern is non-NULL, return a basis for the kernel
2295
2296 //Algo: we do row-howell (triangular matrix). The idea is
2297 // Ax = b <=> AT T^-1x = b
2298 // y := T^-1 x, solve AT y = b
2299 // and return Ty.
2300 //Howell does not compute the trafo, hence we need to cheat:
2301 //B := (I_n | A^t)^t, then the top part of the Howell form of
2302 //B will give a useful trafo
2303 //Then we can find x by back-substitution and lcm/gcd to find the denominator
2304 //The defining property of Howell makes this work.
2305
2306 coeffs R = A->basecoeffs();
2308 m->howell(); // since m contains the identity, we'll have A->cols()
2309 // many cols.
2310 number den = n_Init(1, R);
2311
2312 bigintmat * B = new bigintmat(A->rows(), 1, R);
2313 for(int i=1; i<= b->cols(); i++)
2314 {
2315 int A_col = A->cols();
2316 b->getcol(i, B);
2317 B->skalmult(den, R);
2318 for(int j = B->rows(); j>0; j--)
2319 {
2320 number Ai = m->view(m->rows()-B->rows() + j, A_col);
2321 if (n_IsZero(Ai, R) &&
2322 n_IsZero(B->view(j, 1), R))
2323 {
2324 continue; //all is fine: 0*x = 0
2325 }
2326 else if (n_IsZero(B->view(j, 1), R))
2327 {
2328 x->rawset(x->rows() - B->rows() + j, i, n_Init(0, R));
2329 A_col--;
2330 }
2331 else if (n_IsZero(Ai, R))
2332 {
2333 delete m;
2334 delete B;
2335 n_Delete(&den, R);
2336 return 0;
2337 }
2338 else
2339 {
2340 // solve ax=db, possibly enlarging d
2341 // so x = db/a
2342 number Bj = B->view(j, 1);
2343 number g = n_Gcd(Bj, Ai, R);
2344 number xi;
2345 if (n_Equal(Ai, g, R))
2346 { //good: den stable!
2347 xi = n_Div(Bj, Ai, R);
2348 }
2349 else
2350 { //den <- den * (a/g), so old sol. needs to be adjusted
2351 number inc_d = n_Div(Ai, g, R);
2352 n_InpMult(den, inc_d, R);
2353 x->skalmult(inc_d, R);
2354 B->skalmult(inc_d, R);
2355 xi = n_Div(Bj, g, R);
2356 n_Delete(&inc_d, R);
2357 } //now for the back-substitution:
2358 x->rawset(x->rows() - B->rows() + j, i, xi);
2359 for(int k=j; k>0; k--)
2360 {
2361 //B[k] = B[k] - x[k]A[k][j]
2362 number s = n_Mult(xi, m->view(m->rows()-B->rows() + k, A_col), R);
2363 B->rawset(k, 1, n_Sub(B->view(k, 1), s, R));
2364 n_Delete(&s, R);
2365 }
2366 n_Delete(&g, R);
2367 A_col--;
2368 }
2369 if (!A_col)
2370 {
2371 if (B->isZero()) break;
2372 else
2373 {
2374 delete m;
2375 delete B;
2376 n_Delete(&den, R);
2377 return 0;
2378 }
2379 }
2380 }
2381 }
2382 delete B;
2383 bigintmat *T = new bigintmat(A->cols(), A->cols(), R);
2384 T->copySubmatInto(m, 1, 1, A->cols(), A->cols(), 1, 1);
2385 if (kern)
2386 {
2387 int i, j;
2388 for(i=1; i<= A->cols(); i++)
2389 {
2390 for(j=m->rows(); j>A->cols(); j--)
2391 {
2392 if (!n_IsZero(m->view(j, i), R)) break;
2393 }
2394 if (j>A->cols()) break;
2395 }
2396 Print("Found nullity (kern dim) of %d\n", i-1);
2397 bigintmat * ker = new bigintmat(A->rows(), i-1, R);
2398 ker->copySubmatInto(T, 1, 1, A->rows(), i-1, 1, 1);
2399 kern->swapMatrix(ker);
2400 delete ker;
2401 }
2402 delete m;
2403 bigintmat * y = bimMult(T, x);
2404 x->swapMatrix(y);
2405 delete y;
2406 x->simplifyContentDen(&den);
2407#if 0
2408 PrintS("sol = 1/");
2409 n_Print(den, R);
2410 PrintS(" *\n");
2411 x->Print();
2412 PrintLn();
2413#endif
2414 return den;
2415}
g
Definition cfModGcd.cc:4098
static FORCE_INLINE number n_Gcd(number a, number b, const coeffs r)
in Z: return the gcd of 'a' and 'b' in Z/nZ, Z/2^kZ: computed as in the case Z in Z/pZ,...
Definition coeffs.h:665