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

1.5     ! iwane       1: /* $OpenXM: OpenXM/src/ox_ntl/ntlconv.cpp,v 1.4 2003/11/17 12:04:20 iwane Exp $ */
1.1       iwane       2:
                      3: #include <NTL/ZZX.h>
1.2       iwane       4: #include <NTL/mat_ZZ.h>
1.1       iwane       5:
                      6: #include <strstream>
                      7:
                      8: #include "ntl.h"
                      9:
                     10: #if __NTL_DEBUG
                     11: #define __NTL_PRINT (1)
                     12: #endif
                     13:
1.3       iwane      14: /*==========================================================================*
                     15:  * Block interrupt input
                     16:  *==========================================================================*/
1.2       iwane      17: #define BLOCK_NEW_CMO()         BLOCK_INPUT()
                     18: #define UNBLOCK_NEW_CMO()       UNBLOCK_INPUT()
                     19:
                     20:
                     21:
1.1       iwane      22: /*==========================================================================*
                     23:  * Check string format
                     24:  *==========================================================================*/
                     25:
                     26: #define NtlIsSpace(c)  ((c) == ' ' || (c) == '\t')
                     27: #define NtlIsDigit(c)  ((c) >= '0' && (c) <= '9')
                     28:
                     29: /****************************************************************************
                     30:  *
                     31:  * test for string format of integer
                     32:  *
                     33:  * PARAM : I : str    : string
                     34:  *       : O : endptr :
                     35:  * RETURN: !0         : the string tests true
                     36:  *       :  0         : the string tests false
                     37:  *
                     38:  ****************************************************************************/
                     39: static int
                     40: ntl_isZZstr_(const char *str, char const **endptr)
                     41: {
                     42:        while (NtlIsSpace(*str))
                     43:                str++;
                     44:
                     45:        /* NTL reject "+999"  */
                     46:        if (*str == '-')
                     47:                str++;
                     48:
                     49:        if (!NtlIsDigit(*str))
                     50:                return (0);
                     51:
                     52:        str++;
                     53:
                     54:        while (NtlIsDigit(*str))
                     55:                str++;
                     56:
                     57:        *endptr = str;
                     58:
                     59:        return (!0);
                     60:
                     61: }
                     62:
                     63: static int
                     64: ntl_isZZstr(const char *str)
                     65: {
                     66:        const char *ptr;
                     67:        int ret = ntl_isZZstr_(str, &ptr);
                     68:        if (!ret)
                     69:                return (ret);
                     70:
                     71:        while (NtlIsSpace(*ptr))
                     72:                ptr++;
                     73:
                     74:        return (*ptr == '\0');
                     75: }
                     76:
                     77:
                     78: /****************************************************************************
                     79:  *
                     80:  * test for string format of univariate polynomials with integer coefficients
                     81:  * in NTL style.
                     82:  *
                     83:  * PARAM : I : str    : string
                     84:  *       : O : endptr :
                     85:  * RETURN: !0         : the string tests true
                     86:  *       :  0         : the string tests false
                     87:  *
                     88:  ****************************************************************************/
                     89: static int
                     90: ntl_isZZXstr_(const char *str, char const **endptr)
                     91: {
                     92:        const char *s;
                     93:
                     94:        while (NtlIsSpace(*str))
                     95:                str++;
                     96:
                     97:        if (*str != '[')
                     98:                return (0);
                     99:
                    100:        str++;
                    101:
                    102:        while (*str != ']' && *str != '\0') {
                    103:
                    104:                if (!ntl_isZZstr_(str, &s))
                    105:                        return (0);
                    106:                str = s;
                    107:
                    108:                while (NtlIsSpace(*str))
                    109:                        str++;
                    110:        }
                    111:
                    112:        while (NtlIsSpace(*str))
                    113:                str++;
                    114:
                    115:        if (*str != ']')
                    116:                return (0);
                    117:
                    118:        str++;
                    119:        *endptr = str;
                    120:        return (!0);
                    121: }
                    122:
                    123: static int
                    124: ntl_isZZXstr(const char *str)
                    125: {
                    126:        const char *ptr;
                    127:        int ret = ntl_isZZXstr_(str, &ptr);
                    128:        if (!ret)
                    129:                return (ret);
                    130:
                    131:        while (NtlIsSpace(*ptr))
                    132:                ptr++;
                    133:
                    134:        return (*ptr == '\0');
                    135: }
                    136:
                    137:
                    138: /*==========================================================================*
                    139:  * Convert
                    140:  *==========================================================================*/
                    141: /****************************************************************************
                    142:  * convert ZZ to cmo_zz
                    143:  *
                    144:  * PARAM : I : z       : integer
                    145:  * RETURN: cmo_zz
                    146:  ****************************************************************************/
                    147: cmo_zz *
                    148: ZZ_to_cmo_zz(const ZZ &z)
                    149: {
                    150:        cmo_zz *c;
1.3       iwane     151:        char *ptr;
1.1       iwane     152:
1.4       iwane     153:        std::ostrstream sout;
1.1       iwane     154:        sout << z << '\0';
1.3       iwane     155:        ptr = sout.str();
1.1       iwane     156:
1.2       iwane     157:        BLOCK_NEW_CMO();
1.3       iwane     158:        c = new_cmo_zz_set_string(ptr);
1.2       iwane     159:        UNBLOCK_NEW_CMO();
1.3       iwane     160:
                    161:        delete [] ptr;
1.1       iwane     162:
                    163:        return (c);
                    164: }
                    165:
                    166:
                    167: /****************************************************************************
                    168:  * convert cmo to ZZ which is integer
                    169:  *
                    170:  * PARAM : O : z       : integer.
                    171:  *       : I : c       : cmo.
                    172:  * RETURN: success     : NTL_SUCCESS
                    173:  *       : failure     : NTL_FAILURE
                    174:  ****************************************************************************/
                    175: int
                    176: cmo_to_ZZ(ZZ &z, cmo *c)
                    177: {
                    178:        int ret = NTL_SUCCESS;
                    179:        char *str;
                    180:
                    181:        switch (c->tag) {
                    182:        case CMO_ZERO:
1.2       iwane     183:        case CMO_NULL:
1.1       iwane     184:                z = to_ZZ(0);
                    185:                break;
                    186:        case CMO_ZZ:
                    187:        {
                    188:                str = new_string_set_cmo(c);
                    189:                z = to_ZZ(str);
                    190:                break;
                    191:        }
                    192:        case CMO_INT32:
                    193:                z = to_ZZ(((cmo_int32 *)c)->i);
                    194:                break;
                    195:        case CMO_STRING:
                    196:        {
                    197:                str = ((cmo_string *)c)->s;
                    198:                if (!ntl_isZZstr(str))
                    199:                        return (NTL_FAILURE);
                    200:                z = to_ZZ(str);
                    201:                break;
                    202:        }
                    203:        default:
                    204:                ret = NTL_FAILURE;
                    205:                break;
                    206:        }
                    207:
                    208:        return (ret);
                    209: }
                    210:
                    211:
                    212: /****************************************************************************
                    213:  * convert cmo to ZZX which is polynomial in Z[x]
                    214:  *
                    215:  * PARAM : O : f       : polynomial in Z[x]
                    216:  *       : I : m       : cmo.
                    217:  *       : O : x       : indeterminate
                    218:  * RETURN: success     : NTL_SUCCESS
                    219:  *       : failure     : NTL_FAILURE
                    220:  ****************************************************************************/
