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

Annotation of OpenXM/src/ox_ntl/ntlconv.cpp, Revision 1.1

1.1     ! iwane       1: /* $OpenXM$ */
        !             2:
        !             3: #include <NTL/ZZX.h>
        !             4:
        !             5: #include <strstream>
        !             6:
        !             7: #include "ntl.h"
        !             8:
        !             9: #if __NTL_DEBUG
        !            10: #define __NTL_PRINT (1)
        !            11: #endif
        !            12:
        !            13: /*==========================================================================*
        !            14:  * Check string format
        !            15:  *==========================================================================*/
        !            16:
        !            17: #define NtlIsSpace(c)  ((c) == ' ' || (c) == '\t')
        !            18: #define NtlIsDigit(c)  ((c) >= '0' && (c) <= '9')
        !            19:
        !            20: /****************************************************************************
        !            21:  *
        !            22:  * test for string format of integer
        !            23:  *
        !            24:  * PARAM : I : str    : string
        !            25:  *       : O : endptr :
        !            26:  * RETURN: !0         : the string tests true
        !            27:  *       :  0         : the string tests false
        !            28:  *
        !            29:  ****************************************************************************/
        !            30: static int
        !            31: ntl_isZZstr_(const char *str, char const **endptr)
        !            32: {
        !            33:        while (NtlIsSpace(*str))
        !            34:                str++;
        !            35:
        !            36:        /* NTL reject "+999"  */
        !            37:        if (*str == '-')
        !            38:                str++;
        !            39:
        !            40:        if (!NtlIsDigit(*str))
        !            41:                return (0);
        !            42:
        !            43:        str++;
        !            44:
        !            45:        while (NtlIsDigit(*str))
        !            46:                str++;
        !            47:
        !            48:        *endptr = str;
        !            49:
        !            50:        return (!0);
        !            51:
        !            52: }
        !            53:
        !            54: static int
        !            55: ntl_isZZstr(const char *str)
        !            56: {
        !            57:        const char *ptr;
        !            58:        int ret = ntl_isZZstr_(str, &ptr);
        !            59:        if (!ret)
        !            60:                return (ret);
        !            61:
        !            62:        while (NtlIsSpace(*ptr))
        !            63:                ptr++;
        !            64:
        !            65:        return (*ptr == '\0');
        !            66: }
        !            67:
        !            68:
        !            69: /****************************************************************************
        !            70:  *
        !            71:  * test for string format of univariate polynomials with integer coefficients
        !            72:  * in NTL style.
        !            73:  *
        !            74:  * PARAM : I : str    : string
        !            75:  *       : O : endptr :
        !            76:  * RETURN: !0         : the string tests true
        !            77:  *       :  0         : the string tests false
        !            78:  *
        !            79:  ****************************************************************************/
        !            80: static int
        !            81: ntl_isZZXstr_(const char *str, char const **endptr)
        !            82: {
        !            83:        const char *s;
        !            84:
        !            85:        while (NtlIsSpace(*str))
        !            86:                str++;
        !            87:
        !            88:        if (*str != '[')
        !            89:                return (0);
        !            90:
        !            91:        str++;
        !            92:
        !            93:        while (*str != ']' && *str != '\0') {
        !            94:
        !            95:                if (!ntl_isZZstr_(str, &s))
        !            96:                        return (0);
        !            97:                str = s;
        !            98:
        !            99:                while (NtlIsSpace(*str))
        !           100:                        str++;
        !           101:        }
        !           102:
        !           103:        while (NtlIsSpace(*str))
        !           104:                str++;
        !           105:
        !           106:        if (*str != ']')
        !           107:                return (0);
        !           108:
        !           109:        str++;
        !           110:        *endptr = str;
        !           111:        return (!0);
        !           112: }
        !           113:
        !           114: static int
        !           115: ntl_isZZXstr(const char *str)
        !           116: {
        !           117:        const char *ptr;
        !           118:        int ret = ntl_isZZXstr_(str, &ptr);
        !           119:        if (!ret)
        !           120:                return (ret);
        !           121:
        !           122:        while (NtlIsSpace(*ptr))
        !           123:                ptr++;
        !           124:
        !           125:        return (*ptr == '\0');
        !           126: }
        !           127:
        !           128:
        !           129: /*==========================================================================*
        !           130:  * Convert
        !           131:  *==========================================================================*/
        !           132: /****************************************************************************
        !           133:  * convert ZZ to cmo_zz
        !           134:  *
        !           135:  * PARAM : I : z       : integer
        !           136:  * RETURN: cmo_zz
        !           137:  ****************************************************************************/
        !           138: cmo_zz *
        !           139: ZZ_to_cmo_zz(const ZZ &z)
        !           140: {
        !           141:        cmo_zz *c;
        !           142:
        !           143:        ostrstream sout;
        !           144:        sout << z << '\0';
        !           145:
        !           146:        c = new_cmo_zz_set_string(sout.str());
        !           147:
        !           148:        return (c);
        !           149: }
        !           150:
        !           151:
        !           152: /****************************************************************************
        !           153:  * convert cmo to ZZ which is integer
        !           154:  *
        !           155:  * PARAM : O : z       : integer.
        !           156:  *       : I : c       : cmo.
        !           157:  * RETURN: success     : NTL_SUCCESS
        !           158:  *       : failure     : NTL_FAILURE
        !           159:  ****************************************************************************/
        !           160: int
        !           161: cmo_to_ZZ(ZZ &z, cmo *c)
        !           162: {
        !           163:        int ret = NTL_SUCCESS;
        !           164:        char *str;
        !           165:
        !           166:        switch (c->tag) {
        !           167:        case CMO_ZERO:
        !           168:                z = to_ZZ(0);
        !           169:                break;
        !           170:        case CMO_ZZ:
        !           171:        {
        !           172:                str = new_string_set_cmo(c);
        !           173:                z = to_ZZ(str);
        !           174:                break;
        !           175:        }
        !           176:        case CMO_INT32:
        !           177:                z = to_ZZ(((cmo_int32 *)c)->i);
        !           178:                break;
        !           179:        case CMO_STRING:
        !           180:        {
        !           181:                str = ((cmo_string *)c)->s;
        !           182:                if (!ntl_isZZstr(str))
        !           183:                        return (NTL_FAILURE);
        !           184:                z = to_ZZ(str);
        !           185:                break;
        !           186:        }
        !           187:        default:
        !           188:                ret = NTL_FAILURE;
        !           189:                break;
        !           190:        }
        !           191:
        !           192:        return (ret);
        !           193: }
        !           194:
        !           195:
        !           196: /****************************************************************************
        !           197:  * convert cmo to ZZX which is polynomial in Z[x]
        !           198:  *
        !           199:  * PARAM : O : f       : polynomial in Z[x]
        !           200:  *       : I : m       : cmo.
        !           201:  *       : O : x       : indeterminate
        !           202:  * RETURN: success     : NTL_SUCCESS
        !           203:  *       : failure     : NTL_FAILURE
        !           204:  ****************************************************************************/
        !           205: int
        !           206: cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x)
        !           207: {
        !           208:        char *str;
        !           209:        int ret;
        !           210:
        !           211:        switch (m->tag) {
        !           212:        case CMO_STRING:   /* [ 3 4 7 ] ==> 3+4*x+7*x^2 */
        !           213:                str = ((cmo_string *)m)->s;
        !           214:                ret = ntl_isZZXstr(str);
        !           215:
        !           216:                if (!ret) {
        !           217:                        /* format error */
        !           218:                        return (NTL_FAILURE);
        !           219:                }
        !           220:                {
        !           221:                        istrstream sin(str, strlen(str));
        !           222:                        sin >> f;
        !           223:                }
        !           224:                break;
        !           225:        case CMO_RECURSIVE_POLYNOMIAL:
        !           226:        {
        !           227:                cmo_recursive_polynomial *rec = (cmo_recursive_polynomial *)m;
        !           228:                cmo_polynomial_in_one_variable *poly = (cmo_polynomial_in_one_variable *)rec->coef;
        !           229:                cell *el;
        !           230:                int len;
        !           231:
        !           232:                if (poly->tag != CMO_POLYNOMIAL_IN_ONE_VARIABLE) {
        !           233:                        return (NTL_FAILURE);
        !           234:                }
        !           235:
        !           236:                el = list_first((cmo_list *)poly);
        !           237:                len = list_length((cmo_list *)poly);
        !           238:
        !           239:                f = 0;
        !           240:
        !           241:                while (!list_endof((cmo_list *)poly, el)) {
        !           242:                        ZZ c;
        !           243:                        cmo *coef = el->cmo;
        !           244:                        int exp = el->exp;
        !           245:
        !           246:                        ret = cmo_to_ZZ(c, coef);
        !           247:                        if (ret != NTL_SUCCESS) {
        !           248:                                return (NTL_FAILURE);
        !           249:                        }
        !           250:
        !           251:                        SetCoeff(f, exp, c);
        !           252:
        !           253:                        el = list_next(el);
        !           254:                }
        !           255:
        !           256:
        !           257:                el = list_first(rec->ringdef);
        !           258:                x = (cmo_indeterminate *)el->cmo;
        !           259:
        !           260:                break;
        !           261:        }
        !           262:        default:
        !           263:                break;
        !           264:        }
        !           265:        return (NTL_SUCCESS);
        !           266: }
        !           267:
        !           268: /****************************************************************************
        !           269:  * convert polynomial in Z[x] to cmo_recursive_polynomial
        !           270:  *
        !           271:  * PARAM : I : factor  : polynomial in Z[x]
        !           272:  *       : I : x       : indeterminate
        !           273:  * RETURN:
        !           274:  ****************************************************************************/
        !           275: cmo_recursive_polynomial *
        !           276: ZZX_to_cmo(ZZX &factor, cmo_indeterminate *x)
        !           277: {
        !           278:        cmo_recursive_polynomial *rec;
        !           279:        cmo_polynomial_in_one_variable *poly;
        !           280:
        !           281:        cmo_list *ringdef;
        !           282:        int i;
        !           283:        cmo *coef;
        !           284:
        !           285:        ringdef = new_cmo_list();
        !           286:        list_append(ringdef, (cmo *)x);
        !           287:
        !           288:        poly = new_cmo_polynomial_in_one_variable(0);
        !           289:        for (i = deg(factor); i >= 0; i--) {
        !           290:                if (coeff(factor, i) == 0)
        !           291:                        continue;
        !           292:
        !           293:                coef = (cmo *)ZZ_to_cmo_zz(coeff(factor, i));
        !           294:                list_append_monomial((cmo_list *)poly, coef, i);
        !           295:        }
        !           296:
        !           297:        rec = new_cmo_recursive_polynomial(ringdef, (cmo *)poly);
        !           298:        return (rec);
        !           299: }
        !           300:
        !           301:
        !           302: /****************************************************************************
        !           303:  * convert pair of factor and multiplicity to cmo_list
        !           304:  *
        !           305:  * PARAM : I : factor  : polynomial in Z[x]
        !           306:  *       : I : d       : multiplicity
        !           307:  *       : I : x       : indeterminate
        !           308:  * RETURN:
        !           309:  ****************************************************************************/
        !           310: cmo_list *
        !           311: ZZX_int_to_cmo(ZZX &factor, int d, cmo_indeterminate *x)
        !           312: {
        !           313:        cmo_recursive_polynomial *poly;
        !           314:        cmo_int32 *deg;
        !           315:        cmo_list *list;
        !           316:
        !           317:        poly = ZZX_to_cmo(factor, x);
        !           318:        deg = new_cmo_int32(d);
        !           319:
        !           320:        list = list_appendl(NULL, poly, deg, NULL);
        !           321:
        !           322:        return (list);
        !           323: }
        !           324:
        !           325:
        !           326: /****************************************************************************
        !           327:  * convert vec_pair_ZZX_long(list which pair of factor and multiplicity)
        !           328:  * to cmo_list
        !           329:  *
        !           330:  * PARAM : I : factors : list which pair of factor and multiplicity
        !           331:  *       :   :         :  [[factor1,multiplicity1][factor2,multiplicity2]...]
        !           332:  *       : I : x       : indeterminate
        !           333:  * RETURN:
        !           334:  ****************************************************************************/
        !           335: cmo_list *
        !           336: vec_pair_ZZX_long_to_cmo(vec_pair_ZZX_long &factors, cmo_indeterminate *x)
        !           337: {
        !           338:        int i;
        !           339:        cmo_list *list = new_cmo_list();
        !           340:        cmo_list *factor;
        !           341:
        !           342:        for (i = 0; i < factors.length(); i++) {
        !           343:                factor = ZZX_int_to_cmo(factors[i].a, (int)factors[i].b, x);
        !           344:                list_append(list, (cmo *)factor);
        !           345:        }
        !           346:
        !           347:        return (list);
        !           348: }
        !           349:
        !           350:

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