Mercurial > octave-nkf
comparison scripts/polynomial/ppval.m @ 18429:90568d7fe382 stable
ppval.m: Return correctly sized output for scalar polynomial input (bug #41448).
* ppval.m: Return correctly sized output for scalar polynomial input
(bug #41448).
author | Rik <rik@octave.org> |
---|---|
date | Mon, 03 Feb 2014 14:25:11 -0800 |
parents | d63878346099 |
children | 4197fc428c7d |
comparison
equal
deleted
inserted
replaced
18428:4d2e15abe5e7 | 18429:90568d7fe382 |
---|---|
30 function yi = ppval (pp, xi) | 30 function yi = ppval (pp, xi) |
31 | 31 |
32 if (nargin != 2) | 32 if (nargin != 2) |
33 print_usage (); | 33 print_usage (); |
34 endif | 34 endif |
35 if (! (isstruct (pp) && strcmp (pp.form, "pp"))) | 35 if (! (isstruct (pp) && isfield (pp, "form") && strcmp (pp.form, "pp"))) |
36 error ("ppval: first argument must be a pp-form structure"); | 36 error ("ppval: first argument must be a pp-form structure"); |
37 endif | 37 endif |
38 | 38 |
39 ## Extract info. | 39 ## Extract info. |
40 [x, P, n, k, d] = unmkpp (pp); | 40 [x, P, n, k, d] = unmkpp (pp); |
52 idx = lookup (x, xi, "lr"); | 52 idx = lookup (x, xi, "lr"); |
53 | 53 |
54 P = reshape (P, [d, n * k]); | 54 P = reshape (P, [d, n * k]); |
55 P = shiftdim (P, nd); | 55 P = shiftdim (P, nd); |
56 P = reshape (P, [n, k, d]); | 56 P = reshape (P, [n, k, d]); |
57 Pidx = P(idx(:), :);#2d matrix size x: coefs*prod(d) y: prod(sxi) | 57 Pidx = P(idx(:), :); # 2D matrix size: x = coefs*prod(d), y = prod(sxi) |
58 | 58 |
59 if (isvector (xi)) | 59 if (isvector (xi)) |
60 Pidx = reshape (Pidx, [xn, k, d]); | 60 Pidx = reshape (Pidx, [xn, k, d]); |
61 Pidx = shiftdim (Pidx, 1); | 61 Pidx = shiftdim (Pidx, 1); |
62 dimvec = [d, xn]; | 62 dimvec = [d, xn]; |
94 yi = reshape (yi, sxi); | 94 yi = reshape (yi, sxi); |
95 elseif (isfield (pp, "orient") && strcmp (pp.orient, "first")) | 95 elseif (isfield (pp, "orient") && strcmp (pp.orient, "first")) |
96 yi = shiftdim (yi, nd); | 96 yi = shiftdim (yi, nd); |
97 endif | 97 endif |
98 | 98 |
99 ## FIXME: Why is this commented out, rather than just removed? | 99 if (d == 1) |
100 #if (d == 1) | 100 yi = reshape (yi, sxi); |
101 # yi = reshape (yi, sxi); | 101 endif |
102 #endif | |
103 | 102 |
104 endfunction | 103 endfunction |
105 | 104 |
106 | 105 |
107 %!shared b, c, pp, pp2, xi, abserr | 106 %!shared b, c, pp, pp2, xi, abserr |
109 %! c = ones (2); | 108 %! c = ones (2); |
110 %! pp = mkpp (b, c); | 109 %! pp = mkpp (b, c); |
111 %! abserr = 1e-14; | 110 %! abserr = 1e-14; |
112 %! pp2 = mkpp (b, [c;c], 2); | 111 %! pp2 = mkpp (b, [c;c], 2); |
113 %! xi = [1.1 1.3 1.9 2.1]; | 112 %! xi = [1.1 1.3 1.9 2.1]; |
113 %! | |
114 %!assert (ppval (pp, 1.1), 1.1, abserr) | 114 %!assert (ppval (pp, 1.1), 1.1, abserr) |
115 %!assert (ppval (pp, 2.1), 1.1, abserr) | 115 %!assert (ppval (pp, 2.1), 1.1, abserr) |
116 %!assert (ppval (pp, xi), [1.1 1.3 1.9 1.1], abserr) | 116 %!assert (ppval (pp, xi), [1.1 1.3 1.9 1.1], abserr) |
117 %!assert (ppval (pp, xi.'), [1.1 1.3 1.9 1.1].', abserr) | 117 %!assert (ppval (pp, xi.'), [1.1 1.3 1.9 1.1].', abserr) |
118 %!assert (ppval (pp2, 1.1), [1.1;1.1], abserr) | 118 %!assert (ppval (pp2, 1.1), [1.1;1.1], abserr) |
119 %!assert (ppval (pp2, 2.1), [1.1;1.1], abserr) | 119 %!assert (ppval (pp2, 2.1), [1.1;1.1], abserr) |
120 %!assert (ppval (pp2, xi), [1.1 1.3 1.9 1.1;1.1 1.3 1.9 1.1], abserr) | 120 %!assert (ppval (pp2, xi), [1.1 1.3 1.9 1.1;1.1 1.3 1.9 1.1], abserr) |
121 %!assert (ppval (pp2, xi'), [1.1 1.3 1.9 1.1;1.1 1.3 1.9 1.1], abserr) | 121 %!assert (ppval (pp2, xi'), [1.1 1.3 1.9 1.1;1.1 1.3 1.9 1.1], abserr) |
122 %!assert (size (ppval (pp2, [xi;xi])), [2 2 4]) | 122 %!assert (size (ppval (pp2, [xi;xi])), [2 2 4]) |
123 %!assert (ppval (mkpp([0 1],1), magic (3)), ones(3,3)) | |
124 %! | |
123 %!test | 125 %!test |
124 %! breaks = [0, 1, 2, 3]; | 126 %! breaks = [0, 1, 2, 3]; |
125 %! coefs = rand (6, 4); | 127 %! coefs = rand (6, 4); |
126 %! pp = mkpp (breaks, coefs, 2); | 128 %! pp = mkpp (breaks, coefs, 2); |
127 %! ret = zeros (2, 4, 2); | 129 %! ret = zeros (2, 4, 2); |
128 %! ret(:,:,1) = ppval (pp, breaks'); | 130 %! ret(:,:,1) = ppval (pp, breaks'); |
129 %! ret(:,:,2) = ppval (pp, breaks'); | 131 %! ret(:,:,2) = ppval (pp, breaks'); |
130 %! assert (ppval (pp, [breaks',breaks']), ret) | 132 %! assert (ppval (pp, [breaks',breaks']), ret) |
131 | 133 |
134 %% Test input validation | |
135 %!error ppval () | |
136 %!error ppval (1) | |
137 %!error ppval (1,2,3) | |
138 %!error <argument must be a pp-form structure> ppval (1,2) | |
139 %!error <argument must be a pp-form structure> ppval (struct ("a", 1), 2) | |
140 %!error <argument must be a pp-form structure> ppval (struct ("form", "ab"), 2) | |
141 |