1.2       iwane     221: cmo_list *
                    222: mat_zz_to_cmo(mat_ZZ &mat)
                    223: {
                    224:        cmo_list *list;
                    225:
                    226:        cmo_zz *zz;
                    227:        int row, col;
                    228:        int i, j;
                    229:
                    230:        row = (int)mat.NumRows();
                    231:        col = (int)mat.NumCols();
                    232:
                    233:        BLOCK_NEW_CMO();
                    234:        list = list_appendl(NULL, new_cmo_int32(row), new_cmo_int32(col), NULL);
                    235:
                    236:        for (i = 0; i < row; i++) {
                    237:                for (j = 0; j < col; j++) {
                    238:                        zz = ZZ_to_cmo_zz(mat[i][j]);
                    239:                        list_append(list, (cmo *)zz);
                    240:                }
                    241:        }
                    242:        UNBLOCK_NEW_CMO();
                    243:
                    244:        return (list);
                    245: }
                    246:
                    247:
                    248:
                    249:
                    250: /****************************************************************************
                    251:  * convert cmo to ZZX which is polynomial in Z[x]
                    252:  *
                    253:  * PARAM : O : f       : polynomial in Z[x]
                    254:  *       : I : m       : cmo.
                    255:  *       : O : x       : indeterminate
                    256:  * RETURN: success     : NTL_SUCCESS
                    257:  *       : failure     : NTL_FAILURE
                    258:  ****************************************************************************/
                    259: int
                    260: cmo_to_mat_zz(mat_ZZ &mat, cmo *m)
                    261: {
                    262:        cmo_list *list;
                    263:        int ret;
                    264:        ZZ row, col, size;
                    265:        int len;
                    266:        cell *el;
                    267:        int i, j;
                    268:        int c, r;
                    269:
                    270:        if (m->tag != CMO_LIST) {
                    271:                return (NTL_FAILURE);
                    272:        }
                    273:
                    274:        list = (cmo_list *)m;
                    275:        len = list_length(list);
                    276:
                    277:        if (len < 2) {
                    278:                return (NTL_FAILURE);
                    279:        }
                    280:
                    281:        el = list_first(list);
                    282:        ret = cmo_to_ZZ(row, el->cmo);
                    283:        if (ret != NTL_SUCCESS) {
                    284:                return (ret);
                    285:        }
                    286:
                    287:        el = list_next(el);
                    288:        ret = cmo_to_ZZ(col, el->cmo);
                    289:        if (ret != NTL_SUCCESS) {
                    290:                return (ret);
                    291:        }
                    292:
                    293:        mul(size, row, col);
                    294:
                    295:        if (len - 2 != size) {
                    296:                return (NTL_FAILURE);
                    297:        }
                    298:
                    299:        /* row and col is less than INT_MAX */
                    300:        r = to_int(row);
                    301:        c = to_int(col);
                    302:        mat.SetDims(r, c);
                    303:        for (i = 0; i < r; i++) {
                    304:                for (j = 0; j < c; j++) {
                    305:                        el = list_next(el);
                    306:                        ret = cmo_to_ZZ(mat[i][j], el->cmo);
                    307:                        if (ret) {
                    308:                                return (ret);
                    309:                        }
                    310:                }
                    311:        }
                    312:        return (NTL_SUCCESS);
                    313: }
                    314:
                    315:
                    316:
                    317: /****************************************************************************
                    318:  * convert cmo to ZZX which is polynomial in Z[x]
                    319:  *
                    320:  * PARAM : O : f       : polynomial in Z[x]
                    321:  *       : I : m       : cmo.
                    322:  *       : O : x       : indeterminate
                    323:  * RETURN: success     : NTL_SUCCESS
                    324:  *       : failure     : NTL_FAILURE
                    325:  ****************************************************************************/
