classdef Function < sympy.Expr

    properties(Access=private)
        vars
        rhs
    end

    methods(Access=public)
        function o = Function(f, vars, expr)
            o@sympy.Expr(f);
            if nargin > 1; o.vars = vars; end;
            if nargin > 2; o.rhs = expr; end
        end

        function obj = getObj(o)
            if isempty(o.rhs)
                obj = getObj@sympy.Base(o);
            else
                obj = o.rhs;
            end
        end

        function o = subsref(f, s)
            if s.type ~= '()'; error("符号函数只能使用圆括号操作符"); end
            subs = s.subs; n = length(subs);
            if n == length(f.vars)
                varsl = pylist;
                for i = 1:n
                    subsi = sympy.to_sp_object(subs{i});
                    varsl.append(pytuple(f.vars{i}, subsi));
                end
            else
                error(fprintf("函数期望输入%d个参数，单数输入了%d个参数", length(f.vars), length(s.subs)));
            end
            obj = getObj(f);
            o = sympy.Expr(obj.subs(varsl));
        end

        function f = subsasgn(f, s, b)
            if ~isa(b, 'sympy.Base'); error("需要输入符号表达式，常数也不允许"); end
            f.rhs = b;
        end

        function disp(o)
            obj = o.obj;
            if isempty(o.rhs); disp(obj.__str__());
            else
                f = obj.__str__();
                expr = o.rhs.__str__();
                expr = strrep(expr, '**', '^');
                fprintf("%s = \n%s\n", f, expr);
            end
        end
    end

end