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,58 @@
* Van der Corput sequence 31/01/2017
VDCS CSECT
USING VDCS,R13 base register
B 72(R15) skip savearea
DC 17F'0' savearea
STM R14,R12,12(R13) prolog
ST R13,4(R15) " <-
ST R15,8(R13) " ->
LR R13,R15 " addressability
ZAP B,=P'2' b=2 (base)
ZAP M,=P'-1' m=-1
SR R6,R6 i=0
LOOPI CH R6,=H'10' do i=0 to 10
BH ELOOPI
AP M,=P'1' w=m+1
ZAP V,=P'0' v=0
ZAP S,=P'1' s=1
ZAP N,M n=m
WHILE CP N,=P'0' do while n<>0
BE EWHILE
MP S,B s=s*b
ZAP PL16,N n
DP PL16,B n/b
ZAP W,PL16+8(8) w=n mod b
MP W,=P'100000' *100000
ZAP PL16,W w
DP PL16,S w/s
ZAP W,PL16(8) w=w/s
AP V,W v=v+(n mod b)*100000/s
ZAP PL16,N n
DP PL16,B n/b
ZAP N,PL16(8) n=n/b
B WHILE
EWHILE XDECO R6,XDEC edit i
MVC PG+0(3),XDEC+9 output i
MVC PG+3(3),=C' 0.'
UNPK Z,V unpack v
OI Z+L'Z-1,X'F0' edit v
MVC PG+6(5),Z+11 output v (v/100000)
XPRNT PG,L'PG print buffer
LA R6,1(R6) i=i+1
B LOOPI
ELOOPI L R13,4(0,R13) epilog
LM R14,R12,12(R13) " restore
XR R15,R15 " rc=0
BR R14 exit
B DS PL8
M DS PL8
V DS PL8
S DS PL8
N DS PL8
W DS PL8 packed
Z DS ZL16 zoned
PL16 DS PL16 packed max
PG DC CL80' ' buffer
XDEC DS CL12 work area for xdeco
YREGS
END VDCS

View file

@ -0,0 +1,39 @@
/*
* Return the _n_th term of the van der Corput sequence.
* Uses the current _ibase_.
*/
define v(n) {
auto c, r, s
s = scale
scale = 0 /* to use integer division */
/*
* c = count digits of n
* r = reverse the digits of n
*/
for (0; n != 0; n /= 10) {
c += 1
r = (10 * r) + (n % 10)
}
/* move radix point to left of digits */
scale = length(r) + 6
r /= 10 ^ c
scale = s
return r
}
t = 10
for (b = 2; b <= 4; b++) {
"base "; b
obase = b
for (i = 0; i < 10; i++) {
ibase = b
" "; v(i)
ibase = t
}
obase = t
}
quit

View file

@ -1,35 +0,0 @@
#include <stdio.h>
void vc(int n, int base, int *num, int *denom)
{
int p = 0, q = 1;
while (n) {
p = p * base + (n % base);
q *= base;
n /= base;
}
*num = p;
*denom = q;
while (p) { n = p; p = q % p; q = n; }
*num /= q;
*denom /= q;
}
int main()
{
int d, n, i, b;
for (b = 2; b < 6; b++) {
printf("base %d:", b);
for (i = 0; i < 10; i++) {
vc(i, b, &n, &d);
if (n) printf(" %d/%d", n, d);
else printf(" 0");
}
printf("\n");
}
return 0;
}

View file

@ -1,4 +0,0 @@
base 2: 0 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16
base 3: 0 1/3 2/3 1/9 4/9 7/9 2/9 5/9 8/9 1/27
base 4: 0 1/4 1/2 3/4 1/16 5/16 9/16 13/16 1/8 3/8
base 5: 0 1/5 2/5 3/5 4/5 1/25 6/25 11/25 16/25 21/25

View file

@ -1,25 +0,0 @@
(defn van-der-corput
"Get the nth element of the van der Corput sequence."
([n]
;; Default base = 2
(van-der-corput n 2))
([n base]
(let [s (/ 1 base)] ;; A multiplicand to shift to the right of the decimal.
;; We essentially want to reverse the digits of n and put them after the
;; decimal point. So, we repeatedly pull off the lowest digit of n, scale
;; it to the right of the decimal point, and accumulate that.
(loop [sum 0
n n
scale s]
(if (zero? n)
sum ;; Base case: no digits left, so we're done.
(recur (+ sum (* (rem n base) scale)) ;; Accumulate the least digit
(quot n base) ;; Drop a digit of n
(* scale s))))))) ;; Move farther past the decimal
(clojure.pprint/print-table
(cons :base (range 10)) ;; column headings
(for [base (range 2 6)] ;; rows
(into {:base base}
(for [n (range 10)] ;; table entries
[n (van-der-corput n base)]))))