1.1       iwane     326: int
                    327: cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x)
                    328: {
                    329:        char *str;
                    330:        int ret;
                    331:
                    332:        switch (m->tag) {
                    333:        case CMO_STRING:   /* [ 3 4 7 ] ==> 3+4*x+7*x^2 */
                    334:                str = ((cmo_string *)m)->s;
                    335:                ret = ntl_isZZXstr(str);
                    336:
                    337:                if (!ret) {
                    338:                        /* format error */
                    339:                        return (NTL_FAILURE);
                    340:                }
                    341:                {
1.4       iwane     342:                        std::istrstream sin(str, strlen(str));
1.1       iwane     343:                        sin >> f;
                    344:                }
                    345:                break;
                    346:        case CMO_RECURSIVE_POLYNOMIAL:
                    347:        {
                    348:                cmo_recursive_polynomial *rec = (cmo_recursive_polynomial *)m;
                    349:                cmo_polynomial_in_one_variable *poly = (cmo_polynomial_in_one_variable *)rec->coef;
                    350:                cell *el;
                    351:                int len;
                    352:
                    353:                if (poly->tag != CMO_POLYNOMIAL_IN_ONE_VARIABLE) {
                    354:                        return (NTL_FAILURE);
                    355:                }
                    356:
                    357:                el = list_first((cmo_list *)poly);
                    358:                len = list_length((cmo_list *)poly);
                    359:
                    360:                f = 0;
                    361:
                    362:                while (!list_endof((cmo_list *)poly, el)) {
                    363:                        ZZ c;
                    364:                        cmo *coef = el->cmo;
                    365:                        int exp = el->exp;
                    366:
                    367:                        ret = cmo_to_ZZ(c, coef);
                    368:                        if (ret != NTL_SUCCESS) {
                    369:                                return (NTL_FAILURE);
                    370:                        }
                    371:
                    372:                        SetCoeff(f, exp, c);
                    373:
                    374:                        el = list_next(el);
                    375:                }
                    376:
                    377:
                    378:                el = list_first(rec->ringdef);
                    379:                x = (cmo_indeterminate *)el->cmo;
                    380:
                    381:                break;
                    382:        }
                    383:        default:
                    384:                break;
                    385:        }
                    386:        return (NTL_SUCCESS);
                    387: }
