[BACK]Return to ntl.cpp CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_ntl

Annotation of OpenXM/src/ox_ntl/ntl.cpp, Revision 1.6

1.6     ! iwane       1: /* $OpenXM: OpenXM/src/ox_ntl/ntl.cpp,v 1.5 2004/07/04 02:31:51 iwane Exp $ */
1.1       iwane       2:
                      3: #include <NTL/ZZXFactoring.h>
1.3       iwane       4: #include <NTL/LLL.h>
1.1       iwane       5:
1.2       iwane       6: #include "ox_toolkit.h"
1.1       iwane       7: #include "ntl.h"
                      8:
1.2       iwane       9: #if __NTL_DEBUG
                     10: #define __NTL_PRINT (1)
                     11: #endif
1.1       iwane      12:
1.3       iwane      13: #define DPRINTF(x)     printf x; fflush(stdout)
                     14:
1.1       iwane      15: /****************************************************************************
                     16:  *
                     17:  * Factorize polynomial over the integers.
                     18:  *
                     19:  * PARAM : I : arg  : polynomial
                     20:  *       : I : argc :
                     21:  * RETURN: [num,[[factor1,multiplicity1],[factor2,multiplicity2],...]]
                     22:  *
                     23:  * EX    : ntl_fctr([2*x^11+4*x^8-2*x^6+2*x^5-4*x^3-2],1) ==>
                     24:          : [2,[[x-1,1],[x^4+x^3+x^2+x+1,1],[x+1,2],[x^2-x+1,2]]]
                     25:  *
                     26:  ****************************************************************************/
1.6     ! iwane      27: oxstack_node *
        !            28: ntl_fctr(oxstack_node **arg, int argc)
1.1       iwane      29: {
1.6     ! iwane      30:        oxstack_node *p = arg[0];
        !            31:        cmo *poly = p->c;
1.1       iwane      32:        cmo_indeterminate *x;
1.6     ! iwane      33:        cmo *err;
1.1       iwane      34:        ZZX f;
                     35:        int ret;
                     36:
                     37:        if (argc != 1) {
1.6     ! iwane      38:                err = ((cmo *)new_cmo_error2((cmo *)new_cmo_string("Invalid Parameter(#)")));
        !            39:                p = oxstack_node_init(err);
        !            40:                return (p);
1.1       iwane      41:        }
                     42:
                     43:        ret = cmo_to_ZZX(f, poly, x);
                     44:        if (ret != NTL_SUCCESS) {
                     45:                /* format error */
1.6     ! iwane      46:                err = (cmo *)new_cmo_error2((cmo *)new_cmo_string("Invalid Parameter(type)"));
        !            47:                p = oxstack_node_init(err);
        !            48:                return (p);
1.1       iwane      49:        }
                     50:
1.2       iwane      51: #if __NTL_PRINT
                     52:        cout << "input: " << f << endl;
                     53: #endif
                     54:
1.3       iwane      55:        cmon_factors_t *factors = new_cmon_factors();
                     56:        factors->x = x;
                     57:
                     58:        factor(*factors->cont, *factors->f, f);
1.1       iwane      59:
1.2       iwane      60: #if __NTL_PRINT
1.3       iwane      61:        cout << "fctr : " << *factors->f << endl;
1.2       iwane      62: #endif
                     63:
1.6     ! iwane      64:        p = oxstack_node_init(NULL);
        !            65:        p->p = factors;
1.1       iwane      66:
1.6     ! iwane      67:        return (p);
1.3       iwane      68: }
1.1       iwane      69:
1.3       iwane      70: /****************************************************************************
                     71:  *
                     72:  * Factorize polynomial over the integers.
                     73:  *
                     74:  * PARAM : I : arg  : polynomial
                     75:  *       : I : argc :
                     76:  * RETURN: [num,[[factor1,multiplicity1],[factor2,multiplicity2],...]]
                     77:  *
                     78:  * EX    : ntl_fctr([2*x^11+4*x^8-2*x^6+2*x^5-4*x^3-2],1) ==>
                     79:          : [2,[[x-1,1],[x^4+x^3+x^2+x+1,1],[x+1,2],[x^2-x+1,2]]]
                     80:  *
                     81:  ****************************************************************************/
1.6     ! iwane      82: oxstack_node *
        !            83: ntl_lll(oxstack_node **arg, int argc)
