MeatAxe  2.4
src/meataxe.h
00001 /* ============================= C MeatAxe ==================================
00002    File:        $Id: meataxe.h,v 1.3 2007-09-03 21:35:22 mringe Exp $
00003    Comment:     MeatAxe library external interfaces.
00004    --------------------------------------------------------------------------
00005    (C) Copyright 2004 Michael Ringe, Lehrstuhl D fuer Mathematik,
00006    RWTH Aachen, Germany  <mringe@math.rwth-aachen.de>
00007    This program is free software; see the file COPYING for details.
00008    ========================================================================== */
00009 
00010 #ifndef MEATAXE_H_INCLUDED
00011 #define MEATAXE_H_INCLUDED
00012 
00013 #include <stdio.h>
00014 #include <stdarg.h>
00015 
00016 #define MTX_VERSION "2.4"
00017 
00018 #if defined GCC
00019 #define MTX_PRINTF_ATTRIBUTE(f,v) __attribute__((format(printf,f,v)))
00020 #else
00021 #define MTX_PRINTF_ATTRIBUTE(f,v) 
00022 #endif
00023 
00024 extern char *MtxVersion;        
00030 #define FM_READ     0x01        
00031 #define FM_CREATE   0x02        
00032 #define FM_APPEND   0x03        
00033 #define FM_TEXT     0x10
00034 #define FM_LIB      0x20
00035 #define FM_NOERROR  0x40
00036 
00040 int SysCreateDirectory(const char *name);
00041 int SysGetPid();
00042 void SysInit(void);
00043 void *SysMalloc(size_t nbytes);
00044 FILE *SysFopen(const char *name, int mode);
00045 void SysFree(void *x);
00046 int SysFseek(FILE *f,long pos);
00047 int SysFseekRelative(FILE *file, long distance);
00048 void *SysRealloc(void *buf, size_t nbytes);
00049 int SysReadLong32(FILE *f, long *buf, int n);
00050 #define SysReadLong SysReadLong32
00051 int SysReadLongX(FILE *f, long *buf, int n);
00052 int SysRemoveDirectory(const char *name);
00053 int SysRemoveFile(const char *name);
00054 void SysSetTimeLimit(long nsecs);
00055 long SysTimeUsed(void);
00056 int SysWriteLong32(FILE *f, const long *buf, int n);
00057 #define SysWriteLong SysWriteLong32
00058 int SysWriteLongX(FILE *f, const long *buf, int n);
00059 
00060 #define ALLOC(type) ((type *) SysMalloc(sizeof(type)))
00061 #define NALLOC(type,n) ((type *) SysMalloc((size_t)(n) * sizeof(type)))
00062 #define NREALLOC(x,type,n)\
00063         ((type *) SysRealloc(x,(size_t)(n) * sizeof(type)))
00064 #define FREE(x) SysFree(x)
00065 
00070 /* ---------------------------------------------------------------------------------------------- */
00071 
00077 /* Data types and constants
00078    ------------------------ */
00079 
00080 #if ZZZ==0
00081 
00082 typedef unsigned char FEL;              
00083 typedef FEL *PTR;                       
00084 #define FF_ZERO ((FEL)0)                
00085 #define FF_ONE ((FEL)1)                 
00086 #define ZZZVERSION 6
00087 
00088 #elif ZZZ==1
00089 
00090 typedef unsigned short FEL;
00091 typedef unsigned short *PTR;
00092 #define FF_ZERO ((FEL)0xFFFF)
00093 #define FF_ONE ((FEL)0)
00094 #define ZZZVERSION 0x101
00095 
00096 #else
00097 
00098 #error "ZZZ undefined"
00099 
00100 #endif
00101 
00102 /* Kernel variables and functions
00103    ------------------------------ */
00104 
00105 extern int FfOrder;             
00106 extern int FfChar;              
00107 extern FEL FfGen;               
00108 extern int FfNoc;               
00109 extern size_t FfCurrentRowSize;
00110 
00111 
00112 /* Arithmetic */
00113 FEL FfAdd(FEL a, FEL b);
00114 FEL FfSub(FEL a, FEL b);
00115 FEL FfMul(FEL a, FEL b);
00116 FEL FfDiv(FEL a, FEL b);
00117 FEL FfNeg(FEL a);
00118 FEL FfInv(FEL a);
00119 
00120 int FfMakeTables(int field);
00121 int FfSetField(int field);
00122 int FfSetNoc(int noc);
00123 
00124 
00125 void FfAddMulRow(PTR dest, PTR src, FEL f);
00126 PTR FfAddRow(PTR dest, PTR src);
00127 PTR FfAddRowPartial(PTR dest, PTR src, int first, int len);
00128 PTR FfAlloc(int nor);
00129 int FfCmpRows(PTR p1, PTR p2);
00130 void FfCleanRow(PTR row, PTR matrix, int nor, const int *piv);
00131 void FfCleanRow2(PTR row, PTR matrix, int nor, const int *piv, PTR row2);
00132 void FfCleanRowAndRepeat(PTR row, PTR mat, int nor, const int *piv, 
00133     PTR row2, PTR mat2);
00134 void FfCopyRow(PTR dest, PTR src);
00135 FEL FfEmbed(FEL a, int subfield);
00136 FEL FfExtract(PTR row, int col);
00137 void FfExtractColumn(PTR mat,int nor,int col,PTR result);
00138 int FfFindPivot(PTR row, FEL *mark);
00139 void FfFree(PTR x);
00140 FEL FfFromInt(int l);
00141 PTR FfGetPtr(PTR base, int row);
00142 void FfInsert(PTR row, int col, FEL mark);
00143 void FfMulRow(PTR row, FEL mark);
00144 FILE *FfReadHeader(const char *name, int *fld, int *nor, int *noc);
00145 int FfReadRows(FILE *f, PTR buf, int n);
00146 FEL FfRestrict(FEL a, int subfield);
00147 size_t FfRowSize(int noc);
00148 FEL FfScalarProduct(PTR a, PTR b);
00149 int FfSeekRow(FILE *f, int pos);
00150 int FfStepPtr(PTR *x);
00151 void FfSwapRows(PTR dest, PTR src);
00152 const char *FfToGap(FEL f);
00153 int FfToInt(FEL f);
00154 size_t FfTrueRowSize(int noc);
00155 FILE *FfWriteHeader(const char *name, int fld, int nor, int noc);
00156 int FfWriteRows(FILE *f, PTR buf, int n);
00157 
00158 
00159 /* --------------------------------------------------------------------------
00160    Macro versions of kernel functions
00161    -------------------------------------------------------------------------- */
00162 
00163 
00164 #if ZZZ==0
00165 
00166 extern FEL mtx_tmult[256][256];
00167 extern FEL mtx_tadd[256][256];
00168 extern FEL mtx_taddinv[256], mtx_tmultinv[256];
00169 extern FEL mtx_tffirst[256][2];
00170 extern FEL mtx_textract[8][256];
00171 extern FEL mtx_tnull[8][256];
00172 extern FEL mtx_tinsert[8][256];
00173 extern long mtx_embedord[4];
00174 extern FEL mtx_embed[4][16];
00175 extern FEL mtx_restrict[4][256];
00176 
00177 #define FfAdd(a,b) ((FEL)mtx_tadd[(int)(unsigned char)a][(int)(unsigned char)b])
00178 #define FfDiv(a,b) FfMul((a),FfInv(b))
00179 #define FfInv(a) (mtx_tmultinv[(int)(unsigned char)a])
00180 #define FfMul(a,b) ((FEL)mtx_tmult[(int)(unsigned char)a][(int)(unsigned char)b])
00181 #define FfNeg(a) (mtx_taddinv[(int)(unsigned char)a])
00182 #define FfSub(a,b) FfAdd((a),FfNeg(b))
00183 
00184 #elif ZZZ==1
00185 
00186 #define FfExtract(row,col) ((FEL)((row)[(col)-1]))
00187 #define FfInsert(row,col,mark) ((void)((row)[(col)-1] = mark))
00188 
00189 
00190 #endif
00191 
00194 /* ------------------------------------------------------------------
00195    Other low-level functions (zzz2.c)
00196    ------------------------------------------------------------------ */
00197 
00198 void FfMapRow(PTR row, PTR matrix, int nor, PTR result);
00199 void FfPermRow(PTR row, const long *perm, PTR result);
00200 int FfSumAndIntersection(PTR wrk1, int *nor1, int *nor2, PTR wrk2, int *piv);
00201 
00202 
00203 /* ------------------------------------------------------------------
00204    Library initialization and cleanup
00205    ------------------------------------------------------------------ */
00206 
00207 extern int Mtx_IsInitialized;
00208 extern int Mtx_IsX86;
00209 extern int MtxOpt_UseOldWordGenerator;
00210 
00211 int MtxInitLibrary();
00212 void MtxCleanupLibrary();
00213 
00214 
00228 typedef struct {
00229     char *S;    /* pointer to NUL terminated string */
00230 } String;
00231 
00232 String StrAlloc(size_t initial_capacity);
00233 void StrFree(String *s);
00234 void StrAppend(String *s, const char *text);
00235 MTX_PRINTF_ATTRIBUTE(2,3)
00236 void StrAppendF(String *s, const char *fmt, ...);
00237 MTX_PRINTF_ATTRIBUTE(2,3)
00238 void StrPrintF(String *s, const char *fmt, ...);
00239 
00245 /* ------------------------------------------------------------------
00246    Application framework
00247    ------------------------------------------------------------------ */
00248 
00249 extern char MtxBinDir[];        /* MeatAxe library directory */
00250 extern char MtxLibDir[];        /* MeatAxe program directory */
00251 
00252 #define APP_MAX_ARGS 50
00253 
00275 typedef struct
00276 {
00277     const char *Name;           
00278     const char *Description;    
00279     const char *Help;           
00280 } MtxApplicationInfo_t;
00281 
00282 
00283 
00284 
00291 typedef struct
00292 {
00293     MtxApplicationInfo_t const *AppInfo;        
00294     int OrigArgC;                               
00295     const char **OrigArgV;                      
00296     int ArgC;                                   
00297     const char **ArgV;                          
00298     int OptEnd;                                 
00299     unsigned long IsDone[APP_MAX_ARGS];         
00300     const char *OptArg;                         
00301     int OptInd;                                 
00302     char TempDirName[200];                      
00303 } MtxApplication_t;
00304 
00305 
00306 MtxApplication_t *AppAlloc(MtxApplicationInfo_t const *ai, int argc, const char **argv);
00307 int AppFree(MtxApplication_t *a);
00308 int AppGetOption(MtxApplication_t *app, const char *spec);
00309 int AppGetCountedOption(MtxApplication_t *app, const char *spec);
00310 const char *AppGetTextOption(MtxApplication_t *app, const char *spec, 
00311     const char *dflt);
00312 int AppGetIntOption(MtxApplication_t *app, const char *spec, int dflt, 
00313     int min, int max);
00314 int AppGetArguments(MtxApplication_t *app, int min_argc, int max_argc);
00315 const char *AppCreateTempDir(MtxApplication_t *app);
00316 
00317 #define MTX_COMMON_OPTIONS_SYNTAX \
00318     "[<Options>]"
00319 
00320 #define MTX_COMMON_OPTIONS_DESCRIPTION \
00321     "    -Q ...................... Quiet, no messages\n" \
00322     "    -V ...................... Verbose, more messages\n" \
00323     "    -T <MaxTime> ............ Set CPU time limit [s]\n"
00324 
00325 
00326 /* ------------------------------------------------------------------
00327    Messages and error handling
00328    ------------------------------------------------------------------ */
00329                                 /* Error message codes (%E arguments) */
00330 #define MTX_ERR_NOMEM       1   /* Not enough memory */
00331 #define MTX_ERR_GAME_OVER   2   /* Time limit exceeded */
00332 #define MTX_ERR_DIV0        8   /* Division by 0 or singular Matrix */
00333 #define MTX_ERR_FILEFMT     24  /* Bad format */
00334 #define MTX_ERR_BADARG      31  /* Bad argument */
00335 #define MTX_ERR_RANGE       33  /* Out of range */
00336 #define MTX_ERR_NOTECH      34  /* Matrix not in chelon form */
00337 #define MTX_ERR_NOTSQUARE   35  /* Matrix not square */
00338 #define MTX_ERR_INCOMPAT    36  /* Arguments are incompatible */
00339 #define MTX_ERR_BADUSAGE    41  /* Bad command line */
00340 #define MTX_ERR_OPTION      42  /* Bad usage of option */
00341 #define MTX_ERR_NARGS       43  /* Bad number of arguments */
00342 #define MTX_ERR_NOTMATRIX   51  /* Not a matrix */
00343 #define MTX_ERR_NOTPERM     53  /* Not a permutation */
00344 
00345 
00346 
00350 typedef struct { const char *Name; const char *BaseName; } MtxFileInfo_t;
00351 
00355 typedef struct { const MtxFileInfo_t *FileInfo; int LineNo; const char *Text; } 
00356     MtxErrorRecord_t;
00357 
00358 typedef void MtxErrorHandler_t(const MtxErrorRecord_t *);
00359 
00360 #define MTX_DEFINE_FILE_INFO \
00361     static MtxFileInfo_t Mtx_ThisFile = { __FILE__, NULL };
00362 
00363 int MtxError(MtxFileInfo_t *fi, int line, const char *text, ...);
00364 MtxErrorHandler_t *MtxSetErrorHandler(MtxErrorHandler_t *h);
00365 
00366 
00367 #define MTX_ERROR(msg) MtxError(&Mtx_ThisFile,__LINE__,msg)
00368 #define MTX_ERROR1(msg,a1) MtxError(&Mtx_ThisFile,__LINE__,msg,a1)
00369 #define MTX_ERROR2(msg,a1,a2) MtxError(&Mtx_ThisFile,__LINE__,msg,a1,a2)
00370 #define MTX_ERROR3(msg,a1,a2,a3) MtxError(&Mtx_ThisFile,__LINE__,msg,a1,a2,a3)
00371 #define MTX_ERROR4(msg,a1,a2,a3,a4) MtxError(&Mtx_ThisFile,__LINE__,\
00372     msg,a1,a2,a3,a4)
00373 #define MTX_ERROR5(msg,a1,a2,a3,a4,a5) MtxError(&Mtx_ThisFile,__LINE__,\
00374     msg,a1,a2,a3,a4,a5)
00375 #define MTX_ERROR7(msg,a1,a2,a3,a4,a5,a6,a7) MtxError(&Mtx_ThisFile,__LINE__,\
00376     msg,a1,a2,a3,a4,a5,a6,a7)
00377 
00378 
00379 #define MTX_VERIFY(e) ((void)((e) ||\
00380     MtxError(&Mtx_ThisFile,__LINE__,"Assertion failed: %s",#e)))
00381 
00382 #ifdef _DEBUG
00383 #define DEBUG 1
00384 #endif
00385 
00386 #ifdef DEBUG
00387 #define MTX_ASSERT(e) MTX_VERIFY(e)
00388 #else
00389 #define MTX_ASSERT(e)
00390 #undef PARANOID
00391 #endif
00392 
00393 
00394 
00395 /* ------------------------------------------------------------------
00396    Messages 
00397    ------------------------------------------------------------------ */
00398 
00399 int MtxFormatMessage(char *buf, int bufsize, const char *msg, va_list al);
00400 int MtxPrintMessage(FILE *f, const char *fmt, ...);
00401 
00402 extern int MtxMessageLevel;
00403 #define MSG0 (MtxMessageLevel >= 0)
00404 #define MSG1 (MtxMessageLevel >= 1)
00405 #define MSG2 (MtxMessageLevel >= 2)
00406 #define MSG3 (MtxMessageLevel >= 3)
00407 #define MSG4 (MtxMessageLevel >= 4)
00408 #define MESSAGE(level,args)\
00409   (MtxMessageLevel>=(level) ? ( printf args , fflush(stdout), 1) : 0 )
00410 
00411 
00412 /* ------------------------------------------------------------------
00413    Miscellaneous
00414    ------------------------------------------------------------------ */
00415 
00416 void MtxRandomInit(unsigned seed);
00417 long int MtxRandom(void);
00418 int MtxRandomInt(int max);
00419 long gcd(long a, long b);
00420 long lcm(long a, long b);
00421 
00422 
00423 /* ------------------------------------------------------------------
00424    Structured text files (stfXXX.c)
00425    ------------------------------------------------------------------ */
00426 
00431 typedef struct
00432 {
00433     FILE *File;         
00434     char *LineBuf;      
00435     char *GetPtr;       
00436     int LineBufSize;    
00437     int OutPos;         
00438     int LineNo;         
00439 } StfData;
00440 
00441 int StfClose(StfData *f);
00442 StfData *StfOpen(const char *name, int mode);
00443 
00444 int StfPut(StfData *f, const char *text);
00445 int StfPutInt(StfData *f, int value);
00446 int StfPutString(StfData *f, const char *text);
00447 int StfPutVector(StfData *f, int size, const int *value);
00448 int StfBeginEntry(StfData *f, const char *name);
00449 int StfEndEntry(StfData *f);
00450 int StfWriteValue(StfData *f, const char *name, const char *value);
00451 int StfWriteInt(StfData *f, const char *name, int value);
00452 int StfWriteString(StfData *f, const char *name, const char *value);
00453 int StfWriteVector(StfData *f, const char *name, int size, const int *value);
00454 
00455 int StfReadLine(StfData *f);
00456 const char *StfGetName(StfData *f);
00457 int StfGetInt(StfData *f, int *buf);
00458 int StfGetString(StfData *f, char *buf, size_t bufsize);
00459 int StfMatch(StfData *f, const char *pattern);
00460 int StfGetVector(StfData *f, int *bufsize, int *buf);
00461 
00462 
00463 /* ------------------------------------------------------------------
00464    MeatAxe files
00465    ------------------------------------------------------------------ */
00466 
00470 typedef struct
00471 {
00472     unsigned long Magic;        
00473     int Field;                  
00474     int Nor;                    
00475     int Noc;                    
00476     FILE *File;                 
00477     char *Name;                 
00478 } MtxFile_t;
00479 
00480 int MfIsValid(const MtxFile_t *file);
00481 MtxFile_t *MfOpen(const char *name);
00482 MtxFile_t *MfCreate(const char *name, int field, int nor, int noc);
00483 int MfClose(MtxFile_t *file);
00484 int MfReadLong(MtxFile_t *f, long *buf, int nrows);
00485 int MfReadRows(MtxFile_t *f, PTR buf, int nrows);
00486 int MfWriteLong(MtxFile_t *f, const long *buf, int count);
00487 int MfWriteRows(MtxFile_t *f, PTR buf, int nrows);
00488 
00489 
00490 /* ---------------------------------------------------------------------------------------------- */
00491 
00492 typedef struct
00493 {
00494     unsigned long Magic;
00495     int Field;          
00496     int Nor;            
00497     int Noc;            
00498     PTR Data;           
00499     size_t RowSize;     
00500     int *PivotTable;    
00501 } Matrix_t;
00502 
00503 Matrix_t *MatAdd(Matrix_t *dest, const Matrix_t *src);
00504 Matrix_t *MatAddMul(Matrix_t *dest, const Matrix_t *src, FEL coeff);
00505 Matrix_t *MatAlloc(int field, int nor, int noc);
00506 int MatClean(Matrix_t *mat, const Matrix_t *sub);
00507 int MatCompare(const Matrix_t *a, const Matrix_t *b);
00508 int MatCopyRegion(Matrix_t *dest, int destrow, int destcol, 
00509     const Matrix_t *src, int row1, int col1, int nrows, int ncols);
00510 Matrix_t *MatCut(const Matrix_t *src, int row1, int col1, int nrows, int ncols);
00511 Matrix_t *MatCutRows(const Matrix_t *src, int row1, int nrows);
00512 Matrix_t *MatDup(const Matrix_t *src);
00513 int MatEchelonize(Matrix_t *mat);
00514 int MatFree(Matrix_t *mat);
00515 PTR MatGetPtr(const Matrix_t *mat, int row);
00516 Matrix_t *MatId(int fl, int nor);
00517 Matrix_t *MatInverse(const Matrix_t *src);
00518 int MatIsValid(const Matrix_t *m);
00519 Matrix_t *MatLoad(const char *fn);
00520 Matrix_t *MatMul(Matrix_t *dest, const Matrix_t *src);
00521 Matrix_t *MatMulScalar(Matrix_t *dest, FEL coeff);
00522 long MatNullity(const Matrix_t *mat);
00523 long MatNullity__(Matrix_t *mat);
00524 Matrix_t *MatNullSpace(const Matrix_t *mat);
00525 Matrix_t *MatNullSpace_(Matrix_t *mat, int flags);
00526 Matrix_t *MatNullSpace__(Matrix_t *mat);
00527 int MatOrder(const Matrix_t *mat);
00528 int MatPivotize(Matrix_t *mat);
00529 Matrix_t *MatPower(const Matrix_t *mat, long n);
00530 void MatPrint(const char *name, const Matrix_t *m);
00531 Matrix_t *MatRead(FILE *f);
00532 int MatSave(const Matrix_t *mat, const char *fn);
00533 FEL MatTrace(const Matrix_t *mat);
00534 Matrix_t *MatTransposed(const Matrix_t *src);
00535 int MatWrite(const Matrix_t *mat, FILE *f);
00536 
00537 /* For internal use only */
00538 void Mat_DeletePivotTable(Matrix_t *mat);
00539 
00540 
00541 
00542 /* ------------------------------------------------------------------
00543    Greased matrices
00544    ------------------------------------------------------------------ */
00545 
00551 typedef struct {
00552     long ***tabs; /* tables for different remainders
00553                      of byte numbers mod grrows */
00554     int *nrvals;  /* number of values produced by each table */
00555     int nrtabs;   /* number of tables used */
00556 } GrExtractionTable_t;
00557 
00558 const GrExtractionTable_t *GrGetExtractionTable(int fl,int grrows);
00559 
00560 
00565 typedef struct
00566 {
00567     unsigned long Magic;
00568     int Field, Nor, Noc;
00569     int GrRows;                 /* Grease level (# of rows, 0=no grease) */
00570     int GrBlockSize;            /* Vectors per block (= Field^GrRows) */
00571     int NumVecs;                /* Total number of vectors in <PrecalcData> */
00572     PTR PrecalcData;            /* Precalculated data */
00573     const GrExtractionTable_t
00574         *ExtrTab;               /* Extraction table */
00575     int MPB;                    /* Number of marks per byte */
00576 } GreasedMatrix_t;
00577 
00578 void GrMapRow(PTR v,GreasedMatrix_t *M, PTR w);
00579 GreasedMatrix_t *GrMatAlloc(const Matrix_t *m, int gr_rows);
00580 int GrMatFree(GreasedMatrix_t *mat);
00581 int GrMatIsValid(const GreasedMatrix_t *mat);
00582 
00583 
00584 
00585 /* ------------------------------------------------------------------
00586    Permutations
00587    ------------------------------------------------------------------ */
00588 
00590 typedef struct
00591 {
00592     unsigned long Magic;  
00593     int Degree;           
00594     long *Data;           
00595 } Perm_t;
00596 
00597 
00598 Perm_t *PermAlloc(int deg);
00599 int PermCompare(const Perm_t *a, const Perm_t *b);
00600 Perm_t *PermDup(const Perm_t *src);
00601 int PermFree(Perm_t *p);
00602 Perm_t *PermInverse(const Perm_t *src);
00603 int PermIsValid(const Perm_t *p);
00604 Perm_t *PermLoad(const char *fn);
00605 Perm_t *PermMul(Perm_t *dest, const Perm_t *src);
00606 int PermOrder(const Perm_t *perm);
00607 void PermPrint(const char *name, const Perm_t *perm);
00608 Perm_t *PermPower(const Perm_t *p, int n);
00609 Perm_t *PermRead(FILE *f);
00610 int PermSave(const Perm_t *perm, const char *fn);
00611 int PermWrite(const Perm_t *perm, FILE *f);
00612 
00613 void Perm_ConvertOld(long *data, int len);
00614 
00615 /* ------------------------------------------------------------------
00616    Polynomials
00617    ------------------------------------------------------------------ */
00618 
00619 typedef struct
00620 {
00621     unsigned long Magic;
00622     int Field;          
00623     int Degree;         
00624     FEL *Data;          
00626     int BufSize;        
00627 }
00628 Poly_t;
00629 
00630 
00631 Poly_t *PolAdd(Poly_t *dest, const Poly_t *src);
00632 Poly_t *PolAlloc(int fl, int n);
00633 int PolCompare(const Poly_t *a, const Poly_t *b);
00634 Poly_t *PolDerive(Poly_t *pol);
00635 Poly_t *PolDivMod(Poly_t *a, const Poly_t *b);
00636 Poly_t *PolDup(const Poly_t *p);
00637 int PolFree(Poly_t *p);
00638 Poly_t *PolGcd(const Poly_t *a, const Poly_t *b);
00639 int PolGcdEx(const Poly_t *a, const Poly_t *b, Poly_t **result);
00640 int PolIsValid(const Poly_t *p);
00641 Poly_t *PolMod(Poly_t *a, const Poly_t *b);
00642 void Pol_Normalize(Poly_t *p);
00643 Poly_t *PolLoad(const char *fn);
00644 Poly_t *PolMul(Poly_t *dest, const Poly_t *src);
00645 void PolPrint(char *name, const Poly_t *p);
00646 Poly_t *PolRead(FILE *f);
00647 int PolSave(const Poly_t *pol, const char *fn);
00648 int PolWrite(const Poly_t *p, FILE *f);
00649 
00650 
00651 
00652 /* ------------------------------------------------------------------
00653    Factored polynomials
00654    ------------------------------------------------------------------ */
00655 
00656 typedef struct 
00657 {
00658     unsigned long Magic;
00659     int NFactors;       
00660     int BufSize;        
00661     Poly_t **Factor;    
00662     int *Mult;          
00663 } FPoly_t;
00664 
00665 FPoly_t *FpAlloc();
00666 int FpFree(FPoly_t *x);
00667 int FpIsValid(const FPoly_t *p);
00668 FPoly_t *FpMul(FPoly_t *dest, const FPoly_t *src);
00669 FPoly_t *FpMulP(FPoly_t *dest, const Poly_t *src, int pwr);
00670 int FpPrint(const char *name, const FPoly_t *p);
00671 
00672 /* ---------------------------------------------------------------------------------------------- */
00673 
00674 typedef struct
00675 {
00676     unsigned long Magic;  
00677     int Size;             
00678     int BufSize;          
00679     long Data[1];         
00681 } BitString_t;            
00683 BitString_t *BsAlloc(int size);
00684 int BsAnd(BitString_t *dest, const BitString_t *src);
00685 int BsClear(BitString_t *bs, int i);
00686 int BsClearAll(BitString_t *bs);
00687 int BsCompare(const BitString_t *a, const BitString_t *b);
00688 BitString_t *BsCopy(BitString_t *dest, const BitString_t *src);
00689 BitString_t *BsDup(const BitString_t *src);
00690 int BsFree(BitString_t *bs);
00691 int BsIntersectionCount(const BitString_t *a, const BitString_t *b);
00692 int BsIsSub(const BitString_t *a, const BitString_t *b);
00693 int BsIsValid(const BitString_t *bs);
00694 int BsMinus(BitString_t *dest, const BitString_t *src);
00695 int BsOr(BitString_t *dest, const BitString_t *src);
00696 void BsPrint(const char *name, const BitString_t *bs);
00697 BitString_t *BsRead(FILE *f);
00698 int BsSet(BitString_t *bs, int i);
00699 int BsTest(const BitString_t *bs, int i);
00700 int BsWrite(BitString_t *bs, FILE *f);
00701 
00702 #ifndef DEBUG
00703 
00704 #define BS_BPL (sizeof(long) * 8)
00705 #define BsSet(bs,i) ((bs)->Data[(i) / BS_BPL] |= 1L << ((i) % BS_BPL))
00706 #define BsClear(bs,i) ((bs)->Data[(i) / BS_BPL] &= ~(1L << ((i) % BS_BPL)))
00707 #define BsTest(bs,i) (((bs)->Data[(i) / BS_BPL] & (1L << ((i) % BS_BPL))) != 0 ? 1 : 0)
00708 
00709 #endif
00710 
00711 
00712 /* ------------------------------------------------------------------
00713    Sets
00714    ------------------------------------------------------------------ */
00715 
00716 typedef struct
00717 {
00718     unsigned long Magic;        
00719     int Size;                   
00720     int BufSize;                
00721     long *Data;                 
00722 } Set_t;
00723 
00724 
00725 Set_t *SetAlloc();
00726 int SetContains(const Set_t *set, long elem);
00727 Set_t *SetDup(const Set_t *s);
00728 int SetFree(Set_t *x);
00729 int SetInsert(Set_t *set, long elem);
00730 int SetIsValid(const Set_t *s);
00731 int SetPrint(char *name, const Set_t *s);
00732 
00733 
00734 /* ------------------------------------------------------------------
00735    Integer matrices
00736    ------------------------------------------------------------------ */
00737 
00738 typedef struct
00739 {
00740     unsigned long Magic;
00741     int Nor;    
00742     int Noc;    
00743     long *Data; 
00744 } IntMatrix_t;
00745 
00746 IntMatrix_t *ImatAlloc(int nor, int noc);
00747 int ImatFree(IntMatrix_t *mat);
00748 int ImatIsValid(const IntMatrix_t *m);
00749 IntMatrix_t *ImatLoad(const char *fn);
00750 IntMatrix_t *ImatRead(FILE *f);
00751 int ImatSave(const IntMatrix_t *mat, const char *file_name);
00752 int ImatWrite(const IntMatrix_t *mat, FILE *f);
00753 
00754 
00755 /* --------------------------------------------------------------------------
00756    Polymorphic objects
00757    -------------------------------------------------------------------------- */
00758 
00759 void *XDup(void *a);
00760 int XIsCompatible(void *a, void *b);
00761 void XFree(void *a);
00762 void *XInverse(void *a);
00763 void *XLoad(const char *fn);
00764 void XMul(void *a, void *b);
00765 long XOrder(void *a);
00766 void *XPower(void *a, int n);
00767 int XSave(void *a, const char *fn);
00768 
00769 
00770 
00771 
00772 
00773 
00774 
00775 /* --------------------------------------------------------------------------
00776    Matrix sets
00777    -------------------------------------------------------------------------- */
00778 
00783 typedef struct
00784 {
00785     Matrix_t *Matrix;
00786     int PivRow;
00787     int PivCol;
00788     FEL PivMark;
00789 } MatrixSetElement_t;
00790 
00791 
00795 typedef struct {
00796     unsigned long Magic;
00797     int Len;
00798     MatrixSetElement_t *List;
00799 } MatrixSet_t;
00800 
00801 MatrixSet_t *MsAlloc();
00802 int MsClean(const MatrixSet_t *set, Matrix_t *mat);
00803 int MsCleanAndAppend(MatrixSet_t *set, Matrix_t *mat);
00804 int MsFree(MatrixSet_t *set);
00805 int MsIsValid(const MatrixSet_t *set);
00806 
00807 
00808 
00809 /* --------------------------------------------------------------------------
00810    Matrix representations
00811    -------------------------------------------------------------------------- */
00812 
00813 typedef struct
00814 {
00815     unsigned long Magic;
00816     int NGen;
00817     Matrix_t **Gen;
00818 } MatRep_t;
00819 
00820 #define MR_COPY_GENERATORS  0x0001
00821 
00822 int MrAddGenerator(MatRep_t *rep, Matrix_t *gen, int flags);
00823 MatRep_t *MrAlloc(int ngen, Matrix_t **gen, int flags);
00824 int MrChangeBasis(MatRep_t *rep, const Matrix_t *trans);
00825 int MrIsValid(const MatRep_t *rep);
00826 int MrFree(MatRep_t *rep);
00827 MatRep_t *MrLoad(const char *basename, int ngen);
00828 int MrSave(const MatRep_t *rep, const char *basename);
00829 MatRep_t *MrTransposed(const MatRep_t *rep);
00830 
00831 
00832 
00833 /* ------------------------------------------------------------------
00834    The word generator
00835    ------------------------------------------------------------------ */
00836 
00837 typedef struct 
00838 {
00839     const MatRep_t *Rep;        
00840     Matrix_t *Basis[8];         
00841     int N2[8];                  
00842     int *Description;           
00843 } WgData_t;
00844 
00845 
00846 WgData_t *WgAlloc(const MatRep_t *rep);
00847 int *WgDescribeWord(WgData_t *b, long n);
00848 int WgFree(WgData_t *b);
00849 Matrix_t *WgMakeWord(WgData_t *b, long n);
00850 void WgMakeFingerPrint(WgData_t *b, int fp[6]);
00851 const char *WgSymbolicName(WgData_t *b, long n);
00852 
00853 
00854 
00855 
00856 
00857 /* ------------------------------------------------------------------
00858    Spin-up, Split, Quotients, etc.
00859    ------------------------------------------------------------------ */
00860 
00861 #define SF_FIRST        0x0001  /* Try only the first seed vector */
00862 #define SF_EACH         0x0002  /* Try each seed vector */
00863 #define SF_MAKE         0x0004  /* Try all 1-dimensional subspaces */
00864 #define SF_SUB          0x0010  /* Try until finding a proper subspace */
00865 #define SF_CYCLIC       0x0020  /* Try until finding a cyclic vector */
00866 #define SF_COMBINE      0x0040  /* Combine the spans */
00867 #define SF_SEED_MASK    0x000F
00868 #define SF_MODE_MASK    0x00F0
00869 #define SF_STD          0x0100  /* Spin up 'canonically' */
00870 
00871 Matrix_t *QProjection(const Matrix_t *subspace, const Matrix_t *vectors);
00872 Matrix_t *QAction(const Matrix_t *sub, const Matrix_t *gen);
00873 Matrix_t *SAction(const Matrix_t *sub, const Matrix_t *gen);
00874 
00875 typedef struct
00876 {
00877     int MaxSubspaceDimension;
00878     int MaxTries;
00879     int Result;
00880 } SpinUpInfo_t;
00881 
00882 int SpinUpInfoInit(SpinUpInfo_t *info);
00883 Matrix_t *SpinUp(const Matrix_t *seed, const MatRep_t *rep, int flags,
00884     IntMatrix_t **script, SpinUpInfo_t *info);
00885 Matrix_t *SpinUpWithScript(const Matrix_t *seed, const MatRep_t *rep, 
00886     const IntMatrix_t *script);
00887 int Split(Matrix_t *subspace, const MatRep_t *rep, 
00888           MatRep_t **sub, MatRep_t **quot);
00889 
00890 int ConvertSpinUpScript(IntMatrix_t *script);
00891 
00892 Matrix_t *SpinUpWithPermutations(const Matrix_t *seed, int ngen, 
00893     const Perm_t **gen, int flags, IntMatrix_t **script, SpinUpInfo_t *info);
00894 
00895 
00896 /* ------------------------------------------------------------------
00897    Seed vector generator
00898    ------------------------------------------------------------------ */
00899 
00900 long MakeSeedVector(const Matrix_t *basis, long lastno, PTR vec);
00901 
00902 
00903 
00904 /* ------------------------------------------------------------------
00905    Miscellaneous algorithms
00906    ------------------------------------------------------------------ */
00907 
00908 Matrix_t *MatInsert_(Matrix_t *mat, const Poly_t *pol);
00909 Matrix_t *MatInsert(const Matrix_t *mat, const Poly_t *pol);
00910 int IsSubspace(const Matrix_t *sub, const Matrix_t *space, int ngen);
00911 
00912 Matrix_t *MatTensor(const Matrix_t *m1, const Matrix_t *m2);
00913 int MatrixToVector(const Matrix_t *mat, Matrix_t *vecs, int n);
00914 Matrix_t *VectorToMatrix(Matrix_t *vecs, int n, int noc);
00915 Matrix_t *TensorMap(Matrix_t *vec, const Matrix_t *a, const Matrix_t *b);
00916 
00917 int StablePower(const Matrix_t *mat, int *pwr, Matrix_t **ker);
00918 int StablePower_(Matrix_t *mat, int *pwr, Matrix_t **ker);
00919 
00920 
00921 
00922 /* ------------------------------------------------------------------
00923    Polynomial factorization (Berlekamp algorithm)
00924    ------------------------------------------------------------------ */
00925 
00926 FPoly_t *Factorization(const Poly_t *pol);
00927 
00928 
00929 
00930 /* ------------------------------------------------------------------
00931    Characteristic and minimal polynomials (charpol.c, minpol.c)
00932    ------------------------------------------------------------------ */
00933 
00934 extern long CharPolSeed;
00935 Poly_t *CharPolFactor(const Matrix_t *mat);
00936 FPoly_t *CharPol(const Matrix_t *mat);
00937 Poly_t *MinPolFactor(Matrix_t *mat);
00938 FPoly_t *MinPol(Matrix_t *mat);
00939 
00940 
00941 
00942 /* ------------------------------------------------------------------
00943    The lattice package
00944    ------------------------------------------------------------------ */
00950 #define MAXGEN 20       /* Max. number of generators */
00951 #define LAT_MAXCF 200   /* Max. number of composition factors */
00952 #define MAXCYCL 30000   /* Max. number of cyclic submodules */
00953 #define MAXDOTL 90000   /* Max. number of dotted lines */
00954 #define MAXNSUB 20000   /* Max. number of submodules */
00955 #define LAT_MAXBASENAME 100
00956 
00957 typedef struct
00958 {
00959     long dim, num, mult;
00960     long idword;                /* Identifying word */
00961     Poly_t *idpol;
00962     long peakword;              /* Peak word */
00963     Poly_t *peakpol;
00964     long nmount;                /* Number of mountains */
00965     long ndotl;                 /* Number of dotted lines */
00966     long spl;                   /* Degree of splitting field */
00967 }
00968 CfInfo;
00969 
00970 typedef struct
00971 {
00972     char BaseName[LAT_MAXBASENAME];     
00973     int Field;                          
00974     int NGen;                           
00975     int NCf;                            
00976     CfInfo Cf[LAT_MAXCF];               
00977     int NSocles;                        
00978     int *Socle;                         
00979     int NHeads;                         
00980     int *Head;                          
00981 } Lat_Info;
00982 
00983 int Lat_ReadInfo(Lat_Info *li, const char *basename);
00984 int Lat_WriteInfo(const Lat_Info *li);
00985 const char *Lat_CfName(const Lat_Info *li, int cf);
00986 int Lat_AddHead(Lat_Info *li, int *mult);
00987 int Lat_AddSocle(Lat_Info *li, int *mult);
00988 
00989 
00990 #define LAT_RG_INVERT           0x0001  /* Invert generators */
00991 #define LAT_RG_TRANSPOSE        0x0002  /* Transpose generators */
00992 #define LAT_RG_STD              0x0004  /* Use standard form */
00993 
00994 MatRep_t *Lat_ReadCfGens(Lat_Info *info, int cf, int flags);
00995 
01001 /* ------------------------------------------------------------------
01002    Tensor condensation package
01003    ------------------------------------------------------------------ */
01004 
01013 typedef struct
01014 {
01015     char NameM[LAT_MAXBASENAME];        
01016     char NameN[LAT_MAXBASENAME];        
01017     int Dim;                            
01018     int NCf;                            
01019     int CfIndex[2][LAT_MAXCF];          
01020 } TkData_t;
01021 
01022 int TK_ReadInfo(TkData_t *tki, const char *name);
01023 int TK_WriteInfo(TkData_t *tki, const char *name);
01024 
01025 
01026 
01027 
01028 /* !!!!!!!!!!!!!!! 2.3 STUFF below !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
01029 /* !!!!!!!!!!!!!!! 2.3 STUFF below !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
01030 /* !!!!!!!!!!!!!!! 2.3 STUFF below !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
01031 
01032 
01033 
01034 /* ------------------------------------------------------------------
01035    Return codes
01036    ------------------------------------------------------------------ */
01037 
01038 #define EXIT_OK         0       /* Exit code: normal end */
01039 #define EXIT_ERR        1       /*            error */
01040 
01041 
01042 
01043 /* ------------------------------------------------------------------
01044    Function operating on representations and vector spaces
01045    ------------------------------------------------------------------ */
01046 
01047 int IsIsomorphic(const MatRep_t *rep1, const CfInfo *info1,
01048     const MatRep_t *rep2, Matrix_t  **trans, int use_pw);
01049 int MakeEndomorphisms(const MatRep_t *rep, const Matrix_t *nsp,
01050     Matrix_t *endo[]);
01051 Matrix_t *HomogeneousPart(MatRep_t *m, MatRep_t *s, Matrix_t *npw, 
01052     const IntMatrix_t *op, int dimends);
01053 
01054 
01055 
01056 /* ------------------------------------------------------------------
01057    Lattice drawing functions
01058    ------------------------------------------------------------------ */
01059 
01063 typedef struct
01064 {
01065     double PosX, PosY;          /* Position [0..1] */
01066     unsigned long UserData;     /* User-defined attributes */
01067     int Layer;                  /* Layer number */
01068     double Score;               /* Used in optimization */
01069     int ScoreCount;
01070 } LdNode_t;
01071 
01075 typedef struct
01076 {
01077     int NNodes;
01078     LdNode_t *Nodes;
01079     int *IsSub;         /* Incidence relation, <NNodes> * <NNodes> entries */
01080     int *LayerNo;       /* Layer numbers */
01081     int NLayers;
01082 } LdLattice_t;
01083 
01084 #define LD_ISSUB(l,i,k) ((l)->IsSub[(i) * (l)->NNodes + (k)])
01085 
01086 LdLattice_t *LdAlloc(int num_nodes);
01087 int LdFree(LdLattice_t *l);
01088 int LdAddIncidence(LdLattice_t *lat, int sub, int sup);
01089 int LdSetPositions(LdLattice_t *l);
01090 
01091 
01092 
01093 
01094 /* OLD STUFF */
01095 int ChangeBasisOLD(const Matrix_t *M, int ngen, const Matrix_t *gen[],
01096         Matrix_t *newgen[]);
01097 
01098 
01099 #endif  /* !defined(_MEATAXE_H_) */
01100 
01101