1.5     ! iwane     388:
        !           389:
1.1       iwane     390:
                    391: /****************************************************************************
                    392:  * convert polynomial in Z[x] to cmo_recursive_polynomial
                    393:  *
                    394:  * PARAM : I : factor  : polynomial in Z[x]
                    395:  *       : I : x       : indeterminate
                    396:  * RETURN:
                    397:  ****************************************************************************/
                    398: cmo_recursive_polynomial *
                    399: ZZX_to_cmo(ZZX &factor, cmo_indeterminate *x)
                    400: {
                    401:        cmo_recursive_polynomial *rec;
                    402:        cmo_polynomial_in_one_variable *poly;
                    403:
                    404:        cmo_list *ringdef;
                    405:        int i;
                    406:        cmo *coef;
                    407:
1.2       iwane     408:        BLOCK_NEW_CMO();
                    409:
1.1       iwane     410:        ringdef = new_cmo_list();
                    411:        list_append(ringdef, (cmo *)x);
                    412:
                    413:        poly = new_cmo_polynomial_in_one_variable(0);
                    414:        for (i = deg(factor); i >= 0; i--) {
                    415:                if (coeff(factor, i) == 0)
                    416:                        continue;
                    417:
                    418:                coef = (cmo *)ZZ_to_cmo_zz(coeff(factor, i));
                    419:                list_append_monomial((cmo_list *)poly, coef, i);
                    420:        }
                    421:
                    422:        rec = new_cmo_recursive_polynomial(ringdef, (cmo *)poly);
1.2       iwane     423:
                    424:        UNBLOCK_NEW_CMO();
                    425:
1.1       iwane     426:        return (rec);
                    427: }
                    428:
                    429:
                    430: /****************************************************************************
                    431:  * convert pair of factor and multiplicity to cmo_list
                    432:  *
                    433:  * PARAM : I : factor  : polynomial in Z[x]
                    434:  *       : I : d       : multiplicity
                    435:  *       : I : x       : indeterminate
                    436:  * RETURN:
                    437:  ****************************************************************************/
                    438: cmo_list *
                    439: ZZX_int_to_cmo(ZZX &factor, int d, cmo_indeterminate *x)
                    440: {
                    441:        cmo_recursive_polynomial *poly;
                    442:        cmo_int32 *deg;
                    443:        cmo_list *list;
                    444:
                    445:        poly = ZZX_to_cmo(factor, x);
1.2       iwane     446:
                    447:        BLOCK_NEW_CMO();
                    448:
1.1       iwane     449:        deg = new_cmo_int32(d);
1.2       iwane     450:        list = list_appendl(NULL, poly, deg, NULL);
1.1       iwane     451:
1.2       iwane     452:        UNBLOCK_NEW_CMO();
1.1       iwane     453:
                    454:        return (list);
                    455: }
                    456:
                    457:
                    458: /****************************************************************************
                    459:  * convert vec_pair_ZZX_long(list which pair of factor and multiplicity)
                    460:  * to cmo_list
                    461:  *
                    462:  * PARAM : I : factors : list which pair of factor and multiplicity
                    463:  *       :   :         :  [[factor1,multiplicity1][factor2,multiplicity2]...]
                    464:  *       : I : x       : indeterminate
                    465:  * RETURN:
                    466:  ****************************************************************************/
                    467: cmo_list *
                    468: vec_pair_ZZX_long_to_cmo(vec_pair_ZZX_long &factors, cmo_indeterminate *x)
                    469: {
                    470:        int i;
1.2       iwane     471:        cmo_list *list;
1.1       iwane     472:        cmo_list *factor;
                    473:
1.2       iwane     474:        BLOCK_NEW_CMO();
                    475:
                    476:        list = new_cmo_list();
1.1       iwane     477:        for (i = 0; i < factors.length(); i++) {
                    478:                factor = ZZX_int_to_cmo(factors[i].a, (int)factors[i].b, x);
                    479:                list_append(list, (cmo *)factor);
                    480:        }
                    481:
1.2       iwane     482:        UNBLOCK_NEW_CMO();
                    483:
1.1       iwane     484:        return (list);
                    485: }
