September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,138 @@
-- FORWARD DIFFERENCE --------------------------------------------------------
-- forwardDifference :: Int -> [Int] -> [Int]
on forwardDifference(n, xs)
set lng to length of xs
-- atLength :: [Int] -> Bool
script atLength
property fullLength : lng
property ndx : n
on |λ|(ds)
(atLength's fullLength) - (length of ds) atLength's ndx
end |λ|
end script
-- fd :: [Int] -> [Int]
script fd
on |λ|(xs)
script minus
on |λ|(a, b)
a - b
end |λ|
end script
zipWith(minus, tail(xs), xs)
end |λ|
end script
|until|(atLength, fd, xs)
end forwardDifference
-- TEST ----------------------------------------------------------------------
on run
set xs to {90, 47, 58, 29, 22, 32, 55, 5, 55, 73}
script test
on |λ|(n)
intercalate(" -> [", ¬
{{n}} & intercalate(", ", forwardDifference(n, xs))) & "]"
end |λ|
end script
intercalate(linefeed, map(test, enumFromTo(1, 9)))
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- min :: Ord a => a -> a -> a
on min(x, y)
if y < x then
y
else
x
end if
end min
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- tail :: [a] -> [a]
on tail(xs)
if length of xs > 1 then
items 2 thru -1 of xs
else
{}
end if
end tail
-- until :: (a -> Bool) -> (a -> a) -> a -> a
on |until|(p, f, x)
set mp to mReturn(p)
set v to x
tell mReturn(f)
repeat until mp's |λ|(v)
set v to |λ|(v)
end repeat
end tell
return v
end |until|
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to min(length of xs, length of ys)
set lst to {}
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, item i of ys)
end repeat
return lst
end tell
end zipWith

View file

@ -1,12 +0,0 @@
REAL :: n=10, list(n)
list = ( 90, 47, 58, 29, 22, 32, 55, 5, 55, 73 )
WRITE(Format='i1, (i6)') 0, list
DO i = 1, n-1
ALIAS(list,1, diff,n-i) ! rename list(1 ... n-i) with diff
diff = list($+1) - diff ! $ is the running left hand array index
WRITE(Format='i1, (i6)') i, diff
ENDDO
END

View file

@ -1,10 +0,0 @@
0 90 47 58 29 22 32 55 5 55 73
1 -43 11 -29 -7 10 23 -50 50 18
2 54 -40 22 17 13 -73 100 -32
3 -94 62 -5 -4 -86 173 -132
4 156 -67 1 -82 259 -305
5 -223 68 -83 341 -564
6 291 -151 424 -905
7 -442 575 -1329
8 1017 -1904
9 -2921

View file

@ -0,0 +1,57 @@
(() => {
'use strict';
// forwardDifference :: [Int] -> [Int]
const forwardDifference = (n, xs) => {
const fd = xs => zipWith((a, b) => a - b, tail(xs), xs);
return until(
m => m.index < 1,
m => ({
index: m.index - 1,
list: fd(m.list)
}), {
index: n,
list: xs
}
)
.list;
};
// GENERIC FUNCTIONS ---------------------------------------
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = (f, xs, ys) => {
const ny = ys.length;
return (xs.length <= ny ? xs : xs.slice(0, ny))
.map((x, i) => f(x, ys[i]));
};
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = (p, f, x) => {
const go = x => p(x) ? x : go(f(x));
return go(x);
};
// tail :: [a] -> [a]
const tail = xs => xs.length ? xs.slice(1) : undefined;
// TEST ----------------------------------------------------
// range :: Int -> Int -> [Int]
const range = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
// show :: a -> String
const show = x => JSON.stringify(x);
// Sample
const test = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73];
return range(1, 9)
.map(x => `${x} ${show(forwardDifference(x, test))}`)
.join('\n');
})();

View file

@ -1 +0,0 @@
ndiff(A, n::Integer) = n < 1 ? A : diff(ndiff(A, n-1))

View file

@ -1,13 +0,0 @@
julia> s = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73]
julia> [ndiff(s, i) for i in 0:9]
10-element Array{Any,1}:
[90,47,58,29,22,32,55,5,55,73]
[-43,11,-29,-7,10,23,-50,50,18]
[54,-40,22,17,13,-73,100,-32]
[-94,62,-5,-4,-86,173,-132]
[156,-67,1,-82,259,-305]
[-223,68,-83,341,-564]
[291,-151,424,-905]
[-442,575,-1329]
[1017,-1904]
[-2921]

View file

@ -0,0 +1,36 @@
// version 1.1.2
fun forwardDifference(ia: IntArray, order: Int): IntArray {
if (order < 0) throw IllegalArgumentException("Order must be non-negative")
if (order == 0) return ia
val size = ia.size
if (size == 0) return ia // same empty array
if (order >= size) return intArrayOf() // new empty array
var old = ia
var new = old
var count = order
while (count-- >= 1) {
new = IntArray(old.size - 1)
for (i in 0 until new.size) new[i] = old[i + 1] - old[i]
old = new
}
return new
}
fun printArray(ia: IntArray) {
print("[")
for (i in 0 until ia.size) {
print("%5d".format(ia[i]))
if (i < ia .size - 1) print(", ")
}
println("]")
}
fun main(args: Array<String>) {
val ia = intArrayOf(90, 47, 58, 29, 22, 32, 55, 5, 55, 73)
for (order in 0..ia.size) {
val fd = forwardDifference(ia, order)
print("%2d".format(order) + ": ")
printArray(fd)
}
}

