Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,4 +1,8 @@
|
|||
Provide code that produces a list of numbers which is the n-th order forward difference, given a non-negative integer (specifying the order) and a list of numbers. The first-order forward difference of a list of numbers (A) is a new list (B) where B<sub>n</sub> = A<sub>n+1</sub> - A<sub>n</sub>. List B should have one fewer element as a result. The second-order forward difference of A will be the same as the first-order forward difference of B. That new list will have two fewer elements than A and one less than B. The goal of this task is to repeat this process up to the desired order.
|
||||
Provide code that produces a list of numbers which is the n-th order forward difference, given a non-negative integer (specifying the order) and a list of numbers.
|
||||
The first-order forward difference of a list of numbers (A) is a new list (B) where B<sub>n</sub> = A<sub>n+1</sub> - A<sub>n</sub>. List B should have one fewer element as a result.
|
||||
The second-order forward difference of A will be the same as the first-order forward difference of B.
|
||||
That new list will have two fewer elements than A and one less than B.
|
||||
The goal of this task is to repeat this process up to the desired order.
|
||||
|
||||
For a more formal description, see the related [http://mathworld.wolfram.com/ForwardDifference.html Mathworld article].
|
||||
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ int main()
|
|||
std::vector<double> dest;
|
||||
nth_forward_difference(1, array, array+10, std::back_inserter(dest));
|
||||
|
||||
// outut dest
|
||||
// output dest
|
||||
std::copy(dest.begin(), dest.end(), std::ostream_iterator<double>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,20 +1,19 @@
|
|||
import std.stdio;
|
||||
|
||||
T[] forwardDifference(T)(in T[] data, in int level) pure nothrow
|
||||
in {
|
||||
assert(level >= 0 && level < data.length);
|
||||
} body {
|
||||
//auto result = data.dup; // not nothrow
|
||||
auto result = data ~ []; // slower
|
||||
foreach (i; 0 .. level)
|
||||
foreach (j, ref el; result[0 .. $ - i - 1])
|
||||
el = result[j + 1] - el;
|
||||
result.length -= level;
|
||||
return result;
|
||||
}
|
||||
in {
|
||||
assert(level >= 0 && level < data.length);
|
||||
} body {
|
||||
auto result = data.dup;
|
||||
foreach (immutable i; 0 .. level)
|
||||
foreach (immutable j, ref el; result[0 .. $ - i - 1])
|
||||
el = result[j + 1] - el;
|
||||
result.length -= level;
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto data = [90.5, 47, 58, 29, 22, 32, 55, 5, 55, 73.5];
|
||||
foreach (level; 0 .. data.length)
|
||||
writeln(forwardDifference(data, level));
|
||||
import std.stdio;
|
||||
|
||||
const data = [90.5, 47, 58, 29, 22, 32, 55, 5, 55, 73.5];
|
||||
foreach (immutable level; 0 .. data.length)
|
||||
forwardDifference(data, level).writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio, std.algorithm, std.range, std.array;
|
||||
|
||||
auto forwardDifference(Range)(Range d, in int level) {
|
||||
auto forwardDifference(Range)(Range d, in int level) pure {
|
||||
foreach (immutable _; 0 .. level)
|
||||
d = d.zip(d.dropOne).map!(a => a[0] - a[1]).array;
|
||||
return d;
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import std.stdio;
|
||||
|
||||
T[] forwardDifference(T)(T[] s, in int n) pure {
|
||||
foreach (_; 0 .. n)
|
||||
s[] -= s[1 .. $];
|
||||
T[] forwardDifference(T)(T[] s, in int n) pure nothrow @nogc {
|
||||
foreach (immutable i; 0 .. n)
|
||||
s[0 .. $ - i - 1] = s[1 .. $ - i] - s[0 .. $ - i - 1];
|
||||
return s[0 .. $ - n];
|
||||
}
|
||||
void main() {
|
||||
immutable A = [90.5, 47, 58, 29, 22, 32, 55, 5, 55, 73.5];
|
||||
foreach (level; 0 .. A.length)
|
||||
writeln(forwardDifference(A.dup, level));
|
||||
foreach (immutable level; 0 .. A.length)
|
||||
forwardDifference(A.dup, level).writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
import std.stdio, std.range;
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.range;
|
||||
|
||||
auto D = [90.5, 47, 58, 29, 22, 32, 55, 5, 55, 73.5];
|
||||
auto R = recurrence!q{(a[n-1].dup[] -= a[n-1][1..$])[0..$-1]}(D);
|
||||
foreach (di; take(R, D.length))
|
||||
writeln(di);
|
||||
writefln("%(%s\n%)",
|
||||
recurrence!q{ (a[n - 1][0 .. $ - 1] =
|
||||
a[n - 1][1 .. $] -
|
||||
a[n - 1][0 .. $ - 1])[0 .. $] }(D)
|
||||
.take(D.length));
|
||||
}
|
||||
|
|
|
|||
12
Task/Forward-difference/HicEst/forward-difference.hicest
Normal file
12
Task/Forward-difference/HicEst/forward-difference.hicest
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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
|
||||
1
Task/Forward-difference/Julia/forward-difference.julia
Normal file
1
Task/Forward-difference/Julia/forward-difference.julia
Normal file
|
|
@ -0,0 +1 @@
|
|||
ndiff(A, n::Integer) = n < 1 ? A : diff(ndiff(A, n-1))
|
||||
1
Task/Forward-difference/MATLAB/forward-difference.m
Normal file
1
Task/Forward-difference/MATLAB/forward-difference.m
Normal file
|
|
@ -0,0 +1 @@
|
|||
Y = diff(X,n);
|
||||
|
|
@ -1,44 +1,41 @@
|
|||
/*REXX program to compute the forward difference of a list of numbers.*/
|
||||
/* ┌───────────────────────────────────────────────────────────────────┐
|
||||
│ /\ n n n-k │
|
||||
│ {delta} / \ n [ƒ] (x) ≡ Σ Ç ∙ (-1) ∙ ƒ(x+k) │
|
||||
│ /____\ k=0 k │
|
||||
│ │
|
||||
│ {n=order} {Ç=comb or binomial coeff.} │
|
||||
└───────────────────────────────────────────────────────────────────┘*/
|
||||
numeric digits 100 /*ensure enough accuracy. */
|
||||
arg numbers ',' ord /*input: x1 x2 x3 x4 ... ,ORD */
|
||||
if numbers=='' then numbers='90 47 58 29 22 32 55 5 55 73' /*default?*/
|
||||
w=words(numbers) /*set W to # of numbers in list.*/
|
||||
|
||||
do i=1 for w /*validate input (valid numbers?)*/
|
||||
@.i=word(numbers,i) /*assign the next # in the list. */
|
||||
if \datatype(@.i,'N') then call ser @.i "isn't a valid number"
|
||||
@.i=@.i/1 /*normalize the #, prettify the #*/
|
||||
end /*i*/
|
||||
/*═════════════════════════════════════════process the (optional) input.*/
|
||||
if w==0 then call ser 'no numbers were specified'
|
||||
if ord\=='' & ord<0 then call ser ord "(ner) can't be negative"
|
||||
if ord\=='' & ord>w then call ser ord "(ner) can't be greater than" w
|
||||
say right(w 'numbers: ' numbers,64) /*sloppy way to do a header, ... */
|
||||
say right(copies('─',length(numbers)),64) /* and the header fence.*/
|
||||
if ord=='' then do; bot=0; top=w; end /*define default ners.*/
|
||||
else do; bot=n; top=n; end /*just a specific ner?*/
|
||||
butchers/mangles some of the characters in the documentation box. -->
|
||||
<lang>/*REXX program computes the forward difference of a list of numbers.
|
||||
╔════════════════════════════════════════════════════════════════════╗
|
||||
║ /\ n n n-k ║
|
||||
║ / \ n [ƒ] (x) ≡ Σ C ∙ (-1) ∙ ƒ(x+k) ║
|
||||
║ /____\ k=0 k ║
|
||||
║ ↑ ↑ ↑ ║
|
||||
║ │ │ │ ║
|
||||
║ {delta}─────┘ {n=order} {C=comb or binomial coeff.} ║
|
||||
╚════════════════════════════════════════════════════════════════════╝*/
|
||||
numeric digits 100 /*ensure enough accuracy (digits)*/
|
||||
parse arg xxx ',' N /*input: ε1 ε2 ε3 ε4 ··· , order*/
|
||||
if xxx=='' then xxx='90 47 58 29 22 32 55 5 55 73' /*default numbers.*/
|
||||
w=words(xxx) /*set W to # of numbers in list.*/
|
||||
/* [↓] validate the input numbers*/
|
||||
do i=1 for w; _=word(xxx,i) /*process each number 1 at a time*/
|
||||
if \datatype(_,'N') then call ser _ "isn't a valid number"
|
||||
@.i=_/1 /*normalize the #, prettify the #*/
|
||||
end /*i*/ /* [↑] removes superfluous stuff*/
|
||||
/* [↓] process (optional) order.*/
|
||||
if w==0 then call ser 'no numbers were specified.'
|
||||
if N\=='' & N<0 then call ser N "(order) can't be negative."
|
||||
if N\=='' & N>w then call ser N "(order) can't be greater than" w
|
||||
say right(w 'numbers:', 44) xxx /*display the header ··· */
|
||||
say left('', 44)copies('─', length(xxx)+2) /* and the header fence.*/
|
||||
if N=='' then do; bot=0; top=w; end /*define default orders. */
|
||||
else do; bot=N; top=N; end /*just a specific order? */
|
||||
/*═════════════════════════════════════════where da rubber meets da road*/
|
||||
do order=bot to top; do r=1 for w; !.r=@.r; end; $=
|
||||
do j=1 for order; d=!.j; do k=j+1 to w /*order diff.*/
|
||||
parse value !.k !.k-d with d !.k
|
||||
end /*k*/
|
||||
do #=bot to top; do r=1 for w; !.r=@.r; end; $=
|
||||
do j=1 for #; d=!.j; do k=j+1 to w
|
||||
parse value !.k !.k-d with d !.k
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
do i=order+1 to w /*build list.*/
|
||||
$=$ !.i/1 /*append it. */
|
||||
end /*i*/
|
||||
if $=='' then $='[null]' /*pretty null*/
|
||||
what=right(order,length(w))th(order)'-order' /*nice format*/
|
||||
say what 'forward difference vector = ' strip($) /*display it.*/
|
||||
do i=#+1 to w; $=$ !.i/1; end
|
||||
if $=='' then $='[null]'
|
||||
say right(#, 7)th(#)'-order forward difference vector = ' strip($)
|
||||
end /*o*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SER subroutine──────────────────────*/
|
||||
ser: say; say '*** error! ***'; say arg(1); say; exit 13
|
||||
/*──────────────────────────────────TH subroutine───────────────────────*/
|
||||
th: parse arg ?; return word('th st nd rd',1+?//10*(?//100%10\==1)*(?//10<4))
|
||||
/*──────────────────────────────────one─liner subroutines───────────────*/
|
||||
ser: say; say '***error!***'; say arg(1); say; exit 13
|
||||
th: arg ?; return word('th st nd rd',1+?//10*(?//100%10\==1)*(?//10<4))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue