langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,63 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
numeric digits 20
class RAvgSimpleMoving public
properties private
window = java.util.Queue
period
sum
properties constant
exMsg = 'Period must be a positive integer'
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method RAvgSimpleMoving(period_) public
if \period_.datatype('w') then signal RuntimeException(exMsg)
if period_ <= 0 then signal RuntimeException(exMsg)
window = LinkedList()
period = period_
sum = 0
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method newNum(num) public
sum = sum + num
window.add(num)
if window.size() > period then do
rmv = (Rexx window.remove())
sum = sum - rmv
end
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getAvg() public returns Rexx
if window.isEmpty() then do
avg = 0
end
else do
avg = sum / window.size()
end
return avg
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method run_samples(args = String[]) public static
testData = [Rexx 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
windowSizes = [Rexx 3, 5]
loop windSize over windowSizes
ma = RAvgSimpleMoving(windSize)
loop xVal over testData
ma.newNum(xVal)
say 'Next number =' xVal.right(5)', SMA =' ma.getAvg().format(10, 9)
end xVal
say
end windSize
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method main(args = String[]) public static
run_samples(args)
return

View file

@ -0,0 +1,25 @@
let sma (n, s, q) x =
let l = Queue.length q and s = s +. x in
Queue.push x q;
if l < n then
(n, s, q), s /. float (l + 1)
else (
let s = s -. Queue.pop q in
(n, s, q), s /. float l
)
let _ =
let periodLst = [ 3; 5 ] in
let series = [ 1.; 2.; 3.; 4.; 5.; 5.; 4.; 3.; 2.; 1. ] in
List.iter (fun d ->
Printf.printf "SIMPLE MOVING AVERAGE: PERIOD = %d\n" d;
ignore (
List.fold_left (fun o x ->
let o, m = sma o x in
Printf.printf "Next number = %-2g, SMA = %g\n" x m;
o
) (d, 0., Queue.create ()) series;
);
print_newline ();
) periodLst

View file

@ -0,0 +1,22 @@
let sma_create period =
let q = Queue.create ()
and sum = ref 0.0 in
fun x ->
sum := !sum +. x;
Queue.push x q;
if Queue.length q > period then
sum := !sum -. Queue.pop q;
!sum /. float (Queue.length q)
let () =
let periodLst = [ 3; 5 ] in
let series = [ 1.; 2.; 3.; 4.; 5.; 5.; 4.; 3.; 2.; 1. ] in
List.iter (fun d ->
Printf.printf "SIMPLE MOVING AVERAGE: PERIOD = %d\n" d;
let sma = sma_create d in
List.iter (fun x ->
Printf.printf "Next number = %-2g, SMA = %g\n" x (sma x);
) series;
print_newline ();
) periodLst

View file

@ -0,0 +1,83 @@
#import <Foundation/Foundation.h>
@interface MovingAverage : NSObject {
unsigned int period;
NSMutableArray *window;
double sum;
}
@end
@implementation MovingAverage
// init with default period
- (id)init {
self = [super init];
if(self) {
period = 10;
window = [[NSMutableArray alloc] init];
sum = 0.0;
}
return self;
}
// init with specified period
- (id)initWithPeriod:(unsigned int)thePeriod {
self = [super init];
if(self) {
period = thePeriod;
window = [[NSMutableArray alloc] init];
sum = 0.0;
}
return self;
}
// clear
- (void)dealloc {
[window release];
[super dealloc];
}
// add a new number to the window
- (void)add:(double)val {
sum += val;
[window addObject:[NSNumber numberWithDouble:val]];
if([window count] > period) {
NSNumber *n = [window objectAtIndex:0];
sum -= [n doubleValue];
[window removeObjectAtIndex:0];
}
}
// get the average value
- (double)avg {
if([window count] == 0) {
return 0; // technically the average is undefined
}
return sum / [window count];
}
// set the period, resizes current window
- (void)setPeriod:(unsigned int)thePeriod {
// make smaller?
if(thePeriod < [window count]) {
for(int i = 0; i < thePeriod; ++i) {
NSNumber *n = [window objectAtIndex:0];
sum -= [n doubleValue];
[window removeObjectAtIndex:0];
}
}
period = thePeriod;
}
// get the period (window size)
- (unsigned int)period {
return period;
}
// clear the window and current sum
- (void)clear {
[window removeAllObjects];
sum = 0;
}
@end

View file

@ -0,0 +1,11 @@
double testData[10] = {1,2,3,4,5,5,4,3,2,1};
int periods[2] = {3,5};
for(int i = 0; i < 2; ++i) {
MovingAverage *ma = [[MovingAverage alloc] initWithPeriod:periods[i]];
for(int j = 0; j < 10; ++j) {
[ma add:testData[j]];
NSLog(@"Next number = %f, SMA = %f", testData[j], [ma avg]);
}
[ma release];
NSLog(@"\n");
}

View file

@ -0,0 +1,42 @@
def max 1000
Class MovingAverage
'==================
indexbase 1
double average,invperiod,mdata[max]
sys index,period
method Setup(double a,p)
sys i
Period=p
invPeriod=1/p
index=0
average=a
for i=1 to period
mdata[i]=a
next
end method
method Data(double v) as double
sys i
index++
if index>period then index=1 'recycle
i=index+1 'for oldest data
if i>period then i=1 'recycle
mdata[index]=v
average=average+invperiod*(v-mdata[i])
end method
end class
'TEST
'====
MovingAverage A
A.Setup 100,10 'initial value and period
A.data 50
'...
print A.average 'reult 95

View file

@ -0,0 +1,27 @@
declare
fun {CreateSMA Period}
Xs = {NewCell nil}
in
fun {$ X}
Xs := {List.take X|@Xs Period}
{FoldL @Xs Number.'+' 0.0}
/
{Int.toFloat {Min Period {Length @Xs}}}
end
end
in
for Period in [3 5] do
SMA = {CreateSMA Period}
in
{System.showInfo "\nSTART PERIOD "#Period}
for I in 1..5 do
{System.showInfo " Number = "#I#" , SMA = "#{SMA {Int.toFloat I}}}
end
for I in 5..1;~1 do
{System.showInfo " Number = "#I#" , SMA = "#{SMA {Int.toFloat I}}}
end
end

View file

@ -0,0 +1,5 @@
sma_per(n)={
sma_v=vector(n);
sma_i = 0;
n->if(sma_i++>#sma_v,sma_v[sma_i=1]=n;0,sma_v[sma_i]=n;0)+sum(i=1,#sma_v,sma_v[i])/#sma_v
};

View file

@ -0,0 +1,20 @@
SMA: procedure (N) returns (float byaddr);
declare N fixed;
declare A(*) fixed controlled,
(p, q) fixed binary static initial (0);
if allocation(A) = 0 then signal error;
p = p + 1; if q < 20 then q = q + 1;
if p > hbound(A, 1) then p = 1;
A(p) = N;
return (sum(float(A))/q);
I: ENTRY (Period);
declare Period fixed binary;
if allocation(A) > 0 then FREE A;
allocate A(Period);
A = 0;
p = 0;
end SMA;

View file

@ -0,0 +1,10 @@
sub sma (Int $period where (* > 0)) returns Sub {
my $sum = 0;
my @a;
return sub ($x) {
@a.push: $x;
$sum += $x;
$sum -= @a.shift if @a > $period;
return $sum / @a;
}
}

View file

@ -0,0 +1,19 @@
Procedure.d SMA(Number, Period=0)
Static P
Static NewList L()
Protected Sum=0
If Period<>0
P=Period
EndIf
LastElement(L())
AddElement(L())
L()=Number
While ListSize(L())>P
FirstElement(L())
DeleteElement(L(),1)
Wend
ForEach L()
sum+L()
Next
ProcedureReturn sum/ListSize(L())
EndProcedure

View file

@ -0,0 +1,8 @@
:1->C
:While 1
:Prompt I
:C->dim(L1)
:I->L1(C)
:Disp mean(L1)
:1+C->C
:End

View file

@ -0,0 +1,10 @@
movinavg(list,p)
Func
Local r, i, z
For i,1,dim(list)
max(i-p,0)→z
sum(mid(list,z+1,i-z))/(i-z)→r[i]
EndFor
r
EndFunc

View file

@ -0,0 +1,11 @@
movinav2(x_,v_)
Prgm
If getType(x_)="STR" Then
{}→list
v_→p
Return
EndIf
right(augment(list,{x_}),p)→list
sum(list)/dim(list)→#v_
EndPrgm