A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
9
Task/Forward-difference/0DESCRIPTION
Normal file
9
Task/Forward-difference/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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].
|
||||
|
||||
Algorithmic options:
|
||||
*Iterate through all previous forward differences and re-calculate a new array each time.
|
||||
*Use this formula (from [[wp:Forward difference|Wikipedia]]):
|
||||
:<math>\Delta^n [f](x)= \sum_{k=0}^n {n \choose k} (-1)^{n-k} f(x+k)</math>
|
||||
:([[Pascal's Triangle]] may be useful for this option)
|
||||
2
Task/Forward-difference/1META.yaml
Normal file
2
Task/Forward-difference/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Arithmetic operations
|
||||
21
Task/Forward-difference/ALGOL-68/forward-difference.alg
Normal file
21
Task/Forward-difference/ALGOL-68/forward-difference.alg
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
main:(
|
||||
MODE LISTREAL = [1:0]REAL;
|
||||
|
||||
OP - = (LISTREAL a,b)LISTREAL: (
|
||||
[UPB a]REAL out;
|
||||
FOR i TO UPB out DO out[i]:=a[i]-b[i] OD;
|
||||
out
|
||||
);
|
||||
|
||||
FORMAT real fmt=$zzz-d.d$;
|
||||
FORMAT repeat fmt = $n(UPB s-1)(f(real fmt)",")f(real fmt)$;
|
||||
FORMAT list fmt = $"("f(UPB s=1|real fmt|repeat fmt)")"$;
|
||||
|
||||
FLEX [1:0] REAL s := (90, 47, 58, 29, 22, 32, 55, 5, 55, 73);
|
||||
|
||||
printf((list fmt,s,$";"l$));
|
||||
TO UPB s-1 DO
|
||||
s := s[2:] - s[:UPB s-1];
|
||||
printf((list fmt,s,$";"l$))
|
||||
OD
|
||||
)
|
||||
9
Task/Forward-difference/APL/forward-difference.apl
Normal file
9
Task/Forward-difference/APL/forward-difference.apl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
list ← 90 47 58 29 22 32 55 5 55 73
|
||||
|
||||
fd ← {⍺=0:⍵⋄(⍺-1)∇(1↓⍵)-(¯1↓⍵)}
|
||||
|
||||
1 fd list
|
||||
¯43 11 ¯29 ¯7 10 23 ¯50 50 18
|
||||
|
||||
2 fd list
|
||||
54 ¯40 22 17 13 ¯73 100 ¯32
|
||||
20
Task/Forward-difference/AWK/forward-difference-1.awk
Normal file
20
Task/Forward-difference/AWK/forward-difference-1.awk
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/awk -f
|
||||
BEGIN {
|
||||
if (p<1) {p = 1};
|
||||
}
|
||||
|
||||
function diff(s, p) {
|
||||
n = split(s, a, " ");
|
||||
for (j = 1; j <= p; j++) {
|
||||
for(i = 1; i <= n-j; i++) {
|
||||
a[i] = a[i+1] - a[i];
|
||||
}
|
||||
}
|
||||
s = "";
|
||||
for (i = 1; i <= n-p; i++) s = s" "a[i];
|
||||
return s;
|
||||
}
|
||||
|
||||
{
|
||||
print diff($0, p);
|
||||
}
|
||||
27
Task/Forward-difference/AWK/forward-difference-2.awk
Normal file
27
Task/Forward-difference/AWK/forward-difference-2.awk
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/awk -f
|
||||
BEGIN {
|
||||
if (p<1) {p = 1};
|
||||
}
|
||||
|
||||
function diff(s, p) {
|
||||
# pascal triangled with sign changes
|
||||
b[1] = (p%2) ? 1 : -1;
|
||||
for (j=1; j < p; j++) {
|
||||
b[j+1] = -b[j]*(p-j)/j;
|
||||
};
|
||||
|
||||
n = split(s, a, " ");
|
||||
s = "";
|
||||
for (j = 1; j <= n-p+1; j++) {
|
||||
c = 0;
|
||||
for(i = 1; i <= p; i++) {
|
||||
c += b[i]*a[j+i-1];
|
||||
}
|
||||
s = s" "c;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
{
|
||||
print diff($0, p);
|
||||
}
|
||||
50
Task/Forward-difference/Ada/forward-difference.ada
Normal file
50
Task/Forward-difference/Ada/forward-difference.ada
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
with Ada.Text_Io;
|
||||
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
|
||||
with Ada.containers.Vectors;
|
||||
|
||||
procedure Forward_Difference is
|
||||
package Flt_Vect is new Ada.Containers.Vectors(Positive, Float);
|
||||
use Flt_Vect;
|
||||
procedure Print(Item : Vector) is
|
||||
begin
|
||||
if not Item.Is_Empty then
|
||||
Ada.Text_IO.Put('[');
|
||||
for I in 1..Item.Length loop
|
||||
Put(Item => Item.Element(Positive(I)), Fore => 1, Aft => 1, Exp => 0);
|
||||
if Positive(I) < Positive(Item.Length) then
|
||||
Ada.Text_Io.Put(", ");
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_Io.Put_line("]");
|
||||
else
|
||||
Ada.Text_IO.Put_Line("Empty List");
|
||||
end if;
|
||||
|
||||
end Print;
|
||||
|
||||
function Diff(Item : Vector; Num_Passes : Natural) return Vector is
|
||||
A : Vector := Item;
|
||||
B : Vector := Empty_Vector;
|
||||
begin
|
||||
if not A.Is_Empty then
|
||||
for I in 1..Num_Passes loop
|
||||
for I in 1..Natural(A.Length) - 1 loop
|
||||
B.Append(A.Element(I + 1) - A.Element(I));
|
||||
end loop;
|
||||
Move(Target => A, Source => B);
|
||||
end loop;
|
||||
end if;
|
||||
return A;
|
||||
end Diff;
|
||||
Values : array(1..10) of Float := (90.0, 47.0, 58.0, 29.0, 22.0, 32.0, 55.0, 5.0, 55.0, 73.0);
|
||||
A : Vector;
|
||||
begin
|
||||
for I in Values'range loop
|
||||
A.Append(Values(I)); -- Fill the vector
|
||||
end loop;
|
||||
Print(Diff(A, 1));
|
||||
Print(Diff(A, 2));
|
||||
Print(Diff(A, 9));
|
||||
Print(Diff(A, 10));
|
||||
print(Diff(A, 0));
|
||||
end Forward_Difference;
|
||||
17
Task/Forward-difference/AutoHotkey/forward-difference.ahk
Normal file
17
Task/Forward-difference/AutoHotkey/forward-difference.ahk
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
MsgBox % diff("2,3,4,3",1)
|
||||
MsgBox % diff("2,3,4,3",2)
|
||||
MsgBox % diff("2,3,4,3",3)
|
||||
MsgBox % diff("2,3,4,3",4)
|
||||
|
||||
diff(list,ord) { ; high order forward differences of a list
|
||||
Loop %ord% {
|
||||
L =
|
||||
Loop Parse, list, `, %A_Space%%A_Tab%
|
||||
If (A_Index=1)
|
||||
p := A_LoopField
|
||||
Else
|
||||
L .= "," A_LoopField-p, p := A_LoopField
|
||||
list := SubStr(L,2)
|
||||
}
|
||||
Return list
|
||||
}
|
||||
30
Task/Forward-difference/BBC-BASIC/forward-difference.bbc
Normal file
30
Task/Forward-difference/BBC-BASIC/forward-difference.bbc
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
DIM A(9)
|
||||
A() = 90.0, 47.0, 58.0, 29.0, 22.0, 32.0, 55.0, 5.0, 55.0, 73.0
|
||||
PRINT "Original array: " FNshowarray(A())
|
||||
PROCforward_difference(1, A(), B())
|
||||
PRINT "Forward diff 1: " FNshowarray(B())
|
||||
PROCforward_difference(2, A(), C())
|
||||
PRINT "Forward diff 2: " FNshowarray(C())
|
||||
PROCforward_difference(9, A(), D())
|
||||
PRINT "Forward diff 9: " FNshowarray(D())
|
||||
END
|
||||
|
||||
DEF PROCforward_difference(n%, a(), RETURN b())
|
||||
LOCAL c%, i%, j%
|
||||
DIM b(DIM(a(),1) - n%)
|
||||
FOR i% = 0 TO DIM(b(),1)
|
||||
b(i%) = a(i% + n%)
|
||||
c% = 1
|
||||
FOR j% = 1 TO n%
|
||||
c% = -INT(c% * (n% - j% + 1) / j% + 0.5)
|
||||
b(i%) += c% * a(i% + n% - j%)
|
||||
NEXT
|
||||
NEXT
|
||||
ENDPROC
|
||||
|
||||
DEF FNshowarray(a())
|
||||
LOCAL i%, a$
|
||||
FOR i% = 0 TO DIM(a(),1)
|
||||
a$ += STR$(a(i%)) + ", "
|
||||
NEXT
|
||||
= LEFT$(LEFT$(a$))
|
||||
111
Task/Forward-difference/C++/forward-difference-1.cpp
Normal file
111
Task/Forward-difference/C++/forward-difference-1.cpp
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
#include <vector>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
|
||||
// calculate first order forward difference
|
||||
// requires:
|
||||
// * InputIterator is an input iterator
|
||||
// * OutputIterator is an output iterator
|
||||
// * The value type of InputIterator is copy-constructible and assignable
|
||||
// * The value type of InputIterator supports operator -
|
||||
// * The result type of operator- is assignable to the value_type of OutputIterator
|
||||
// returns: The iterator following the output sequence
|
||||
template<typename InputIterator, typename OutputIterator>
|
||||
OutputIterator forward_difference(InputIterator first, InputIterator last,
|
||||
OutputIterator dest)
|
||||
{
|
||||
// special case: for empty sequence, do nothing
|
||||
if (first == last)
|
||||
return dest;
|
||||
|
||||
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
|
||||
|
||||
value_type temp = *first++;
|
||||
while (first != last)
|
||||
{
|
||||
value_type temp2 = *first++;
|
||||
*dest++ = temp2 - temp;
|
||||
temp = temp2;
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
// calculate n-th order forward difference.
|
||||
// requires:
|
||||
// * InputIterator is an input iterator
|
||||
// * OutputIterator is an output iterator
|
||||
// * The value type of InputIterator is copy-constructible and assignable
|
||||
// * The value type of InputIterator supports operator -
|
||||
// * The result type of operator- is assignable to the value_type of InputIterator
|
||||
// * The result type of operator- is assignable to the value_type of OutputIterator
|
||||
// * order >= 0
|
||||
// returns: The iterator following the output sequence
|
||||
template<typename InputIterator, typename OutputIterator>
|
||||
OutputIterator nth_forward_difference(int order,
|
||||
InputIterator first, InputIterator last,
|
||||
OutputIterator dest)
|
||||
{
|
||||
// special case: If order == 0, just copy input to output
|
||||
if (order == 0)
|
||||
return std::copy(first, last, dest);
|
||||
|
||||
// second special case: If order == 1, just forward to the first-order function
|
||||
if (order == 1)
|
||||
return forward_difference(first, last, dest);
|
||||
|
||||
// intermediate results are stored in a vector
|
||||
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
|
||||
std::vector<value_type> temp_storage;
|
||||
|
||||
// fill the vector with the result of the first order forward difference
|
||||
forward_difference(first, last, std::back_inserter(temp_storage));
|
||||
|
||||
// the next n-2 iterations work directly on the vector
|
||||
typename std::vector<value_type>::iterator begin = temp_storage.begin(),
|
||||
end = temp_storage.end();
|
||||
for (int i = 1; i < order-1; ++i)
|
||||
end = forward_difference(begin, end, begin);
|
||||
|
||||
// the final iteration writes directly to the output iterator
|
||||
return forward_difference(begin, end, dest);
|
||||
}
|
||||
|
||||
// example usage code
|
||||
#include <iostream>
|
||||
|
||||
int main()
|
||||
{
|
||||
double array[10] = { 90.0, 47.0, 58.0, 29.0, 22.0, 32.0, 55.0, 5.0, 55.0, 73.0 };
|
||||
|
||||
// this stores the results in the vector dest
|
||||
std::vector<double> dest;
|
||||
nth_forward_difference(1, array, array+10, std::back_inserter(dest));
|
||||
|
||||
// outut dest
|
||||
std::copy(dest.begin(), dest.end(), std::ostream_iterator<double>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
// however, the results can also be output as they are calculated
|
||||
nth_forward_difference(2, array, array+10, std::ostream_iterator<double>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
nth_forward_difference(9, array, array+10, std::ostream_iterator<double>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
nth_forward_difference(10, array, array+10, std::ostream_iterator<double>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
nth_forward_difference(0, array, array+10, std::ostream_iterator<double>(std::cout, " "));
|
||||
std::cout << std::endl;
|
||||
|
||||
// finally, the results can also be written into the original array
|
||||
// (which of course destroys the original content)
|
||||
double* end = nth_forward_difference(3, array, array+10, array);
|
||||
|
||||
for (double* p = array; p < end; ++p)
|
||||
std::cout << *p << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
16
Task/Forward-difference/C++/forward-difference-2.cpp
Normal file
16
Task/Forward-difference/C++/forward-difference-2.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#include <iostream>
|
||||
#include <numeric>
|
||||
// Calculate the Forward Difference of a series if integers showing each order
|
||||
//
|
||||
// Nigel Galloway. August 20th., 2012
|
||||
//
|
||||
int main() {
|
||||
int x[] = {NULL,-43,11,-29,-7,10,23,-50,50,18};
|
||||
const int N = sizeof(x) / sizeof(int) - 1;
|
||||
for (int ord = 0; ord < N - 1; ord++) {
|
||||
std::adjacent_difference(x+1, x + N + 1 - ord, x);
|
||||
for (int i = 1; i < N - ord; i++) std::cout << x[i] << ' ';
|
||||
std::cout << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
13
Task/Forward-difference/C++/forward-difference-3.cpp
Normal file
13
Task/Forward-difference/C++/forward-difference-3.cpp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <iostream>
|
||||
#include <numeric>
|
||||
// Calculate the Forward Difference of a series if integers
|
||||
//
|
||||
// Nigel Galloway. August 20th., 2012
|
||||
//
|
||||
int main() {
|
||||
int x[] = {NULL,-43,11,-29,-7,10,23,-50,50,18};
|
||||
const int N = sizeof(x) / sizeof(int) - 1;
|
||||
for (int ord = 0; ord < N - 1; ord++) std::adjacent_difference(x+1, x + N + 1 - ord, x);
|
||||
std::cout << x[1] << std::endl;
|
||||
return 0;
|
||||
}
|
||||
14
Task/Forward-difference/C++/forward-difference-4.cpp
Normal file
14
Task/Forward-difference/C++/forward-difference-4.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
// Calculate the Forward Difference of a series if integers using Pascal's Triangle
|
||||
// For this example the 9th line of Pascal's Triangle is stored in P.
|
||||
//
|
||||
// Nigel Galloway. August 20th., 2012
|
||||
//
|
||||
int main() {
|
||||
const int P[] = {1,-8,28,-56,70,-56,28,-8,1};
|
||||
int x[] = {-43,11,-29,-7,10,23,-50,50,18};
|
||||
std::transform(x, x + sizeof(x) / sizeof(int), P, x, std::multiplies<int>());
|
||||
std::cout << std::accumulate(x, x + sizeof(x) / sizeof(int), 0) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
28
Task/Forward-difference/C-sharp/forward-difference.cs
Normal file
28
Task/Forward-difference/C-sharp/forward-difference.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
class Program
|
||||
{
|
||||
static IEnumerable<int> ForwardDifference(IEnumerable<int> sequence, uint order = 1u)
|
||||
{
|
||||
switch (order)
|
||||
{
|
||||
case 0u:
|
||||
return sequence;
|
||||
case 1u:
|
||||
return sequence.Skip(1).Zip(sequence, (next, current) => next - current);
|
||||
default:
|
||||
return ForwardDifference(ForwardDifference(sequence), order - 1u);
|
||||
}
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
IEnumerable<int> sequence = new[] { 90, 47, 58, 29, 22, 32, 55, 5, 55, 73 };
|
||||
do
|
||||
{
|
||||
Console.WriteLine(string.Join(", ", sequence));
|
||||
} while ((sequence = ForwardDifference(sequence)).Any());
|
||||
}
|
||||
}
|
||||
39
Task/Forward-difference/C/forward-difference-1.c
Normal file
39
Task/Forward-difference/C/forward-difference-1.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
double* fwd_diff(double* x, unsigned int len, unsigned int order)
|
||||
{
|
||||
unsigned int i, j;
|
||||
double* y;
|
||||
|
||||
/* handle two special cases */
|
||||
if (order >= len) return 0;
|
||||
|
||||
y = malloc(sizeof(double) * len);
|
||||
if (!order) {
|
||||
memcpy(y, x, sizeof(double) * len);
|
||||
return y;
|
||||
}
|
||||
|
||||
/* first order diff goes from x->y, later ones go from y->y */
|
||||
for (j = 0; j < order; j++, x = y)
|
||||
for (i = 0, len--; i < len; i++)
|
||||
y[i] = x[i + 1] - x[i];
|
||||
|
||||
y = realloc(y, sizeof(double) * len);
|
||||
return y;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
double *y, x[] = {90, 47, 58, 29, 22, 32, 55, 5, 55, 73};
|
||||
int i, len = sizeof(x) / sizeof(x[0]);
|
||||
|
||||
y = fwd_diff(x, len, 1);
|
||||
for (i = 0; i < len - 1; i++)
|
||||
printf("%g ", y[i]);
|
||||
putchar('\n');
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
Task/Forward-difference/C/forward-difference-2.c
Normal file
33
Task/Forward-difference/C/forward-difference-2.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int* binomCoeff(int n) {
|
||||
int *b = calloc(n+1,sizeof(int));
|
||||
int j;
|
||||
b[0] = n%2 ? -1 : 1;
|
||||
for (j=1 ; j<=n; j++)
|
||||
b[j] = -b[j-1]*(n+1-j)/j;
|
||||
|
||||
return(b);
|
||||
};
|
||||
|
||||
main () {
|
||||
double array[] = { 90, 47, 58, 29, 22, 32, 55, 5, 55, 73 };
|
||||
size_t lenArray = sizeof(array)/sizeof(array[0]);
|
||||
|
||||
int p = 4; // order
|
||||
int *b = binomCoeff(p); // pre-compute binomial coefficients for order p
|
||||
|
||||
int j, k;
|
||||
|
||||
// compute p-th difference
|
||||
for (k=0 ; k < lenArray; k++)
|
||||
for (array[k] *= b[0], j=1 ; j<=p; j++)
|
||||
array[k] += b[j] * array[k+j];
|
||||
|
||||
free(b);
|
||||
|
||||
// resulting series is shorter by p elements
|
||||
lenArray -= p;
|
||||
for (k=0 ; k < lenArray; k++) printf("%f ",array[k]);
|
||||
printf("\n");
|
||||
}
|
||||
2
Task/Forward-difference/Clojure/forward-difference.clj
Normal file
2
Task/Forward-difference/Clojure/forward-difference.clj
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(defn fwd-diff [nums order]
|
||||
(nth (iterate #(map - (next %) %) nums) order))
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
forward_difference = (arr, n) ->
|
||||
# Find the n-th order forward difference for arr using
|
||||
# a straightforward recursive algorithm.
|
||||
# Assume arr is integers and n <= arr.length.
|
||||
return arr if n == 0
|
||||
arr = forward_difference(arr, n-1)
|
||||
(arr[i+1] - arr[i] for i in [0...arr.length - 1])
|
||||
|
||||
arr = [-1, 0, 1, 8, 27, 64, 125, 216]
|
||||
for n in [0..arr.length]
|
||||
console.log n, forward_difference arr, n
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(defun forward-difference (list)
|
||||
(mapcar #'- (rest list) list))
|
||||
|
||||
(defun nth-forward-difference (list n)
|
||||
(setf list (copy-list list))
|
||||
(loop repeat n do (map-into list #'- (rest list) list))
|
||||
(subseq list 0 (- (length list) n)))
|
||||
20
Task/Forward-difference/D/forward-difference-1.d
Normal file
20
Task/Forward-difference/D/forward-difference-1.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
13
Task/Forward-difference/D/forward-difference-2.d
Normal file
13
Task/Forward-difference/D/forward-difference-2.d
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import std.stdio, std.algorithm, std.range, std.array;
|
||||
|
||||
auto forwardDifference(Range)(Range d, in int level) {
|
||||
foreach (_; 0 .. level)
|
||||
d = zip(d[1 .. $], d).map!q{ a[0] - a[1] }().array();
|
||||
return d;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
12
Task/Forward-difference/D/forward-difference-3.d
Normal file
12
Task/Forward-difference/D/forward-difference-3.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import std.stdio;
|
||||
|
||||
T[] forwardDifference(T)(T[] s, in int n) pure {
|
||||
foreach (_; 0 .. n)
|
||||
s[] -= s[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));
|
||||
}
|
||||
8
Task/Forward-difference/D/forward-difference-4.d
Normal file
8
Task/Forward-difference/D/forward-difference-4.d
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import std.stdio, std.range;
|
||||
|
||||
void main() {
|
||||
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);
|
||||
}
|
||||
32
Task/Forward-difference/E/forward-difference.e
Normal file
32
Task/Forward-difference/E/forward-difference.e
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
pragma.enable("accumulator")
|
||||
/** Single step. */
|
||||
def forwardDifference(seq :List) {
|
||||
return accum [] for i in 0..(seq.size() - 2) {
|
||||
_.with(seq[i + 1] - seq[i])
|
||||
}
|
||||
}
|
||||
|
||||
/** Iterative implementation of the goal. */
|
||||
def nthForwardDifference1(var seq :List, n :(int >= 0)) {
|
||||
for _ in 1..n { seq := forwardDifference(seq) }
|
||||
return seq
|
||||
}
|
||||
|
||||
/** Imperative implementation of the goal. */
|
||||
def nthForwardDifference2(seq :List, n :(int >= 0)) {
|
||||
def buf := seq.diverge()
|
||||
def finalSize := seq.size() - n
|
||||
for lim in (finalSize..!seq.size()).descending() {
|
||||
for i in 0..!lim {
|
||||
buf[i] := buf[i + 1] - buf[i]
|
||||
}
|
||||
}
|
||||
return buf.run(0, finalSize)
|
||||
}
|
||||
|
||||
? def sampleData := [90, 47, 58, 29, 22, 32, 55, 5, 55, 73]
|
||||
> for n in 0..10 {
|
||||
> def r1 := nthForwardDifference1(sampleData, n)
|
||||
> require(r1 == nthForwardDifference2(sampleData, n))
|
||||
> println(r1)
|
||||
> }
|
||||
9
Task/Forward-difference/Factor/forward-difference.factor
Normal file
9
Task/Forward-difference/Factor/forward-difference.factor
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
USING: kernel math math.vectors sequences ;
|
||||
IN: rosetacode
|
||||
|
||||
: 1-order ( seq -- seq' )
|
||||
[ rest-slice ] keep v- ;
|
||||
|
||||
: n-order ( seq n -- seq' )
|
||||
dup 0 <=
|
||||
[ drop ] [ [ 1-order ] times ] if ;
|
||||
15
Task/Forward-difference/Forth/forward-difference.fth
Normal file
15
Task/Forward-difference/Forth/forward-difference.fth
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
: forward-difference
|
||||
dup 0
|
||||
?do
|
||||
swap rot over i 1+ - 0
|
||||
?do dup i cells + dup cell+ @ over @ - swap ! loop
|
||||
swap rot
|
||||
loop -
|
||||
;
|
||||
|
||||
create a
|
||||
90 , 47 , 58 , 29 , 22 , 32 , 55 , 5 , 55 , 73 ,
|
||||
|
||||
: test a 10 9 forward-difference bounds ?do i ? loop ;
|
||||
|
||||
test
|
||||
20
Task/Forward-difference/Fortran/forward-difference-1.f
Normal file
20
Task/Forward-difference/Fortran/forward-difference-1.f
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
MODULE DIFFERENCE
|
||||
IMPLICIT NONE
|
||||
|
||||
CONTAINS
|
||||
|
||||
SUBROUTINE Fdiff(a, n)
|
||||
INTEGER, INTENT(IN) :: a(:), n
|
||||
INTEGER :: b(SIZE(a))
|
||||
INTEGER :: i, j, arraysize
|
||||
|
||||
b = a
|
||||
arraysize = SIZE(b)
|
||||
DO i = arraysize-1, arraysize-n, -1
|
||||
DO j = 1, i
|
||||
b(j) = b(j+1) - b(j)
|
||||
END DO
|
||||
END DO
|
||||
WRITE (*,*) b(1:arraysize-n)
|
||||
END SUBROUTINE Fdiff
|
||||
END MODULE DIFFERENCE
|
||||
13
Task/Forward-difference/Fortran/forward-difference-2.f
Normal file
13
Task/Forward-difference/Fortran/forward-difference-2.f
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
PROGRAM TEST
|
||||
|
||||
USE DIFFERENCE
|
||||
IMPLICIT NONE
|
||||
|
||||
INTEGER :: array(10) = (/ 90, 47, 58, 29, 22, 32, 55, 5, 55, 73 /)
|
||||
INTEGER :: i
|
||||
|
||||
DO i = 1, 9
|
||||
CALL Fdiff(array, i)
|
||||
END DO
|
||||
|
||||
END PROGRAM TEST
|
||||
18
Task/Forward-difference/Go/forward-difference.go
Normal file
18
Task/Forward-difference/Go/forward-difference.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := []int{90, 47, 58, 29, 22, 32, 55, 5, 55, 73}
|
||||
fmt.Println(a)
|
||||
fmt.Println(fd(a, 9))
|
||||
}
|
||||
|
||||
func fd(a []int, ord int) []int {
|
||||
for i := 0; i < ord; i++ {
|
||||
for j := 0; j < len(a)-i-1; j++ {
|
||||
a[j] = a[j+1] - a[j]
|
||||
}
|
||||
}
|
||||
return a[:len(a)-ord]
|
||||
}
|
||||
15
Task/Forward-difference/Haskell/forward-difference.hs
Normal file
15
Task/Forward-difference/Haskell/forward-difference.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
forwardDifference xs = zipWith (-) (tail xs) xs
|
||||
|
||||
nthForwardDifference xs n = iterate forwardDifference xs !! n
|
||||
|
||||
> take 10 (iterate forwardDifference [90, 47, 58, 29, 22, 32, 55, 5, 55, 73])
|
||||
[[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]]
|
||||
12
Task/Forward-difference/HicEst/forward-difference-1.hicest
Normal file
12
Task/Forward-difference/HicEst/forward-difference-1.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
|
||||
10
Task/Forward-difference/HicEst/forward-difference-2.hicest
Normal file
10
Task/Forward-difference/HicEst/forward-difference-2.hicest
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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
|
||||
8
Task/Forward-difference/IDL/forward-difference.idl
Normal file
8
Task/Forward-difference/IDL/forward-difference.idl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
print,(x = randomu(seed,8)*100)
|
||||
15.1473 58.0953 82.7465 16.8637 97.7182 59.7856 17.7699 74.9154
|
||||
print,ts_diff(x,1)
|
||||
-42.9479 -24.6513 65.8828 -80.8545 37.9326 42.0157 -57.1455 0.000000
|
||||
print,ts_diff(x,2)
|
||||
-18.2967 -90.5341 146.737 -118.787 -4.08316 99.1613 0.000000 0.000000
|
||||
print,ts_diff(x,3)
|
||||
72.2374 -237.271 265.524 -114.704 -103.244 0.000000 0.000000 0.000000
|
||||
17
Task/Forward-difference/Icon/forward-difference.icon
Normal file
17
Task/Forward-difference/Icon/forward-difference.icon
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
procedure main(A) # Compute all forward difference orders for argument list
|
||||
every order := 1 to (*A-1) do showList(order, fdiff(A, order))
|
||||
end
|
||||
|
||||
procedure fdiff(A, order)
|
||||
every 1 to order do {
|
||||
every put(B := [], A[i := 2 to *A] - A[i-1])
|
||||
A := B
|
||||
}
|
||||
return A
|
||||
end
|
||||
|
||||
procedure showList(order, L)
|
||||
writes(right(order,3),": ")
|
||||
every writes(!L," ")
|
||||
write()
|
||||
end
|
||||
1
Task/Forward-difference/J/forward-difference-1.j
Normal file
1
Task/Forward-difference/J/forward-difference-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
fd=: 2&(-~/\)
|
||||
1
Task/Forward-difference/J/forward-difference-2.j
Normal file
1
Task/Forward-difference/J/forward-difference-2.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
fd=:}.-}:^:
|
||||
7
Task/Forward-difference/J/forward-difference-3.j
Normal file
7
Task/Forward-difference/J/forward-difference-3.j
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
list =: 90 47 58 29 22 32 55 5 55 73 NB. Some numbers
|
||||
|
||||
1 fd list
|
||||
_43 11 _29 _7 10 23 _50 50 18
|
||||
|
||||
2 fd list
|
||||
54 _40 22 17 13 _73 100 _32
|
||||
17
Task/Forward-difference/J/forward-difference-4.j
Normal file
17
Task/Forward-difference/J/forward-difference-4.j
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
1 2 3 fd list NB. First, second, and third forward differences (simultaneously)
|
||||
43 _11 29 7 _10 _23 50 _50 _18
|
||||
54 _40 22 17 13 _73 100 _32 0
|
||||
94 _62 5 4 86 _173 132 0 0
|
||||
|
||||
a: fd list NB. All forward differences
|
||||
90 47 58 29 22 32 55 5 55 73
|
||||
43 _11 29 7 _10 _23 50 _50 _18 0
|
||||
54 _40 22 17 13 _73 100 _32 0 0
|
||||
94 _62 5 4 86 _173 132 0 0 0
|
||||
156 _67 1 _82 259 _305 0 0 0 0
|
||||
223 _68 83 _341 564 0 0 0 0 0
|
||||
291 _151 424 _905 0 0 0 0 0 0
|
||||
442 _575 1329 0 0 0 0 0 0 0
|
||||
1017 _1904 0 0 0 0 0 0 0 0
|
||||
2921 0 0 0 0 0 0 0 0 0
|
||||
0 0 0 0 0 0 0 0 0 0
|
||||
27
Task/Forward-difference/Java/forward-difference.java
Normal file
27
Task/Forward-difference/Java/forward-difference.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import java.util.Arrays;
|
||||
public class FD {
|
||||
public static void main(String args[]) {
|
||||
double[] a = {90, 47, 58, 29, 22, 32, 55, 5, 55, 73};
|
||||
System.out.println(Arrays.toString(dif(a, 1)));
|
||||
System.out.println(Arrays.toString(dif(a, 2)));
|
||||
System.out.println(Arrays.toString(dif(a, 9)));
|
||||
System.out.println(Arrays.toString(dif(a, 10))); //let's test
|
||||
System.out.println(Arrays.toString(dif(a, 11)));
|
||||
System.out.println(Arrays.toString(dif(a, -1)));
|
||||
System.out.println(Arrays.toString(dif(a, 0)));
|
||||
}
|
||||
|
||||
public static double[] dif(double[] a, int n) {
|
||||
if (n < 0)
|
||||
return null; // if the programmer was dumb
|
||||
|
||||
for (int i = 0; i < n && a.length > 0; i++) {
|
||||
double[] b = new double[a.length - 1];
|
||||
for (int j = 0; j < b.length; j++){
|
||||
b[j] = a[j+1] - a[j];
|
||||
}
|
||||
a = b; //"recurse"
|
||||
}
|
||||
return a;
|
||||
}
|
||||
}
|
||||
12
Task/Forward-difference/Logo/forward-difference.logo
Normal file
12
Task/Forward-difference/Logo/forward-difference.logo
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
to fwd.diff :l
|
||||
if empty? :l [output []]
|
||||
if empty? bf :l [output []]
|
||||
output fput (first bf :l)-(first :l) fwd.diff bf :l
|
||||
end
|
||||
to nth.fwd.diff :n :l
|
||||
if :n = 0 [output :l]
|
||||
output nth.fwd.diff :n-1 fwd.diff :l
|
||||
end
|
||||
|
||||
show nth.fwd.diff 9 [90 47 58 29 22 32 55 5 55 73]
|
||||
[-2921]
|
||||
5
Task/Forward-difference/Lua/forward-difference.lua
Normal file
5
Task/Forward-difference/Lua/forward-difference.lua
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
function dif(a, b, ...)
|
||||
if(b) then return b-a, dif(b, ...) end
|
||||
end
|
||||
function dift(t) return {dif(unpack(t))} end
|
||||
print(unpack(dift{1,3,6,10,15}))
|
||||
1
Task/Forward-difference/MATLAB/forward-difference-1.m
Normal file
1
Task/Forward-difference/MATLAB/forward-difference-1.m
Normal file
|
|
@ -0,0 +1 @@
|
|||
Y = diff(X,n);
|
||||
5
Task/Forward-difference/MATLAB/forward-difference-2.m
Normal file
5
Task/Forward-difference/MATLAB/forward-difference-2.m
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
diff([1 2 3 4 5])
|
||||
|
||||
ans =
|
||||
|
||||
1 1 1 1
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
i={3,5,12,1,6,19,6,2,4,9};
|
||||
Differences[i]
|
||||
|
|
@ -0,0 +1 @@
|
|||
{2, 7, -11, 5, 13, -13, -4, 2, 5}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
i={3,5,12,1,6,19,6,2,4,9};
|
||||
Differences[i,n]
|
||||
1
Task/Forward-difference/Maxima/forward-difference.maxima
Normal file
1
Task/Forward-difference/Maxima/forward-difference.maxima
Normal file
|
|
@ -0,0 +1 @@
|
|||
ldiff(u, n) := block([m: length(u)], for j thru n do u: makelist(u[i + 1] - u[i], i, 1, m - j), u);
|
||||
43
Task/Forward-difference/PHP/forward-difference.php
Normal file
43
Task/Forward-difference/PHP/forward-difference.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
function forwardDiff($anArray, $times = 1) {
|
||||
if ($times <= 0) { return $anArray; }
|
||||
for ($accumilation = array(), $i = 1, $j = count($anArray); $i < $j; ++$i) {
|
||||
$accumilation[] = $anArray[$i] - $anArray[$i - 1];
|
||||
}
|
||||
if ($times === 1) { return $accumilation; }
|
||||
return forwardDiff($accumilation, $times - 1);
|
||||
}
|
||||
|
||||
class ForwardDiffExample extends PweExample {
|
||||
|
||||
function _should_run_empty_array_for_single_elem() {
|
||||
$expected = array($this->rand()->int());
|
||||
$this->spec(forwardDiff($expected))->shouldEqual(array());
|
||||
}
|
||||
|
||||
function _should_give_diff_of_two_elem_as_single_elem() {
|
||||
$twoNums = array($this->rand()->int(), $this->rand()->int());
|
||||
$expected = array($twoNums[1] - $twoNums[0]);
|
||||
$this->spec(forwardDiff($twoNums))->shouldEqual($expected);
|
||||
}
|
||||
|
||||
function _should_compute_correct_forward_diff_for_longer_arrays() {
|
||||
$diffInput = array(10, 2, 9, 6, 5);
|
||||
$expected = array(-8, 7, -3, -1);
|
||||
$this->spec(forwardDiff($diffInput))->shouldEqual($expected);
|
||||
}
|
||||
|
||||
function _should_apply_more_than_once_if_specified() {
|
||||
$diffInput = array(4, 6, 9, 3, 4);
|
||||
$expectedAfter1 = array(2, 3, -6, 1);
|
||||
$expectedAfter2 = array(1, -9, 7);
|
||||
$this->spec(forwardDiff($diffInput, 1))->shouldEqual($expectedAfter1);
|
||||
$this->spec(forwardDiff($diffInput, 2))->shouldEqual($expectedAfter2);
|
||||
}
|
||||
|
||||
function _should_return_array_unaltered_if_no_times() {
|
||||
$this->spec(forwardDiff($expected = array(1,2,3), 0))->shouldEqual($expected);
|
||||
}
|
||||
|
||||
}
|
||||
10
Task/Forward-difference/Perl/forward-difference.pl
Normal file
10
Task/Forward-difference/Perl/forward-difference.pl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
sub dif {
|
||||
my @s = @_;
|
||||
map { $s[$_+1] - $s[$_] } 0 .. $#s-1
|
||||
}
|
||||
|
||||
sub difn {
|
||||
my ($n, @s) = @_;
|
||||
@s = dif @s foreach 1..$n;
|
||||
@s
|
||||
}
|
||||
5
Task/Forward-difference/PicoLisp/forward-difference.l
Normal file
5
Task/Forward-difference/PicoLisp/forward-difference.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(de fdiff (Lst)
|
||||
(mapcar - (cdr Lst) Lst) )
|
||||
|
||||
(for (L (90 47 58 29 22 32 55 5 55 73) L (fdiff L))
|
||||
(println L) )
|
||||
24
Task/Forward-difference/Python/forward-difference.py
Normal file
24
Task/Forward-difference/Python/forward-difference.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
>>> dif = lambda s: [x-s[i] for i,x in enumerate(s[1:])]
|
||||
>>> # or, dif = lambda s: [x-y for x,y in zip(s[1:],s)]
|
||||
>>> difn = lambda s, n: difn(dif(s), n-1) if n else s
|
||||
|
||||
>>> s = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73]
|
||||
>>> difn(s, 0)
|
||||
[90, 47, 58, 29, 22, 32, 55, 5, 55, 73]
|
||||
>>> difn(s, 1)
|
||||
[-43, 11, -29, -7, 10, 23, -50, 50, 18]
|
||||
>>> difn(s, 2)
|
||||
[54, -40, 22, 17, 13, -73, 100, -32]
|
||||
|
||||
>>> from pprint import pprint
|
||||
>>> pprint( [difn(s, i) for i in xrange(10)] )
|
||||
[[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]]
|
||||
21
Task/Forward-difference/R/forward-difference.r
Normal file
21
Task/Forward-difference/R/forward-difference.r
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
forwarddif <- function(a, n) {
|
||||
if ( n == 1 )
|
||||
a[2:length(a)] - a[1:length(a)-1]
|
||||
else {
|
||||
r <- forwarddif(a, 1)
|
||||
forwarddif(r, n-1)
|
||||
}
|
||||
}
|
||||
|
||||
fdiff <- function(a, n) {
|
||||
r <- a
|
||||
for(i in 1:n) {
|
||||
r <- r[2:length(r)] - r[1:length(r)-1]
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
v <- c(90, 47, 58, 29, 22, 32, 55, 5, 55, 73)
|
||||
|
||||
print(forwarddif(v, 9))
|
||||
print(fdiff(v, 9))
|
||||
44
Task/Forward-difference/REXX/forward-difference-1.rexx
Normal file
44
Task/Forward-difference/REXX/forward-difference-1.rexx
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*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?*/
|
||||
/*═════════════════════════════════════════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*/
|
||||
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.*/
|
||||
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))
|
||||
35
Task/Forward-difference/REXX/forward-difference-2.rexx
Normal file
35
Task/Forward-difference/REXX/forward-difference-2.rexx
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/* REXX ***************************************************************
|
||||
* Forward differences
|
||||
* 18.08.2012 Walter Pachl derived from PL/I
|
||||
**********************************************************************/
|
||||
Do n=-1 To 11
|
||||
Call differences '90 47 58 29 22 32 55 5 55 73',n
|
||||
End
|
||||
Exit
|
||||
|
||||
differences: Procedure
|
||||
Parse Arg a,n
|
||||
m=words(a)
|
||||
Select
|
||||
When n<0 Then Say 'n is negative:' n '<' 0
|
||||
When n>m Then Say 'n is too large:' n '>' m
|
||||
Otherwise Do
|
||||
Do i=1 By 1 while a<>''
|
||||
Parse Var a a.i a
|
||||
End
|
||||
Do i = 1 to n;
|
||||
t = a.i;
|
||||
Do j = i+1 to m;
|
||||
u = a.j
|
||||
a.j = a.j-t;
|
||||
t = u;
|
||||
end;
|
||||
end;
|
||||
ol=''
|
||||
Do k=n+1 to m
|
||||
ol=ol a.k
|
||||
End
|
||||
Say n ol
|
||||
End
|
||||
End
|
||||
Return
|
||||
7
Task/Forward-difference/Ruby/forward-difference-1.rb
Normal file
7
Task/Forward-difference/Ruby/forward-difference-1.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def dif(s)
|
||||
s.each_cons(2).collect { |x, y| y - x }
|
||||
end
|
||||
|
||||
def difn(s, n)
|
||||
n.times.inject(s) { |s, | dif(s) }
|
||||
end
|
||||
2
Task/Forward-difference/Ruby/forward-difference-2.rb
Normal file
2
Task/Forward-difference/Ruby/forward-difference-2.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
p dif([1, 23, 45, 678]) # => [22, 22, 633]
|
||||
p difn([1, 23, 45, 678], 2) # => [0, 611]
|
||||
3
Task/Forward-difference/Scala/forward-difference-1.scala
Normal file
3
Task/Forward-difference/Scala/forward-difference-1.scala
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def fdiff(xs: List[Int]) = (xs.tail, xs).zipped.map(_ - _)
|
||||
|
||||
def fdiffn(i: Int, xs: List[Int]): List[Int] = if (i == 1) fdiff(xs) else fdiffn(i - 1, fdiff(xs))
|
||||
2
Task/Forward-difference/Scala/forward-difference-2.scala
Normal file
2
Task/Forward-difference/Scala/forward-difference-2.scala
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
val l=List(90,47,58,29,22,32,55,5,55,73)
|
||||
(1 to 9)foreach(x=>println(fdiffn(x,l)))
|
||||
11
Task/Forward-difference/Scheme/forward-difference.ss
Normal file
11
Task/Forward-difference/Scheme/forward-difference.ss
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(define (forward-diff lst)
|
||||
(if (or (null? lst) (null? (cdr lst)))
|
||||
'()
|
||||
(cons (- (cadr lst) (car lst))
|
||||
(forward-diff (cdr lst)))))
|
||||
|
||||
(define (nth-forward-diff n xs)
|
||||
(if (= n 0)
|
||||
xs
|
||||
(nth-forward-diff (- n 1)
|
||||
(forward-diff xs))))
|
||||
13
Task/Forward-difference/Smalltalk/forward-difference.st
Normal file
13
Task/Forward-difference/Smalltalk/forward-difference.st
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Array extend [
|
||||
difference [
|
||||
^self allButFirst with: self allButLast collect: [ :a :b | a - b ]
|
||||
]
|
||||
|
||||
nthOrderDifference: n [
|
||||
^(1 to: n) inject: self into: [ :old :unused | old difference ]
|
||||
]
|
||||
]
|
||||
|
||||
s := #(90 47 58 29 22 32 55 5 55 73)
|
||||
1 to: s size - 1 do: [ :i |
|
||||
(s nthOrderDifference: i) printNl ]
|
||||
23
Task/Forward-difference/Tcl/forward-difference.tcl
Normal file
23
Task/Forward-difference/Tcl/forward-difference.tcl
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
proc do_fwd_diff {list} {
|
||||
set previous [lindex $list 0]
|
||||
set new [list]
|
||||
foreach current [lrange $list 1 end] {
|
||||
lappend new [expr {$current - $previous}]
|
||||
set previous $current
|
||||
}
|
||||
return $new
|
||||
}
|
||||
|
||||
proc fwd_diff {list order} {
|
||||
while {$order >= 1} {
|
||||
set list [do_fwd_diff $list]
|
||||
incr order -1
|
||||
}
|
||||
return $list
|
||||
}
|
||||
|
||||
set a {90.5 47 58 29 22 32 55 5 55 73.5}
|
||||
|
||||
for {set order 0} {$order <= 10} {incr order} {
|
||||
puts [format "%d\t%s" $order [fwd_diff $a $order]]
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue