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

1.3     ! iwane       1: /* $OpenXM: OpenXM/src/ox_ntl/ntlconv.cpp,v 1.2 2003/11/15 09:06: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:
                    153:        ostrstream sout;
                    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:        int ret;
                    226:
                    227:        cmo_zz *zz;
                    228:        int row, col;
                    229:        int i, j;
                    230:
                    231:        row = (int)mat.NumRows();
                    232:        col = (int)mat.NumCols();
                    233:
                    234:        BLOCK_NEW_CMO();
                    235:        list = list_appendl(NULL, new_cmo_int32(row), new_cmo_int32(col), NULL);
                    236:
                    237:        for (i = 0; i < row; i++) {
                    238:                for (j = 0; j < col; j++) {
                    239:                        zz = ZZ_to_cmo_zz(mat[i][j]);
                    240:                        list_append(list, (cmo *)zz);
                    241:                }
                    242:        }
                    243:        UNBLOCK_NEW_CMO();
                    244:
                    245:        return (list);
                    246: }
                    247:
                    248:
                    249:
                    250:
                    251: /****************************************************************************
                    252:  * convert cmo to ZZX which is polynomial in Z[x]
                    253:  *
                    254:  * PARAM : O : f       : polynomial in Z[x]
                    255:  *       : I : m       : cmo.
                    256:  *       : O : x       : indeterminate
                    257:  * RETURN: success     : NTL_SUCCESS
                    258:  *       : failure     : NTL_FAILURE
                    259:  ****************************************************************************/
                    260: int
                    261: cmo_to_mat_zz(mat_ZZ &mat, cmo *m)
                    262: {
                    263:        cmo_list *list;
                    264:        int ret;
                    265:        ZZ row, col, size;
                    266:        int len;
                    267:        cell *el;
                    268:        int i, j;
                    269:        int c, r;
                    270:
                    271:        if (m->tag != CMO_LIST) {
                    272:                return (NTL_FAILURE);
                    273:        }
                    274:
                    275:        list = (cmo_list *)m;
                    276:        len = list_length(list);
                    277:
                    278:        if (len < 2) {
                    279:                return (NTL_FAILURE);
                    280:        }
                    281:
                    282:        el = list_first(list);
                    283:        ret = cmo_to_ZZ(row, el->cmo);
                    284:        if (ret != NTL_SUCCESS) {
                    285:                return (ret);
                    286:        }
                    287:
                    288:        el = list_next(el);
                    289:        ret = cmo_to_ZZ(col, el->cmo);
                    290:        if (ret != NTL_SUCCESS) {
                    291:                return (ret);
                    292:        }
                    293:
                    294:        mul(size, row, col);
                    295:
                    296:        if (len - 2 != size) {
                    297:                return (NTL_FAILURE);
                    298:        }
                    299:
                    300:        /* row and col is less than INT_MAX */
                    301:        r = to_int(row);
                    302:        c = to_int(col);
                    303:        mat.SetDims(r, c);
                    304:        for (i = 0; i < r; i++) {
                    305:                for (j = 0; j < c; j++) {
                    306:                        el = list_next(el);
                    307:                        ret = cmo_to_ZZ(mat[i][j], el->cmo);
                    308:                        if (ret) {
                    309:                                return (ret);
                    310:                        }
                    311:                }
                    312:        }
                    313:        return (NTL_SUCCESS);
                    314: }
                    315:
                    316:
                    317:
                    318:
                    319:
                    320:
                    321:
                    322:
                    323:
                    324: /****************************************************************************
                    325:  * convert cmo to ZZX which is polynomial in Z[x]
                    326:  *
                    327:  * PARAM : O : f       : polynomial in Z[x]
                    328:  *       : I : m       : cmo.
                    329:  *       : O : x       : indeterminate
                    330:  * RETURN: success     : NTL_SUCCESS
                    331:  *       : failure     : NTL_FAILURE
                    332:  ****************************************************************************/
