Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -0,0 +1,88 @@
|
|||
* Averages/Simple moving average 26/08/2015
|
||||
AVGSMA CSECT
|
||||
USING AVGSMA,R12
|
||||
LR R12,R15
|
||||
ST R14,SAVER14
|
||||
ZAP II,=P'0' ii=0
|
||||
LA R7,1
|
||||
LH R3,NA
|
||||
SRA R3,1 na/2
|
||||
LOOPA CR R7,R3 do i=1 to na/2
|
||||
BH ELOOPA
|
||||
AP II,=P'1000' ii=ii+1000
|
||||
LR R1,R7 i
|
||||
MH R1,=H'6'
|
||||
LA R4,A-6(R1)
|
||||
MVC 0(6,R4),II a(i)=ii
|
||||
LH R1,NA na
|
||||
SR R1,R7 -i
|
||||
MH R1,=H'6'
|
||||
LA R4,A(R1)
|
||||
MVC 0(6,R4),II a(na+1-i)=ii
|
||||
LA R7,1(R7)
|
||||
B LOOPA
|
||||
ELOOPA XPRNT =CL30' n sma3 sma5 ',30
|
||||
XPRNT =CL30' ----- ----------- -----------',30
|
||||
LA R7,1 i=1
|
||||
LOOP CH R7,NA do i=1 to na
|
||||
BH RETURN
|
||||
STH R7,N n=i
|
||||
XDECO R7,C i
|
||||
MVC BUF+1(5),C+7
|
||||
MVC P,=H'3' p=3
|
||||
BAL R14,SMA
|
||||
MVC C(13),EDMASK
|
||||
ED C(13),SS sma(3,i)
|
||||
MVC BUF+7(11),C+2
|
||||
MVC P,=H'5' p=5
|
||||
BAL R14,SMA
|
||||
MVC C(13),EDMASK
|
||||
ED C(13),SS sma(5,i)
|
||||
MVC BUF+19(11),C+2
|
||||
XPRNT BUF,30 output i,sma3,sma5
|
||||
LA R7,1(R7)
|
||||
B LOOP
|
||||
* ***** sub sma(p,n) returns(PL6)
|
||||
SMA LH R5,N
|
||||
SH R5,P
|
||||
A R5,=F'1' ia=n-p+1
|
||||
C R5,=F'1'
|
||||
BH OKIA
|
||||
LA R5,1 ia=1
|
||||
OKIA LH R6,NA ib=na
|
||||
CH R6,N
|
||||
BL OKIB
|
||||
LH R6,N ib=n
|
||||
OKIB ZAP II,=P'0' ii=0
|
||||
ZAP SS,=P'0' ss=0
|
||||
LR R3,R5 k=ia
|
||||
LOOPK CR R3,R6 do k=ia to ib
|
||||
BH ELOOPK
|
||||
AP II,=P'1' ii=ii+1
|
||||
LR R1,R3
|
||||
MH R1,=H'6'
|
||||
LA R4,A-6(R1)
|
||||
MVC C(6),0(R4) ss=ss+a(k)
|
||||
AP SS,C(6)
|
||||
LA R3,1(R3)
|
||||
B LOOPK
|
||||
ELOOPK ZAP C,SS
|
||||
DP C,II
|
||||
ZAP SS,C(10) ss=ss/ii
|
||||
BR R14
|
||||
RETURN L R14,SAVER14 restore caller address
|
||||
XR R15,R15
|
||||
BR R14
|
||||
SAVER14 DS F
|
||||
NN EQU 10
|
||||
NA DC AL2(NN)
|
||||
A DS (NN)PL6
|
||||
II DS PL6
|
||||
SS DS PL6
|
||||
P DS H
|
||||
N DS H
|
||||
C DS CL16
|
||||
BUF DC CL30' ' buffer
|
||||
EDMASK DC X'4020202020202021204B202020' CL13
|
||||
YREGS
|
||||
END AVGSMA
|
||||
|
|
@ -29,7 +29,7 @@
|
|||
aCount := thePeriod.
|
||||
].
|
||||
|
||||
#var aSum := Summing new:(Real new &int:0) foreach:theList.
|
||||
#var aSum := theList summarize:(Real new &int:0).
|
||||
|
||||
^ aSum / aCount.
|
||||
].
|
||||
|
|
@ -41,15 +41,15 @@
|
|||
#var SMA3 := SMA new:3.
|
||||
#var SMA5 := SMA new:5.
|
||||
|
||||
control forrange &int:1 &int:5 &do: (&int:i)
|
||||
1 to:5 &doEach: (:i)
|
||||
[
|
||||
consoleEx writeLine:"sma3 + " :i :" = ": (SMA3 += i).
|
||||
consoleEx writeLine:"sma5 + " :i :" = ": (SMA5 += i).
|
||||
console write:"sma3 + " :i :" = ": (SMA3 += i) &paddingRight:30 &with:#32.
|
||||
console writeLine:"sma5 + " :i :" = ": (SMA5 += i).
|
||||
].
|
||||
|
||||
control forrange &int:5 &int:1 &do: (&int:i)
|
||||
5 to:1 &doEach: (:i)
|
||||
[
|
||||
consoleEx writeLine:"sma3 + " :i :" = ": (SMA3 += i).
|
||||
consoleEx writeLine:"sma5 + " :i :" = ": (SMA5 += i).
|
||||
console write:"sma3 + " :i :" = ": (SMA3 += i) &paddingRight:30 &with:#32.
|
||||
console writeLine:"sma5 + " :i :" = ": (SMA5 += i).
|
||||
].
|
||||
].
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
program sma;
|
||||
type
|
||||
tsma = record
|
||||
smaValue : array of double;
|
||||
smaAverage,
|
||||
smaSumOld,
|
||||
smaSumNew,
|
||||
smaRezActLength : double;
|
||||
smaActLength,
|
||||
smaLength,
|
||||
smaPos :NativeInt;
|
||||
smaIsntFull: boolean;
|
||||
end;
|
||||
|
||||
procedure smaInit(var sma:tsma;p: NativeUint);
|
||||
Begin
|
||||
with sma do
|
||||
Begin
|
||||
setlength(smaValue,0);
|
||||
setlength(smaValue,p);
|
||||
smaLength:= p;
|
||||
smaActLength := 0;
|
||||
smaAverage:= 0.0;
|
||||
smaSumOld := 0.0;
|
||||
smaSumNew := 0.0;
|
||||
smaPos := p-1;
|
||||
smaIsntFull := true
|
||||
end;
|
||||
end;
|
||||
|
||||
function smaAddValue(var sma:tsma;v: double):double;
|
||||
Begin
|
||||
with sma do
|
||||
Begin
|
||||
IF smaIsntFull then
|
||||
Begin
|
||||
inc(smaActLength);
|
||||
smaRezActLength := 1/smaActLength;
|
||||
smaIsntFull := smaActLength < smaLength ;
|
||||
end;
|
||||
smaSumOld := smaSumOld+v-smaValue[smaPos];
|
||||
smaValue[smaPos] := v;
|
||||
smaSumNew := smaSumNew+v;
|
||||
|
||||
smaPos := smaPos-1;
|
||||
if smaPos < 0 then
|
||||
begin
|
||||
smaSumOld:= smaSumNew;
|
||||
smaSumNew:= 0.0;
|
||||
smaPos := smaLength-1;
|
||||
end;
|
||||
smaAverage := smaSumOld *smaRezActLength;
|
||||
smaAddValue:= smaAverage;
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
sma3,sma5:tsma;
|
||||
i : LongInt;
|
||||
begin
|
||||
smaInit(sma3,3);
|
||||
smaInit(sma5,5);
|
||||
For i := 1 to 5 do
|
||||
Begin
|
||||
write('Inserting ',i,' into sma3 ',smaAddValue(sma3,i):0:4);
|
||||
writeln(' Inserting ',i,' into sma5 ',smaAddValue(sma5,i):0:4);
|
||||
end;
|
||||
For i := 5 downto 1 do
|
||||
Begin
|
||||
write('Inserting ',i,' into sma3 ',smaAddValue(sma3,i):0:4);
|
||||
writeln(' Inserting ',i,' into sma5 ',smaAddValue(sma5,i):0:4);
|
||||
end;
|
||||
//speed test
|
||||
smaInit(sma3,3);
|
||||
For i := 1 to 100000000 do
|
||||
smaAddValue(sma3,i);
|
||||
writeln('100''000''000 insertions ',sma3.smaAverage:0:4);
|
||||
end.
|
||||
|
|
@ -1,24 +1,26 @@
|
|||
/*REXX program illustrates simple moving average using a simple list. */
|
||||
parse arg p q n . /*get some arguments (maybe). */
|
||||
if p=='' then p=3 /*the 1st period (default: 3).*/
|
||||
if q=='' then q=5 /* " 2nd " " 5 */
|
||||
if n=='' then n=10 /*number of items in the list.*/
|
||||
@.=0 /*define stemmed array, init 0*/
|
||||
/*──────────────────────────────────────────build 1st half of the list. */
|
||||
do j=1 for n%2; @.j=j; end /* ··· increasing values.*/
|
||||
/*──────────────────────────────────────────build 2nd half of the list. */
|
||||
do k=n%2 to 1 by -1; @.j=k; j=j+1; end /* ··· decreasing values.*/
|
||||
/*──────────────────────────────────────────perform a simple moving avg.*/
|
||||
/*REXX program illustrates simple moving average using a constructed list. */
|
||||
parse arg p q n . /*get optional arguments from the C.L. */
|
||||
if p=='' then p=3 /*the 1st period (the default is: 3).*/
|
||||
if q=='' then q=5 /* " 2nd " " " " 5).*/
|
||||
if n=='' then n=10 /*the number of items in the list. */
|
||||
@.=0 /*define array with initial zero values*/
|
||||
/* [↓] build 1st half of list*/
|
||||
do j=1 for n%2; @.j=j; end /* ··· increasing values.*/
|
||||
/* [↓] build 2nd half of list*/
|
||||
do k=n%2 to 1 by -1; @.j=k; j=j+1; end /* ··· decreasing values.*/
|
||||
|
||||
say ' ' " SMA with " ' SMA with '
|
||||
say ' number ' " period" p' ' ' period' q
|
||||
say ' ──────── ' "──────────" '──────────'
|
||||
do m=1 for n
|
||||
say center(@.m,10) left(sma(p,m),11) left(sma(q,m),11)
|
||||
end /*m*/ /* [↑] show simple moving avg.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SMA subroutine──────────────────────*/
|
||||
sma: procedure expose @.; parse arg p,j; s=0; i=0
|
||||
do k=max(1,j-p+1) to j+p for p while k<=j; i=i+1
|
||||
s=s+@.k
|
||||
end /*k*/
|
||||
|
||||
/* [↓] perform a simple moving average*/
|
||||
do m=1 for n
|
||||
say center(@.m, 10) left(sma(p,m), 11) left(sma(q,m), 11)
|
||||
end /*m*/ /* [↑] show a simple moving average.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
sma: procedure expose @.; parse arg p,j; s=0; i=0
|
||||
do k=max(1,j-p+1) to j+p for p while k<=j; i=i+1
|
||||
s=s+@.k
|
||||
end /*k*/
|
||||
return s/i
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue