Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,8 @@
generic
Max_Elements : Positive;
type Number is digits <>;
package Moving is
procedure Add_Number (N : Number);
function Moving_Average (N : Number) return Number;
function Get_Average return Number;
end Moving;

View file

@ -0,0 +1,40 @@
with Ada.Containers.Vectors;
package body Moving is
use Ada.Containers;
package Number_Vectors is new Ada.Containers.Vectors
(Element_Type => Number,
Index_Type => Natural);
Current_List : Number_Vectors.Vector := Number_Vectors.Empty_Vector;
procedure Add_Number (N : Number) is
begin
if Natural (Current_List.Length) >= Max_Elements then
Current_List.Delete_First;
end if;
Current_List.Append (N);
end Add_Number;
function Get_Average return Number is
Average : Number := 0.0;
procedure Sum (Position : Number_Vectors.Cursor) is
begin
Average := Average + Number_Vectors.Element (Position);
end Sum;
begin
Current_List.Iterate (Sum'Access);
if Current_List.Length > 1 then
Average := Average / Number (Current_List.Length);
end if;
return Average;
end Get_Average;
function Moving_Average (N : Number) return Number is
begin
Add_Number (N);
return Get_Average;
end Moving_Average;
end Moving;

View file

@ -0,0 +1,19 @@
with Ada.Text_IO;
with Moving;
procedure Main is
package Three_Average is new Moving (Max_Elements => 3, Number => Float);
package Five_Average is new Moving (Max_Elements => 5, Number => Float);
begin
for I in 1 .. 5 loop
Ada.Text_IO.Put_Line ("Inserting" & Integer'Image (I) &
" into max-3: " & Float'Image (Three_Average.Moving_Average (Float (I))));
Ada.Text_IO.Put_Line ("Inserting" & Integer'Image (I) &
" into max-5: " & Float'Image (Five_Average.Moving_Average (Float (I))));
end loop;
for I in reverse 1 .. 5 loop
Ada.Text_IO.Put_Line ("Inserting" & Integer'Image (I) &
" into max-3: " & Float'Image (Three_Average.Moving_Average (Float (I))));
Ada.Text_IO.Put_Line ("Inserting" & Integer'Image (I) &
" into max-5: " & Float'Image (Five_Average.Moving_Average (Float (I))));
end loop;
end Main;

View file

@ -1,22 +0,0 @@
function movingaverage(::Type{T} = Float64; lim::Integer = -1) where T<:Real
buffer = Vector{T}(0)
if lim == -1
# unlimited buffer
return (y::T) -> begin
push!(buffer, y)
return mean(buffer)
end
else
# limited size buffer
return (y) -> begin
push!(buffer, y)
if length(buffer) > lim shift!(buffer) end
return mean(buffer)
end
end
end
test = movingaverage()
@show test(1.0) # mean([1])
@show test(2.0) # mean([1, 2])
@show test(3.0) # mean([1, 2, 3])

View file

@ -0,0 +1,29 @@
using Statistics
"""
Return a function that computes a moving average over a buffer of values.
Optional Named Arguments:
- ` buffer = buffer::Vector{T}`: optional initial buffer of values of
type `T` (default empty Float64 array)
- `limit = limit::Integer`: optional maximum length of the buffer;
if -1 (default), the buffer is unlimited
Returns:
- a function `(y::T) -> mean(buffer)` that, when called with a new value `y`,
appends it to the buffer (and removes the oldest value if the buffer exceeds
`limit`), and returns the mean of the buffer
"""
function movingaverage(; buffer::Vector{T} = Float64[], limit::Integer = -1) where T <: Real
return (y::T) -> begin
push!(buffer, T(y))
limit > 0 && length(buffer) > limit && popfirst!(buffer)
return mean(buffer)
end
end
test = movingaverage()
@show test(1.0) # mean([1])
@show test(2.0) # mean([1, 2])
@show test(3.0) # mean([1, 2, 3])

View file

@ -1,13 +1,13 @@
function sma(period)
local t = {}
function sum(t)
sum = 0
local function sum(t)
local tot = 0
for _, v in ipairs(t) do
sum = sum + v
tot = tot + v
end
return sum
return tot
end
function average(n)
local function average(n)
if #t == period then table.remove(t, 1) end
t[#t + 1] = n
return sum(t) / #t

View file

@ -0,0 +1,62 @@
with javascript_semantics
-- (optional) put first part in sma.e for encapsulation/reuse
sequence sma = {} -- ((period,history,circnxt))
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)
return iff(l=0?0: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
--</sma.e>
--include sma.e
constant sma3 = new_sma(3)
constant sma5 = new_sma(5)
printf(1," x sma3 sma5\n")
for x in {1,2,3,4,5,5,4,3,2,1} do
atom a3 = moving_average(sma3,x),
a5 = moving_average(sma5,x)
printf(1,"%2g %=6.3g %=4.3g\n",{x,a3,a5})
end for

View file

@ -0,0 +1,24 @@
local fmt = require "fmt"
local function sma(period)
local i = 0
local sum = 0
local storage = {}
return function(input)
if #storage < period then
sum += input
storage:insert(input)
end
sum += input - storage[i + 1]
storage[i + 1] = input
i = (i + 1) % period
return sum / #storage
end
end
local sma3 = sma(3)
local sma5 = sma(5)
print(" x sma3 sma5")
for {1, 2, 3, 4, 5, 5, 4, 3, 2, 1} as x do
fmt.print("%5.3f %5.3f %5.3f", x, sma3(x), sma5(x))
end

View file

@ -0,0 +1,26 @@
#This version allows a user to enter numbers one at a time to figure this into the SMA calculations
$inputs = @() #Create an array to hold all inputs as they are entered.
$period1 = 3 #Define the periods you want to utilize
$period2 = 5
Write-host "Enter numbers to observe their moving averages." -ForegroundColor Green
function getSMA ($inputs, [int]$period) #Function takes a array of entered values and a period (3 and 5 in this case)
{
if($inputs.Count -lt $period){$period = $inputs.Count} #Makes sure that if there's less numbers than the designated period (3 in this case), the number of availble values is used as the period instead.
for($count = 0; $count -lt $period; $count++) #Loop sums the latest available values
{
$result += $inputs[($inputs.Count) - $count - 1]
}
return ($result | ForEach-Object -begin {$sum=0 }-process {$sum+=$_} -end {$sum/$period}) #Gets the average for a given period
}
while($true) #Infinite loop so the user can keep entering numbers
{
try{$inputs += [decimal] (Read-Host)}catch{Write-Host "Enter only numbers" -ForegroundColor Red} #Enter the numbers. Error checking to help mitigate bad inputs (non-number values)
"Added " + $inputs[(($inputs.Count) - 1)] + ", sma($period1) = " + (getSMA $inputs $Period1) + ", sma($period2) = " + (getSMA $inputs $period2)
}

View file

@ -0,0 +1,37 @@
data = "1,2,3,4,5,5,4,3,2,1"
token = Split(data,",")
stream = ""
WScript.StdOut.WriteLine "Number" & vbTab & "SMA3" & vbTab & "SMA5"
For j = LBound(token) To UBound(token)
If Len(stream) = 0 Then
stream = token(j)
Else
stream = stream & "," & token(j)
End If
WScript.StdOut.WriteLine token(j) & vbTab & Round(SMA(stream,3),2) & vbTab & Round(SMA(stream,5),2)
Next
Function SMA(s,p)
If Len(s) = 0 Then
SMA = 0
Exit Function
End If
d = Split(s,",")
sum = 0
If UBound(d) + 1 >= p Then
c = 0
For i = UBound(d) To LBound(d) Step -1
sum = sum + Int(d(i))
c = c + 1
If c = p Then
Exit For
End If
Next
SMA = sum / p
Else
For i = UBound(d) To LBound(d) Step -1
sum = sum + Int(d(i))
Next
SMA = sum / (UBound(d) + 1)
End If
End Function