2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,6 +1,14 @@
|
|||
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 tdefmodule Diff do
|
||||
;Task:
|
||||
Provide code that produces a list of numbers which is the <big>n<sup>th</sup></big> 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 <big>'''A'''</big> is a new list <big>'''B'''</big>, where <big><b>B</b><sub>n</sub> = <b>A</b><sub>n+1</sub> - <b>A</b><sub>n</sub></big>.
|
||||
|
||||
List <big>'''B'''</big> should have one fewer element as a result.
|
||||
|
||||
The second-order forward difference of <big>'''A'''</big> will be:
|
||||
<pre>
|
||||
tdefmodule Diff do
|
||||
def forward(arr,i\\1) do
|
||||
forward(arr,[],i)
|
||||
end
|
||||
|
|
@ -16,14 +24,22 @@ The second-order forward difference of A will be tdefmodule Diff do
|
|||
def forward([val1|[val2|vals]],diffs,i) do
|
||||
forward([val2|vals],diffs++[val2-val1],i)
|
||||
end
|
||||
endhe same as the first-order forward difference of B.
|
||||
That new list will have two fewer elements than A and one less than B.
|
||||
end
|
||||
</pre>
|
||||
The same as the first-order forward difference of <big>'''B'''</big>.
|
||||
|
||||
That new list will have two fewer elements than <big>'''A'''</big> and one less than <big>'''B'''</big>.
|
||||
|
||||
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].
|
||||
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)
|
||||
|
||||
;Algorithmic options:
|
||||
* Iterate through all previous forward differences and re-calculate a new array each time.
|
||||
* Use this formula (from [[wp:Forward difference|Wikipedia]]):
|
||||
<big><big>
|
||||
::: <math>\Delta^n [f](x)= \sum_{k=0}^n {n \choose k} (-1)^{n-k} f(x+k)</math>
|
||||
</big></big>
|
||||
::: ([[Pascal's Triangle]] may be useful for this option.)
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
fd=:}.-}:^:
|
||||
fd=: }. - }: ^:
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
list =: 90 47 58 29 22 32 55 5 55 73 NB. Some numbers
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,24 +1,23 @@
|
|||
/*REXX program computes the forward difference of a list of numbers. */
|
||||
numeric digits 100 /*ensure enough accuracy (decimal digs)*/
|
||||
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */
|
||||
if e=='' then e='90 47 58 29 22 32 55 5 55 73' /*use some default numbers. */
|
||||
#=words(e) /*# is the number of elements in list.*/
|
||||
/* [↓] assign list numbers to @ array.*/
|
||||
do i=1 for #; @.i=word(e,i)/1; end /*process each number one at a time. */
|
||||
/* [↓] process the optional order. */
|
||||
if N=='' then parse value 0 # # with bot top N /*define default order range. */
|
||||
else parse var N bot 1 top /*Specified? Use only 1 order*/
|
||||
say right(# 'numbers:', 44) e /*display the header (title) and ··· */
|
||||
say left('',44)copies('─',length(e)+2) /*display the header fence. */
|
||||
/* [↓] where da rubber meets da road. */
|
||||
do o=bot to top; do r=1 for #; !.r=@.r; end; $=
|
||||
do j=1 for o; d=!.j; do k=j+1 to #; parse value !.k !.k-d with d !.k; end
|
||||
end /*j*/
|
||||
do i=o+1 to #; $=$ !.i/1; end
|
||||
if $=='' then $='[null]'
|
||||
say right(o,7)th(o)'─order forward difference vector =' $
|
||||
end /*o*/
|
||||
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
th: arg ?; return word('th st nd rd',1+?//10*(?//100%10\==1)*(?//10<4))
|
||||
/*REXX program computes the forward difference of a list of numbers. */
|
||||
numeric digits 100 /*ensure enough accuracy (decimal digs)*/
|
||||
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */
|
||||
if e=='' then e=90 47 58 29 22 32 55 5 55 73 /*Not specified? Then use the default.*/
|
||||
#=words(e) /*# is the number of elements in list.*/
|
||||
/* [↓] assign list numbers to @ array.*/
|
||||
do i=1 for #; @.i=word(e, i)/1; end /*i*/ /*process each number one at a time. */
|
||||
/* [↓] process the optional order. */
|
||||
if N=='' then parse value 0 # # with bot top N /*define the default order range. */
|
||||
else parse var N bot 1 top /*Not specified? Then use only 1 order*/
|
||||
say right(# 'numbers:', 44) e /*display the header title and ··· */
|
||||
say left('', 44)copies('─', length(e)+2) /* " " " fence. */
|
||||
/* [↓] where da rubber meets da road. */
|
||||
do o=bot to top; do r=1 for #; !.r=@.r; end /*r*/; $=
|
||||
do j=1 for o; d=!.j; do k=j+1 to #; parse value !.k !.k-d with d !.k; end /*k*/
|
||||
end /*j*/
|
||||
do i=o+1 to #; $=$ !.i/1; end /*i*/
|
||||
if $=='' then $=' [null]'
|
||||
say right(o, 7)th(o)'─order forward difference vector =' $
|
||||
end /*o*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
th: procedure; x=abs(arg(1)); return word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))
|
||||
|
|
|
|||
|
|
@ -1,30 +1,29 @@
|
|||
/*REXX program computes the forward difference of a list of numbers. */
|
||||
numeric digits 100 /*ensure enough accuracy (decimal digs)*/
|
||||
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */
|
||||
if e=='' then e='90 47 58 29 22 32 55 5 55 73' /*use some default numbers. */
|
||||
#=words(e) /*# is the number of elements in list.*/
|
||||
/* [↓] verify list items are numeric. */
|
||||
do i=1 for #; _=word(e,i) /*process each number one at a time. */
|
||||
if \datatype(_,'N') then call ser _ "isn't a valid number"; @.i=_/1
|
||||
end /*i*/ /* [↑] removes superfluous stuff. */
|
||||
/* [↓] process the optional order. */
|
||||
if N=='' then parse value 0 # # with bot top N /*define default order range. */
|
||||
else parse var N bot 1 top /*Specified? Use only 1 order*/
|
||||
if #==0 then call ser "no numbers were specified."
|
||||
if N<0 then call ser N "(order) can't be negative."
|
||||
if N># then call ser N "(order) can't be greater than" #
|
||||
say right(# 'numbers:', 44) e /*display the header (title) and ··· */
|
||||
say left('',44)copies('─',length(e)+2) /*display the header fence. */
|
||||
/* [↓] where da rubber meets da road. */
|
||||
do o=bot to top; do r=1 for #; !.r=@.r; end; $=
|
||||
do j=1 for o; d=!.j; do k=j+1 to #; parse value !.k !.k-d with d !.k; end
|
||||
end /*j*/
|
||||
do i=o+1 to #; $=$ !.i/1; end
|
||||
if $=='' then $=' [null]'
|
||||
say right(o,7)th(o)'─order forward difference vector =' $
|
||||
end /*o*/
|
||||
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
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))
|
||||
/*REXX program computes the forward difference of a list of numbers. */
|
||||
numeric digits 100 /*ensure enough accuracy (decimal digs)*/
|
||||
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */
|
||||
if e=='' then e=90 47 58 29 22 32 55 5 55 73 /*Not specified? Then use the default.*/
|
||||
#=words(e) /*# is the number of elements in list.*/
|
||||
/* [↓] verify list items are numeric. */
|
||||
do i=1 for #; _=word(e, i) /*process each number one at a time. */
|
||||
if \datatype(_, 'N') then call ser _ "isn't a valid number"; @.i=_/1
|
||||
end /*i*/ /* [↑] removes superfluous stuff. */
|
||||
/* [↓] process the optional order. */
|
||||
if N=='' then parse value 0 # # with bot top N /*define the default order range. */
|
||||
else parse var N bot 1 top /*Not specified? Then use only 1 order*/
|
||||
if #==0 then call ser "no numbers were specified."
|
||||
if N<0 then call ser N "(order) can't be negative."
|
||||
if N># then call ser N "(order) can't be greater than" #
|
||||
say right(# 'numbers:', 44) e /*display the header (title) and ··· */
|
||||
say left('', 44)copies('─', length(e)+2) /*display the header fence. */
|
||||
/* [↓] where da rubber meets da road. */
|
||||
do o=bot to top; do r=1 for #; !.r=@.r; end /*r*/; $=
|
||||
do j=1 for o; d=!.j; do k=j+1 to #; parse value !.k !.k-d with d !.k; end /*k*/
|
||||
end /*j*/
|
||||
do i=o+1 to #; $=$ !.i/1; end /*i*/
|
||||
if $=='' then $=' [null]'
|
||||
say right(o, 7)th(o)'─order forward difference vector =' $
|
||||
end /*o*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say; say '***error***'; say arg(1); say; exit 13
|
||||
th: procedure; x=abs(arg(1)); return word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))
|
||||
|
|
|
|||
|
|
@ -1,34 +1,33 @@
|
|||
/*REXX program computes the forward difference of a list of numbers. */
|
||||
numeric digits 100 /*ensure enough accuracy (decimal digs)*/
|
||||
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */
|
||||
if e=='' then e='90 47 58 29 22 32 55 5 55 73' /*use some default numbers. */
|
||||
#=words(e); w=5 /*# is the number of elements in list.*/
|
||||
/* [↓] verify list items are numeric. */
|
||||
do i=1 for #; _=word(e,i) /*process each number one at a time. */
|
||||
if \datatype(_,'N') then call ser _ "isn't a valid number"; @.i=_/1
|
||||
w=max(w,length(@.i)) /*use the maximum length of an element.*/
|
||||
end /*i*/ /* [↑] removes superfluous stuff. */
|
||||
/* [↓] process the optional order. */
|
||||
if N=='' then parse value 0 # # with bot top N /*define default order range. */
|
||||
else parse var N bot 1 top /*Specified? Use only 1 order*/
|
||||
if #==0 then call ser "no numbers were specified."
|
||||
if N<0 then call ser N "(order) can't be negative."
|
||||
if N># then call ser N "(order) can't be greater than" #
|
||||
_=; do k=1 for #; _=_ right(@.k,w); end /*k*/; _=substr(_,2)
|
||||
say right(# 'numbers:', 44) _ /*display the header (title) and ··· */
|
||||
say left('',44)copies('─',w*#+#) /*display the header fence. */
|
||||
/* [↓] where da rubber meets da road. */
|
||||
do o=bot to top; do r=1 for #; !.r=@.r; end /*r*/; $=
|
||||
do j=1 for o; d=!.j; do k=j+1 to #; parse value !.k !.k-d with d !.k
|
||||
w=max(w,length(!.k))
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
do i=o+1 to #; $=$ right(!.i/1,w); end /*i*/
|
||||
if $=='' then $='[null]'
|
||||
say right(o,7)th(o)'─order forward difference vector =' $
|
||||
end /*o*/
|
||||
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
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))
|
||||
/*REXX program computes the forward difference of a list of numbers. */
|
||||
numeric digits 100 /*ensure enough accuracy (decimal digs)*/
|
||||
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */
|
||||
if e=='' then e=90 47 58 29 22 32 55 5 55 73 /*Not specified? Then use the default.*/
|
||||
#=words(e); w=5 /*# is the number of elements in list.*/
|
||||
/* [↓] verify list items are numeric. */
|
||||
do i=1 for #; _=word(e, i) /*process each number one at a time. */
|
||||
if \datatype(_, 'N') then call ser _ "isn't a valid number"; @.i=_/1
|
||||
w=max(w, length(@.i)) /*use the maximum length of an element.*/
|
||||
end /*i*/ /* [↑] removes superfluous stuff. */
|
||||
/* [↓] process the optional order. */
|
||||
if N=='' then parse value 0 # # with bot top N /*define the default order range. */
|
||||
else parse var N bot 1 top /*Not specified? Then use only 1 order*/
|
||||
if #==0 then call ser "no numbers were specified."
|
||||
if N<0 then call ser N "(order) can't be negative."
|
||||
if N># then call ser N "(order) can't be greater than" #
|
||||
_=; do k=1 for #; _=_ right(@.k, w); end /*k*/; _=substr(_, 2)
|
||||
say right(# 'numbers:', 44) _ /*display the header title and ··· */
|
||||
say left('', 44)copies('─', w*#+#) /* " " " fence. */
|
||||
/* [↓] where da rubber meets da road. */
|
||||
do o=bot to top; do r=1 for #; !.r=@.r; end /*r*/; $=
|
||||
do j=1 for o; d=!.j; do k=j+1 to #; parse value !.k !.k-d with d !.k
|
||||
w=max(w, length(!.k))
|
||||
end /*k*/
|
||||
end /*j*/
|
||||
do i=o+1 to #; $=$ right(!.i/1, w); end /*i*/
|
||||
if $=='' then $=' [null]'
|
||||
say right(o, 7)th(o)'─order forward difference vector =' $
|
||||
end /*o*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say; say '***error***'; say arg(1); say; exit 13
|
||||
th: procedure; x=abs(arg(1)); return word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
10 DATA 9,0,1,2,4,7,4,2,1,0
|
||||
20 LET p=1
|
||||
30 READ n: DIM b(n)
|
||||
40 FOR i=1 TO n
|
||||
50 READ b(i)
|
||||
60 NEXT i
|
||||
70 FOR j=1 TO p
|
||||
80 FOR i=1 TO n-j
|
||||
90 LET b(i)=b(i+1)-b(i)
|
||||
100 NEXT i
|
||||
110 NEXT j
|
||||
120 FOR i=1 TO n-p
|
||||
130 PRINT b(i);" ";
|
||||
140 NEXT i
|
||||
Loading…
Add table
Add a link
Reference in a new issue