一百個例題 (91 ~95)
Ching-Tang Tseng
Hamilton, New Zealand
6 October 2024
\ Bisection method to find real roots of equation in Lina64 ABC forth \ Equation: y=ln|140/(80-x)|-((60+x)/53.0516477) \ Guessing range: root1[-100, -50], root2[50, 80], root3[80, 100]. \ =============================================== \ (1). Variables declare heap variable Counter integer Steps 10 reals X Y XA XB XP FA FB FP EPS TEMP \ =============================================== \ (2). Input heap \ Equation is defined here. : FX {{ Y = LN ( ABS ( 140.0 e 0 / ( 80.0 e 0 - X ) ) ) - ( ( 60.0 e 0 + X ) / 53.0516477 e 0 ) }} ; \ guessing range of roots are defined here. : root1 {{ XA = -100.0 e 0 }} {{ XB = -50.0 e 0 }} ; : root2 {{ XA = 50.0 e 0 }} {{ XB = 80.0 e 0 }} ; : root3 {{ XA = 80.0 e 0 }} {{ XB = 100.0 e 0 }} ; \ All parts above are changeable setting. \ =============================================== \ =============================================== \ (3). Main code heap \ All parts below are fixed. : Fbisection 0 COUNTER ! BEGIN {{ X = XA }} FX {{ FA = Y }} {{ X = XB }} FX {{ FB = Y }} {{ EPS = ABS ( XA - XB ) - 1.0 E -12 }} EPS F0> WHILE {{ XP = ( XA + XB ) / 2.0 e 0 }} {{ X = XP }} FX {{ FP = Y }} {{ TEMP = FA * FP }} TEMP F0> IF {{ XA = XP }} ELSE {{ XB = XP }} THEN 1 COUNTER +! REPEAT CR ." Counts = " COUNTER @ . {{ TEMP = ( XA + XB ) / 2.0 e 0 }} CR ." Solution = " TEMP F. cr ; : Bbisection BASIC 10 let steps = 0 20 LET { X = XA } 30 RUN FX 40 LET { FA = Y } 50 LET { X = XB } 60 RUN FX 70 LET { FB = Y } 80 LET { EPS = ABS ( XA - XB ) - 1.0 E -12 } 90 IF { EPS <= 0.0 e 0 } THEN 210 100 LET { XP = ( XA + XB ) / 2.0 e 0 } 110 LET { X = XP } 120 RUN FX 130 LET { FP = Y } 140 LET { TEMP = FA * FP } 150 IF { TEMP > 0.0 e 0 } THEN 180 160 LET { XB = XP } 170 GOTO 190 180 LET { XA = XP } 190 LET Steps = Steps + 1 200 GOTO -20 210 PRINT " Steps = " ; steps 220 LET { TEMP = ( XA + XB ) / 2.0 e 0 } 230 run cr ." Solution = " TEMP f. cr 240 END ; \ =============================================== \ =============================================== \ (4). Manipulation heap : mainF BASIC 10 run root1 20 run Fbisection 30 run root2 40 run Fbisection 50 run root3 60 run Fbisection 100 end ; : mainB root1 Bbisection root2 Bbisection root3 Bbisection ; \ Russia: Moscow Radio Station signed voice : Signal MSO T/2 MSO T/2 HDO 3T/2 MSI T/2 MLA T/2 MSI T/2 HDO T/2 HRE T/2 HDO 1T MSO 2T ; : signed CTONE ADAGIO Signal 500 ms Signal s" DeLaswedge! This, Is, Moscow, Radio! Station! " girlsay ; : main cr ." ===== FORTH style program outcomes ===== " cr s" FORTH style program outcomes " girlsay mainF cr cr ." ===== BASIC style program outcomes ===== " cr s" BASIC style program outcomes " boysay mainB cr ." ============ the end =================== " cr s" The end " boysay signed ; \ =================================================== \s main ===== FORTH style program outcomes ===== Counts = 46 Solution = -59.99999999 Counts = 45 Solution = 67.291782187 Counts = 45 Solution = 88.517724806 ===== BASIC style program outcomes ===== Steps = 46 Solution = -59.99999999 Steps = 45 Solution = 67.291782187 Steps = 45 Solution = 88.517724806 ============ the end ===================
\ A procedure to evaluate a determinant by the leading diagonal method, using largest pivots. 2008-08-23 \ Lina64 version 20180508 \ ****************************** \ (1).Data structure declared heap 6 integers N I J K P Q 2 reals a d 20 20 (MATRIX) IA 20 20 MATRIX AA \ ****************************** \ (2).Data input heap : INPUT-IDATA [[ N = 4 ]] [[ IA ( 1 1 ) = 1 ]] [[ IA ( 1 2 ) = 2 ]] [[ IA ( 1 3 ) = 3 ]] [[ IA ( 1 4 ) = 4 ]] [[ IA ( 2 1 ) = 2 ]] [[ IA ( 2 2 ) = 3 ]] [[ IA ( 2 3 ) = 4 ]] [[ IA ( 2 4 ) = 1 ]] [[ IA ( 3 1 ) = 3 ]] [[ IA ( 3 2 ) = 4 ]] [[ IA ( 3 3 ) = 1 ]] [[ IA ( 3 4 ) = 2 ]] [[ IA ( 4 1 ) = 4 ]] [[ IA ( 4 2 ) = 1 ]] [[ IA ( 4 3 ) = 2 ]] [[ IA ( 4 4 ) = 3 ]] ; : INPUT-DATA INPUT-IDATA basic 10 for i = 1 to N 20 for j = 1 to N 30 let { AA ( i j ) = i>r ( IA ( i j ) ) } 40 next j 50 next i 60 end ; \ ******************************* \ (3).Auxiliary instructions heap : SHOW-AA BASIC 10 run cr 20 FOR I = 1 TO N 30 FOR J = 1 TO N 40 let { a = AA ( I J ) } 50 run a fs. 3 spaces 60 NEXT J 70 run cr 80 NEXT I 90 END ; \ ******************************** \ (4).Main code heap 1.0 e -12 fconstant feps : (DET) BASIC \ 10 RUN INPUT-DATA 20 LET { D = f1.0e0 } 30 FOR K = 1 TO ( N - 1 ) 40 LET P = K 50 LET Q = K 60 LET { A = ABS ( AA ( K K ) ) } 70 FOR I = K TO N 80 FOR J = K TO N 90 IF { ABS ( AA ( I J ) ) > A } THEN 110 100 GOTO 140 110 LET P = I 120 LET Q = J 130 LET { A = ABS ( AA ( I J ) ) } 140 NEXT J 150 NEXT I \ End of search for largest element. 160 IF P <> K THEN 180 170 GOTO 230 180 FOR J = K TO N 190 LET { A = AA ( P J ) } 200 LET { AA ( P J ) = AA ( K J ) } 210 LET { AA ( K J ) = NEGATE ( A ) } 220 NEXT J \ End of interchange of rows P and K. 230 IF Q <> K THEN 250 240 GOTO 300 250 FOR I = K TO N 260 LET { A = AA ( I Q ) } 270 LET { AA ( I Q ) = AA ( I K ) } 280 LET { AA ( I K ) = NEGATE ( A ) } 290 NEXT I \ End of interchange of columns Q and K. \ Largest element is now the pivot. 300 LET { D = D * AA ( K K ) } 310 IF { ABS ( D ) < feps } THEN 430 320 FOR I = K + 1 TO N 330 FOR J = K + 1 TO N 340 LET { A = AA ( K J ) * AA ( I K ) / AA ( K K ) } 350 LET { AA ( I J ) = AA ( I J ) - A } 360 IF { ABS ( AA ( I J ) ) < ABS ( A ) * feps } THEN 380 370 GOTO 390 380 LET { AA ( I J ) = f0.0e0 } 390 NEXT J 400 NEXT I \ End of reduction to upper triangular form. 410 NEXT K 420 LET { D = D * AA ( N N ) } 430 IF { ABS ( D ) > feps } THEN 450 440 LET { D = f0.0e0 } 450 LET { D = D } \ Determinant is now in D \ 460 PRINT { D } 470 END ; \ ********************************* \ (5).Manipulation heap : round ( f1 -- f2 ) over abs nlog 12 > if fround then ; : DET BASIC 10 RUN INPUT-DATA 20 run cr ." matrix " n . ." X " n . ." : " cr show-aa cr 30 IF N = 1 THEN 50 40 GOTO 70 50 LET { D = AA ( 1 1 ) } 60 GOTO 120 70 IF N = 2 THEN 90 80 GOTO 110 90 LET { D = AA ( 1 1 ) * AA ( 2 2 ) - AA ( 1 2 ) * AA ( 2 1 ) } 100 GOTO 120 110 RUN (DET) 120 PRINT { " determinant = " ; D } 130 let { d = d round } 140 run cr ." determinant = " d f. cr 150 END ; det \ ********** the end *************** \ det = 160
\ Calculating Fourier coefficients in Lina64 ABC forth \ =============================================== \ (1) Data structure declare heap 12 value km km 2 / value nm 2 integers k n km array f km array t km array u nm array a nm array b 5 reals c d q r m \ =============================================== \ =============================================== \ (2) Data input heap : input {{ m = i>r ( nm ) }} {{ f ( 0 ) = 1.0 e 0 }} {{ f ( 1 ) = 6.666 e -1 }} {{ f ( 2 ) = 3.333 e -1 }} {{ f ( 3 ) = 0.0 e 0 }} {{ f ( 4 ) = -3.333 e -1 }} {{ f ( 5 ) = -6.666 e -1 }} {{ f ( 6 ) = -1.0 e 0 }} {{ f ( 7 ) = -6.666 e -1 }} {{ f ( 8 ) = -3.333 e -1 }} {{ f ( 9 ) = 0.0 e 0 }} {{ f ( 10 ) = 3.333 e -1 }} {{ f ( 11 ) = 6.666 e -1 }} {{ f ( 12 ) = 1.0 e 0 }} ; \ All parts above are changeable setting. \ =============================================== \ =============================================== \ All parts below are fixed. \ (3) Main code heap : main basic 10 run input 20 for n = 1 to nm 30 let { q = 0.0 e 0 } 40 let { r = 0.0 e 0 } 50 for k = 1 to km 60 let { c = i>r ( k ) } 70 let { d = i>r ( n ) } 80 let { t ( k ) = ( f ( k ) * cos ( ( d * c * f(pi) ) / m ) ) / m } 90 let { u ( k ) = ( f ( k ) * sin ( ( d * c * f(pi) ) / m ) ) / m } 100 let { q = q + t ( k ) } 110 let { r = r + u ( k ) } 120 next k 130 let { a ( n ) = q } 140 let { b ( n ) = r } 150 print " format 1================== " 160 print " ( " ; n ; " ):: " , { q , r } 200 print " format 2=================== " 210 print " a ( " ; n ; " ) = " ; { q } 220 print " b ( " ; n ; " ) = " ; { r } 300 print " format 3=================== " 310 run cr ." a ( " n . ." ) = " q f. 320 run cr ." b ( " n . ." ) = " r f. cr cr 400 next n 410 end ; \ =============================================== \ =============================================== \ (4) Manipulation heap : check main basic 20 for k = 0 to 360 30 let { c = i>r ( k ) } 40 let { d = f(2pi) * c / 360.0 e 0 } 50 let { m = a ( 1 ) * cos ( d ) + a ( 3 ) * cos ( 3.0 e 0 * d ) + a ( 5 ) * cos ( 5.0 e 0 * d ) } 60 run cr d f. 5 spaces m f. 100 next k 110 end ; \ ================================================
\ Integrations in lina64 ABC FORTH \ Integration.f \ Author: Ching-Tang Tseng \ 20161107, Hamilton, NZ 9 reals a b h sum1 sum2 x f(x) f(a) f(b) 2 integers n i 1000 value approximations : 1/x ( -- ) basic 10 let { f(x) = 1. E 0 / x } 20 end ; : 1/(1+x**2) ( -- ) basic 10 let { f(x) = 1. E 0 / ( 1.0 e 0 + x * x ) } 20 end ; defer function ' 1/x is function : range1/x ( -- ) basic 10 let n = approximations 20 let { a = f1.0e0 } 30 let { b = 100.0 e 0 } 40 end ; : range1/(1+x**2) ( -- ) basic 10 let n = approximations 20 let { a = f0.0e0 } 30 let { b = f2.0e0 } 40 end ; defer setting ' range1/x is setting \ All parts above are changeable setting. \ All parts below are fixed. : SimpsonInit ( -- ) basic 10 run Setting 20 let { h = ( b - a ) / I>R ( n ) } 30 let { x = a + h / 2. E 0 } 40 run function 50 let { sum1 = f(x) } 60 let { sum2 = 0. E 0 } 70 let { x = a } 80 run function 90 let { f(a) = f(x) } 100 let { x = b } 110 run function 120 let { f(b) = f(x) } 130 end ; : Simpson ( -- ) basic 10 run SimpsonInit 20 for i = 1 to n - 1 30 let { x = a + h * I>R ( i ) + h / 2. E 0 } 40 run function 50 let { sum1 = sum1 + f(x) } 60 let { x = a + h * I>R ( i ) } 70 run function 80 let { sum2 = sum2 + f(x) } 90 next i 100 print " Simpson integration = " ; { ( h / 6. E 0 ) * ( f(a) + f(b) + 4. E 0 * sum1 + 2. E 0 * sum2 ) } 110 end ; : L-Rectangle ( -- ) basic 10 run Setting 20 let { h = ( b - a ) / I>R ( n ) } 30 let { x = a } 40 let { sum1 = 0. E 0 } 50 for i = 1 to n 60 run function 70 let { sum1 = sum1 + h * f(x) } 80 let { x = x + h } 90 next i 100 print " L-Rectangle integration = " ; { sum1 } 110 end ; : R-Rectangle ( -- ) basic 10 run Setting 20 let { h = ( b - a ) / I>R ( n ) } 30 let { x = a } 40 let { sum1 = 0. E 0 } 50 for i = 1 to n 60 let { x = x + h } 70 run function 80 let { sum1 = sum1 + h * f(x) } 90 next i 100 print " R-Rectangle integration = " ; { sum1 } 110 end ; : M-Rectangle ( -- ) basic 10 run Setting 20 let { h = ( b - a ) / I>R ( n ) } 30 let { x = a } 40 let { sum1 = 0. E 0 } 50 for i = 1 to n 60 let { x = x + h / 2. E 0 } 70 run function 80 let { sum1 = sum1 + h * f(x) } 90 let { x = x + h / 2. E 0 } 100 next i 110 print " M-Rectangle integration = " ; { sum1 } 120 end ; : Trapezoidal ( -- ) basic 10 run Setting 20 let { h = ( b - a ) / I>R ( n ) } 30 let { sum1 = 0. E 0 } 40 let { x = b } 50 run function 60 let { f(b) = f(x) } 70 let { x = a } 80 run function 90 let { f(a) = f(x) } 100 let { sum1 = h * ( f(b) + f(a) ) / 2. E 0 } 110 for i = 1 to n - 1 120 let { x = x + h } 130 run function 140 let { sum1 = sum1 + h * f(x) } 150 next i 160 print " Trapezoidal integration = " ; { sum1 } 170 end ; \ Output description depends on input setting. : integrations ( -- ) L-Rectangle R-Rectangle M-Rectangle Trapezoidal Simpson ; : function1 cr cr ." Integration of 1/x from 1 to 100 = ln(100) " cr integrations cr cr 18 spaces ." ln(100) = " {{ ln ( 100.0 e 0 ) }} fs. cr ; : function2 ( -- ) cr cr ." Integration of 1/(1+x^2) from 0 to 2 = atan(2) " cr integrations cr cr 18 spaces ." atan(2) = " {{ atan ( f2.0e0 ) }} fs. cr ; : (main) ['] 1/x is function ['] range1/x is setting function1 ['] 1/(1+x**2) is function ['] range1/(1+x**2) is setting function2 ; : main cr ." =====Integration===== " cr ." approximations = 1000 " 1000 to approximations (main) cr ." ========================================= " cr ." approximations = 10000 " 10000 to approximations (main) cr ." =======the end======= " ; main \s ching@center:~$ cd lina64 ching@center:~/lina64$ ./f5124 AMDX86 ciforth 5.3.0 fload 94 A : ISN'T UNIQUE B : ISN'T UNIQUE I : ISN'T UNIQUE =====Integration===== approximations = 1000 Integration of 1/x from 1 to 100 = ln(100) L-Rectangle integration = 4.6549910575 E 0 R-Rectangle integration = 4.5569810575 E 0 M-Rectangle integration = 4.6047625486 E 0 Trapezoidal integration = 4.6059860575 E 0 Simpson integration = 4.6051703849 E 0 ln(100) = 4.6051701859 E 0 Integration of 1/(1+x^2) from 0 to 2 = atan(2) L-Rectangle integration = 1.1079486644 E 0 R-Rectangle integration = 1.1063486644 E 0 M-Rectangle integration = 1.1071487444 E 0 Trapezoidal integration = 1.1071486644 E 0 Simpson integration = 1.1071487177 E 0 atan(2) = 1.1071487177 E 0 ========================================= approximations = 10000 Integration of 1/x from 1 to 100 = ln(100) L-Rectangle integration = 4.6100788525 E 0 R-Rectangle integration = 4.6002778525 E 0 M-Rectangle integration = 4.6051661027 E 0 Trapezoidal integration = 4.6051783525 E 0 Simpson integration = 4.605170186 E 0 ln(100) = 4.6051701859 E 0 Integration of 1/(1+x^2) from 0 to 2 = atan(2) L-Rectangle integration = 1.1072287172 E 0 R-Rectangle integration = 1.1070687172 E 0 M-Rectangle integration = 1.107148718 E 0 Trapezoidal integration = 1.1071487172 E 0 Simpson integration = 1.1071487177 E 0 atan(2) = 1.1071487177 E 0 =======the end======= OK
\ magic square matrics generator \ Author: Ching-Tang Tseng \ 20150528, Hamilton, New Zealand \ ****************************** \ (1).Data structure declared heap \ Attention! max is a reserved word of mathematical function. 10 integers x y l n q sqd2 q2 nr maxx msum 100 100 (matrix) sq \ *********************************** \ (2).Data input heap : 4n+2init basic \ 10 let n = 6 20 let msum = n * ( n ** 2 + 1 ) / 2 30 let sqd2 = n / 2 :: q2 = sqd2 ** 2 40 let l = ( n - 2 ) / 4 50 let x = sqd2 / 2 + 1 :: y = 1 60 let nr = 1 70 end ; : 2n+1init basic \ 10 let n = 7 20 let nr = 1 30 let x = n - ( n / 2 ) 40 let y = 1 50 let msum = n * ( n ** 2 + 1 ) / 2 50 let maxx = n * n 60 end ; : 4ninit basic \ 10 let n = 8 20 let msum = n * ( n ** 2 + 1 ) / 2 30 let q = n / 4 40 let nr = 1 50 end ; \ ******************************** \ (3).Auxiliary instructions heap : check-columms-and-rows basic 10 For y = 1 To n 20 let nr = 0 :: q = 0 30 For x = 1 To n 40 let nr = nr + sq ( x y ) 50 let q = q + sq ( y x ) 60 Next x 70 If nr <> msum Or q <> msum Then 90 80 goto 100 90 run cr x 4 .r y 4 .r cr -1 abort" Error1: value <> magic sum " cr 100 Next y 110 end ; : check-diagonals basic 10 let nr = 0 :: q = 0 20 For x = 1 To n 30 let nr = nr + sq ( x x ) 40 let q = q + sq ( x ( n - x + 1 ) ) 50 Next x 60 If nr <> msum Or q <> msum Then 80 70 goto 90 80 run cr nr . 3 spaces q . 3 spaces cr -1 abort" Error2: value <> magic sum " cr 90 end ; : EraseSq basic 10 for y = 1 to 100 20 for x = 1 to 100 30 let sq ( x y ) = 0 40 next x 50 next y 60 end ; : PrintSq basic 10 Print " Magic square size : " ; n ; " * " ; n 20 Print " The magic sum = " ; msum 30 run cr 40 For y = 1 To n 50 For x = 1 To n 60 let q = sq ( x y ) 70 run q 4 .R 80 Next x 90 run cr 100 Next y 110 end ; \ ******************************* \ (4).Main code heap : 2n+1 basic 10 if sq ( x y ) = 0 then 30 20 goto 100 30 let sq ( x y ) = nr 40 if nr mod n = 0 then 70 50 let x = x + 1 55 let y = y - 1 60 goto 80 70 let y = y + 1 80 let nr = nr + 1 100 if x > n then 120 110 goto 200 120 let x = 1 130 if sq ( x y ) <> 0 then 150 140 goto 200 150 let x = x + 1 200 if y < 1 then 220 210 goto 300 220 let y = n 230 if sq ( x y ) <> 0 then 250 240 goto 300 250 let y = y - 1 300 if nr > maxx then 400 310 goto -10 400 end ; : 4n basic 10 for y = 1 to n 20 for x = q + 1 to n - q 30 let sq ( x y ) = 1 40 next x 50 next y 60 For x = 1 To n 70 For y = q + 1 To n - q 80 let sq ( x y ) = sq ( x y ) xor 1 90 Next y 100 Next x 110 let q = n * n + 1 120 for y = 1 To n 130 for x = 1 To n 140 if sq ( x y ) = 0 Then 170 150 let sq ( x y ) = nr 160 goto 180 170 let sq ( x y ) = q - nr 180 let nr = nr + 1 190 Next x 200 Next y 210 end ; \ Attention! in ABC FORTH, index can not to be an algebraic experssion : x+sqd2 x sqd2 + ; : y+sqd2 y sqd2 + ; : l+1 l 1+ ; : 4n+2 basic 10 if sq ( x y ) = 0 then 30 20 goto 130 30 let sq ( x y ) = nr 40 let sq ( x+sqd2 y+sqd2 ) = nr + q2 50 let sq ( x+sqd2 y ) = nr + q2 * 2 60 let sq ( x y+sqd2 ) = nr + q2 * 3 70 if nr mod sqd2 = 0 then 90 80 goto 110 90 let y = y + 1 100 goto 120 110 let x = x + 1 :: y = y - 1 120 let nr = nr + 1 130 if x > sqd2 then 150 140 goto 200 150 let x = 1 160 if sq ( x y ) <> 0 then 180 170 goto 200 180 let x = x + 1 200 if y < 1 then 220 210 goto 260 220 let y = sqd2 230 if sq ( x y ) <> 0 then 250 240 goto 260 250 let y = y - 1 260 if nr > q2 then 300 270 goto -10 300 for y = 1 to sqd2 310 for x = 1 to l 320 let q = sq ( x y ) 330 let sq ( x y ) = sq ( x y+sqd2 ) 340 let sq ( x y+sqd2 ) = q 350 next x 360 next y 400 let y = ( sqd2 / 2 ) + 1 410 let q = sq ( 1 y ) 420 let sq ( 1 y ) = sq ( 1 y+sqd2 ) 430 let sq ( 1 y+sqd2 ) = q 440 let q = sq ( l+1 y ) 450 let sq ( l+1 y ) = sq ( l+1 y+sqd2 ) 460 let sq ( l+1 y+sqd2 ) = q 500 for y = 1 to sqd2 510 for x = n - l + 2 to n 520 let q = sq ( x y ) 530 let sq ( x y ) = sq ( x y+sqd2 ) 540 let sq ( x y+sqd2 ) = q 550 next x 560 next y 1000 end ; \ ********************** \ (5).Manipulation heap : magics ( n -- ) [[ n ]] ! EraseSq basic 10 if n mod 2 = 1 then 100 20 if n mod 4 = 2 then 200 30 goto 300 100 run 2n+1Init 110 run 2n+1 120 goto 400 200 run 4n+2Init 210 run 4n+2 220 goto 400 300 run 4nInit 310 run 4n 400 run PrintSq 500 run check-columms-and-rows 510 run check-diagonals 1000 end ; : main ( -- ) 21 3 do i magics loop ; main \ ********** the end ***************
沒有留言:
張貼留言