1.1       iwane     333: int
                    334: cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x)
                    335: {
                    336:        char *str;
                    337:        int ret;
                    338:
                    339:        switch (m->tag) {
                    340:        case CMO_STRING:   /* [ 3 4 7 ] ==> 3+4*x+7*x^2 */
                    341:                str = ((cmo_string *)m)->s;
                    342:                ret = ntl_isZZXstr(str);
                    343:
                    344:                if (!ret) {
                    345:                        /* format error */
                    346:                        return (NTL_FAILURE);
                    347:                }
                    348:                {
                    349:                        istrstream sin(str, strlen(str));
                    350:                        sin >> f;
                    351:                }
                    352:                break;
                    353:        case CMO_RECURSIVE_POLYNOMIAL:
                    354:        {
                    355:                cmo_recursive_polynomial *rec = (cmo_recursive_polynomial *)m;
                    356:                cmo_polynomial_in_one_variable *poly = (cmo_polynomial_in_one_variable *)rec->coef;
                    357:                cell *el;
                    358:                int len;
                    359:
                    360:                if (poly->tag != CMO_POLYNOMIAL_IN_ONE_VARIABLE) {
                    361:                        return (NTL_FAILURE);
                    362:                }
                    363:
                    364:                el = list_first((cmo_list *)poly);
                    365:                len = list_length((cmo_list *)poly);
                    366:
                    367:                f = 0;
                    368:
                    369:                while (!list_endof((cmo_list *)poly, el)) {
                    370:                        ZZ c;
                    371:                        cmo *coef = el->cmo;
                    372:                        int exp = el->exp;
                    373:
                    374:                        ret = cmo_to_ZZ(c, coef);
                    375:                        if (ret != NTL_SUCCESS) {
                    376:                                return (NTL_FAILURE);
                    377:                        }
                    378:
                    379:                        SetCoeff(f, exp, c);
                    380:
                    381:                        el = list_next(el);
                    382:                }
                    383:
                    384:
                    385:                el = list_first(rec->ringdef);
                    386:                x = (cmo_indeterminate *)el->cmo;
                    387:
                    388:                break;
                    389:        }
                    390:        default:
                    391:                break;
                    392:        }
                    393:        return (NTL_SUCCESS);
                    394: }
                    395:
                    396: /****************************************************************************
                    397:  * convert polynomial in Z[x] to cmo_recursive_polynomial
                    398:  *
                    399:  * PARAM : I : factor  : polynomial in Z[x]
                    400:  *       : I : x       : indeterminate
                    401:  * RETURN:
                    402:  ****************************************************************************/
                    403: cmo_recursive_polynomial *
                    404: ZZX_to_cmo(ZZX &factor, cmo_indeterminate *x)
                    405: {
                    406:        cmo_recursive_polynomial *rec;
                    407:        cmo_polynomial_in_one_variable *poly;
                    408:
                    409:        cmo_list *ringdef;
                    410:        int i;
                    411:        cmo *coef;
                    412:
1.2       iwane     413:        BLOCK_NEW_CMO();
                    414:
1.1       iwane     415:        ringdef = new_cmo_list();
                    416:        list_append(ringdef, (cmo *)x);
                    417:
                    418:        poly = new_cmo_polynomial_in_one_variable(0);
                    419:        for (i = deg(factor); i >= 0; i--) {
                    420:                if (coeff(factor, i) == 0)
                    421:                        continue;
                    422:
                    423:                coef = (cmo *)ZZ_to_cmo_zz(coeff(factor, i));
                    424:                list_append_monomial((cmo_list *)poly, coef, i);
                    425:        }
                    426:
                    427:        rec = new_cmo_recursive_polynomial(ringdef, (cmo *)poly);
1.2       iwane     428:
                    429:        UNBLOCK_NEW_CMO();
                    430:
1.1       iwane     431:        return (rec);
                    432: }
                    433:
                    434:
                    435: /****************************************************************************
                    436:  * convert pair of factor and multiplicity to cmo_list
                    437:  *
                    438:  * PARAM : I : factor  : polynomial in Z[x]
                    439:  *       : I : d       : multiplicity
                    440:  *       : I : x       : indeterminate
                    441:  * RETURN:
                    442:  ****************************************************************************/
                    443: cmo_list *
                    444: ZZX_int_to_cmo(ZZX &factor, int d, cmo_indeterminate *x)
                    445: {
                    446:        cmo_recursive_polynomial *poly;
                    447:        cmo_int32 *deg;
                    448:        cmo_list *list;
                    449:
                    450:        poly = ZZX_to_cmo(factor, x);
1.2       iwane     451:
                    452:        BLOCK_NEW_CMO();
                    453:
1.1       iwane     454:        deg = new_cmo_int32(d);
1.2       iwane     455:        list = list_appendl(NULL, poly, deg, NULL);
1.1       iwane     456:
1.2       iwane     457:        UNBLOCK_NEW_CMO();
1.1       iwane     458:
                    459:        return (list);
                    460: }
                    461:
                    462:
                    463: /****************************************************************************
                    464:  * convert vec_pair_ZZX_long(list which pair of factor and multiplicity)
                    465:  * to cmo_list
                    466:  *
                    467:  * PARAM : I : factors : list which pair of factor and multiplicity
                    468:  *       :   :         :  [[factor1,multiplicity1][factor2,multiplicity2]...]
                    469:  *       : I : x       : indeterminate
                    470:  * RETURN:
                    471:  ****************************************************************************/
                    472: cmo_list *
                    473: vec_pair_ZZX_long_to_cmo(vec_pair_ZZX_long &factors, cmo_indeterminate *x)
                    474: {
                    475:        int i;
1.2       iwane     476:        cmo_list *list;
1.1       iwane     477:        cmo_list *factor;
                    478:
1.2       iwane     479:        BLOCK_NEW_CMO();
                    480:
                    481:        list = new_cmo_list();
1.1       iwane     482:        for (i = 0; i < factors.length(); i++) {
                    483:                factor = ZZX_int_to_cmo(factors[i].a, (int)factors[i].b, x);
                    484:                list_append(list, (cmo *)factor);
                    485:        }
                    486:
1.2       iwane     487:        UNBLOCK_NEW_CMO();
                    488:
1.1       iwane     489:        return (list);
                    490: }
