-
Notifications
You must be signed in to change notification settings - Fork 5
/
vec2poly.m
67 lines (56 loc) · 1.47 KB
/
vec2poly.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
function poly = vec2poly(vec,n,varargin)
% poly = vec2poly(vec,n,maple)
% ----------------------------
% Converts a row vector into a string representing the polynomial.
%
% poly = cell, each row contains the polynomial corresponding with
% the rows of vec
%
% vec = matrix, each row is interpreted as a coefficient vector of
% a polynomial
%
% n = scalar, number of indeterminates
%
% maplestr = boolean, if set to 1 will output a string which can be used
% in maple, default=0.
%
% CALLS
% -----
%
%
% Kim Batselier, 2011-08-01
if nargin == 2
maplestr = 0;
else
maplestr = varargin{1};
end
[p q] = size(vec);
poly = cell(p,1);
for i = 1 : p
coefI = find(vec(i,:));
ncoef = length(coefI);
for j = 1 : ncoef
if vec(i,coefI(j)) < 0
signstr = ' ';
else
signstr = ' + ';
end
if ~maplestr
poly{i,1} = [poly{i,1} signstr num2str(vec(i,coefI(j))) ' ' exp2str(frte(n,coefI(j))) ];
else
poly{i,1} = [poly{i,1} signstr num2str(vec(i,coefI(j))) '*' exp2str(frte(n,coefI(j))) ];
end
end
end
function str = exp2str(exp)
str = [];
for z = 1 : length(exp)
if exp(z) == 0
elseif exp(z) == 1
str = [str ' x_' num2str(z)];
else
str = [str ' x_' num2str(z) '^' num2str(exp(z)) ];
end
end
end
end