version 1.1, 2003/11/03 03:11:21 |
version 1.7, 2013/10/18 01:28:52 |
|
|
/* $OpenXM$ */ |
/* $OpenXM: OpenXM/src/ox_ntl/ntl.cpp,v 1.6 2008/09/19 10:55:40 iwane Exp $ */ |
|
|
#include <NTL/ZZXFactoring.h> |
#include <NTL/ZZXFactoring.h> |
#include <NTL/ZZ_pXFactoring.h> |
#include <NTL/LLL.h> |
|
|
#include <iostream> |
#include "ox_toolkit.h" |
#include <strstream> |
|
|
|
#include "ntl.h" |
#include "ntl.h" |
|
|
|
#if __NTL_DEBUG |
|
#define __NTL_PRINT (1) |
|
#endif |
|
|
/*==========================================================================* |
#define DPRINTF(x) printf x; fflush(stdout) |
* Check string format |
|
*==========================================================================*/ |
|
|
|
#define NtlIsSpace(c) ((c) == ' ' || (c) == '\t') |
|
#define NtlIsDigit(c) ((c) >= '0' && (c) <= '9') |
|
|
|
/**************************************************************************** |
/**************************************************************************** |
* |
* |
* test for string format of integer |
* Factorize polynomial over the integers. |
* |
* |
* PARAM : I : str : string |
* PARAM : I : arg : polynomial |
* : O : endptr : |
* : I : argc : |
* RETURN: !0 : the string tests true |
* RETURN: [num,[[factor1,multiplicity1],[factor2,multiplicity2],...]] |
* : 0 : the string tests false |
|
* |
* |
|
* EX : ntl_fctr([2*x^11+4*x^8-2*x^6+2*x^5-4*x^3-2],1) ==> |
|
: [2,[[x-1,1],[x^4+x^3+x^2+x+1,1],[x+1,2],[x^2-x+1,2]]] |
|
* |
****************************************************************************/ |
****************************************************************************/ |
static int |
oxstack_node * |
ntl_isZZstr_(const char *str, char const **endptr) |
ntl_fctr(oxstack_node **arg, int argc) |
{ |
{ |
while (NtlIsSpace(*str)) |
oxstack_node *p = arg[0]; |
str++; |
cmo *poly = p->c; |
|
cmo_indeterminate *x; |
|
cmo *err; |
|
ZZX f; |
|
int ret; |
|
char emss[25] = "Invalid Parameter(#)"; |
|
char emst[25] = "Invalid Parameter(type)"; |
|
|
/* NTL reject "+999" */ |
if (argc != 1) { |
if (*str == '-') |
err = ((cmo *)new_cmo_error2((cmo *)new_cmo_string(emss))); |
str++; |
p = oxstack_node_init(err); |
|
return (p); |
|
} |
|
|
if (!NtlIsDigit(*str)) |
ret = cmo_to_ZZX(f, poly, x); |
return (0); |
if (ret != NTL_SUCCESS) { |
|
/* format error */ |
|
err = (cmo *)new_cmo_error2((cmo *)new_cmo_string(emst)); |
|
p = oxstack_node_init(err); |
|
return (p); |
|
} |
|
|
str++; |
#if __NTL_PRINT |
|
std::cout << "input: " << f << std::endl; |
|
#endif |
|
|
while (NtlIsDigit(*str)) |
cmon_factors_t *factors = new_cmon_factors(); |
str++; |
factors->x = x; |
|
|
*endptr = str; |
factor(*factors->cont, *factors->f, f); |
|
|
return (!0); |
#if __NTL_PRINT |
|
std::cout << "fctr : " << *factors->f << std::endl; |
|
#endif |
|
|
} |
p = oxstack_node_init(NULL); |
|
p->p = factors; |
|
|
static int |
return (p); |
ntl_isZZstr(const char *str) |
|
{ |
|
const char *ptr; |
|
int ret = ntl_isZZstr_(str, &ptr); |
|
if (!ret) |
|
return (ret); |
|
|
|
while (NtlIsSpace(*ptr)) |
|
ptr++; |
|
|
|
return (*ptr == '\0'); |
|
} |
} |
|
|
|
|
/**************************************************************************** |
/**************************************************************************** |
* |
* |
* test for string format of univariate polynomials with integer coefficients |
* Factorize polynomial over the integers. |
* in NTL style. |
|
* |
* |
* PARAM : I : str : string |
* PARAM : I : arg : polynomial |
* : O : endptr : |
* : I : argc : |
* RETURN: !0 : the string tests true |
* RETURN: [num,[[factor1,multiplicity1],[factor2,multiplicity2],...]] |
* : 0 : the string tests false |
|
* |
* |
|
* EX : ntl_fctr([2*x^11+4*x^8-2*x^6+2*x^5-4*x^3-2],1) ==> |
|
: [2,[[x-1,1],[x^4+x^3+x^2+x+1,1],[x+1,2],[x^2-x+1,2]]] |
|
* |
****************************************************************************/ |
****************************************************************************/ |
static int |
oxstack_node * |
ntl_isZZXstr_(const char *str, char const **endptr) |
ntl_lll(oxstack_node **arg, int argc) |
{ |
{ |
const char *s; |
oxstack_node *p = arg[0]; |
|
cmo *err; |
|
ZZX f; |
|
int ret; |
|
cmon_mat_zz_t *mat; |
|
char emss[25] = "Invalid Parameter(#)"; |
|
char emst[25] = "Invalid Parameter(type)"; |
|
|
while (NtlIsSpace(*str)) |
if (argc != 1) { |
str++; |
err = ((cmo *)new_cmo_error2((cmo *)new_cmo_string(emss))); |
|
p = oxstack_node_init(err); |
|
return (p); |
|
} |
|
|
if (*str != '[') |
mat = new_cmon_mat_zz(); |
return (0); |
ret = cmo_to_mat_zz(*mat->mat, p->c); |
|
if (ret != NTL_SUCCESS) { |
str++; |
delete_cmon_mat_zz(mat); |
|
/* format error */ |
while (*str != ']' && *str != '\0') { |
err = ((cmo *)new_cmo_error2((cmo *)new_cmo_string(emst))); |
|
p = oxstack_node_init(err); |
if (!ntl_isZZstr_(str, &s)) |
return (p); |
return (0); |
|
str = s; |
|
|
|
while (NtlIsSpace(*str)) |
|
str++; |
|
} |
} |
|
|
while (NtlIsSpace(*str)) |
#if __NTL_PRINT |
str++; |
std::cout << "input: " << (*mat->mat) << std::endl; |
|
#endif |
|
|
if (*str != ']') |
ZZ det2; |
return (0); |
mat_ZZ U; |
|
|
str++; |
#if __NTL_PRINT |
*endptr = str; |
std::cout << "output: " << (*mat->mat) << std::endl; |
return (!0); |
std::cout << U <<std:: endl; |
} |
#endif |
|
|
static int |
p = oxstack_node_init(NULL); |
ntl_isZZXstr(const char *str) |
p->p = mat; |
{ |
return (p); |
const char *ptr; |
|
int ret = ntl_isZZXstr_(str, &ptr); |
|
if (!ret) |
|
return (ret); |
|
|
|
while (NtlIsSpace(*ptr)) |
|
ptr++; |
|
|
|
return (*ptr == '\0'); |
|
} |
} |
|
|
|
|
/*==========================================================================* |
|
* Convert |
|
*==========================================================================*/ |
|
static cmo_zz * |
|
ZZ_to_cmo_zz(const ZZ &z) |
|
{ |
|
cmo_zz *c; |
|
|
|
ostrstream sout; |
#if __NTL_DEBUG /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ |
sout << z << '\0'; |
|
|
|
c = new_cmo_zz_set_string(sout.str()); |
#include <unistd.h> |
|
#include <gc/gc.h> |
|
|
return (c); |
void ntl_free(void *ptr, size_t size) {} |
} |
|
|
|
|
void *ntl_realloc(void *org, size_t old, size_t size) |
static int |
|
cmo_to_ZZ(ZZ &z, cmo *c) |
|
{ |
{ |
int ret = NTL_SUCCESS; |
void *ptr = GC_realloc(org, size); |
char *str; |
return (ptr); |
|
|
switch (c->tag) { |
|
case CMO_ZERO: |
|
z = to_ZZ(0); |
|
break; |
|
case CMO_ZZ: |
|
{ |
|
str = new_string_set_cmo(c); |
|
istrstream sin(str, strlen(str)); |
|
sin >> z; |
|
break; |
|
} |
|
case CMO_INT32: |
|
z = to_ZZ(((cmo_int32 *)c)->i); |
|
break; |
|
case CMO_STRING: |
|
{ |
|
str = ((cmo_string *)c)->s; |
|
if (!ntl_isZZstr(str)) |
|
return (NTL_FAILURE); |
|
|
|
istrstream sin(str, strlen(str)); |
|
sin >> z; |
|
break; |
|
} |
|
default: |
|
ret = NTL_FAILURE; |
|
break; |
|
} |
|
|
|
return (ret); |
|
} |
} |
|
|
|
|
static int |
int |
cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x) |
main(int argc, char *argv[]) |
{ |
{ |
char *str; |
#if 0 |
int ret; |
{ |
|
ZZX f; |
|
const char *str = "12345"; |
|
int num = 1; |
|
char *var = "x"; |
|
cmo *c; |
|
|
switch (m->tag) { |
f = to_ZZ(str);; |
case CMO_STRING: /* [ 3 4 7 ] ==> 3+4*x+7*x^2 */ |
|
str = ((cmo_string *)m)->s; |
|
ret = 0; //ntl_isZZXstr(str); |
|
|
|
if (!ret) { |
cmo_indeterminate *x = new_cmo_indeterminate((cmo *)new_cmo_string(var)); |
/* format error */ |
cmo_recursive_polynomial *poly = ZZX_to_cmo(f, x); |
return (NTL_FAILURE); |
cmo *arg[3]; |
} |
|
|
|
{ |
arg[0] = (cmo *)poly; |
istrstream sin(str, strlen(str)); |
|
sin >> f; |
|
} |
|
break; |
|
case CMO_RECURSIVE_POLYNOMIAL: |
|
{ |
|
cmo_recursive_polynomial *rec = (cmo_recursive_polynomial *)m; |
|
cmo_polynomial_in_one_variable *poly = (cmo_polynomial_in_one_variable *)rec->coef; |
|
cell *el; |
|
int len; |
|
|
|
if (poly->tag != CMO_POLYNOMIAL_IN_ONE_VARIABLE) { |
c = ntl_fctr(arg, num); |
return (NTL_FAILURE); |
|
} |
|
|
|
el = list_first((cmo_list *)poly); |
|
len = list_length((cmo_list *)poly); |
|
|
|
f = 0; |
|
|
|
while (!list_endof((cmo_list *)poly, el)) { |
|
ZZ c; |
|
ostrstream sout; |
|
cmo *coef = el->cmo; |
|
int exp = el->exp; |
|
|
|
ret = cmo_to_ZZ(c, coef); |
|
if (ret != NTL_SUCCESS) { |
|
return (NTL_FAILURE); |
|
} |
|
|
|
SetCoeff(f, exp, c); |
|
|
|
el = list_next(el); |
|
} |
|
|
|
|
|
el = list_first(rec->ringdef); |
|
x = (cmo_indeterminate *)el->cmo; |
|
|
|
break; |
|
} |
|
default: |
|
break; |
|
} |
|
return (NTL_SUCCESS); |
|
} |
} |
|
#endif |
|
|
/**************************************************************************** |
|
* |
|
* |
|
* |
|
* PARAM : I : arg : polynomial |
|
* : I : argc : |
|
* RETURN: [[num, 1],[factor1,multiplicity1],[factor2,multiplicity2],...] |
|
* |
|
* EX : |
|
* |
|
****************************************************************************/ |
|
static cmo_recursive_polynomial * |
|
ZZX_to_cmo(ZZX &factor, cmo_indeterminate *x) |
|
{ |
{ |
cmo_recursive_polynomial *rec; |
|
cmo_polynomial_in_one_variable *poly; |
ZZ cont; |
|
ZZX fac; |
|
vec_pair_ZZX_long facs; |
|
// char xx[2] = "x"; |
|
// cmo_indeterminate *x = new_cmo_indeterminate((cmo *)new_cmo_string(xx)); |
|
|
cmo_list *ringdef; |
// std::istringstream istr("[3 -3 -6]"); |
int i; |
// istr >> fac; |
cmo *coef; |
|
|
|
ringdef = new_cmo_list(); |
SetCoeff(fac, 0, 3); |
list_append(ringdef, (cmo *)x); |
SetCoeff(fac, 1, -3); |
|
SetCoeff(fac, 2, -6); |
|
|
poly = new_cmo_polynomial_in_one_variable(0); |
|
for (i = deg(factor); i >= 0; i--) { |
|
if (coeff(factor, i) == 0) |
|
continue; |
|
|
|
coef = (cmo *)ZZ_to_cmo_zz(coeff(factor, i)); |
factor(cont, facs, fac); |
list_append_monomial((cmo_list *)poly, coef, i); |
|
} |
|
|
|
rec = new_cmo_recursive_polynomial(ringdef, (cmo *)poly); |
// mpz_clear(ppp->mpz); |
return (rec); |
|
} |
} |
|
|
|
if (0) { |
|
mp_set_memory_functions(GC_malloc, ntl_realloc, ntl_free); |
|
for (int i = 0;; i++) { |
|
|
static cmo_list * |
// cmon_factors_t *mat = new_cmon_factors(cont, facs, x); |
new_cmo_pair_ZZX_int(ZZX &factors, int d, cmo_indeterminate *x) |
// cmo *aaa = convert_cmon((cmo *)mat); |
{ |
// delete_cmon((cmo *)mat); |
cmo_recursive_polynomial *poly; |
|
cmo_int32 *deg; |
|
cmo_list *list; |
|
|
|
poly = ZZX_to_cmo(factors, x); |
if (i % 1000 == 0) { |
deg = new_cmo_int32(d); |
printf("GC-counts: %lu, size: %u\n", GC_gc_no, GC_get_heap_size()); |
|
GC_gcollect(); |
list = list_appendl(NULL, poly, deg, NULL); |
usleep(1); |
|
} |
return (list); |
|
} |
|
|
|
|
|
/**************************************************************************** |
|
* |
|
* convert vec_pair_ZZX_long(list of factor and multiplicity) to cmo_list |
|
* |
|
* PARAM : I : factors : list of factor and multiplicity |
|
* : : : [[factor1,multiplicity1][factor2,multiplicity2]...] |
|
* : I : x : indeterminate |
|
* RETURN: |
|
* |
|
****************************************************************************/ |
|
static cmo_list * |
|
factors_to_cmo(vec_pair_ZZX_long &factors, cmo_indeterminate *x) |
|
{ |
|
int i; |
|
cmo_list *list = new_cmo_list(); |
|
cmo_list *factor; |
|
|
|
for (i = 0; i < factors.length(); i++) { |
|
factor = new_cmo_pair_ZZX_int(factors[i].a, factors[i].b, x); |
|
list_append(list, (cmo *)factor); |
|
} |
} |
|
|
return (list); |
|
} |
} |
|
|
|
|
/**************************************************************************** |
return (0); |
* |
|
* Factorize polynomial over the integers. |
|
* |
|
* PARAM : I : arg : polynomial |
|
* : I : argc : |
|
* RETURN: [num,[[factor1,multiplicity1],[factor2,multiplicity2],...]] |
|
* |
|
* EX : ntl_fctr([2*x^11+4*x^8-2*x^6+2*x^5-4*x^3-2],1) ==> |
|
: [2,[[x-1,1],[x^4+x^3+x^2+x+1,1],[x+1,2],[x^2-x+1,2]]] |
|
* |
|
****************************************************************************/ |
|
cmo * |
|
ntl_fctr(cmo **arg, int argc) |
|
{ |
|
cmo *poly = arg[0]; |
|
cmo_indeterminate *x; |
|
ZZX f; |
|
ZZ c; |
|
|
|
int ret; |
|
|
|
vec_pair_ZZX_long factors; |
|
|
|
cmo_list *ans; |
|
cmo *fcts; |
|
|
|
if (argc != 1) { |
|
return ((cmo *)new_cmo_error2((cmo *)new_cmo_string("Invalid Parameter(#)"))); |
|
} |
|
|
|
ret = cmo_to_ZZX(f, poly, x); |
|
if (ret != NTL_SUCCESS) { |
|
/* format error */ |
|
return ((cmo *)new_cmo_error2((cmo *)new_cmo_string("Invalid Parameter(type)"))); |
|
} |
|
|
|
factor(c, factors, f); |
|
|
|
cmo_zz *zz = ZZ_to_cmo_zz(c); |
|
|
|
ans = new_cmo_list(); |
|
|
|
fcts = (cmo *)factors_to_cmo(factors, x); |
|
|
|
list_appendl(ans, zz, (cmo *)fcts, NULL); |
|
|
|
return ((cmo *)ans); |
|
} |
} |
|
|
|
#endif /* __NTL_DEBUG @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ |
|
|