View file

@ -1,6 +0,0 @@
| :base | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|-------+---+-----+-----+-----+------+------+------+-------+-------+-------|
| 2 | 0 | 1/2 | 1/4 | 3/4 | 1/8 | 5/8 | 3/8 | 7/8 | 1/16 | 9/16 |
| 3 | 0 | 1/3 | 2/3 | 1/9 | 4/9 | 7/9 | 2/9 | 5/9 | 8/9 | 1/27 |
| 4 | 0 | 1/4 | 1/2 | 3/4 | 1/16 | 5/16 | 9/16 | 13/16 | 1/8 | 3/8 |
| 5 | 0 | 1/5 | 2/5 | 3/5 | 4/5 | 1/25 | 6/25 | 11/25 | 16/25 | 21/25 |

View file

@ -1,11 +0,0 @@
(defun van-der-Corput (n base)
(loop for d = 1 then (* d base) while (<= d n)
finally
(return (/ (parse-integer
(reverse (write-to-string n :base base))
:radix base)
d))))
(loop for base from 2 to 5 do
(format t "Base ~a: ~{~6a~^~}~%" base
(loop for i to 10 collect (van-der-Corput i base))))

View file

@ -1,4 +0,0 @@
Base 2: 0 1/2 1/4 3/4 1/8 5/8 3/8 7/8 1/16 9/16 5/16
Base 3: 0 1/3 2/3 1/9 4/9 7/9 2/9 5/9 8/9 1/27 10/27
Base 4: 0 1/4 1/2 3/4 1/16 5/16 9/16 13/16 1/8 3/8 5/8
Base 5: 0 1/5 2/5 3/5 4/5 1/25 6/25 11/25 16/25 21/25 2/25

View file

@ -0,0 +1,35 @@
FUNCTION VDC(N,BASE) !Calculates a Van der Corput number...
Converts 1234 in decimal to 4321 in V, and P = 10000.
INTEGER N !For this integer,
INTEGER BASE !In this base.
INTEGER I !A copy of N that can be damaged.
INTEGER P !Successive powers of BASE.
INTEGER V !Accumulates digits.
P = 1 ! = BASE**0
V = 0 !Start with no digits, as if N = 0.
I = N !Here we go.
DO WHILE (I .NE. 0) !While something remains,
V = V*BASE + MOD(I,BASE) !Extract its low-order digit.
I = I/BASE !Reduce it by a power.
P = P*BASE !And track the power.
END DO !Thus extract the digits in reverse order: right-to-left.
VDC = V/FLOAT(P) !The power is one above the highest digit.
END FUNCTION VDC !Numerology is weird.
PROGRAM POKE
INTEGER FIRST,LAST !Might as well document some constants.
PARAMETER (FIRST = 0,LAST = 9) !Thus, the first ten values.
INTEGER I,BASE !Steppers.
REAL VDC !Stop the compiler moaning about undeclared items.
WRITE (6,1) FIRST,LAST,(I, I = FIRST,LAST) !Announce.
1 FORMAT ("Calculates values ",I0," to ",I0," of the ",
1 "Van der Corput sequence, in various bases."/
2 "Base",666I9)
DO BASE = 2,13 !A selection of bases.
WRITE (6,2) BASE,(VDC(I,BASE), I = FIRST,LAST) !Show the specified span.
2 FORMAT (I4,666F9.6) !Aligns with FORMAT 1.
END DO !On to the next base.
END

View file

@ -0,0 +1,34 @@
// version 1.1.2
data class Rational(val num: Int, val denom: Int)
fun vdc(n: Int, base: Int): Rational {
var p = 0
var q = 1
var nn = n
while (nn != 0) {
p = p * base + nn % base
q *= base
nn /= base
}
val num = p
val denom = q
while (p != 0) {
nn = p
p = q % p
q = nn
}
return Rational(num / q, denom / q)
}
fun main(args: Array<String>) {
for (b in 2..5) {
print("base $b:")
for (i in 0..9) {
val(num, denom) = vdc(i, b)
if (num != 0) print(" $num/$denom")
else print(" 0")
}
println()
}
}