1.3       iwane      84: {
1.6     ! iwane      85:        oxstack_node *p = arg[0];
        !            86:        cmo *err;
1.3       iwane      87:        ZZX f;
                     88:        int ret;
                     89:        cmon_mat_zz_t *mat;
                     90:
                     91:        if (argc != 1) {
1.6     ! iwane      92:                err = ((cmo *)new_cmo_error2((cmo *)new_cmo_string("Invalid Parameter(#)")));
        !            93:                p = oxstack_node_init(err);
        !            94:                return (p);
1.3       iwane      95:        }
                     96:
                     97:        mat = new_cmon_mat_zz();
1.6     ! iwane      98:        ret = cmo_to_mat_zz(*mat->mat, p->c);
1.3       iwane      99:        if (ret != NTL_SUCCESS) {
                    100:                delete_cmon_mat_zz(mat);
                    101:                /* format error */
1.6     ! iwane     102:                err = ((cmo *)new_cmo_error2((cmo *)new_cmo_string("Invalid Parameter(type)")));
        !           103:                p = oxstack_node_init(err);
        !           104:                return (p);
1.3       iwane     105:        }
1.1       iwane     106:
1.3       iwane     107: #if __NTL_PRINT
                    108:        cout << "input: " << (*mat->mat) << endl;
                    109: #endif
                    110:
                    111:        ZZ det2;
1.5       iwane     112:        mat_ZZ U;
1.3       iwane     113:
                    114: #if __NTL_PRINT
                    115:        cout << "output: " << (*mat->mat) << endl;
1.5       iwane     116:        cout << U << endl;
1.3       iwane     117: #endif
                    118:
1.6     ! iwane     119:        p = oxstack_node_init(NULL);
        !           120:        p->p = mat;
        !           121:        return (p);
1.1       iwane     122: }
                    123:
1.2       iwane     124:
1.3       iwane     125:
                    126: #if __NTL_DEBUG /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
1.2       iwane     127:
1.4       iwane     128: #include <unistd.h>
                    129: #include <gc/gc.h>
                    130: #include <strstream>
                    131: #include "gmp.h"
                    132:
                    133: void ntl_free(void *ptr, size_t size) {}
                    134:
                    135: void *ntl_realloc(void *org, size_t old, size_t size)
                    136: {
                    137:        void *ptr = GC_realloc(org, size);
                    138:        return (ptr);
                    139: }
                    140:
                    141:
1.2       iwane     142: int
                    143: main(int argc, char *argv[])
                    144: {
1.4       iwane     145: #if 0
                    146: {
1.2       iwane     147:        ZZX f;
1.4       iwane     148:        const char *str = "12345";
1.2       iwane     149:        int num = 1;
                    150:        char *var = "x";
                    151:        cmo *c;
                    152:
                    153:        f = to_ZZ(str);;
                    154:
                    155:        cmo_indeterminate *x = new_cmo_indeterminate((cmo *)new_cmo_string(var));
                    156:        cmo_recursive_polynomial *poly = ZZX_to_cmo(f, x);
                    157:        cmo *arg[3];
                    158:
                    159:        arg[0] = (cmo *)poly;
                    160:
                    161:        c = ntl_fctr(arg, num);
1.4       iwane     162: }
                    163: #endif
                    164:
                    165: {
                    166:        cmo_zz *n;
                    167:
                    168:        istrstream istr("[3 -3 -6]");
                    169:        ZZ cont;
                    170:        ZZX fac;
                    171:        vec_pair_ZZX_long facs;
                    172:        cmo_indeterminate *x = new_cmo_indeterminate((cmo *)new_cmo_string("x"));
                    173:        istr >> fac;
                    174:
                    175:        factor(cont, facs, fac);
                    176:
                    177:        //      mpz_clear(ppp->mpz);
                    178:        mp_set_memory_functions(GC_malloc, ntl_realloc, ntl_free);
                    179:        for (int i = 0;; i++) {
                    180:
                    181:                cmon_factors_t *mat = new_cmon_factors(cont, facs, x);
                    182:                cmo *aaa = convert_cmon((cmo *)mat);
                    183:                delete_cmon((cmo *)mat);
                    184:
                    185:                if (i % 1000 == 0) {
                    186:                        printf("GC-counts: %d,   size: %u\n", GC_gc_no, GC_get_heap_size());
                    187:                        GC_gcollect();
                    188:                        usleep(1);
                    189:                }
                    190:        }
                    191: }
                    192:
1.2       iwane     193:
                    194:        return (0);
                    195: }
                    196:
1.4       iwane     197: #endif /* __NTL_DEBUG @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
1.1       iwane     198:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>