View file

@ -1 +0,0 @@
Y = diff(X,n);

View file

@ -1,5 +0,0 @@
diff([1 2 3 4 5])
ans =
1 1 1 1

View file

@ -0,0 +1,15 @@
function fwd_diff_n(sequence s, integer order)
if order>=length(s) then ?9/0 end if
for i=1 to order do
for j=1 to length(s)-1 do
s[j] = s[j+1]-s[j]
end for
s = s[1..-2]
end for
return s
end function
constant s = {90, 47, 58, 29, 22, 32, 55, 5, 55, 73}
for i=1 to 9 do
?fwd_diff_n(s,i)
end for

View file

@ -1,37 +1,37 @@
create table list (n integer, v real);
insert into list values (0, 90);
insert into list values (1, 47);
insert into list values (2, 58);
insert into list values (3, 29);
insert into list values (4, 22);
insert into list values (5, 32);
insert into list values (6, 55);
insert into list values (7, 5);
insert into list values (8, 55);
insert into list values (9, 73);
create view diff1 as select list.n, (select next.v from list as next where next.n = list.n + 1) - list.v as v from list;
create view diff2 as select list.n, (select next.v from diff1 as next where next.n = list.n + 1) - list.v as v from diff1 as list;
select * from diff1;
0|-43.0
1|11.0
2|-29.0
3|-7.0
4|10.0
5|23.0
6|-50.0
7|50.0
8|18.0
9|
select * from diff2;
0|54.0
1|-40.0
2|22.0
3|17.0
4|13.0
5|-73.0
6|100.0
7|-32.0
8|
9|
WITH RECURSIVE
T0 (N, ITEM, LIST, NEW_LIST) AS
(
SELECT 1,
NULL,
'90,47,58,29,22,32,55,5,55,73' || ',',
NULL
UNION ALL
SELECT CASE
WHEN SUBSTR(LIST, INSTR(LIST, ',') + 1, LENGTH(LIST)) = ''
THEN N + 1
ELSE N
END,
CASE
WHEN SUBSTR(LIST, INSTR(LIST, ',') + 1, LENGTH(LIST)) <> ''
THEN SUBSTR(LIST, 1, INSTR(LIST, ',') - 1)
ELSE NULL
END,
CASE
WHEN SUBSTR(LIST, INSTR(LIST, ',') + 1, LENGTH(LIST)) = ''
THEN IFNULL(NEW_LIST || (SUBSTR(LIST, 1, INSTR(LIST, ',') - 1) - ITEM) || ',', '')
ELSE SUBSTR(LIST, INSTR(LIST, ',') + 1, LENGTH(LIST))
END,
CASE
WHEN SUBSTR(LIST, INSTR(LIST, ',') + 1, LENGTH(LIST)) <> ''
THEN IFNULL(NEW_LIST, '') || IFNULL((SUBSTR(LIST, 1, INSTR(LIST, ',') - 1) - ITEM) || ',', '')
ELSE NULL
END
FROM T0
WHERE INSTR(LIST, ',') > 0
)
SELECT N,
TRIM(LIST, ',') LIST
FROM T0
WHERE NEW_LIST IS NULL
AND LIST <> ''
ORDER BY N;

View file

@ -0,0 +1,8 @@
forwardDifference(x(1), n) := forwardDifferenceHelper(x, n, [x]);
forwardDifferenceHelper(x(1), n, result(2)) :=
let
difference := tail(x) - x[1 ... size(x) - 1];
in
result when n = 0 or size(x) = 1 else
forwardDifferenceHelper(difference, n - 1, result ++ [difference]);

View file

@ -0,0 +1,3 @@
forwardDifference(x(1),n) :=
x when n = 0 or size(x) = 1 else
forwardDifference(tail(x) - x[1 ... size(x) - 1], n - 1);

View file

@ -1,15 +1,15 @@
func dif(arr) {
gather {
range(0, arr.end-1).each { |i|
take(arr[i+1] - arr[i]);
for i (0 ..^ arr.end) {
take(arr[i+1] - arr[i])
}
}
}
func difn(n, arr) {
n.times { arr = dif(arr) };
arr;
{ arr = dif(arr) } * n
arr
}
say dif([1, 23, 45, 678]); # => [22, 22, 633]
say difn(2, [1, 23, 45, 678]); # => [0, 611]
say dif([1, 23, 45, 678]) # => [22, 22, 633]
say difn(2, [1, 23, 45, 678]) # => [0, 611]

View file

@ -0,0 +1 @@
gen y=x[_n+1]-x[_n]

View file

@ -0,0 +1,10 @@
* First create a dataset
clear all
set obs 100
gen i=_n
tsset i
gen x=rnormal()
* Differences
display "Difference order?" _request(k)
gen y=D${k}F${k}.x

View file

@ -0,0 +1,10 @@
fcn forwardDiff(lst){
if(lst.len()<2)
return(T);
return(T(lst[1]-lst[0]).extend(forwardDiff(lst[1,*])))
}
fcn nthForwardDiff(n,xs){
if(n==0)
return(xs);
return(nthForwardDiff(n-1,forwardDiff(xs))) // tail recursion
}

View file

@ -0,0 +1 @@
nthForwardDiff(9,T(90, 47, 58, 29, 22, 32, 55, 5, 55, 73)).println();