version 1.1, 2003/11/08 12:34:00 |
version 1.6, 2013/10/18 01:28:52 |
|
|
/* $OpenXM$ */ |
/* $OpenXM: OpenXM/src/ox_ntl/ntlconv.cpp,v 1.5 2003/11/27 14:19:50 iwane Exp $ */ |
|
|
#include <NTL/ZZX.h> |
#include <NTL/ZZX.h> |
|
#include <NTL/mat_ZZ.h> |
|
|
#include <strstream> |
#include <sstream> |
|
#include <string> |
|
#include <string.h> |
|
|
#include "ntl.h" |
#include "ntl.h" |
|
|
|
|
#endif |
#endif |
|
|
/*==========================================================================* |
/*==========================================================================* |
|
* Block interrupt input |
|
*==========================================================================*/ |
|
#define BLOCK_NEW_CMO() BLOCK_INPUT() |
|
#define UNBLOCK_NEW_CMO() UNBLOCK_INPUT() |
|
|
|
|
|
|
|
/*==========================================================================* |
* Check string format |
* Check string format |
*==========================================================================*/ |
*==========================================================================*/ |
|
|
|
|
ZZ_to_cmo_zz(const ZZ &z) |
ZZ_to_cmo_zz(const ZZ &z) |
{ |
{ |
cmo_zz *c; |
cmo_zz *c; |
|
const char *ptr; |
|
|
ostrstream sout; |
std::ostringstream sout; |
sout << z << '\0'; |
sout << z << '\0'; |
|
std::string tmp = sout.str(); |
|
ptr = tmp.c_str(); |
|
|
c = new_cmo_zz_set_string(sout.str()); |
BLOCK_NEW_CMO(); |
|
c = new_cmo_zz_set_string((char*)ptr); |
|
UNBLOCK_NEW_CMO(); |
|
|
return (c); |
return (c); |
} |
} |
|
|
Line 165 cmo_to_ZZ(ZZ &z, cmo *c) |
|
Line 181 cmo_to_ZZ(ZZ &z, cmo *c) |
|
|
|
switch (c->tag) { |
switch (c->tag) { |
case CMO_ZERO: |
case CMO_ZERO: |
|
case CMO_NULL: |
z = to_ZZ(0); |
z = to_ZZ(0); |
break; |
break; |
case CMO_ZZ: |
case CMO_ZZ: |
Line 202 cmo_to_ZZ(ZZ &z, cmo *c) |
|
Line 219 cmo_to_ZZ(ZZ &z, cmo *c) |
|
* RETURN: success : NTL_SUCCESS |
* RETURN: success : NTL_SUCCESS |
* : failure : NTL_FAILURE |
* : failure : NTL_FAILURE |
****************************************************************************/ |
****************************************************************************/ |
|
cmo_list * |
|
mat_zz_to_cmo(mat_ZZ &mat) |
|
{ |
|
cmo_list *list; |
|
|
|
cmo_zz *zz; |
|
int row, col; |
|
int i, j; |
|
|
|
row = (int)mat.NumRows(); |
|
col = (int)mat.NumCols(); |
|
|
|
BLOCK_NEW_CMO(); |
|
list = list_appendl(NULL, new_cmo_int32(row), new_cmo_int32(col), NULL); |
|
|
|
for (i = 0; i < row; i++) { |
|
for (j = 0; j < col; j++) { |
|
zz = ZZ_to_cmo_zz(mat[i][j]); |
|
list_append(list, (cmo *)zz); |
|
} |
|
} |
|
UNBLOCK_NEW_CMO(); |
|
|
|
return (list); |
|
} |
|
|
|
|
|
|
|
|
|
/**************************************************************************** |
|
* convert cmo to ZZX which is polynomial in Z[x] |
|
* |
|
* PARAM : O : f : polynomial in Z[x] |
|
* : I : m : cmo. |
|
* : O : x : indeterminate |
|
* RETURN: success : NTL_SUCCESS |
|
* : failure : NTL_FAILURE |
|
****************************************************************************/ |
int |
int |
|
cmo_to_mat_zz(mat_ZZ &mat, cmo *m) |
|
{ |
|
cmo_list *list; |
|
int ret; |
|
ZZ row, col, size; |
|
int len; |
|
cell *el; |
|
int i, j; |
|
int c, r; |
|
|
|
if (m->tag != CMO_LIST) { |
|
return (NTL_FAILURE); |
|
} |
|
|
|
list = (cmo_list *)m; |
|
len = list_length(list); |
|
|
|
if (len < 2) { |
|
return (NTL_FAILURE); |
|
} |
|
|
|
el = list_first(list); |
|
ret = cmo_to_ZZ(row, el->cmo); |
|
if (ret != NTL_SUCCESS) { |
|
return (ret); |
|
} |
|
|
|
el = list_next(el); |
|
ret = cmo_to_ZZ(col, el->cmo); |
|
if (ret != NTL_SUCCESS) { |
|
return (ret); |
|
} |
|
|
|
mul(size, row, col); |
|
|
|
if (len - 2 != size) { |
|
return (NTL_FAILURE); |
|
} |
|
|
|
/* row and col is less than INT_MAX */ |
|
r = to_int(row); |
|
c = to_int(col); |
|
mat.SetDims(r, c); |
|
for (i = 0; i < r; i++) { |
|
for (j = 0; j < c; j++) { |
|
el = list_next(el); |
|
ret = cmo_to_ZZ(mat[i][j], el->cmo); |
|
if (ret) { |
|
return (ret); |
|
} |
|
} |
|
} |
|
return (NTL_SUCCESS); |
|
} |
|
|
|
|
|
|
|
/**************************************************************************** |
|
* convert cmo to ZZX which is polynomial in Z[x] |
|
* |
|
* PARAM : O : f : polynomial in Z[x] |
|
* : I : m : cmo. |
|
* : O : x : indeterminate |
|
* RETURN: success : NTL_SUCCESS |
|
* : failure : NTL_FAILURE |
|
****************************************************************************/ |
|
int |
cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x) |
cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x) |
{ |
{ |
char *str; |
char *str; |
Line 218 cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x) |
|
Line 340 cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x) |
|
return (NTL_FAILURE); |
return (NTL_FAILURE); |
} |
} |
{ |
{ |
istrstream sin(str, strlen(str)); |
std::istringstream sin(str); |
sin >> f; |
sin >> f; |
} |
} |
break; |
break; |
Line 227 cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x) |
|
Line 349 cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x) |
|
cmo_recursive_polynomial *rec = (cmo_recursive_polynomial *)m; |
cmo_recursive_polynomial *rec = (cmo_recursive_polynomial *)m; |
cmo_polynomial_in_one_variable *poly = (cmo_polynomial_in_one_variable *)rec->coef; |
cmo_polynomial_in_one_variable *poly = (cmo_polynomial_in_one_variable *)rec->coef; |
cell *el; |
cell *el; |
int len; |
|
|
|
if (poly->tag != CMO_POLYNOMIAL_IN_ONE_VARIABLE) { |
if (poly->tag != CMO_POLYNOMIAL_IN_ONE_VARIABLE) { |
return (NTL_FAILURE); |
return (NTL_FAILURE); |
} |
} |
|
|
el = list_first((cmo_list *)poly); |
el = list_first((cmo_list *)poly); |
len = list_length((cmo_list *)poly); |
|
|
|
f = 0; |
f = 0; |
|
|
Line 265 cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x) |
|
Line 385 cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x) |
|
return (NTL_SUCCESS); |
return (NTL_SUCCESS); |
} |
} |
|
|
|
|
|
|
/**************************************************************************** |
/**************************************************************************** |
* convert polynomial in Z[x] to cmo_recursive_polynomial |
* convert polynomial in Z[x] to cmo_recursive_polynomial |
* |
* |
Line 282 ZZX_to_cmo(ZZX &factor, cmo_indeterminate *x) |
|
Line 404 ZZX_to_cmo(ZZX &factor, cmo_indeterminate *x) |
|
int i; |
int i; |
cmo *coef; |
cmo *coef; |
|
|
|
BLOCK_NEW_CMO(); |
|
|
ringdef = new_cmo_list(); |
ringdef = new_cmo_list(); |
list_append(ringdef, (cmo *)x); |
list_append(ringdef, (cmo *)x); |
|
|
Line 295 ZZX_to_cmo(ZZX &factor, cmo_indeterminate *x) |
|
Line 419 ZZX_to_cmo(ZZX &factor, cmo_indeterminate *x) |
|
} |
} |
|
|
rec = new_cmo_recursive_polynomial(ringdef, (cmo *)poly); |
rec = new_cmo_recursive_polynomial(ringdef, (cmo *)poly); |
|
|
|
UNBLOCK_NEW_CMO(); |
|
|
return (rec); |
return (rec); |
} |
} |
|
|
Line 315 ZZX_int_to_cmo(ZZX &factor, int d, cmo_indeterminate * |
|
Line 442 ZZX_int_to_cmo(ZZX &factor, int d, cmo_indeterminate * |
|
cmo_list *list; |
cmo_list *list; |
|
|
poly = ZZX_to_cmo(factor, x); |
poly = ZZX_to_cmo(factor, x); |
deg = new_cmo_int32(d); |
|
|
|
|
BLOCK_NEW_CMO(); |
|
|
|
deg = new_cmo_int32(d); |
list = list_appendl(NULL, poly, deg, NULL); |
list = list_appendl(NULL, poly, deg, NULL); |
|
|
|
UNBLOCK_NEW_CMO(); |
|
|
return (list); |
return (list); |
} |
} |
|
|
|
|
vec_pair_ZZX_long_to_cmo(vec_pair_ZZX_long &factors, cmo_indeterminate *x) |
vec_pair_ZZX_long_to_cmo(vec_pair_ZZX_long &factors, cmo_indeterminate *x) |
{ |
{ |
int i; |
int i; |
cmo_list *list = new_cmo_list(); |
cmo_list *list; |
cmo_list *factor; |
cmo_list *factor; |
|
|
|
BLOCK_NEW_CMO(); |
|
|
|
list = new_cmo_list(); |
for (i = 0; i < factors.length(); i++) { |
for (i = 0; i < factors.length(); i++) { |
factor = ZZX_int_to_cmo(factors[i].a, (int)factors[i].b, x); |
factor = ZZX_int_to_cmo(factors[i].a, (int)factors[i].b, x); |
list_append(list, (cmo *)factor); |
list_append(list, (cmo *)factor); |
} |
} |
|
|
|
UNBLOCK_NEW_CMO(); |
|
|
return (list); |
return (list); |
} |
} |
|
|
|
|
|
/**************************************************************************** |
|
* convert local object to cmo. |
|
* for SM_popCMO |
|
* |
|
* PARAM : I : p : cmo |
|
* RETURN: cmo |
|
****************************************************************************/ |
|
cmo * |
|
convert_cmon(cmo *p) |
|
{ |
|
switch (p->tag) { |
|
case CMON_ZZ: |
|
{ |
|
cmon_zz_t *z = (cmon_zz_t *)p; |
|
return ((cmo *)ZZ_to_cmo_zz(*z->z)); |
|
} |
|
case CMON_ZZX: |
|
{ |
|
cmon_zzx_t *f = (cmon_zzx_t *)p; |
|
return ((cmo *)ZZX_to_cmo(*f->f, f->x)); |
|
} |
|
case CMON_FACTORS: |
|
{ |
|
cmon_factors_t *f = (cmon_factors_t *)p; |
|
cmo_zz *z = ZZ_to_cmo_zz(*f->cont); |
|
cmo_list *list = vec_pair_ZZX_long_to_cmo(*f->f, f->x); |
|
return ((cmo *)list_appendl(NULL, (cmo *)z, list, NULL)); |
|
} |
|
case CMON_MAT_ZZ: |
|
{ |
|
cmon_mat_zz_t *m = (cmon_mat_zz_t *)p; |
|
cmo_list *list = mat_zz_to_cmo(*m->mat); |
|
return ((cmo *)list); |
|
} |
|
default: |
|
return (p); |
|
} |
|
} |
|
|
|
|
|
|
|
/**************************************************************************** |
|
* convert tag of local object to tag of cmo. |
|
* for SM_pushCMOtag |
|
* |
|
* PARAM : I : p : cmo |
|
* RETURN: tag |
|
****************************************************************************/ |
|
int |
|
get_cmon_tag(cmo *p) |
|
{ |
|
switch (p->tag) { |
|
case CMON_ZZ: |
|
return (CMO_ZZ); |
|
case CMON_ZZX: |
|
return (CMO_RECURSIVE_POLYNOMIAL); |
|
case CMON_FACTORS: |
|
return (CMO_LIST); |
|
case CMON_MAT_ZZ: |
|
return (CMON_MAT_ZZ); |
|
default: |
|
return (p->tag); |
|
} |
|
} |
|
|
|
|
|
|
|
|