Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,50 @@
|
|||
sequence sma = {} -- {{period,history,circnxt}} (private to sma.e)
|
||||
integer sma_free = 0
|
||||
|
||||
global function new_sma(integer period)
|
||||
integer res
|
||||
if sma_free then
|
||||
res = sma_free
|
||||
sma_free = sma[sma_free]
|
||||
sma[res] = {period,{},0}
|
||||
else
|
||||
sma = append(sma,{period,{},0})
|
||||
res = length(sma)
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
global procedure add_sma(integer sidx, atom val)
|
||||
integer period, circnxt
|
||||
sequence history
|
||||
{period,history,circnxt} = sma[sidx]
|
||||
sma[sidx][2] = 0 -- (kill refcount)
|
||||
if length(history)<period then
|
||||
history = append(history,val)
|
||||
else
|
||||
circnxt += 1
|
||||
if circnxt>period then
|
||||
circnxt = 1
|
||||
end if
|
||||
sma[sidx][3] = circnxt
|
||||
history[circnxt] = val
|
||||
end if
|
||||
sma[sidx][2] = history
|
||||
end procedure
|
||||
|
||||
global function get_sma_average(integer sidx)
|
||||
sequence history = sma[sidx][2]
|
||||
integer l = length(history)
|
||||
if l=0 then return 0 end if
|
||||
return sum(history)/l
|
||||
end function
|
||||
|
||||
global function moving_average(integer sidx, atom val)
|
||||
add_sma(sidx,val)
|
||||
return get_sma_average(sidx)
|
||||
end function
|
||||
|
||||
global procedure free_sma(integer sidx)
|
||||
sma[sidx] = sma_free
|
||||
sma_free = sidx
|
||||
end procedure
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
include sma.e
|
||||
|
||||
constant sma3 = new_sma(3)
|
||||
constant sma5 = new_sma(5)
|
||||
constant s = {1,2,3,4,5,5,4,3,2,1}
|
||||
integer si
|
||||
|
||||
for i=1 to length(s) do
|
||||
si = s[i]
|
||||
printf(1,"%2g: sma3=%8g, sma5=%8g\n",{si,moving_average(sma3,si),moving_average(sma5,si)})
|
||||
end for
|
||||
Loading…
Add table
Add a link
Reference in a new issue