View file

@ -0,0 +1,20 @@
Procedure.d nBase(n.i,b.i)
Define r.d,s.i=1
While n
s*b
r+(Mod(n,b)/s)
n=Int(n/b)
Wend
ProcedureReturn r
EndProcedure
Define.i b,c
OpenConsole("van der Corput - Sequence")
For b=2 To 5
Print("Base "+Str(b)+": ")
For c=0 To 9
Print(StrD(nBase(c,b),5)+~"\t")
Next
PrintN("")
Next
Input()

View file

@ -1,4 +0,0 @@
def vdc(n, base=2)
str = n.to_s(base).reverse
str.to_i(base).quo(base ** str.length)
end

View file

@ -1,2 +0,0 @@
> Array.new(10){|i| vdc(i,3)}
=> [Rational(0, 1), Rational(1, 3), Rational(2, 3), Rational(1, 9), Rational(4, 9), Rational(7, 9), Rational(2, 9), Rational(5, 9), Rational(8, 9), Rational(1, 27)]

View file

@ -0,0 +1,12 @@
object VanDerCorput extends App {
def compute(n: Int, base: Int = 2) =
Iterator.from(0).
scanLeft(1)((a, _) => a * base).
map(b => (n - 1) / b -> b).
takeWhile(_._1 != 0).
foldLeft(0d)((a, b) => a + (b._1 % base).toDouble / b._2 / base)
val n = scala.io.StdIn.readInt
val b = scala.io.StdIn.readInt
(1 to n).foreach(x => println(compute(x, b)))
}

View file

@ -1,15 +1,15 @@
func vdc(value, base=2) {
while (value[-1] > 0) {
value.append(value[-1] / base -> int);
value.append(value[-1] / base -> int)
}
var (x, sum) = (1, 0);
var (x, sum) = (1, 0)
value.each { |i|
sum += ((i % base) / (x *= base));
sum += ((i % base) / (x *= base))
}
return sum;
return sum
}
2.to(5).each { |base|
var seq = (10.range.map {|i| vdc([i], base) });
"base %d: %s\n".printf(base, seq.map{|n| "%.4f" % n}.join(', '));
 
for base in (2..5) {
var seq = 10.of {|i| vdc([i], base) }
"base %d: %s\n".printf(base, seq.map{|n| "%.4f" % n}.join(', '))
}

View file

@ -0,0 +1,38 @@
mata
// 5th term of Van der Corput sequence
halton(1,1,5)
.625
// the first 10 terms of Van der Corput sequence
halton(10,1)
1
+---------+
1 | .5 |
2 | .25 |
3 | .75 |
4 | .125 |
5 | .625 |
6 | .375 |
7 | .875 |
8 | .0625 |
9 | .5625 |
10 | .3125 |
+---------+
// the first 10 terms of Van der Corput sequence in base 3
ghalton(10,3,0)
1
+---------------+
1 | .3333333333 |
2 | .6666666667 |
3 | .1111111111 |
4 | .4444444444 |
5 | .7777777778 |
6 | .2222222222 |
7 | .5555555556 |
8 | .8888888889 |
9 | .037037037 |
10 | .3703703704 |
+---------------+
end

View file

@ -0,0 +1,13 @@
clear
mata
st_addobs(2500)
st_addvar("double","x")
st_addvar("double","y")
st_addvar("double","z")
k=1::2500
st_store(k,1,k)
st_store(k,2,0.5*runiform(2500,1))
st_store(k,3,0.5:+0.5*halton(2500,1))
end
twoway scatter y x, msize(tiny) color(blue) ///
|| scatter z x, msize(tiny) color(green)

View file

@ -0,0 +1,9 @@
fcn vdc(n,base=2){
vdc:=0.0; denom:=1;
while(n){ reg remainder;
denom *= base;
n, remainder = n.divr(base);
vdc += (remainder.toFloat() / denom);
}
vdc
}

View file

@ -0,0 +1,4 @@
fcn vdc(n,base=2){
str:=n.toString(base).reverse();
str.toInt(base).toFloat()/(base.toFloat().pow(str.len()))
}