Post Reply 
[VA] SRC #012a - Then and Now: Probability
10-16-2022, 05:29 PM
Post: #49
RE: [VA] SRC #012a - Then and Now: Probability
We can cut memory required more than half, by using symmetry.
Removed W, we reduced memory to 2 arrays, P and Q:

Let C = ceil(R/2)

P array elements =     R   *   C
Q array elements = (R+2)*(C+2)

Below code had P and Q with same dimensions, to take advantage of fast MAT Q=P (*)

Total memory (elements) = (R+2)*(C+2) * 2 < (R+3.5)^2


10 DESTROY ALL @ INPUT "[VA]SRC012A R,S= ";R,S @ T0=TIME
20 C=CEIL(R/2) @ OPTION BASE 0 @ REAL P(R+1,C+1),Q(R+1,C+1)
30 P(1,1)=1 @ M=2
40 FOR K=1 TO S
50 MAT Q=P @ Q(1,1)=Q(1,1)*3 ! BUILD Q
60 FOR I=2 TO M-(M=R) @ Q(I,1)=Q(I,1)*1.5 @ J=CEIL(I/2) @ Q(I,J+1)=Q(I,I-J) @ NEXT I
70 IF M<>R THEN 100
80 FOR J=2 TO C @ Q(R,J)=Q(R,J)*1.5 @ NEXT J
90 Q(R,C+1)=Q(R,R-C) @ Q(R,1)=Q(R,1)*3
100 FOR I=1 TO M @ FOR J=1 TO CEIL(I/2) ! BUILD P
110 P(I,J)=Q(I+1,J)+Q(I+1,J+1)+Q(I,J-1)+Q(I,J+1)+Q(I-1,J-1)+Q(I-1,J)
120 NEXT J @ NEXT I
130 IF M<R THEN M=M+1
140 DISP K;TIME-T0
150 NEXT K
160 T=0 @ FOR J=1 TO R-C @ T=T+P(R,J) @ NEXT J @ T=2*T+(2*C-R)*P(R,C)
170 DISP TIME-T0;R;S;T/6^S

>RUN
[VA]SRC012A R,S= 30,60
...
49.11      30 60      9.51234350203E-6

Code run even faster! 5× speed, against C.Ret orignial code (245 sec)


(*) I don't really need a copy, swapping name of array (P,Q) is enough.
Too bad ... VARSWAP don't work for arrays.

>VARSWAP P, Q
ERR:Data Type
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: [VA] SRC #012a - Then and Now: Probability - Albert Chan - 10-16-2022 05:29 PM



User(s) browsing this thread: 3 Guest(s)