def generate_random_matrix(rows, columns, rank): # TODO: change 3911 to q m = randMatrix(rows, columns, min=0, max=Params.FINITE_FIELD) # print("m: {}".format(m)) while m.rank() != rank: m = randMatrix(rows, columns, min=0, max=Params.FINITE_FIELD) return m
def _compare_tensorflow_matrix(variables, expr, use_float=False): f = lambdify(variables, expr, 'tensorflow') if not use_float: random_matrices = [randMatrix(v.rows, v.cols) for v in variables] else: random_matrices = [ randMatrix(v.rows, v.cols) / 100. for v in variables ] graph = tf.Graph() r = None with graph.as_default(): random_variables = [eval(tensorflow_code(i)) for i in random_matrices] session = tf.compat.v1.Session(graph=graph) r = session.run(f(*random_variables)) e = expr.subs({k: v for k, v in zip(variables, random_matrices)}) e = e.doit() if e.is_Matrix: if not isinstance(e, MatrixBase): e = e.as_explicit() e = e.tolist() if not use_float: assert (r == e).all() else: r = [i for row in r for i in row] e = [i for row in e for i in row] assert all( abs(a - b) < 10**-(4 - int(log(abs(a), 10))) for a, b in zip(r, e))
def Det4x4(i): A = randMatrix(4, 4, -4, 4, prng = prng) text = fr''' Calcule o determinante da matriz \[ {latex(A)} .\] ''' answer = int(det(A)) return { 'answer' : answer, 'text' : text}
def Sistema4x4(i): A = randMatrix(4, 4, -5, 5, prng=prng) P, L, D, U = A.LUdecompositionFF() text = fr''' Calcule o determinante da matriz \[ {latex(A)} .\] ''' answer = int(det(A)) return {'answer': answer, 'text': text}
def _compare_tensorflow_matrix_scalar(variables, expr): f = lambdify(variables, expr, 'tensorflow') random_matrices = [ randMatrix(v.rows, v.cols).evalf() / 100 for v in variables] graph = tf.Graph() r = None with graph.as_default(): random_variables = [eval(tensorflow_code(i)) for i in random_matrices] session = tf.compat.v1.Session(graph=graph) r = session.run(f(*random_variables)) e = expr.subs({k: v for k, v in zip(variables, random_matrices)}) e = e.doit() assert abs(r-e) < 10**-6
# Find the induced metric w.r.t. the E1 E2 basis xE = xu.dot(xu) xF = xu.dot(xv) xG = xv.dot(xv) xI = Matrix([[xE, xF], [xF, xG]]) xI_pt = xI.subs(uvpt) # Same as XI_pt!? # Find the shape operator in uv space # Any vector in uv space can be transformed into {E1, E2} space, # multipled by the shape operator, then transformed back into uv space S_pt_uv = (xJ_pt.inv() * S_pt * xJ_pt).evalf() # S is self-adjoint w.r.t. the induced metric XI print 'Verify that S is self-adjoint w.r.t. the induced metrix I' print 'i.e. g(SX,Y) = g(X,SY) for all vectors X,Y' randX = randMatrix(2,1) randY = randMatrix(2,1) gSXY = (S_pt_uv * randX).transpose() * xI_pt * randY gXSY = randX.transpose() * xI_pt * (S_pt_uv * randY) print '{} = g(SX,Y) = g(X,SY) = {}'.format(gSXY, gXSY) # ============ # Plot results # ============ import itertools from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np # Set limits xMin = -2
def generate_Z(alpha, d): Z = randMatrix(alpha, d, 0, 1) while Z.rank() != alpha: Z = randMatrix(alpha, d, 0, 1) return Z
def generate_b_col_vector(alpha): b = randMatrix(alpha, 1, 0, 1) while b.rank() == 0: b = randMatrix(alpha, 1, 0, 1) return b
def generate_b_row_vector(alpha): b = randMatrix(1, alpha, 0, 1) while b.rank() == 0: b = randMatrix(1, alpha, 0, 1) return b