[BACK]Return to num CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / lib

Annotation of OpenXM_contrib2/asir2000/lib/num, Revision 1.3

1.2       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3     ! noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2       noro       27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
                     47:  *
1.3     ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/lib/num,v 1.2 2000/08/21 08:31:41 noro Exp $
1.2       noro       49: */
1.1       noro       50: /* factorial */
                     51:
                     52: def f(N)
                     53: {
                     54:        for ( I = 1, M = 1; I <= N; I++ )
                     55:                M *= I;
                     56:        return M;
                     57: }
                     58:
                     59: /* factorial by recursion */
                     60:
                     61: def recf(X) {
                     62:        if ( X == 0 )
                     63:                return ( 1 );
                     64:        else
                     65:                return ( X * recf(X-1) );
                     66: }
                     67:
                     68: /* Catalan's constant */
                     69:
                     70: def cat(D) {
                     71:        tstart;
                     72:        for ( S = T = P = idiv(10^D,2), I = 1, J = 3; T; I++, J += 2 ) {
                     73:                P = idiv(P*I,J); T = idiv(T*I+P,J); S += T;
                     74:        }
                     75:        tstop;
                     76:        return S;
                     77: }
                     78:
                     79: /* Napier's constant */
                     80:
                     81: def e(D,N)
                     82: {
                     83:        for ( F = 1, S = 1, I = 1; I <= N; I++ ) {
                     84:                S = S*I + 1;
                     85:                F *= I;
                     86:        }
                     87:        T = red(S/F);
                     88:        return idiv(nm(T)*10^D,dn(T));
                     89: }
                     90:
                     91: /* atan */
                     92:
                     93: def at0(X,D)
                     94: {
                     95:        for ( S = T = idiv(D,X), I = 1, Y = X^2, Sgn = -1;
                     96:                T;
                     97:                I += 2, Sgn *= -1 ) {
                     98:                T = idiv(T*I,Y*(I+2)); S += (Sgn*T);
                     99:        }
                    100:        return S;
                    101: }
                    102:
                    103: /* pi */
                    104:
                    105: def pi(D)
                    106: {
                    107:        tstart; Y = 10^D; X = 16*at0(5,Y)-4*at0(239,Y); tstop;
                    108:        return X;
                    109: }
                    110:
                    111: def at1(M,D) {
                    112: for (N = 1, SGN = 1, MM = M*M, A = 0, XN = idiv(D,M);
                    113:        XN;
                    114:        N += 2, XN = idiv(XN,MM), SGN *= -1)
                    115:                A += (SGN*idiv(XN,N));
                    116:        return A;
                    117: }
                    118:
                    119: def pi1(D) {
                    120:        tstart; Y = 10^D; X = 16*at1(5,Y)-4*at1(239,Y); tstop;
                    121:        return X;
                    122: }
                    123:
                    124: def pi2(D) {
                    125:        tstart; Y = 10^D;
                    126:        X = 48*at1(49,Y)+128*at1(57,Y)-20*at1(239,Y)+48*at1(110443,Y);
                    127:        tstop;
                    128:        return X;
                    129: }
                    130:
                    131: /* Bernoulli number */
                    132: def bn(N)
                    133: {
                    134:        B = newvect(N+1); C = c2(N+1);
                    135:        for ( I = 1, B[0] = 1; I <= N; I++ ) {
                    136:                for ( D = C[I+1], J = 0, S = 0; J < I; J++ )
                    137:                        S += D[J]*B[J];
                    138:                B[I] = red(-S/(I+1));
                    139:        }
                    140:        return [B,C];
                    141: }
                    142:
                    143: def bp(N,B,C,V)
                    144: {
                    145:        for ( I = 0, S = 0; I <= N; I++ )
                    146:                S += C[I]*B[N-I]*V^I;
                    147:        return S;
                    148: }
                    149:
                    150: /*
                    151:  * sum(N) = 1^N+2^N+...+n^N
                    152:  */
                    153:
                    154: def sum(N)
                    155: {
                    156:        L = bn(N+1);
                    157:        R = car(L); C = car(cdr(L));
                    158:        S = bp(N+1,R,C[N+1],n);
                    159:        return red((subst(S,n,n+1)-subst(S,n,1))/(N+1));
                    160: }
                    161:
                    162: /* nCi */
                    163:
                    164: def c(N,I)
                    165: {
                    166:        for ( M = 1, J = 0; J < I; J++ )
                    167:                M *= N-J;
                    168:        return red(M/f(I));
                    169: }
                    170:
                    171: def c1(N)
                    172: {
                    173:        A = newvect(N+1); B = newvect(N+1); A[0] = 1;
                    174:        for ( K = 1; K <= N; K++ ) {
                    175:                B[0] = B[K] = 1;
                    176:                for ( J = 1; J < K; J++ ) B[J] = A[J-1]+A[J];
                    177:                T = A; A = B; B = T;
                    178:        }
                    179:        return A;
                    180: }
                    181:
                    182: def c2(N)
                    183: {
                    184:        A = newvect(N+1); A[0] = B = newvect(1); B[0] = 1;
                    185:        for ( K = 1; K <= N; K++ ) {
                    186:                A[K] = B = newvect(K+1); B[0] = B[K] = 1;
                    187:                for ( P = A[K-1], J = 1; J < K; J++ )
                    188:                        B[J] = P[J-1]+P[J];
                    189:        }
                    190:        return A;
                    191: }
                    192:
                    193: def sumd(N,M)
                    194: {
                    195:        for ( I = 1, S = 0; I <= M; I++ )
                    196:                S += I^N;
                    197:        return S;
                    198: }
                    199:
                    200: #if 0
                    201: def sqrt(A,N) {
                    202:        for ( I = 0, X = 1, B = A; I < N; I++, B *= 100, X *= 10 ) {
                    203:                while ( 1 ) {
                    204:                        T = idiv(idiv(B,X) + X,2);
                    205: /*
                    206:                        if ((Y = T - X)== 0)
                    207:                                if ( B == X^2) return (X/(10^I));
                    208:                                else break;
                    209:                        else if ( (Y == 1) || (Y == -1) ) break;
                    210: */
                    211:                        if ( ( (Y = T - X) == 0 ) || (Y == 1) || (Y == -1) ) break;
                    212:                        X = T;
                    213:                }
                    214:        }
                    215:        return (X/(10^I));
                    216: }
                    217: #endif
                    218:
                    219: def sqrt(A) {
                    220:        for ( J = 0, T = A; T >= 2^27; J++ ) {
                    221:                T = idiv(T,2^27)+1;
                    222:        }
                    223:        for ( I = 0; T >= 2; I++ ) {
                    224:                S = idiv(T,2);
                    225:                if ( T = S+S )
                    226:                        T = S;
                    227:                else
                    228:                        T = S+1;
                    229:        }
                    230:        X = (2^27)^idiv(J,2)*2^idiv(I,2);
                    231:        while ( 1 ) {
                    232:                if ( (Y=X^2) < A )
                    233:                        X += X;
                    234:                else if ( Y == A )
                    235:                        return X;
                    236:                else
                    237:                        break;
                    238:        }
                    239:        while ( 1 )
                    240:                if ( (Y = X^2) <= A )
                    241:                        return X;
                    242:                else
                    243:                        X = idiv(A + Y,2*X);
                    244: }
                    245:
                    246: /* internal -> hexadecimal */
                    247:
                    248: def hex(N) {
                    249:        C = newvect(6,["a","b","c","d","e","f"]);
                    250:        for ( I = 0, T = 1; T < N; T *= 16, I++ );
                    251:        B = newvect(I+1);
                    252:        for ( I = 0; N >= 16; I++ ) {
                    253:                B[I] = irem(N,16);
                    254:                N = idiv(N,16);
                    255:        }
                    256:        B[I] = N;
                    257:        for ( ; I >= 0; I-- )
                    258:                if ( (T = B[I]) < 10 )
                    259:                        print(T,0);
                    260:                else
                    261:                        print(C[B[I]-10],0);
                    262:        print("");
                    263: }
                    264:
                    265: /* internal -> binary */
                    266:
                    267: def bin(N) {
                    268:        for ( I = 0, T = 1; T < N; T *= 2, I++ );
                    269:        B = newvect(I+1);
                    270:        for ( I = 0; N >= 2; I++ ) {
                    271:                B[I] = irem(N,2);
                    272:                N = idiv(N,2);
                    273:        }
                    274:        B[I] = N;
                    275:        for ( ; I >= 0; I-- ) {
                    276:                if ( B[I] )
                    277:                        print("1",0);
                    278:                else
                    279:                        print("0",0);
                    280:        }
                    281:        print("");
                    282: }
                    283: end$

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