Debian Codegraph @

dwtest/test_solves.praat

raw
1 # test_solves.praat
2 # djmw 20191201, 20210526
3
4 appendInfoLine: "test_solves.praat"
5
6 @test_weakly_example
7 @test_weakly: 10
8 @test_nonnegative
9 appendInfoLine: "test_solves.praat OK"
10
11 procedure test_weakly_example
12 appendInfoLine: tab$, "solveWeaklyConstrained# (f##, phi#, alpha, delta)"
13 appendInfoLine: tab$, "Example J. ten Berge (1991), page 606"
14 f## = {{ 4, 1, 0.5},
15 ... { 4, -1, -0.5},
16 ... {-4, 1, -0.5},
17 ... {-4, -1, 0.5},
18 ... { 2, 0, 0},
19 ... {-2, 0, 0} }
20
21 phi# = {-1, -1, -1, -1, -1, 1 }
22 alpha = 6
23 delta = 2 / 3
24 result# = solveWeaklyConstrained# (f##, phi#, alpha, delta)
25 inner = inner (result#, result#)
26
27 diff = abs (inner - 7/12)
28 assert diff < 1.0e-14 ; 'diff'
29 diff = abs (result#[1] - (- 4/71))
30 assert diff < 1.0e-14; 'diff'
31 diff = abs (result#[2] - 0)
32 assert abs (result#[2]) < 1.0e-14; 'diff'
33 diff = abs (result#[3] - sqrt (35095/60492))
34 assert diff < 1.0e-14; 'diff'
35 appendInfoLine: tab$, tab$, inner, "; ", result#
36 appendInfoLine: tab$, "Force norm to be one alpha = 1e30, delta=1.0"
37 alpha = 1e30
38 delta=1.0
39 result# = solveWeaklyConstrained# (f##, phi#, alpha, delta)
40 inner = inner (result#, result#)
41 appendInfoLine: tab$, tab$, "norm(result)=", inner
42 diff = abs (inner - 1.0)
43 assert diff < 1.0e-14; 'diff'
44 endproc
45
46 procedure test_nonnegative
47 appendInfoLine: tab$ , "solveNonnegative# (m##, v#, maximumnumberOfIterations, tolerance, info)"
48 a## = {{-4, 2, 2}, {2,4, 2}, {1,1,1},{2,-1,3}}
49 y# = {1,2,1,3}
50 ; octave says:
51 ; A = [-4 2 2; 2 4 2; 1 1 1; 2 -1 3]
52 ; b = [1; 2; 1; 3]
53 ; x = lsqnonneg (A, b)
54 ; x =[0.17687; 0.0000; 0.85941]
55 ;
56 solution# = {0.17687, 0.0, 0.85941}
57 result# = solveNonnegative# (a##, y#, 100, 1e-17, 2)
58 ;appendInfoLine: result#
59 dif = norm (result# - solution#)
60 assert dif < 1e-5
61 a## = {{-4, 2, 2}, {2,4, 2}, {1,1,1},{2,-1,3}}
62 y# = {1,2,1,3}
63 result3# = solveNonnegative# (a##, y#, 3, 1e-17, 2)
64 ;appendInfoLine: result#
65 result# = solveNonnegative# (a##, y#, result3#, 100, 1e-17, 1)
66 ;appendInfoLine: result#
67 dif = norm (result# - solution#)
68 assert dif < 1e-6
69 appendInfoLine: tab$, "Example fromR with library (nnls)"
70 # next example is from R with library
71 # set.seed(1)
72 # n <- 8; p <- 4
73 # x <- matrix(rnorm(n * p), nrow = n)
74 # y <- x %*% matrix(rep(c(1, -1), length.out = p), ncol = 1) + rnorm(n)
75 # library(nnls)
76 # mod1 <- nnls(x, y)
77 # mod1$x
78 a## = { {-0.6264538, 0.57578135, -0.01619026, 0.61982575},
79 ... {0.1836433, -0.30538839, 0.94383621, -0.05612874},
80 ... { -0.8356286, 1.51178117, 0.82122120, -0.15579551},
81 ... { 1.5952808, 0.38984324, 0.59390132, -1.47075238},
82 ... { 0.3295078, -0.62124058, 0.91897737, -0.47815006},
83 ... { -0.8204684, -2.21469989, 0.78213630, 0.41794156},
84 ... { 0.4874291, 1.12493092, 0.07456498, 1.35867955},
85 ... { 0.7383247, -0.04493361, -1.98935170, -0.10278773}}
86 y# = { -1.4505796, 1.4351916, -2.7474526, 2.8550967, 1.9535858, 1.6991128, -0.8215911, -0.3401299}
87 solution# = {1.4907686, 0.0000000, 0.8906014, 0.0000000}
88 result# = solveNonnegative# (a##, y#, 100, 1e-17, 1)
89 dif = norm (result# - solution#)
90 assert dif < 1e-6
91 endproc
92
93 procedure test_weakly: .numberOfTries
94 appendInfoLine: tab$ , "Random size matrices, randomUniform (-1,1) data"
95 for .itry to .numberOfTries
96 .nrow = randomInteger (2, 100)
97 .ncol = randomInteger (2, .nrow)
98 .a## = randomUniform## (.nrow, .ncol, -1, 1)
99 .b# = randomUniform# (.nrow, -1,1)
100 .alpha = randomUniform (0, 10)
101 .delta = randomUniform (0, 1)
102 result# = solveWeaklyConstrained# (.a##, .b#, .alpha, .delta)
103 appendInfoLine: tab$, tab$, .nrow, "x", .ncol, " alpha=", .alpha, "; delta=", .delta
104 endfor
105 endproc