1.2       iwane     491:
                    492:
                    493: /****************************************************************************
                    494:  * convert local object to cmo.
                    495:  * for SM_popCMO
                    496:  *
                    497:  * PARAM : I : p : cmo
                    498:  * RETURN: cmo
                    499:  ****************************************************************************/
                    500: cmo *
                    501: convert_cmon(cmo *p)
                    502: {
                    503:
                    504:        switch (p->tag) {
                    505:        case CMON_ZZ:
                    506:        {
                    507:                cmon_zz_t *z = (cmon_zz_t *)p;
                    508:                return ((cmo *)ZZ_to_cmo_zz(*z->z));
                    509:        }
                    510:        case CMON_ZZX:
                    511:        {
                    512:                cmon_zzx_t *f = (cmon_zzx_t *)p;
                    513:                return ((cmo *)ZZX_to_cmo(*f->f, f->x));
                    514:        }
                    515:        case CMON_FACTORS:
                    516:        {
                    517:                cmon_factors_t *f = (cmon_factors_t *)p;
                    518:                cmo_zz *z = ZZ_to_cmo_zz(*f->cont);
                    519:                cmo_list *list = vec_pair_ZZX_long_to_cmo(*f->f, f->x);
                    520:                return ((cmo *)list_appendl(NULL, (cmo *)z, list, NULL));
                    521:        }
                    522:        case CMON_MAT_ZZ:
                    523:        {
                    524:                cmon_mat_zz_t *m = (cmon_mat_zz_t *)p;
                    525:                cmo_list *list = mat_zz_to_cmo(*m->mat);
                    526:                return ((cmo *)list);
                    527:        }
                    528:        default:
                    529:                return (p);
                    530:        }
                    531: }
                    532:
                    533:
                    534:
                    535: /****************************************************************************
                    536:  * convert tag of local object to tag of cmo.
                    537:  * for SM_pushCMOtag
                    538:  *
                    539:  * PARAM : I : p : cmo
                    540:  * RETURN:  tag
                    541:  ****************************************************************************/
                    542: int
                    543: get_cmon_tag(cmo *p)
                    544: {
                    545:        switch (p->tag) {
                    546:        case CMON_ZZ:
                    547:                return (CMO_ZZ);
                    548:        case CMON_ZZX:
                    549:                return (CMO_RECURSIVE_POLYNOMIAL);
                    550:        case CMON_FACTORS:
                    551:                return (CMO_LIST);
                    552:        case CMON_MAT_ZZ:
                    553:                return (CMON_MAT_ZZ);
                    554:        default:
                    555:                return (p->tag);
                    556:        }
                    557: }
                    558:
                    559:
1.1       iwane     560:
                    561:

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