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