1.2       iwane     486:
                    487:
                    488: /****************************************************************************
                    489:  * convert local object to cmo.
                    490:  * for SM_popCMO
                    491:  *
                    492:  * PARAM : I : p : cmo
                    493:  * RETURN: cmo
                    494:  ****************************************************************************/
                    495: cmo *
                    496: convert_cmon(cmo *p)
                    497: {
                    498:
                    499:        switch (p->tag) {
                    500:        case CMON_ZZ:
                    501:        {
                    502:                cmon_zz_t *z = (cmon_zz_t *)p;
                    503:                return ((cmo *)ZZ_to_cmo_zz(*z->z));
                    504:        }
                    505:        case CMON_ZZX:
                    506:        {
                    507:                cmon_zzx_t *f = (cmon_zzx_t *)p;
                    508:                return ((cmo *)ZZX_to_cmo(*f->f, f->x));
                    509:        }
                    510:        case CMON_FACTORS:
                    511:        {
                    512:                cmon_factors_t *f = (cmon_factors_t *)p;
                    513:                cmo_zz *z = ZZ_to_cmo_zz(*f->cont);
                    514:                cmo_list *list = vec_pair_ZZX_long_to_cmo(*f->f, f->x);
                    515:                return ((cmo *)list_appendl(NULL, (cmo *)z, list, NULL));
                    516:        }
                    517:        case CMON_MAT_ZZ:
                    518:        {
                    519:                cmon_mat_zz_t *m = (cmon_mat_zz_t *)p;
                    520:                cmo_list *list = mat_zz_to_cmo(*m->mat);
                    521:                return ((cmo *)list);
                    522:        }
                    523:        default:
                    524:                return (p);
                    525:        }
                    526: }
                    527:
                    528:
                    529:
                    530: /****************************************************************************
                    531:  * convert tag of local object to tag of cmo.
                    532:  * for SM_pushCMOtag
                    533:  *
                    534:  * PARAM : I : p : cmo
                    535:  * RETURN:  tag
                    536:  ****************************************************************************/
                    537: int
                    538: get_cmon_tag(cmo *p)
                    539: {
                    540:        switch (p->tag) {
                    541:        case CMON_ZZ:
                    542:                return (CMO_ZZ);
                    543:        case CMON_ZZX:
                    544:                return (CMO_RECURSIVE_POLYNOMIAL);
                    545:        case CMON_FACTORS:
                    546:                return (CMO_LIST);
                    547:        case CMON_MAT_ZZ:
                    548:                return (CMON_MAT_ZZ);
                    549:        default:
                    550:                return (p->tag);
                    551:        }
                    552: }
                    553:
                    554:
1.1       iwane     555:
                    556:

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