-
Notifications
You must be signed in to change notification settings - Fork 5
/
diffPoly.m
81 lines (71 loc) · 1.74 KB
/
diffPoly.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function Df = diffPoly(f,n,x)
% Df = diffPoly(f,n,x)
% --------------------
%
% Applies the (partial) differential operator on a polynomial f. Graded
% xelicographic ordering is implicitly assumed. Df has the same dimensions
% as f, pruning is not performed.
%
% Df = row vector, contains the coefficients of the polynomial
%
% df
% --
% dx
%
% f = row vector, contains the coefficients of the multivariate
% polynomial f
%
% n = scalar, number of variables
%
% x = scalar, index of the variable to which the differentation needs
% to take place,
%
% d d d
% 1 = ---, 2 = ---, 3 = ---, etc...
% dx1 dx2 dx3
%
%
% CALLS
% -----
%
% vec2polysys.m
%
% deg.m
%
% getMon.m
%
% Kim Batselier, 2010-01-04
% make sure our f is a column vector
f = f(:);
% check function inputs
if x > n
error(['You cannot differentiate with respect to x' num2str(x) ', there are only ' num2str(n) ' variables.'])
end
if issparse(f)
Df = spalloc(length(f),length(f),length(f));
D = spalloc(length(f),length(f),length(f));
else
% initialize our differential operator
Df = zeros(length(f),1);
D = zeros(length(f),length(f));
end
% determine degree of the polynomial
d = deg(f,n);
% we need a monomial base to construct our operator
base = getMon(d,n);
pos = [];
coeff = [];
for i = 1 : d
% for each degree:
% get the positions of 'x' to the ith degree
pos = [pos find(base(:,x)==i)'];
% make a coefficient vector for that degree
coeff = [coeff i*ones(1,length(find(base(:,x)==i)))];
end
[Y I] = sort(pos);
coeff = coeff(I);
for i = 1 : length(Y)
D(i,Y(i)) = coeff(i);
end
Df = (D*f)';
end