|
| 1 | +using RandomMatrices |
| 2 | +using Base.Test |
| 3 | + |
1 | 4 | # Excursions in formal power series (fps) |
2 | | -MaxSeriesSize=10 |
3 | | -MaxRange = 50 |
4 | | -MatrixSize=15 |
| 5 | +MaxSeriesSize=8 |
| 6 | +MaxRange = 20 |
| 7 | +MatrixSize=10 |
5 | 8 | TT=Int64 |
6 | 9 |
|
7 | | -(n1, n2, n3) = round(Int,rand(3)*MaxSeriesSize) |
| 10 | +(n1, n2, n3) = rand(1:MaxSeriesSize, 3) |
8 | 11 |
|
9 | | -X = FormalPowerSeries{TT}(round(Int,rand(n1)*MaxRange)) |
10 | | -Y = FormalPowerSeries{TT}(round(Int,rand(n2)*MaxRange)) |
11 | | -Z = FormalPowerSeries{TT}(round(Int,rand(n3)*MaxRange)) |
| 12 | +X = FormalPowerSeries{TT}(rand(1:MaxRange, n1)) |
| 13 | +Y = FormalPowerSeries{TT}(rand(1:MaxRange, n2)) |
| 14 | +Z = FormalPowerSeries{TT}(rand(1:MaxRange, n3)) |
12 | 15 |
|
13 | | -c = round(Int,rand()*MatrixSize) #Size of matrix representation to generate |
| 16 | +c = rand(1:MatrixSize) #Size of matrix representation to generate |
14 | 17 |
|
15 | | -nzeros = round(Int,rand()*MaxSeriesSize) |
16 | | -@assert X == trim(X) |
| 18 | +nzeros = rand(1:MaxSeriesSize) |
| 19 | +@test X == trim(X) |
17 | 20 | XX = deepcopy(X) |
18 | 21 | for i=1:nzeros |
19 | | - idx = round(Int,rand()*MaxRange) |
| 22 | + idx = rand(1:MaxRange) |
20 | 23 | if !haskey(XX.c, idx) |
21 | 24 | XX.c[idx] = convert(TT, 0) |
22 | 25 | end |
23 | 26 | end |
24 | | -@assert trim(XX) == X |
| 27 | +@test trim(XX) == X |
25 | 28 |
|
26 | 29 | #Test addition, p.15, (1.3-4) |
27 | | -@assert X+X == 2X |
28 | | -@assert X+Y == Y+X |
29 | | -@assert MatrixForm(X+Y,c) == MatrixForm(X,c)+MatrixForm(Y,c) |
| 30 | +@test X+X == 2X |
| 31 | +@test X+Y == Y+X |
| 32 | +@test MatrixForm(X+Y,c) == MatrixForm(X,c)+MatrixForm(Y,c) |
30 | 33 |
|
31 | 34 | #Test subtraction, p.15, (1.3-4) |
32 | | -@assert X-X == 0X |
33 | | -@assert X-Y == -(Y-X) |
34 | | -@assert MatrixForm(X-Y,c) == MatrixForm(X,c)-MatrixForm(Y,c) |
| 35 | +@test X-X == 0X |
| 36 | +@test X-Y == -(Y-X) |
| 37 | +@test MatrixForm(X-Y,c) == MatrixForm(X,c)-MatrixForm(Y,c) |
35 | 38 |
|
36 | 39 | #Test multiplication, p.15, (1.3-5) |
37 | | -@assert X*Y == Y*X |
38 | | -@assert MatrixForm(X*X,c) == MatrixForm(X,c)*MatrixForm(X,c) |
39 | | -@assert MatrixForm(X*Y,c) == MatrixForm(X,c)*MatrixForm(Y,c) |
40 | | -@assert MatrixForm(Y*X,c) == MatrixForm(Y,c)*MatrixForm(X,c) |
41 | | -@assert MatrixForm(Y*Y,c) == MatrixForm(Y,c)*MatrixForm(Y,c) |
| 40 | +@test X*Y == Y*X |
| 41 | +@test MatrixForm(X*X,c) == MatrixForm(X,c)*MatrixForm(X,c) |
| 42 | +@test MatrixForm(X*Y,c) == MatrixForm(X,c)*MatrixForm(Y,c) |
| 43 | +@test MatrixForm(Y*X,c) == MatrixForm(Y,c)*MatrixForm(X,c) |
| 44 | +@test MatrixForm(Y*Y,c) == MatrixForm(Y,c)*MatrixForm(Y,c) |
42 | 45 |
|
43 | | -@assert X.*Y == Y.*X |
| 46 | +@test X.*Y == Y.*X |
44 | 47 |
|
45 | 48 | #The reciprocal series has associated matrix that is the matrix inverse |
46 | 49 | #of the original series |
47 | 50 | #Force reciprocal to exist |
48 | 51 | X.c[0] = 1 |
49 | | -discrepancy = (norm(inv(float(MatrixForm(X,c)))[1, :]'[:, 1] - tovector(reciprocal(X, c),0:c-1))) |
50 | | -if discrepancy > c*sqrt(eps()) |
51 | | - error(@sprintf("Error %f exceeds tolerance %f", discrepancy, c*sqrt(eps()))) |
| 52 | +discrepancy = (norm(inv(float(MatrixForm(X,c)))[1, :]'[:, 1] - tovector(reciprocal(X, c), 0:c-1))) |
| 53 | +tol = c*√eps() |
| 54 | +if discrepancy > tol |
| 55 | + error(string("Error ", discrepancy, " exceeds tolerance ", tol)) |
52 | 56 | end |
53 | | -#@assert norm(inv(float(MatrixForm(X,c)))[1, :]'[:, 1] - tovector(reciprocal(X, c),c)) < c*sqrt(eps()) |
| 57 | +#@test norm(inv(float(MatrixForm(X,c)))[1, :]'[:, 1] - tovector(reciprocal(X, c),c)) < c*sqrt(eps()) |
54 | 58 |
|
55 | 59 | #Test differentiation |
56 | 60 | XX = derivative(X) |
57 | 61 | for (k, v) in XX.c |
58 | | - k==0 ? continue : true |
59 | | - @assert X.c[k+1] == v/(k+1) |
| 62 | + k==0 && continue |
| 63 | + @test X.c[k+1] == v/(k+1) |
60 | 64 | end |
61 | 65 |
|
62 | 66 | #Test product rule [H, Sec.1.4, p.19] |
63 | | -@assert derivative(X*Y) == derivative(X)*Y + X*derivative(Y) |
| 67 | +@test derivative(X*Y) == derivative(X)*Y + X*derivative(Y) |
64 | 68 |
|
65 | 69 | #Test right distributive law of composition [H, Sec.1.6, p.38] |
66 | | -@assert compose(X,Z)*compose(Y,Z) == compose(X*Y, Z) |
| 70 | +@test compose(X,Z)*compose(Y,Z) == compose(X*Y, Z) |
67 | 71 |
|
68 | 72 | #Test chain rule [H, Sec.1.6, p.40] |
69 | | -@assert derivative(compose(X,Y)) == compose(derivative(X),Y)*derivative(Y) |
| 73 | +@test derivative(compose(X,Y)) == compose(derivative(X),Y)*derivative(Y) |
| 74 | + |
0 commit comments