def test_transpose_is_self_inverse(expr): expr = simplify(expr) assert transpose(transpose(expr)) == expr
def generate_equation(n_ops): expr_size = operand_sizes() out = ae.Matrix("out", expr_size) expr = simplify(generate_expression(n_ops, expr_size)) return aeq.Equations(ae.Equal(out, expr))
def test_transpose_and_invert_same_as_invert_transpose(expr): expr = simplify(expr) assert transpose(invert(expr)) == invert_transpose(expr)
def test_transpose_and_invert_order_does_not_matter(expr): expr = simplify(expr) assert invert(transpose(expr)) == transpose(invert(expr))
def test_invert_is_self_inverse(expr): expr = simplify(expr) assert invert(invert(expr)) == expr
def matrix_chain_generator(): while True: # for _ in range(20): # expression_strategy.example() # generate_matrix_chain() length = random.randrange(4, 10) sizes = [] if 0.95 <= random.random(): sizes.append(1) else: sizes.append(matrix_size()) for i in range(length): rand = random.random() if 0.6 <= rand < 0.95: # square sizes.append(sizes[i]) elif 0.95 <= rand: # vector sizes.append(1) else: # non-square sizes.append(matrix_size()) # print(sizes) operands = [] restart = False for rows, columns in window(sizes): if rows == columns: if rows == 1: restart = True break # operands.append(None) # error op = random.choices([ae.Identity, ae.Transpose, ae.Inverse, ae.InverseTranspose], weights=[2, 1, 1, 1])[0] operands.append(op(generate_operand(rows, columns))) elif rows == 1: operands.append(ae.Transpose(generate_operand(columns, rows))) elif columns == 1: operands.append(generate_operand(rows, columns)) else: op = random.choices([ae.Identity, ae.Transpose], weights=[3, 1])[0] if op == ae.Transpose: operands.append(op(generate_operand(columns, rows))) else: operands.append(op(generate_operand(rows, columns))) if restart: continue # print(operands) expr = ae.Times(*operands) expr = simplify(expr) # print(expr) # print(expr.size) # print((sizes[0], sizes[-1])) if expr.has_property(properties.MATRIX): lhs = ae.Matrix('X'.format(_n), size=(sizes[0], sizes[-1])) elif expr.has_property(properties.VECTOR): lhs = ae.Vector('x'.format(_n), size=(sizes[0], sizes[-1])) yield Equations(ae.Equal(lhs, expr))
def test_simplify(input, expected_output): output = simplify(input) assert output == expected_output, "{} did not simplify to {}, but instead to {}.".format( input, expected_output, output)