Annotation of OpenXM_contrib2/asir2000/lib/num, Revision 1.1.1.1
1.1 noro 1: /* $OpenXM: OpenXM/src/asir99/lib/num,v 1.1.1.1 1999/11/10 08:12:30 noro Exp $ */
2: /* factorial */
3:
4: def f(N)
5: {
6: for ( I = 1, M = 1; I <= N; I++ )
7: M *= I;
8: return M;
9: }
10:
11: /* factorial by recursion */
12:
13: def recf(X) {
14: if ( X == 0 )
15: return ( 1 );
16: else
17: return ( X * recf(X-1) );
18: }
19:
20: /* Catalan's constant */
21:
22: def cat(D) {
23: tstart;
24: for ( S = T = P = idiv(10^D,2), I = 1, J = 3; T; I++, J += 2 ) {
25: P = idiv(P*I,J); T = idiv(T*I+P,J); S += T;
26: }
27: tstop;
28: return S;
29: }
30:
31: /* Napier's constant */
32:
33: def e(D,N)
34: {
35: for ( F = 1, S = 1, I = 1; I <= N; I++ ) {
36: S = S*I + 1;
37: F *= I;
38: }
39: T = red(S/F);
40: return idiv(nm(T)*10^D,dn(T));
41: }
42:
43: /* atan */
44:
45: def at0(X,D)
46: {
47: for ( S = T = idiv(D,X), I = 1, Y = X^2, Sgn = -1;
48: T;
49: I += 2, Sgn *= -1 ) {
50: T = idiv(T*I,Y*(I+2)); S += (Sgn*T);
51: }
52: return S;
53: }
54:
55: /* pi */
56:
57: def pi(D)
58: {
59: tstart; Y = 10^D; X = 16*at0(5,Y)-4*at0(239,Y); tstop;
60: return X;
61: }
62:
63: def at1(M,D) {
64: for (N = 1, SGN = 1, MM = M*M, A = 0, XN = idiv(D,M);
65: XN;
66: N += 2, XN = idiv(XN,MM), SGN *= -1)
67: A += (SGN*idiv(XN,N));
68: return A;
69: }
70:
71: def pi1(D) {
72: tstart; Y = 10^D; X = 16*at1(5,Y)-4*at1(239,Y); tstop;
73: return X;
74: }
75:
76: def pi2(D) {
77: tstart; Y = 10^D;
78: X = 48*at1(49,Y)+128*at1(57,Y)-20*at1(239,Y)+48*at1(110443,Y);
79: tstop;
80: return X;
81: }
82:
83: /* Bernoulli number */
84: def bn(N)
85: {
86: B = newvect(N+1); C = c2(N+1);
87: for ( I = 1, B[0] = 1; I <= N; I++ ) {
88: for ( D = C[I+1], J = 0, S = 0; J < I; J++ )
89: S += D[J]*B[J];
90: B[I] = red(-S/(I+1));
91: }
92: return [B,C];
93: }
94:
95: def bp(N,B,C,V)
96: {
97: for ( I = 0, S = 0; I <= N; I++ )
98: S += C[I]*B[N-I]*V^I;
99: return S;
100: }
101:
102: /*
103: * sum(N) = 1^N+2^N+...+n^N
104: */
105:
106: def sum(N)
107: {
108: L = bn(N+1);
109: R = car(L); C = car(cdr(L));
110: S = bp(N+1,R,C[N+1],n);
111: return red((subst(S,n,n+1)-subst(S,n,1))/(N+1));
112: }
113:
114: /* nCi */
115:
116: def c(N,I)
117: {
118: for ( M = 1, J = 0; J < I; J++ )
119: M *= N-J;
120: return red(M/f(I));
121: }
122:
123: def c1(N)
124: {
125: A = newvect(N+1); B = newvect(N+1); A[0] = 1;
126: for ( K = 1; K <= N; K++ ) {
127: B[0] = B[K] = 1;
128: for ( J = 1; J < K; J++ ) B[J] = A[J-1]+A[J];
129: T = A; A = B; B = T;
130: }
131: return A;
132: }
133:
134: def c2(N)
135: {
136: A = newvect(N+1); A[0] = B = newvect(1); B[0] = 1;
137: for ( K = 1; K <= N; K++ ) {
138: A[K] = B = newvect(K+1); B[0] = B[K] = 1;
139: for ( P = A[K-1], J = 1; J < K; J++ )
140: B[J] = P[J-1]+P[J];
141: }
142: return A;
143: }
144:
145: def sumd(N,M)
146: {
147: for ( I = 1, S = 0; I <= M; I++ )
148: S += I^N;
149: return S;
150: }
151:
152: #if 0
153: def sqrt(A,N) {
154: for ( I = 0, X = 1, B = A; I < N; I++, B *= 100, X *= 10 ) {
155: while ( 1 ) {
156: T = idiv(idiv(B,X) + X,2);
157: /*
158: if ((Y = T - X)== 0)
159: if ( B == X^2) return (X/(10^I));
160: else break;
161: else if ( (Y == 1) || (Y == -1) ) break;
162: */
163: if ( ( (Y = T - X) == 0 ) || (Y == 1) || (Y == -1) ) break;
164: X = T;
165: }
166: }
167: return (X/(10^I));
168: }
169: #endif
170:
171: def sqrt(A) {
172: for ( J = 0, T = A; T >= 2^27; J++ ) {
173: T = idiv(T,2^27)+1;
174: }
175: for ( I = 0; T >= 2; I++ ) {
176: S = idiv(T,2);
177: if ( T = S+S )
178: T = S;
179: else
180: T = S+1;
181: }
182: X = (2^27)^idiv(J,2)*2^idiv(I,2);
183: while ( 1 ) {
184: if ( (Y=X^2) < A )
185: X += X;
186: else if ( Y == A )
187: return X;
188: else
189: break;
190: }
191: while ( 1 )
192: if ( (Y = X^2) <= A )
193: return X;
194: else
195: X = idiv(A + Y,2*X);
196: }
197:
198: /* internal -> hexadecimal */
199:
200: def hex(N) {
201: C = newvect(6,["a","b","c","d","e","f"]);
202: for ( I = 0, T = 1; T < N; T *= 16, I++ );
203: B = newvect(I+1);
204: for ( I = 0; N >= 16; I++ ) {
205: B[I] = irem(N,16);
206: N = idiv(N,16);
207: }
208: B[I] = N;
209: for ( ; I >= 0; I-- )
210: if ( (T = B[I]) < 10 )
211: print(T,0);
212: else
213: print(C[B[I]-10],0);
214: print("");
215: }
216:
217: /* internal -> binary */
218:
219: def bin(N) {
220: for ( I = 0, T = 1; T < N; T *= 2, I++ );
221: B = newvect(I+1);
222: for ( I = 0; N >= 2; I++ ) {
223: B[I] = irem(N,2);
224: N = idiv(N,2);
225: }
226: B[I] = N;
227: for ( ; I >= 0; I-- ) {
228: if ( B[I] )
229: print("1",0);
230: else
231: print("0",0);
232: }
233: print("");
234: }
235: end$
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>