ray 0.0.0.pre1 → 0.0.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/.gitmodules +3 -0
- data/Rakefile +148 -5
- data/VERSION +1 -1
- data/ext/event.c +535 -0
- data/ext/extconf.rb +7 -1
- data/ext/image.c +110 -18
- data/ext/joystick.c +145 -0
- data/ext/ray.c +288 -35
- data/ext/ray.h +54 -2
- data/lib/ray/config.rb +84 -0
- data/lib/ray/dsl.rb +19 -0
- data/lib/ray/dsl/converter.rb +65 -0
- data/lib/ray/dsl/event.rb +52 -0
- data/lib/ray/dsl/event_raiser.rb +21 -0
- data/lib/ray/dsl/event_runner.rb +39 -0
- data/lib/ray/dsl/event_translator.rb +38 -0
- data/lib/ray/dsl/handler.rb +71 -0
- data/lib/ray/dsl/listener.rb +30 -0
- data/lib/ray/dsl/matcher.rb +60 -0
- data/lib/ray/dsl/type.rb +58 -0
- data/lib/ray/game.rb +107 -0
- data/lib/ray/helper.rb +17 -0
- data/lib/ray/image.rb +11 -0
- data/lib/ray/ray.rb +8 -0
- data/lib/ray/scene.rb +102 -0
- data/psp/SDL_psp_main.c +84 -0
- data/psp/bigdecimal/README +60 -0
- data/psp/bigdecimal/bigdecimal.c +4697 -0
- data/psp/bigdecimal/bigdecimal.h +216 -0
- data/psp/bigdecimal/lib/bigdecimal/jacobian.rb +85 -0
- data/psp/bigdecimal/lib/bigdecimal/ludcmp.rb +84 -0
- data/psp/bigdecimal/lib/bigdecimal/math.rb +235 -0
- data/psp/bigdecimal/lib/bigdecimal/newton.rb +77 -0
- data/psp/bigdecimal/lib/bigdecimal/util.rb +65 -0
- data/psp/digest/bubblebabble/bubblebabble.c +142 -0
- data/psp/digest/defs.h +20 -0
- data/psp/digest/digest.c +643 -0
- data/psp/digest/digest.h +32 -0
- data/psp/digest/lib/digest.rb +50 -0
- data/psp/digest/lib/md5.rb +27 -0
- data/psp/digest/lib/sha1.rb +27 -0
- data/psp/digest/md5/md5.c +420 -0
- data/psp/digest/md5/md5.h +80 -0
- data/psp/digest/md5/md5init.c +40 -0
- data/psp/digest/rmd160/rmd160.c +457 -0
- data/psp/digest/rmd160/rmd160.h +56 -0
- data/psp/digest/rmd160/rmd160init.c +40 -0
- data/psp/digest/sha1/sha1.c +269 -0
- data/psp/digest/sha1/sha1.h +39 -0
- data/psp/digest/sha1/sha1init.c +40 -0
- data/psp/digest/sha2/lib/sha2.rb +73 -0
- data/psp/digest/sha2/sha2.c +919 -0
- data/psp/digest/sha2/sha2.h +109 -0
- data/psp/digest/sha2/sha2init.c +52 -0
- data/psp/enumerator/enumerator.c +298 -0
- data/psp/etc/etc.c +559 -0
- data/psp/ext.c +285 -0
- data/psp/fcntl/fcntl.c +187 -0
- data/psp/lib/rbconfig.rb +178 -0
- data/psp/nkf/lib/kconv.rb +367 -0
- data/psp/nkf/nkf-utf8/config.h +88 -0
- data/psp/nkf/nkf-utf8/nkf.c +6040 -0
- data/psp/nkf/nkf-utf8/utf8tbl.c +8500 -0
- data/psp/nkf/nkf-utf8/utf8tbl.h +34 -0
- data/psp/nkf/nkf.c +654 -0
- data/psp/socket/addrinfo.h +173 -0
- data/psp/socket/getaddrinfo.c +676 -0
- data/psp/socket/getnameinfo.c +270 -0
- data/psp/socket/pspsocket.c +71 -0
- data/psp/socket/pspsocket.h +28 -0
- data/psp/socket/socket.c +4662 -0
- data/psp/socket/sockport.h +76 -0
- data/psp/stringio/stringio.c +1306 -0
- data/psp/strscan/strscan.c +1320 -0
- data/psp/syck/bytecode.c +1166 -0
- data/psp/syck/emitter.c +1242 -0
- data/psp/syck/gram.c +1894 -0
- data/psp/syck/gram.h +79 -0
- data/psp/syck/handler.c +174 -0
- data/psp/syck/implicit.c +2990 -0
- data/psp/syck/node.c +408 -0
- data/psp/syck/rubyext.c +2367 -0
- data/psp/syck/syck.c +504 -0
- data/psp/syck/syck.h +456 -0
- data/psp/syck/token.c +2725 -0
- data/psp/syck/yaml2byte.c +257 -0
- data/psp/syck/yamlbyte.h +170 -0
- data/psp/thread/thread.c +1175 -0
- data/psp/zlib/zlib.c +3547 -0
- data/script.rb +10 -0
- data/spec/ray/config_spec.rb +90 -0
- data/spec/ray/conversion_spec.rb +43 -0
- data/spec/ray/event_spec.rb +191 -0
- data/spec/ray/image_spec.rb +43 -1
- data/spec/ray/joystick_spec.rb +17 -0
- data/spec/ray/matcher_spec.rb +73 -0
- data/spec/ray/ray_spec.rb +72 -1
- data/spec/ray/type_spec.rb +17 -0
- data/spec/res/aqua.bmp +0 -0
- data/spec/res/aqua.png +0 -0
- data/spec/res/not_a_jpeg.jpeg +0 -0
- data/spec_runner.rb +4 -0
- metadata +101 -9
- data/ext/Makefile +0 -189
- data/ext/ray +0 -0
- data/ext/ray.bundle +0 -0
- data/ext/ray_ext.bundle +0 -0
- data/ext/ray_ext.so +0 -0
- data/ext/test.rb +0 -21
@@ -0,0 +1,216 @@
|
|
1
|
+
/*
|
2
|
+
*
|
3
|
+
* Ruby BigDecimal(Variable decimal precision) extension library.
|
4
|
+
*
|
5
|
+
* Copyright(C) 2002 by Shigeo Kobayashi(shigeo@tinyforest.gr.jp)
|
6
|
+
*
|
7
|
+
* You may distribute under the terms of either the GNU General Public
|
8
|
+
* License or the Artistic License, as specified in the README file
|
9
|
+
* of this BigDecimal distribution.
|
10
|
+
*
|
11
|
+
* NOTES:
|
12
|
+
* 2003-03-28 V1.0 checked in.
|
13
|
+
*
|
14
|
+
*/
|
15
|
+
|
16
|
+
#ifndef ____BIG_DECIMAL__H____
|
17
|
+
#define ____BIG_DECIMAL__H____
|
18
|
+
|
19
|
+
#if defined(__cplusplus)
|
20
|
+
extern "C" {
|
21
|
+
#endif
|
22
|
+
|
23
|
+
/*
|
24
|
+
* NaN & Infinity
|
25
|
+
*/
|
26
|
+
#define SZ_NaN "NaN"
|
27
|
+
#define SZ_INF "Infinity"
|
28
|
+
#define SZ_PINF "+Infinity"
|
29
|
+
#define SZ_NINF "-Infinity"
|
30
|
+
|
31
|
+
/*
|
32
|
+
* #define VP_EXPORT other than static to let VP_ routines
|
33
|
+
* be called from outside of this module.
|
34
|
+
*/
|
35
|
+
#define VP_EXPORT static
|
36
|
+
|
37
|
+
#define U_LONG unsigned long
|
38
|
+
#define S_LONG long
|
39
|
+
#define U_INT unsigned int
|
40
|
+
#define S_INT int
|
41
|
+
|
42
|
+
/* Exception codes */
|
43
|
+
#define VP_EXCEPTION_ALL ((unsigned short)0x00FF)
|
44
|
+
#define VP_EXCEPTION_INFINITY ((unsigned short)0x0001)
|
45
|
+
#define VP_EXCEPTION_NaN ((unsigned short)0x0002)
|
46
|
+
#define VP_EXCEPTION_UNDERFLOW ((unsigned short)0x0004)
|
47
|
+
#define VP_EXCEPTION_OVERFLOW ((unsigned short)0x0001) /* 0x0008) */
|
48
|
+
#define VP_EXCEPTION_ZERODIVIDE ((unsigned short)0x0001) /* 0x0010) */
|
49
|
+
|
50
|
+
/* Following 2 exceptions cann't controlled by user */
|
51
|
+
#define VP_EXCEPTION_OP ((unsigned short)0x0020)
|
52
|
+
#define VP_EXCEPTION_MEMORY ((unsigned short)0x0040)
|
53
|
+
|
54
|
+
/* Computation mode */
|
55
|
+
#define VP_ROUND_MODE ((unsigned short)0x0100)
|
56
|
+
#define VP_ROUND_UP 1
|
57
|
+
#define VP_ROUND_DOWN 2
|
58
|
+
#define VP_ROUND_HALF_UP 3
|
59
|
+
#define VP_ROUND_HALF_DOWN 4
|
60
|
+
#define VP_ROUND_CEIL 5
|
61
|
+
#define VP_ROUND_FLOOR 6
|
62
|
+
#define VP_ROUND_HALF_EVEN 7
|
63
|
+
|
64
|
+
#define VP_SIGN_NaN 0 /* NaN */
|
65
|
+
#define VP_SIGN_POSITIVE_ZERO 1 /* Positive zero */
|
66
|
+
#define VP_SIGN_NEGATIVE_ZERO -1 /* Negative zero */
|
67
|
+
#define VP_SIGN_POSITIVE_FINITE 2 /* Positive finite number */
|
68
|
+
#define VP_SIGN_NEGATIVE_FINITE -2 /* Negative finite number */
|
69
|
+
#define VP_SIGN_POSITIVE_INFINITE 3 /* Positive infinite number */
|
70
|
+
#define VP_SIGN_NEGATIVE_INFINITE -3 /* Negative infinite number */
|
71
|
+
|
72
|
+
/*
|
73
|
+
* VP representation
|
74
|
+
* r = 0.xxxxxxxxx *BASE**exponent
|
75
|
+
*/
|
76
|
+
typedef struct {
|
77
|
+
VALUE obj; /* Back pointer(VALUE) for Ruby object. */
|
78
|
+
U_LONG MaxPrec; /* Maximum precision size */
|
79
|
+
/* This is the actual size of pfrac[] */
|
80
|
+
/*(frac[0] to frac[MaxPrec] are available). */
|
81
|
+
U_LONG Prec; /* Current precision size. */
|
82
|
+
/* This indicates how much the. */
|
83
|
+
/* the array frac[] is actually used. */
|
84
|
+
S_INT exponent;/* Exponent part. */
|
85
|
+
short sign; /* Attributes of the value. */
|
86
|
+
/*
|
87
|
+
* ==0 : NaN
|
88
|
+
* 1 : Positive zero
|
89
|
+
* -1 : Negative zero
|
90
|
+
* 2 : Positive number
|
91
|
+
* -2 : Negative number
|
92
|
+
* 3 : Positive infinite number
|
93
|
+
* -3 : Negative infinite number
|
94
|
+
*/
|
95
|
+
short flag; /* Not used in vp_routines,space for user. */
|
96
|
+
U_LONG frac[1]; /* Pointer to array of fraction part. */
|
97
|
+
} Real;
|
98
|
+
|
99
|
+
/*
|
100
|
+
* ------------------
|
101
|
+
* EXPORTables.
|
102
|
+
* ------------------
|
103
|
+
*/
|
104
|
+
|
105
|
+
VP_EXPORT Real *
|
106
|
+
VpNewRbClass(U_LONG mx,char *str,VALUE klass);
|
107
|
+
|
108
|
+
VP_EXPORT Real *VpCreateRbObject(U_LONG mx,const char *str);
|
109
|
+
|
110
|
+
VP_EXPORT U_LONG VpBaseFig(void);
|
111
|
+
VP_EXPORT U_LONG VpDblFig(void);
|
112
|
+
VP_EXPORT U_LONG VpBaseVal(void);
|
113
|
+
|
114
|
+
/* Zero,Inf,NaN (isinf(),isnan() used to check) */
|
115
|
+
VP_EXPORT double VpGetDoubleNaN(void);
|
116
|
+
VP_EXPORT double VpGetDoublePosInf(void);
|
117
|
+
VP_EXPORT double VpGetDoubleNegInf(void);
|
118
|
+
VP_EXPORT double VpGetDoubleNegZero(void);
|
119
|
+
|
120
|
+
/* These 2 functions added at v1.1.7 */
|
121
|
+
VP_EXPORT U_LONG VpGetPrecLimit(void);
|
122
|
+
VP_EXPORT U_LONG VpSetPrecLimit(U_LONG n);
|
123
|
+
|
124
|
+
/* Round mode */
|
125
|
+
VP_EXPORT int VpIsRoundMode(unsigned long n);
|
126
|
+
VP_EXPORT unsigned long VpGetRoundMode(void);
|
127
|
+
VP_EXPORT unsigned long VpSetRoundMode(unsigned long n);
|
128
|
+
|
129
|
+
VP_EXPORT int VpException(unsigned short f,const char *str,int always);
|
130
|
+
VP_EXPORT int VpIsNegDoubleZero(double v);
|
131
|
+
VP_EXPORT U_LONG VpNumOfChars(Real *vp,const char *pszFmt);
|
132
|
+
VP_EXPORT U_LONG VpInit(U_LONG BaseVal);
|
133
|
+
VP_EXPORT void *VpMemAlloc(U_LONG mb);
|
134
|
+
VP_EXPORT void VpFree(Real *pv);
|
135
|
+
VP_EXPORT Real *VpAlloc(U_LONG mx, const char *szVal);
|
136
|
+
VP_EXPORT int VpAsgn(Real *c,Real *a,int isw);
|
137
|
+
VP_EXPORT int VpAddSub(Real *c,Real *a,Real *b,int operation);
|
138
|
+
VP_EXPORT int VpMult(Real *c,Real *a,Real *b);
|
139
|
+
VP_EXPORT int VpDivd(Real *c,Real *r,Real *a,Real *b);
|
140
|
+
VP_EXPORT int VpComp(Real *a,Real *b);
|
141
|
+
VP_EXPORT S_LONG VpExponent10(Real *a);
|
142
|
+
VP_EXPORT void VpSzMantissa(Real *a,char *psz);
|
143
|
+
VP_EXPORT int VpToSpecialString(Real *a,char *psz,int fPlus);
|
144
|
+
VP_EXPORT void VpToString(Real *a,char *psz,int fFmt,int fPlus);
|
145
|
+
VP_EXPORT void VpToFString(Real *a,char *psz,int fFmt,int fPlus);
|
146
|
+
VP_EXPORT int VpCtoV(Real *a,const char *int_chr,U_LONG ni,const char *frac,U_LONG nf,const char *exp_chr,U_LONG ne);
|
147
|
+
VP_EXPORT int VpVtoD(double *d,S_LONG *e,Real *m);
|
148
|
+
VP_EXPORT void VpDtoV(Real *m,double d);
|
149
|
+
VP_EXPORT void VpItoV(Real *m,S_INT ival);
|
150
|
+
VP_EXPORT int VpSqrt(Real *y,Real *x);
|
151
|
+
VP_EXPORT int VpActiveRound(Real *y,Real *x,int f,int il);
|
152
|
+
VP_EXPORT int VpMidRound(Real *y, int f, int nf);
|
153
|
+
VP_EXPORT int VpLeftRound(Real *y, int f, int nf);
|
154
|
+
VP_EXPORT void VpFrac(Real *y,Real *x);
|
155
|
+
VP_EXPORT int VpPower(Real *y,Real *x,S_INT n);
|
156
|
+
|
157
|
+
/* VP constants */
|
158
|
+
VP_EXPORT Real *VpOne(void);
|
159
|
+
|
160
|
+
/*
|
161
|
+
* ------------------
|
162
|
+
* MACRO definitions.
|
163
|
+
* ------------------
|
164
|
+
*/
|
165
|
+
#define Abs(a) (((a)>= 0)?(a):(-(a)))
|
166
|
+
#define Max(a, b) (((a)>(b))?(a):(b))
|
167
|
+
#define Min(a, b) (((a)>(b))?(b):(a))
|
168
|
+
|
169
|
+
#define VpMaxPrec(a) ((a)->MaxPrec)
|
170
|
+
#define VpPrec(a) ((a)->Prec)
|
171
|
+
#define VpGetFlag(a) ((a)->flag)
|
172
|
+
|
173
|
+
/* Sign */
|
174
|
+
|
175
|
+
/* VpGetSign(a) returns 1,-1 if a>0,a<0 respectively */
|
176
|
+
#define VpGetSign(a) (((a)->sign>0)?1:(-1))
|
177
|
+
/* Change sign of a to a>0,a<0 if s = 1,-1 respectively */
|
178
|
+
#define VpChangeSign(a,s) {if((s)>0) (a)->sign=(short)Abs((S_LONG)(a)->sign);else (a)->sign=-(short)Abs((S_LONG)(a)->sign);}
|
179
|
+
/* Sets sign of a to a>0,a<0 if s = 1,-1 respectively */
|
180
|
+
#define VpSetSign(a,s) {if((s)>0) (a)->sign=(short)VP_SIGN_POSITIVE_FINITE;else (a)->sign=(short)VP_SIGN_NEGATIVE_FINITE;}
|
181
|
+
|
182
|
+
/* 1 */
|
183
|
+
#define VpSetOne(a) {(a)->frac[0]=(a)->exponent=(a)->Prec=1;(a)->sign=VP_SIGN_POSITIVE_FINITE;}
|
184
|
+
|
185
|
+
/* ZEROs */
|
186
|
+
#define VpIsPosZero(a) ((a)->sign==VP_SIGN_POSITIVE_ZERO)
|
187
|
+
#define VpIsNegZero(a) ((a)->sign==VP_SIGN_NEGATIVE_ZERO)
|
188
|
+
#define VpIsZero(a) (VpIsPosZero(a) || VpIsNegZero(a))
|
189
|
+
#define VpSetPosZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_ZERO)
|
190
|
+
#define VpSetNegZero(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_ZERO)
|
191
|
+
#define VpSetZero(a,s) ( ((s)>0)?VpSetPosZero(a):VpSetNegZero(a) )
|
192
|
+
|
193
|
+
/* NaN */
|
194
|
+
#define VpIsNaN(a) ((a)->sign==VP_SIGN_NaN)
|
195
|
+
#define VpSetNaN(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NaN)
|
196
|
+
|
197
|
+
/* Infinity */
|
198
|
+
#define VpIsPosInf(a) ((a)->sign==VP_SIGN_POSITIVE_INFINITE)
|
199
|
+
#define VpIsNegInf(a) ((a)->sign==VP_SIGN_NEGATIVE_INFINITE)
|
200
|
+
#define VpIsInf(a) (VpIsPosInf(a) || VpIsNegInf(a))
|
201
|
+
#define VpIsDef(a) ( !(VpIsNaN(a)||VpIsInf(a)) )
|
202
|
+
#define VpSetPosInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_POSITIVE_INFINITE)
|
203
|
+
#define VpSetNegInf(a) ((a)->frac[0]=0,(a)->Prec=1,(a)->sign=VP_SIGN_NEGATIVE_INFINITE)
|
204
|
+
#define VpSetInf(a,s) ( ((s)>0)?VpSetPosInf(a):VpSetNegInf(a) )
|
205
|
+
#define VpHasVal(a) (a->frac[0])
|
206
|
+
#define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
|
207
|
+
#define VpExponent(a) (a->exponent)
|
208
|
+
#ifdef _DEBUG
|
209
|
+
int VpVarCheck(Real * v);
|
210
|
+
VP_EXPORT int VPrint(FILE *fp,char *cntl_chr,Real *a);
|
211
|
+
#endif /* _DEBUG */
|
212
|
+
|
213
|
+
#if defined(__cplusplus)
|
214
|
+
} /* extern "C" { */
|
215
|
+
#endif
|
216
|
+
#endif /* ____BIG_DECIMAL__H____ */
|
@@ -0,0 +1,85 @@
|
|
1
|
+
#
|
2
|
+
# require 'bigdecimal/jacobian'
|
3
|
+
#
|
4
|
+
# Provides methods to compute the Jacobian matrix of a set of equations at a
|
5
|
+
# point x. In the methods below:
|
6
|
+
#
|
7
|
+
# f is an Object which is used to compute the Jacobian matrix of the equations.
|
8
|
+
# It must provide the following methods:
|
9
|
+
#
|
10
|
+
# f.values(x):: returns the values of all functions at x
|
11
|
+
#
|
12
|
+
# f.zero:: returns 0.0
|
13
|
+
# f.one:: returns 1.0
|
14
|
+
# f.two:: returns 1.0
|
15
|
+
# f.ten:: returns 10.0
|
16
|
+
#
|
17
|
+
# f.eps:: returns the convergence criterion (epsilon value) used to determine whether two values are considered equal. If |a-b| < epsilon, the two values are considered equal.
|
18
|
+
#
|
19
|
+
# x is the point at which to compute the Jacobian.
|
20
|
+
#
|
21
|
+
# fx is f.values(x).
|
22
|
+
#
|
23
|
+
module Jacobian
|
24
|
+
#--
|
25
|
+
def isEqual(a,b,zero=0.0,e=1.0e-8)
|
26
|
+
aa = a.abs
|
27
|
+
bb = b.abs
|
28
|
+
if aa == zero && bb == zero then
|
29
|
+
true
|
30
|
+
else
|
31
|
+
if ((a-b)/(aa+bb)).abs < e then
|
32
|
+
true
|
33
|
+
else
|
34
|
+
false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
#++
|
39
|
+
|
40
|
+
# Computes the derivative of f[i] at x[i].
|
41
|
+
# fx is the value of f at x.
|
42
|
+
def dfdxi(f,fx,x,i)
|
43
|
+
nRetry = 0
|
44
|
+
n = x.size
|
45
|
+
xSave = x[i]
|
46
|
+
ok = 0
|
47
|
+
ratio = f.ten*f.ten*f.ten
|
48
|
+
dx = x[i].abs/ratio
|
49
|
+
dx = fx[i].abs/ratio if isEqual(dx,f.zero,f.zero,f.eps)
|
50
|
+
dx = f.one/f.ten if isEqual(dx,f.zero,f.zero,f.eps)
|
51
|
+
until ok>0 do
|
52
|
+
s = f.zero
|
53
|
+
deriv = []
|
54
|
+
if(nRetry>100) then
|
55
|
+
raize "Singular Jacobian matrix. No change at x[" + i.to_s + "]"
|
56
|
+
end
|
57
|
+
dx = dx*f.two
|
58
|
+
x[i] += dx
|
59
|
+
fxNew = f.values(x)
|
60
|
+
for j in 0...n do
|
61
|
+
if !isEqual(fxNew[j],fx[j],f.zero,f.eps) then
|
62
|
+
ok += 1
|
63
|
+
deriv <<= (fxNew[j]-fx[j])/dx
|
64
|
+
else
|
65
|
+
deriv <<= f.zero
|
66
|
+
end
|
67
|
+
end
|
68
|
+
x[i] = xSave
|
69
|
+
end
|
70
|
+
deriv
|
71
|
+
end
|
72
|
+
|
73
|
+
# Computes the Jacobian of f at x. fx is the value of f at x.
|
74
|
+
def jacobian(f,fx,x)
|
75
|
+
n = x.size
|
76
|
+
dfdx = Array::new(n*n)
|
77
|
+
for i in 0...n do
|
78
|
+
df = dfdxi(f,fx,x,i)
|
79
|
+
for j in 0...n do
|
80
|
+
dfdx[j*n+i] = df[j]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
dfdx
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
#
|
2
|
+
# Solves a*x = b for x, using LU decomposition.
|
3
|
+
#
|
4
|
+
module LUSolve
|
5
|
+
# Performs LU decomposition of the n by n matrix a.
|
6
|
+
def ludecomp(a,n,zero=0,one=1)
|
7
|
+
prec = BigDecimal.limit(nil)
|
8
|
+
ps = []
|
9
|
+
scales = []
|
10
|
+
for i in 0...n do # pick up largest(abs. val.) element in each row.
|
11
|
+
ps <<= i
|
12
|
+
nrmrow = zero
|
13
|
+
ixn = i*n
|
14
|
+
for j in 0...n do
|
15
|
+
biggst = a[ixn+j].abs
|
16
|
+
nrmrow = biggst if biggst>nrmrow
|
17
|
+
end
|
18
|
+
if nrmrow>zero then
|
19
|
+
scales <<= one.div(nrmrow,prec)
|
20
|
+
else
|
21
|
+
raise "Singular matrix"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
n1 = n - 1
|
25
|
+
for k in 0...n1 do # Gaussian elimination with partial pivoting.
|
26
|
+
biggst = zero;
|
27
|
+
for i in k...n do
|
28
|
+
size = a[ps[i]*n+k].abs*scales[ps[i]]
|
29
|
+
if size>biggst then
|
30
|
+
biggst = size
|
31
|
+
pividx = i
|
32
|
+
end
|
33
|
+
end
|
34
|
+
raise "Singular matrix" if biggst<=zero
|
35
|
+
if pividx!=k then
|
36
|
+
j = ps[k]
|
37
|
+
ps[k] = ps[pividx]
|
38
|
+
ps[pividx] = j
|
39
|
+
end
|
40
|
+
pivot = a[ps[k]*n+k]
|
41
|
+
for i in (k+1)...n do
|
42
|
+
psin = ps[i]*n
|
43
|
+
a[psin+k] = mult = a[psin+k].div(pivot,prec)
|
44
|
+
if mult!=zero then
|
45
|
+
pskn = ps[k]*n
|
46
|
+
for j in (k+1)...n do
|
47
|
+
a[psin+j] -= mult.mult(a[pskn+j],prec)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
raise "Singular matrix" if a[ps[n1]*n+n1] == zero
|
53
|
+
ps
|
54
|
+
end
|
55
|
+
|
56
|
+
# Solves a*x = b for x, using LU decomposition.
|
57
|
+
#
|
58
|
+
# a is a matrix, b is a constant vector, x is the solution vector.
|
59
|
+
#
|
60
|
+
# ps is the pivot, a vector which indicates the permutation of rows performed
|
61
|
+
# during LU decomposition.
|
62
|
+
def lusolve(a,b,ps,zero=0.0)
|
63
|
+
prec = BigDecimal.limit(nil)
|
64
|
+
n = ps.size
|
65
|
+
x = []
|
66
|
+
for i in 0...n do
|
67
|
+
dot = zero
|
68
|
+
psin = ps[i]*n
|
69
|
+
for j in 0...i do
|
70
|
+
dot = a[psin+j].mult(x[j],prec) + dot
|
71
|
+
end
|
72
|
+
x <<= b[ps[i]] - dot
|
73
|
+
end
|
74
|
+
(n-1).downto(0) do |i|
|
75
|
+
dot = zero
|
76
|
+
psin = ps[i]*n
|
77
|
+
for j in (i+1)...n do
|
78
|
+
dot = a[psin+j].mult(x[j],prec) + dot
|
79
|
+
end
|
80
|
+
x[i] = (x[i]-dot).div(a[psin+i],prec)
|
81
|
+
end
|
82
|
+
x
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,235 @@
|
|
1
|
+
#
|
2
|
+
#--
|
3
|
+
# Contents:
|
4
|
+
# sqrt(x, prec)
|
5
|
+
# sin (x, prec)
|
6
|
+
# cos (x, prec)
|
7
|
+
# atan(x, prec) Note: |x|<1, x=0.9999 may not converge.
|
8
|
+
# exp (x, prec)
|
9
|
+
# log (x, prec)
|
10
|
+
# PI (prec)
|
11
|
+
# E (prec) == exp(1.0,prec)
|
12
|
+
#
|
13
|
+
# where:
|
14
|
+
# x ... BigDecimal number to be computed.
|
15
|
+
# |x| must be small enough to get convergence.
|
16
|
+
# prec ... Number of digits to be obtained.
|
17
|
+
#++
|
18
|
+
#
|
19
|
+
# Provides mathematical functions.
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
#
|
23
|
+
# require "bigdecimal"
|
24
|
+
# require "bigdecimal/math"
|
25
|
+
#
|
26
|
+
# include BigMath
|
27
|
+
#
|
28
|
+
# a = BigDecimal((PI(100)/2).to_s)
|
29
|
+
# puts sin(a,100) # -> 0.10000000000000000000......E1
|
30
|
+
#
|
31
|
+
module BigMath
|
32
|
+
|
33
|
+
# Computes the square root of x to the specified number of digits of
|
34
|
+
# precision.
|
35
|
+
#
|
36
|
+
# BigDecimal.new('2').sqrt(16).to_s
|
37
|
+
# -> "0.14142135623730950488016887242096975E1"
|
38
|
+
#
|
39
|
+
def sqrt(x,prec)
|
40
|
+
x.sqrt(prec)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Computes the sine of x to the specified number of digits of precision.
|
44
|
+
#
|
45
|
+
# If x is infinite or NaN, returns NaN.
|
46
|
+
def sin(x, prec)
|
47
|
+
raise ArgumentError, "Zero or negative precision for sin" if prec <= 0
|
48
|
+
return BigDecimal("NaN") if x.infinite? || x.nan?
|
49
|
+
n = prec + BigDecimal.double_fig
|
50
|
+
one = BigDecimal("1")
|
51
|
+
two = BigDecimal("2")
|
52
|
+
x1 = x
|
53
|
+
x2 = x.mult(x,n)
|
54
|
+
sign = 1
|
55
|
+
y = x
|
56
|
+
d = y
|
57
|
+
i = one
|
58
|
+
z = one
|
59
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
60
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
61
|
+
sign = -sign
|
62
|
+
x1 = x2.mult(x1,n)
|
63
|
+
i += two
|
64
|
+
z *= (i-one) * i
|
65
|
+
d = sign * x1.div(z,m)
|
66
|
+
y += d
|
67
|
+
end
|
68
|
+
y
|
69
|
+
end
|
70
|
+
|
71
|
+
# Computes the cosine of x to the specified number of digits of precision.
|
72
|
+
#
|
73
|
+
# If x is infinite or NaN, returns NaN.
|
74
|
+
def cos(x, prec)
|
75
|
+
raise ArgumentError, "Zero or negative precision for cos" if prec <= 0
|
76
|
+
return BigDecimal("NaN") if x.infinite? || x.nan?
|
77
|
+
n = prec + BigDecimal.double_fig
|
78
|
+
one = BigDecimal("1")
|
79
|
+
two = BigDecimal("2")
|
80
|
+
x1 = one
|
81
|
+
x2 = x.mult(x,n)
|
82
|
+
sign = 1
|
83
|
+
y = one
|
84
|
+
d = y
|
85
|
+
i = BigDecimal("0")
|
86
|
+
z = one
|
87
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
88
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
89
|
+
sign = -sign
|
90
|
+
x1 = x2.mult(x1,n)
|
91
|
+
i += two
|
92
|
+
z *= (i-one) * i
|
93
|
+
d = sign * x1.div(z,m)
|
94
|
+
y += d
|
95
|
+
end
|
96
|
+
y
|
97
|
+
end
|
98
|
+
|
99
|
+
# Computes the arctangent of x to the specified number of digits of precision.
|
100
|
+
#
|
101
|
+
# If x is infinite or NaN, returns NaN.
|
102
|
+
# Raises an argument error if x > 1.
|
103
|
+
def atan(x, prec)
|
104
|
+
raise ArgumentError, "Zero or negative precision for atan" if prec <= 0
|
105
|
+
return BigDecimal("NaN") if x.infinite? || x.nan?
|
106
|
+
raise ArgumentError, "x.abs must be less than 1.0" if x.abs>=1
|
107
|
+
n = prec + BigDecimal.double_fig
|
108
|
+
y = x
|
109
|
+
d = y
|
110
|
+
t = x
|
111
|
+
r = BigDecimal("3")
|
112
|
+
x2 = x.mult(x,n)
|
113
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
114
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
115
|
+
t = -t.mult(x2,n)
|
116
|
+
d = t.div(r,m)
|
117
|
+
y += d
|
118
|
+
r += 2
|
119
|
+
end
|
120
|
+
y
|
121
|
+
end
|
122
|
+
|
123
|
+
# Computes the value of e (the base of natural logarithms) raised to the
|
124
|
+
# power of x, to the specified number of digits of precision.
|
125
|
+
#
|
126
|
+
# If x is infinite or NaN, returns NaN.
|
127
|
+
#
|
128
|
+
# BigMath::exp(BigDecimal.new('1'), 10).to_s
|
129
|
+
# -> "0.271828182845904523536028752390026306410273E1"
|
130
|
+
def exp(x, prec)
|
131
|
+
raise ArgumentError, "Zero or negative precision for exp" if prec <= 0
|
132
|
+
return BigDecimal("NaN") if x.infinite? || x.nan?
|
133
|
+
n = prec + BigDecimal.double_fig
|
134
|
+
one = BigDecimal("1")
|
135
|
+
x1 = one
|
136
|
+
y = one
|
137
|
+
d = y
|
138
|
+
z = one
|
139
|
+
i = 0
|
140
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
141
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
142
|
+
x1 = x1.mult(x,n)
|
143
|
+
i += 1
|
144
|
+
z *= i
|
145
|
+
d = x1.div(z,m)
|
146
|
+
y += d
|
147
|
+
end
|
148
|
+
y
|
149
|
+
end
|
150
|
+
|
151
|
+
# Computes the natural logarithm of x to the specified number of digits
|
152
|
+
# of precision.
|
153
|
+
#
|
154
|
+
# Returns x if x is infinite or NaN.
|
155
|
+
#
|
156
|
+
def log(x, prec)
|
157
|
+
raise ArgumentError, "Zero or negative argument for log" if x <= 0 || prec <= 0
|
158
|
+
return x if x.infinite? || x.nan?
|
159
|
+
one = BigDecimal("1")
|
160
|
+
two = BigDecimal("2")
|
161
|
+
n = prec + BigDecimal.double_fig
|
162
|
+
x = (x - one).div(x + one,n)
|
163
|
+
x2 = x.mult(x,n)
|
164
|
+
y = x
|
165
|
+
d = y
|
166
|
+
i = one
|
167
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
168
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
169
|
+
x = x2.mult(x,n)
|
170
|
+
i += two
|
171
|
+
d = x.div(i,m)
|
172
|
+
y += d
|
173
|
+
end
|
174
|
+
y*two
|
175
|
+
end
|
176
|
+
|
177
|
+
# Computes the value of pi to the specified number of digits of precision.
|
178
|
+
def PI(prec)
|
179
|
+
raise ArgumentError, "Zero or negative argument for PI" if prec <= 0
|
180
|
+
n = prec + BigDecimal.double_fig
|
181
|
+
zero = BigDecimal("0")
|
182
|
+
one = BigDecimal("1")
|
183
|
+
two = BigDecimal("2")
|
184
|
+
|
185
|
+
m25 = BigDecimal("-0.04")
|
186
|
+
m57121 = BigDecimal("-57121")
|
187
|
+
|
188
|
+
pi = zero
|
189
|
+
|
190
|
+
d = one
|
191
|
+
k = one
|
192
|
+
w = one
|
193
|
+
t = BigDecimal("-80")
|
194
|
+
while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
|
195
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
196
|
+
t = t*m25
|
197
|
+
d = t.div(k,m)
|
198
|
+
k = k+two
|
199
|
+
pi = pi + d
|
200
|
+
end
|
201
|
+
|
202
|
+
d = one
|
203
|
+
k = one
|
204
|
+
w = one
|
205
|
+
t = BigDecimal("956")
|
206
|
+
while d.nonzero? && ((m = n - (pi.exponent - d.exponent).abs) > 0)
|
207
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
208
|
+
t = t.div(m57121,n)
|
209
|
+
d = t.div(k,m)
|
210
|
+
pi = pi + d
|
211
|
+
k = k+two
|
212
|
+
end
|
213
|
+
pi
|
214
|
+
end
|
215
|
+
|
216
|
+
# Computes e (the base of natural logarithms) to the specified number of
|
217
|
+
# digits of precision.
|
218
|
+
def E(prec)
|
219
|
+
raise ArgumentError, "Zero or negative precision for E" if prec <= 0
|
220
|
+
n = prec + BigDecimal.double_fig
|
221
|
+
one = BigDecimal("1")
|
222
|
+
y = one
|
223
|
+
d = y
|
224
|
+
z = one
|
225
|
+
i = 0
|
226
|
+
while d.nonzero? && ((m = n - (y.exponent - d.exponent).abs) > 0)
|
227
|
+
m = BigDecimal.double_fig if m < BigDecimal.double_fig
|
228
|
+
i += 1
|
229
|
+
z *= i
|
230
|
+
d = one.div(z,m)
|
231
|
+
y += d
|
232
|
+
end
|
233
|
+
y
|
234
|
+
end
|
235
|
+
end
|