This is a static copy of a profile report

Home

spcol (404 calls, 0.501 sec)
Generated 05-Nov-2014 07:53:25 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/curvefit/splines/spcol.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
spap2>spap21subfunction404
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
130
[augknot,addl] = augknt(knots,...
4040.120 s24.0%
137
savl = max(sorted(augknot(1:na...
4040.100 s20.0%
154
term = b(lpt1s,r)./(tr+tl);
18180.040 s8.0%
153
tl = pt1s-augknot(savls+r-j);
18180.040 s8.0%
155
b(lpt1s,r) = saved+tr.*term;
18180.020 s4.0%
All other lines  0.180 s36.0%
Totals  0.501 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
augkntfunction4040.120 s24.0%
sortedfunction4040.070 s14.0%
Self time (built-ins, overhead, etc.)  0.310 s62.0%
Totals  0.501 s100% 
Code Analyzer results
Line numberMessage
74To improve performance, replace ISEMPTY(FIND(X)) with ISEMPTY(FIND( X, 1 )).
77To improve performance, replace ISEMPTY(FIND(X)) with ISEMPTY(FIND( X, 1 )).
168Use of brackets [] is unnecessary. Use parentheses to group, if needed.
202Use of brackets [] is unnecessary. Use parentheses to group, if needed.
258The value assigned to variable 'temp' might be unused.
258Use of brackets [] is unnecessary. Use parentheses to group, if needed.
259Use of brackets [] is unnecessary. Use parentheses to group, if needed.
259Use of brackets [] is unnecessary. Use parentheses to group, if needed.
260Use of brackets [] is unnecessary. Use parentheses to group, if needed.
260Use of brackets [] is unnecessary. Use parentheses to group, if needed.
268For better readability, use newline, semicolon, or comma before this statement.
285Use of brackets [] is unnecessary. Use parentheses to group, if needed.
286Use of brackets [] is unnecessary. Use parentheses to group, if needed.
289Use of brackets [] is unnecessary. Use parentheses to group, if needed.
290Use of brackets [] is unnecessary. Use parentheses to group, if needed.
Coverage results
[ Show coverage for parent directory ]
Total lines in function291
Non-code lines (comments, blank lines)148
Code lines (lines that can run)143
Code lines that did run53
Code lines that did not run90
Coverage (did run/can run)37.06 %
Function listing
   time   calls  line
1 function colloc = spcol(knots,k,tau,varargin)
2 %SPCOL B-spline collocation matrix.
3 %
4 % COLLOC = SPCOL(KNOTS,K,TAU) is the matrix
5 %
6 % [ D^m(i)B_j(TAU(i)) : i=1:length(TAU), j=1:length(KNOTS)-K ] ,
7 %
8 % with D^m(i)B_j the m(i)-fold derivative of B_j,
9 % B_j the j-th B-spline of order K for the knot sequence KNOTS,
10 % TAU a sequence of sites,
11 % both KNOTS and TAU are assumed to be nondecreasing, and
12 % m(i) is the integer #{ j<i : TAU(j) = TAU(i) }, i.e., the 'cumulative'
13 % multiplicity of TAU(i) in TAU.
14 %
15 % This means that the j-th column of COLLOC contains values and, perhaps,
16 % derivatives, at all the entries of the vector TAU, of the j-th
17 % B-spline of order K for the sequence KNOTS, i.e., the B-spline
18 % with knots KNOTS(j:j+K).
19 % The i-th row of COLLOC contains the value at TAU(i) of the m(i)-th
20 % derivative of all these B-splines, with m(i) the number of earlier
21 % entries of TAU that equal TAU(i) .
22 %
23 % Example:
24 % tau = [0,0,0,1,1,2]; % therefore, m equals [0,1,2,0,1,0]
25 % k = 3; knots = augknt(0:2,k); % therefore, knots equals [0,0,0,1,2,2,2]
26 % colloc = spcol(knots,k,tau)
27 %
28 % has the 6 entries of COLLOC(:,j) contain the value, first, and second
29 % derivative of B_j at 0, then the value and first derivative of B_j
30 % at 1, and, finally, the value of B_j at 2, with B_j the j-th B-spline of
31 % order k for the knot sequence knots; e.g., B_2 is the B-spline with
32 % knots 0,0,1,2.
33 %
34 % You can use COLLOC to construct splines with prescribed values and,
35 % perhaps, also some derivatives, at prescribed sites.
36 %
37 % Example:
38 % a = -pi; b = pi; tau = [a a a 0 b b]; k = 5;
39 % knots = augknt([a,0,b],k);
40 % sp = spmak(knots, ( spcol(knots,k,tau) \ ...
41 % [sin(a);cos(a);-sin(a);sin(0);sin(b);cos(b)] ).' )
42 %
43 % provides the quartic spline, on the interval [a,b] with just one interior
44 % knot, at 0, that interpolates the sine function at a,0,b, but also matches
45 % its first and second derivative at a , and its first derivative at b .
46 %
47 % COLLOC = SPCOL(KNOTS,K,TAU,ARG1,ARG2,...) provides the same or a related
48 % matrix, depending on the optional arguments ARG1, ARG2, ... .
49 %
50 % If one of the optional arguments is 'slvblk', then COLLOC is in the al-
51 % most block-diagonal format (specialized for splines) required by SLVBLK.
52 %
53 % If one of the optional arguments is 'sparse', then COLLOC is a sparse
54 % matrix.
55 %
56 % If one of the optional arguments is 'noderiv', then multiplicities are
57 % ignored, i.e., m(i) = 0 for all i.
58 %
59 % The B-spline recurrence relations are used to generate the entries of the
60 % matrix.
61 %
62 % Example:
63 % t = [0,1,1,3,4,6,6,6]; x = linspace(t(1),t(end),101);
64 % c = spcol(t,3,x); plot(x,c)
65 %
66 % uses SPCOL to generate, in c(:,j), a fine sequence of values of the
67 % j-th quadratic B-spline for the given knot sequence t.
68 %
69 % See also SLVBLK, SPARSE, SPAPI, SPAP2, BSPLINE.
70
71 % Copyright 1987-2010 The MathWorks, Inc.
72 % $Revision: 1.1.6.4 $
73
404 74 if ~isempty(find(diff(knots)<0))
75 error(message('SPLINES:SPCOL:knotsdecreasing'))
76 end
404 77 if ~isempty(find(diff(tau)<0))
78 error(message('SPLINES:SPCOL:TAUdecreasing'))
79 end
80
81 % Compute the number n of B-splines of order K supported by the given
82 % knot sequence and return an empty matrix in case there aren't any.
83
404 84 npk=length(knots); n=npk-k;
404 85 if n<1, warning(message('SPLINES:SPCOL:noBsplines'))
86 colloc = zeros(length(tau),0); return
87 end
88
89 % Settle the options:
404 90 slvblk=0; noderiv=0;
404 91 for j=4:nargin
808 92 argj = varargin{j-3};
0.02 808 93 if ~isempty(argj)
808 94 if ischar(argj)
0.01 808 95 if argj(1)=='s', slvblk=1;
404 96 if length(argj)>1, if argj(2)=='p', slvblk=2; end, end
404 97 elseif argj(1)=='n', noderiv=1;
98 else error(message('SPLINES:SPCOL:wronginarg2'))
99 end
100 else
101 switch j % for backward compatibility
102 case 4, slvblk=1;
103 case 5, noderiv=1;
104 end
105 end
808 106 end
808 107 end
108
109 % If NODERIV==0, remove all multiplicities from TAU and generate repetitions
110 % of rows instead of rows containing values of successive derivatives.
404 111 nrows = length(tau); tau = reshape(tau,1,nrows);
404 112 if noderiv
404 113 index = 1:nrows; m = ones(1,nrows); nd = 1; pts = tau;
114 else
115 index = [1 find(diff(tau)>0)+1];
116 m = diff([index nrows+1]); nd = max(m);
117 if nd>k
118 error(message('SPLINES:SPCOL:multtoohigh', sprintf( '%g', k )));
119 end
120 pts = tau(index);
121 end
122
123 %set some abbreviations
0.01 404 124 km1 = k-1;
125
126 % augment knot sequence to provide a K-fold knot at each end, in order to avoid
127 % struggles near ends of basic interval, [KNOTS(1) .. KNOTS(npk)] .
128 % The resulting additional B-splines, if any, will NOT appear in the output.
129
0.12 404 130 [augknot,addl] = augknt(knots,k); naug = length(augknot)-k;
404 131 pts = pts(:); augknot = augknot(:);
132
133 % For each i , determine savl(i) so that K <= savl(i) < naug+1 , and,
134 % within that restriction,
135 % augknot(savl(i)) <= pts(i) < augknot(savl(i)+1) .
136
0.10 404 137 savl = max(sorted(augknot(1:naug),pts), k);
138
0.01 404 139 b = zeros(nrows,k);
140
141 % first do those without derivatives
404 142 index1 = find(m==1);
0.01 404 143 if ~isempty(index1)
404 144 pt1s = pts(index1); savls = savl(index1); lpt1 = length(index1);
145 % initialize the b array.
404 146 lpt1s = index(index1); b(lpt1s,1) = ones(lpt1,1);
147
148 % run the recurrence simultaneously for all pt1(i) .
0.01 404 149 for j=1:km1
0.01 1010 150 saved = zeros(lpt1,1);
1010 151 for r=1:j
0.02 1818 152 tr = augknot(savls+r)-pt1s;
0.04 1818 153 tl = pt1s-augknot(savls+r-j);
0.04 1818 154 term = b(lpt1s,r)./(tr+tl);
0.02 1818 155 b(lpt1s,r) = saved+tr.*term;
0.01 1818 156 saved = tl.*term;
0.01 1818 157 end
1010 158 b(lpt1s,j+1) = saved;
0.01 1010 159 end
404 160 end
161
162 % then do those with derivatives, if any:
404 163 if nd>1
164 indexm=find(m>1);ptss=pts(indexm);savls=savl(indexm);lpts=length(indexm);
165 % initialize the bb array.
166 %temp = [1 zeros(1,km1)]; bb = temp(ones(nd*lpts,1),:);
167 bb = repmat([1 zeros(1,km1)],nd*lpts,1);
168 lptss = nd*[1:lpts];
169
170 % run the recurrence simultaneously for all pts(i) .
171 % First, bring it up to the intended level:
172 for j=1:k-nd
173 saved = zeros(lpts,1);
174 for r=1:j
175 tr = augknot(savls+r)-ptss;
176 tl = ptss-augknot(savls+r-j);
177 term = bb(lptss,r)./(tr+tl);
178 bb(lptss,r) = saved+tr.*term;
179 saved = tl.*term;
180 end
181 bb(lptss,j+1) = saved;
182 end
183
184 % save the B-spline values in successive blocks in bb .
185
186 for jj=1:nd-1
187 j = k-nd+jj; saved = zeros(lpts,1); lptsn = lptss-1;
188 for r=1:j
189 tr = augknot(savls+r)-ptss;
190 tl = ptss-augknot(savls+r-j);
191 term = bb(lptss,r)./(tr+tl);
192 bb(lptsn,r) = saved+tr.*term;
193 saved = tl.*term;
194 end
195 bb(lptsn,j+1) = saved; lptss = lptsn;
196 end
197
198 % now use the fact that derivative values can be obtained by differencing:
199
200 for jj=nd-1:-1:1
201 j = k-jj;
202 temp = repmat([jj:nd-1].',1,lpts)+repmat(lptsn,nd-jj,1); lptss=temp(:);
203 for r=j:-1:1
204 temp = repmat((augknot(savls+r)-augknot(savls+r-j)).'/j,nd-jj,1);
205 bb(lptss,r) = -bb(lptss,r)./temp(:);
206 bb(lptss,r+1) = bb(lptss,r+1) - bb(lptss,r);
207 end
208 end
209
210 % finally, combine appropriately with b by interspersing the multiple
211 % point conditions appropriately:
212 dtau = diff([tau(1)-1 tau(:).' tau(nrows)+1]);
213 index=find(min(dtau(2:nrows+1),dtau(1:nrows))==0); % Determines all rows
214 % involving multiple tau.
215 dtau=diff(tau(index));index2=find(dtau>0)+1; % We need to make sure to
216 index3=[1 (dtau==0)]; % skip unwanted derivs:
217 if ~isempty(index2)
218 index3(index2)=1+nd-m(indexm(1:length(indexm)-1));end
219 b(index,:)=bb(cumsum(index3),:);
220
221 % ... and appropriately enlarge savl
222 index = cumsum([1 (diff(tau)>0)]);
223 savl = savl(index);
224 end
225
226 % Finally, zero out all rows of b corresponding to TAU outside the basic
227 % interval, [knots(1) .. knots(npk)] .
228
404 229 index = find(tau<knots(1)|tau>knots(npk));
404 230 if ~isempty(index)
231 b(index,:) = 0;
232 end
233
234 % The first B-spline of interest begins at KNOTS(1), i.e., at augknot(1+addl)
235 % (since augknot's first knot has exact multiplicity K). If addl<0 ,
236 % this refers to a nonexistent index and means that the first -addl columns
237 % of the collocation matrix should be trivial. This we manage by setting
404 238 savl = savl+max(0,-addl);
239
0.01 404 240 if slvblk % return the collocation matrix in almost block diagonal form.
241 % For this, make the blocks out of the entries with the same
242 % SAVL(i) , with LAST computed from the differences.
243 % There are two issues, the change in the B-splines considered because of
244 % the use of AUGKNOT instead of KNOTS , and the possible drop of B-splines
245 % because the extreme TAU fail to involve the extreme knot intervals.
246
247 % SAVL(j) is the index in AUGKNOT of the left knot for TAU(j) , hence the
248 % corresponding row involves B-splines to index savl(j) wrto augknot, i.e.,
249 % B-splines to index savl(j)-addl wrto KNOTS.
250 % Those with negative index are removed by cutting out their columns (i.e.,
251 % shifting appropriately the blocks in which they lie). Those with index
252 % greater than n will be ignored because of last .
253
404 254 last0 = max(0,savl(1)-max(0,addl)-k); % number of cols in trivial first block
404 255 if addl>0 % if B-splines were added on the left, remove them now:
256 width = km1+k;cc = zeros(nrows*width,1);
257 index = min(k,savl-addl);
258 temp = +repmat(nrows*[0:km1],nrows,1);
259 cc(repmat(([1-nrows:0]+nrows*index).',1,k)+repmat(nrows*[0:km1],nrows,1))=b;
260 b(:)=cc(repmat([1-nrows:0].',1,k)+repmat(nrows*(k+[0:km1]),nrows,1));
261 savl=savl+k-index;
262 end
404 263 ds=(diff(savl));
404 264 index=[0 find(ds>0) nrows];
404 265 rows=diff(index);
404 266 nb=length(index)-1;
0.01 404 267 last=ds(index(2:nb));
404 268 if addl<0 nb=nb+1; rows=[0 rows]; last=[last0 last]; end
404 269 if slvblk==1
404 270 colloc=[41 nb rows k last n-sum(last) b(:).'];
271 else % return the equivalent sparse matrix (cf BKBRK)
272 nr = (1:nrows).'; nc = 1:k; nrnc = nrows*k;
273 ncc = zeros(1,nrows); ncc(1+cumsum(rows(1:(nb-1)))) = last;
274 ncc(1) = last0; ncc = reshape(cumsum(ncc),nrows,1);
275 ijs = [reshape(repmat(nr,1,k),nrnc,1), ...
276 reshape(repmat(ncc,1,k)+repmat(nc,nrows,1), nrnc,1), ...
277 reshape(b,nrnc,1)];
278 index = find(ijs(:,2)>n);
279 if ~isempty(index), ijs(index,:) = []; end
280 colloc = sparse(ijs(:,1),ijs(:,2),ijs(:,3),nrows,n);
281 end
282 else % return the collocation matrix in standard matrix form
283 width = max([n,naug])+km1+km1;
284 cc = zeros(nrows*width,1);
285 cc(repmat([1-nrows:0].',1,k)+ ...
286 repmat(nrows*savl.',1,k)+repmat(nrows*[-km1:0],nrows,1))=b;
287 % (This uses the fact that, for a column vector v and a matrix A ,
288 % v(A)(i,j)=v(A(i,j)), all i,j.)
289 colloc = reshape(cc(repmat([1-nrows:0].',1,n) + ...
290 repmat(nrows*(max(0,addl)+[1:n]),nrows,1)), nrows,n);
291 end