class DocPolynom
% file: @DocPolynom/DocPolynom.m
% Public properties
    properties
        coef@double = [];      

% Class methods
    methods
        function obj = DocPolynom(c)
        % Construct a DocPolynom object using the coefficients supplied
            construct obj
            if isa(c,'DocPolynom')
                obj.coef = c.coef;
            else
                obj.coef = c(:).';
            end
        end % DocPolynom
    
        function c = double(obj)
            c = obj.coef;
        end % double
        
        function s = char(obj)
        % Created a formated display of the polynom
        % as powers of x
	    if all(obj.coef == 0)
            s = '0';
        else
            d = length(obj.coef)-1;
            s = [];
		for a = obj.coef;
		    if a ~= 0;
			if ~isempty(s)
			    if a > 0
                    s = [s ' + '];
                else
                    s = [s ' - '];
                    a = -a;
			    end
			end
			if a ~= 1 | d == 0
			    s = [s num2str(a)];
			    if d > 0
                    s = [s '*'];
			    end
			end
			if d >= 2
			    s = [s 'x^' int2str(d)];
			elseif d == 1
			    s = [s 'x'];
			end
            end
                d = d - 1;
		end
	    end
        end % char
        
        function disp(obj)
            % DISPLAY 
            disp(['     ' char(obj)])
	    end % disp
        
        function b = subsref(a,s)
	% SUBSREF Implement obj([1 ...])
	    switch s.type
		case '()'
		    ind = s.subs{:};
		    for k = 1:length(ind)
			b(k) = eval(strrep(char(a),'x',num2str(ind(k))));
		    end
         	case '.'
            switch s.subs
                 case 'coef'
                   b = a.coef;
                 otherwise
                   error(['''' s.subs '''' ' is not a DocPolynom property'])
            end
		otherwise
		    error('Specify value for x as obj(x)')
	    end
	end % subsref

        function r = plus(obj1,obj2)
        % PLUS  Implement obj1 + obj2 for DocPolynom
            obj1 = DocPolynom(obj1);
            obj2 = DocPolynom(obj2);
            k = length(obj2.coef) - length(obj1.coef);
            r = DocPolynom([zeros(1,k) obj1.coef] + [zeros(1,-k) obj2.coef]);
        end % plus
        
        function r = minus(obj1,obj2)
        % MINUS Implement obj1 - obj2 for DocPolynoms.
            obj1 = DocPolynom(obj1);
            obj2 = DocPolynom(obj2);
            k = length(obj2.coef) - length(obj1.coef);
            r = DocPolynom([zeros(1,k) obj1.coef] - [zeros(1,-k) obj2.coef]);
        end % minus
        
        function r = mtimes(obj1,obj2)
	    % MTIMES   Implement obj1 * obj2 for DocPolynoms.
            obj1 = DocPolynom(obj1);
            obj2 = DocPolynom(obj2);
	        r = DocPolynom(conv(obj1.coef,obj2.coef));
        end % mtimes
        
        function r = roots(obj)
        % ROOTS.  ROOTS(obj) is a vector containing the roots of obj.
	    r = roots(obj.coef);
        end % roots
        
        function y = polyval(obj,x)
        % POLYVAL  POLYVAL(obj,x) evaluates obj at the points x.
            y = 0;
            for a = obj.coef
                y = y.*x + a;
            end
        end % polyval
            
        function q = diff(obj)
        % DIFF  DIFF(obj) is the derivative of the polynom obj.
            c = obj.coef;
            d = length(c) - 1;  % degree
            q = DocPolynom(obj.coef(1:d).*(d:-1:1));
        end % diff
        
          function plot(obj)
        % PLOT  PLOT(obj) plots the polynom obj
            r = max(abs(roots(obj)));
            x = (-1.1:0.01:1.1)*r;
            y = polyval(obj,x);
            plot(x,y);
            title(['y = ' char(obj)])
            xlabel('X')
            ylabel('Y','Rotation',0)
            grid on
        end % plot
        
        
end % class
