[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.7

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

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