Skip to content

Commit 439d4b5

Browse files
committed
Moved tests of fps to test/
1 parent b1098e2 commit 439d4b5

File tree

2 files changed

+77
-73
lines changed

2 files changed

+77
-73
lines changed

src/FormalPowerSeries.jl

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -309,77 +309,4 @@ type FormalLaurentSeries{Coefficients}
309309
c
310310
end
311311
end
312-
313-
314-
###########
315-
# Sandbox #
316-
###########
317-
318-
MaxSeriesSize=10
319-
MaxRange = 50
320-
MatrixSize=15
321-
TT=Int64
322-
323-
(n1, n2, n3) = int(rand(3)*MaxSeriesSize)
324-
325-
X = FormalPowerSeries{TT}(int(rand(n1)*MaxRange))
326-
Y = FormalPowerSeries{TT}(int(rand(n2)*MaxRange))
327-
Z = FormalPowerSeries{TT}(int(rand(n3)*MaxRange))
328-
329-
c = int(rand()*MatrixSize) #Size of matrix representation to generate
330-
331-
nzeros = int(rand()*MaxSeriesSize)
332-
@assert X == trim(X)
333-
XX = deepcopy(X)
334-
for i=1:nzeros
335-
idx = int(rand()*MaxRange)
336-
if !has(XX.c, idx)
337-
XX.c[idx] = convert(TT, 0)
338-
end
339-
end
340-
@assert trim(XX) == X
341-
342-
#Test addition, p.15, (1.3-4)
343-
@assert X+X == 2X
344-
@assert X+Y == Y+X
345-
@assert MatrixForm(X+Y,c) == MatrixForm(X,c)+MatrixForm(Y,c)
346-
347-
#Test subtraction, p.15, (1.3-4)
348-
@assert X-X == 0X
349-
@assert X-Y == -(Y-X)
350-
@assert MatrixForm(X-Y,c) == MatrixForm(X,c)-MatrixForm(Y,c)
351-
352-
#Test multiplication, p.15, (1.3-5)
353-
@assert X*Y == Y*X
354-
@assert MatrixForm(X*X,c) == MatrixForm(X,c)*MatrixForm(X,c)
355-
@assert MatrixForm(X*Y,c) == MatrixForm(X,c)*MatrixForm(Y,c)
356-
@assert MatrixForm(Y*X,c) == MatrixForm(Y,c)*MatrixForm(X,c)
357-
@assert MatrixForm(Y*Y,c) == MatrixForm(Y,c)*MatrixForm(Y,c)
358-
359-
@assert X.*Y == Y.*X
360-
361-
#The reciprocal series has associated matrix that is the matrix inverse
362-
#of the original series
363-
#Force reciprocal to exist
364-
X.c[0] = 1
365-
discrepancy = (norm(inv(float(MatrixForm(X,c)))[1, :]'[:, 1] - tovector(reciprocal(X, c),[0:c-1])))
366-
if discrepancy > c*sqrt(eps())
367-
error(@sprintf("Error %f exceeds tolerance %f", discrepancy, c*sqrt(eps())))
368-
end
369-
#@assert norm(inv(float(MatrixForm(X,c)))[1, :]'[:, 1] - tovector(reciprocal(X, c),c)) < c*sqrt(eps())
370-
371-
#Test differentiation
372-
XX = derivative(X)
373-
for (k, v) in XX.c
374-
k==0 ? continue : true
375-
@assert X.c[k+1] == v/(k+1)
376-
end
377-
378-
#Test product rule [H, Sec.1.4, p.19]
379-
@assert derivative(X*Y) == derivative(X)*Y + X*derivative(Y)
380-
381-
#Test right distributive law of composition [H, Sec.1.6, p.38]
382-
@assert compose(X,Z)*compose(Y,Z) == compose(X*Y, Z)
383312

384-
#Test chain rule [H, Sec.1.6, p.40]
385-
@assert derivative(compose(X,Y)) == compose(derivative(X),Y)*derivative(Y)

test/FormalPowerSeries.jl

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

0 commit comments

Comments
 (0)