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 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(): 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 example3(m=3, n=4, lb=-4, ub=5): A = mymat.MyMat.randint(m, n, lb, ub) A[:, 1] = mymat.MyMat([[1], [0], [0]]) B = A.copy() B.row_transform2(1, 2).row_transform3(2, 1, 2).row_transform3(3, 1, -1) template = '${{matrixB}}$经初等变换${{transform}}$变成${{matrixA}}$.' parameter = {'matrixA': A, 'transform': '?,?', 'matrixB': B} return ElementaryTransformProblem(template, parameter)
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(m=3, lb=-3, ub=3): A = mymat.MyMat.randint(m, m, lb, ub) L = A.tril()[:, :m] U = A.itriu() for k in range(m): if L[k, k] == 0: L[k, k] = np.random.randint(lb, ub) A = mymat.MyMat((L * U).tolist(), dtype=np.float64) return InverseMatrixProblem.fromMatrix(A)
def makeup(m=3, n=4, lb=-5, ub=5): A = mymat.MyMat.randint(m, n, lb, ub) L = A.tril()[:, :m] U = A.itriu() for k in range(m): if L[k, k] == 0: L[k, k] = np.random.randint(lb, ub) A = mymat.MyMat((L * U).tolist(), dtype=np.float64).tofrac() return GaussEliminationProblem.fromMatrix(A)
def makeup(m=3, n=3, lb=-5, ub=5): A = mymat.MyMat.randint(m, n, lb, ub) L = A.tril()[:, :m] U = A.itriu() for k in range(m): if L[k, k] == 0: L[k, k] = np.random.randint(lb, ub) A = mymat.MyMat((L * U).tolist(), dtype=np.float64).tofrac() b = mymat.MyMat.randint(3, 1, lb, ub).tofrac() return LinearEquationProblem.fromEquation(mymat.LinearEquation(A, b))
def example(): A1 = mymat.MyMat.randint(1, 3, -5, 6) A2 = mymat.MyMat.randint(2, 1, -5, 6) A = mymat.MyMat(LA.block_diag(A1, A2)) B1 = mymat.MyMat.randint(3, 2, -5, 6) B2 = mymat.MyMat.zeros(1, 2) B = np.vstack((B1, B2)) template = '用分块矩阵的方法计算${{matrixA}}{{matrixB}}$. (用线条注明分块方案)' parameter = {'matrixA': A.tofrac(), 'matrixB': B.tofrac()} return MatrixMultiplicationProblem(template, parameter)
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(): A = mymat.MyMat('-1 & 2 & -1\\-2 & 2 & 2\\-2 & 5 & -3').tofrac() return InverseMatrixProblem.fromMatrix(A)
def example(): A = mymat.MyMat([[3, -3, 3], [-3, 4, 0], [3, -4, -4]]) b = mymat.MyMat([1, -1, 1]) return LinearEquationProblem.fromEquation(mymat.LinearEquation(A, b))
def example(): A = mymat.MyMat( '3, 0, -12, 3; -3, -4, 16, 17; -5, -4, 21, 30').tofrac() return GaussEliminationProblem.fromMatrix(A)