def solve(self, strategy='lagrange'): N = len(self) if strategy == 'lagrange': ind = [k for k in range(N)] t = '' for k in range(N): t += '{{ydata[%d]}}\\frac{' % k tempind = ind.copy() tempind.pop(k) for l in tempind: t += '(x-{{xdata[%d]}})' % l t += '}{' + str( np.prod( [self['xdata'][k] - self['xdata'][l] for l in tempind])) + '}' template = '$L_n(x)=' + t + '$' self.solution = exam.Solution(template, self.parameter) else: import mymath.interpolate interp = mymath.interpolate.Interpolator1D(self['xdata'], self['ydata'], basis='newton') c = interp.getNewtonCoef() t = str(c[0]) + ' + ' t += ' + '.join( str(c[k + 1]) + ''.join('(x-{{xdata[%d]}})' % l for l in range(k + 1)) for k in range(N - 1)) template = '牛顿多项式为$N_n(x)=' + t + '$' print(template) self.solution = exam.Solution(template, self.parameter)
def example(): A = mymat.MyMat('1,6,5;6,37,23;5,23,75') p = CholeskyProblem(A) template = '分解过程为${{process}$.\n\n答案为$L={{L}}$.' cd = la.CholeskyDecompsition(A) p.solution = exam.Solution(template, parameter={'L': 'L'}, solver=cd) return p
def example(strategy='trapezoid'): function = '\sqrt{1-x^2}' func = lambda x: np.sqrt(1 - x**2) lb, ub = 0, 1 if strategy == 'simpson': n = 4 parameter = { 'function': function, 'strategy': 'simpson', 'lb': lb, 'ub': ub, 'n': n } ip = IntegralProblem(parameter=parameter) template = '计算如下:$n={{n}},h={h}$,\n{{process}}\n函数${{function}}$的积分约为${{answer}}$.' parameter = {'function': function, 'n': n, 'h': (ub - lb) / n} else: n = 8 parameter = { 'function': function, 'strategy': 'trapezoid', 'lb': lb, 'ub': ub, 'n': n } ip = IntegralProblem(parameter=parameter) template = '计算如下:$n={{n}},h={h}$,\n{{process}}\n函数${{function}}$的积分约为${{answer}}$.' parameter = {'function': function, 'n': n, 'h': (ub - lb) / n} integral = mymath.numerical.Integration(func, lb, ub, n, strategy) ip.solution = exam.Solution(template, parameter, solver=integral) return ip
def example(): matrix = mymat.MyMat('1,2;3,3') p = ChasingProblem(matrix) template = '分解过程为${{process}}$.' cd = la.ChasingDecompsition(A) p.solution = exam.Solution(template, parameter={}, solver=cd) return p
def random(): matrix = mymat.MyMat.randint(3, 3, lb=-6, ub=6) parameter = {'matrix': mymat.MyMat(matrix)} pmp = PowerMethodProblem(parameter=parameter) template = '计算过程如下:\n{{process}}\n矩阵${{matrix}}$的主特征值约为${{answer}}$.' parameter = {'answer': 'l', 'matrix': 'A'} pm = mymath.eigenvalue.PowerMethod(matrix) pmp.solution = exam.Solution(template, parameter, solver=pm) return pmp
def example(): matrix = np.array([[1, 1, 0.5], [1, 1, 0.25], [0.5, 0.25, 2]]) parameter = {'matrix': mymat.MyMat(matrix)} pmp = PowerMethodProblem(parameter=parameter) template = '计算过程如下:\n{{process}}\n矩阵${{matrix}}$的主特征值约为${{answer}}$.' parameter = {'matrix': 'A', 'answer': ''} pm = mymath.eigenvalue.PowerMethod(matrix) pmp.solution = exam.Solution(template, parameter, solver=pm) return pmp
def makeup(): matrix = mymat.MyMat.randint(3, 3, lb=-10, ub=10) L = matrix.itril() A = L * L.T p = CholeskyProblem(A) template = '分解过程为${{process}}$.\n\n答案为$L={{L}}$.' cd = la.CholeskyDecompsition(A) p.solution = exam.Solution(template, parameter={'L': L}, solver=cd) return p
def random(): A = mymat.MyMat.randint(3, 3, lb=-3, ub=4) for k in range(1, 4): A[k, k] = np.random.randint(10, 20) b = mymat.MyMat.randint(3, 1, lb=-10, ub=10) eq = mymat.LinearEquation(A, b) parameter = {'equation': eq} lp = LinEqIterProblem(parameter=parameter) template = '雅克比迭代公式为:\n\[{{jacobi}}\]\n高斯-赛德尔迭代公式为:\n\[{{gauss}}\]\n系数矩阵是对角占优矩阵, 故迭代收敛.' parameter = {'jacobi': eq.jacobiIter(), 'gauss': eq.jacobiIter()} lp.solution = exam.Solution(template, parameter) return lp
def example(): A = mymat.MyMat('1, 2, 3;2, 5, 8;3, 8, 14').tofrac() p = LUDecompsitionProblem(A) template = '分解过程为${{process}}$.\n\n答案为$L={{L}},U={{U}}$.' lu = la.LUDecompsition(A) p.solution = exam.Solution(template, parameter={ 'L': 'L', 'U': 'U' }, solver=lu) return p
def example(xdata=np.array([-2, -1, 0, 1, 2]), ydata=np.array([0, 0.2, 0.5, 0.8, 1])): basis = [1, lambda x: x] A = np.array([[b(x) if hasattr(b, '__call__') else b for b in basis] for x in xdata]) lsp = LeastSquareProblem(xdata, ydata, basis='1,x,x^3') G = A.T @ A b = A.T @ ydata a = np.linalg.solve(G, b) neq = mymat.MyMat(G) | (mymat.MyMat(b)).T neq = neq.tolineq() template = '法方程为\n\[{{normeq}}\]\n系数为{{coef}}, 拟合函数为${{function}}$.' parameter = {'normeq': neq, 'coef': a, 'function': np.poly1d(a)} lsp.solution = exam.Solution(template, parameter) return lsp
def makeup(): matrix = mymat.MyMat.randint(3, 3, lb=-10, ub=10) L = matrix.itril() U = matrix.triu() for k in range(3): if U[k, k] == 0: U[k, k] = np.random.randint(1, 10) A = (L * U).tofrac() p = LUDecompsitionProblem(A) template = '分解过程为${{process}}$.\n\n答案为$L={{L}},U={{U}}$.' lu = la.LUDecompsition(A) p.solution = exam.Solution(template, parameter={ 'L': L, 'U': U }, solver=lu) return p
def example(c=np.array([1, 0, 0, 0, 0]), a=-1, b=1): poly = np.poly1d(c) G = np.array([[b - a, (b**2 - a**2) / 2, (b**3 - a**3) / 3], [(b**2 - a**2) / 2, (b**3 - a**3) / 3, (b**4 - a**4) / 4], [(b**3 - a**3) / 3, (b**4 - a**4) / 4, (b**5 - a**5) / 5]]) ap = ApproximationProblem(poly) eq = mymat.LinearEquation( G, np.array([(b**5 - a**5) / 5, (b**6 - a**6) / 6, (b**7 - a**7) / 7])) d = eq.solve() template = '法方程为\[{{normeq}}\]系数为{{coef}}, 最佳逼近多项式为${{poly}}$.' parameter = {'normeq': eq, 'coef': d, 'poly': np.poly1d(d)} ap.solution = exam.Solution(template, parameter) return ap
def example(strategy='牛顿迭代法'): if strategy == '二分法': function = 'sin x -\\frac{1}{2}' func = lambda x: np.sin(x) - 1 / 2 lb, ub, tol = 0, 1, 0.01 template = '用{{strategy}}计算方程${{function}}=0$在$[{{lb}}, {{ub}}]$上的解. (至少迭代5次或误差小于{{tol}})' parameter = { 'function': function, 'strategy': '二分法', 'lb': lb, 'ub': ub, 'tol': tol } ep = EquationProblem(template, parameter) template = '过程如下:\n{{process}}\n方程${function}=0$的解约为${{answer}}$.' parameter = {'function': function} b = mymath.equation.Bisection(func, lb, ub, tol) else: func = lambda x: x * np.tan(x) - 1 dfunc = lambda x: np.tan(x) + x / np.cos(x)**2 tol = 0.0001 x0 = 1 equation = 'x\\tan x=1' template = '用{{strategy}}计算方程${{equation}}$在${{x0}}$附近的解. (至少迭代5次或误差小于{{tol}})' parameter = { 'equation': equation, 'strategy': strategy, 'x0': x0, 'tol': tol } ep = EquationProblem(template, parameter) template = '不动点迭代格式是${{iteration}}$, 过程如下:\n{{process}}\n方程${{equation}}$的解约为${{answer}}$.' parameter = {'iteration': 'x-f(x)/f\'(x)', 'equation': equation} b = mymath.equation.NewtonIter(func, dfunc, x0, tol) ep.solution = exam.Solution(template, parameter, solver=b) return ep