def testEqualWithVariableDifferentValueNegative(self): variable1 = ConstantBuilder().value(3.0).build() variable2 = Mock() variable2.value = 3.1 variable2.dispatch = Mock( side_effect=lambda x: x.visit_variable(variable2)) self.assertFalse(variable1.equal(variable2)) self.assertNotEqual(variable1, variable2)
def testGivenTwoExpressionsWithDifferentConstantTermsThatSumTheSameValueWhenEqualThenReturnsFalse( self): constant = ConstantBuilder().build() double_constant = constant.clon() double_constant.multiply(2.0) self.assertFalse( ExpressionBuilder().term(double_constant).build().equal( ExpressionBuilder().term(constant).term(constant).build()))
def testGivenExpressionWithTwoConstantsWithInverseValuesWhenSimplifyThenExpressionWithConstantWithValue0IsObtained( self): constant1 = ConstantBuilder().build() constant2 = constant1.clon() constant2.multiply(-1.0) expression = ExpressionBuilder().term(constant1).term( constant2).build() expression.simplify_constant() self.assertEqual(expression.get_value_constant(), 0.0)
def testGivenXIs0EquationWhenAddOIsYEquationThenYIsXEquationIsObtained( self): equation = EquationBuilder.x_equals_0() equation.add_equation(EquationBuilder.zero_equals_y()) expected_equation = EquationBuilder().left_term( VariableBuilder().name('x').value(1.0).build()).left_term( ConstantBuilder().value(0).build()).right_term( ConstantBuilder().value(0).build()).right_term( VariableBuilder().name('y').value( 1.0).build()).build() self.assertTrue(equation.equal(expected_equation))
def testGivenExpressionWithConstantAndVariableWhenMultiplyThenGetValuesReturnsMultipliedTermsValues( self): constant = ConstantBuilder().build() variable = VariableBuilder().build() expression = ExpressionBuilder().term(constant).term(variable).build() multiply_value = 2.0 expression.multiply(multiply_value) constant.multiply(multiply_value) variable.multiply(multiply_value) self.assertEqual(expression.get_value_constant(), constant.value) self.assertEqual(expression.get_value_variable(variable.name), variable.value)
def testGivenExpressionWithTwoConstantsWhenSimplifyThenExpressionWithOneConstantWithCorrectValueIsObtained( self): constant1 = ConstantBuilder().build() expression = ExpressionBuilder().term(constant1).term( constant1).build() expression.simplify_constant() self.assertTrue(expression.get_value_constant(), 2 * constant1.value)
def testGivenEquationSameConstantTwiceAtRightWhenSimplifyThenConstantIsSimplified( self): term = ConstantBuilder().build() equation = EquationBuilder().right_term(term).right_term(term).build() equation.simplify_constant(Side.right) self.assertEqual(equation.get_value_constant(Side.right), 2 * term.value)
def testGivenEquationWithSameConstantTwiceAtRightSidesWhenGetValueThenRaisesNotSimplified( self): constant = ConstantBuilder().build() equation = EquationBuilder().right_term(constant).right_term( constant).build() self.assertRaises(NotSimplified, equation.get_value_constant, Side.right)
def testGivenWhenThen0(self): eqs = EquationSystemBuilder().build() eqs.add(EquationBuilder.y_equals_x()) a = EquationBuilder.one_equals_x() b = EquationBuilder().left_term( VariableBuilder().name('z').build()).right_term( VariableBuilder().name('x').build()).right_term( VariableBuilder().name('y').build()).build() a.add_equation(EquationBuilder.one_equals_y()) eqs.add(a) eqs.add(b) reduction_method = ReductionMethod() equation_system_solver = EquationSystemSolver(eqs, reduction_method) equation_system_solver.resolve() self.assertTrue( equation_system_solver.get_solution('x').equal( EquationBuilder.x_equals_1())) self.assertTrue( equation_system_solver.get_solution('y').equal( EquationBuilder.y_equals_1())) self.assertTrue( equation_system_solver.get_solution('z').equal( EquationBuilder().left_term( VariableBuilder().name('z').build()).right_term( ConstantBuilder().value(2).build()).build()))
def simplify_constant(self): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_constants() if 0 == len(terms): return self._remove_terms(terms) self.add_term(ConstantBuilder().value( self._get_added_terms_value(terms)).build())
def apply(self, name, value): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_variables_with_name(name) if len(terms) == 0: raise LookupError self._remove_terms(terms) for term in terms: term.multiply(value) self.add_term(ConstantBuilder().value(term.value).build())
def multiply(self, value): for term in self._term_list: term.multiply(value) if 0 == value: terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_variables() self._remove_terms(terms) if 0 == len(self._term_list): self._term_list.append(ConstantBuilder().value(0).build())
def simplify_variable(self, name): terms_counter_analyzer = TermsCounterAnalyzer(self._term_list) terms = terms_counter_analyzer.get_variables_with_name(name) if 0 == len(terms): return self._remove_terms(terms) variable_value = self._get_added_terms_value(terms) if 0 == variable_value: self.add_term(ConstantBuilder().value(variable_value).build()) else: self.add_term(Variable(name, variable_value))
def testGivenExpressionWithOneVariableWhenAddExpressionWithOtherVariableAndConstantThenExpressionContainsAllTerms( self): variable1 = VariableBuilder().name('x').build() variable2 = VariableBuilder().name('y').build() constant = ConstantBuilder().value(1.0).build() expression1 = ExpressionBuilder().term(variable1).build() expression2 = ExpressionBuilder().term(variable2).term( constant).build() expression1.add_expression(expression2) self.assertEqual(expression1.get_name_set(), {variable1.name, variable2.name}) self.assertEqual(expression1.get_value_constant(), constant.value)
def testEqualWithConstantNegative(self): variable1 = ConstantBuilder().value(3.0).build() variable2 = ConstantBuilder().value(3.1).build() self.assertFalse(variable1.equal(variable2)) self.assertNotEqual(variable1, variable2)
def testHasNameNegative(self): variable1 = ConstantBuilder().build() self.assertFalse(variable1.has_name('x'))
def testEqualWithConstantPositive(self): variable1 = ConstantBuilder().value(3.0).build() variable2 = ConstantBuilder().value(3.0).build() self.assertTrue(variable1.equal(variable2)) self.assertEqual(variable1, variable2)
def testGivenEquationWithOneConstantAtRightWhenGetValueThenConstantValueIsReturned( self): constant = ConstantBuilder().build() equation = EquationBuilder().right_term(constant).build() self.assertEqual(equation.get_value_constant(Side.right), constant.value)
def _init_term(self): self._term = ConstantBuilder().value( -self._equation.get_value_constant(self._remove_from)).build()
def testValue(self): variable = ConstantBuilder().value(3.0).build() self.assertEqual(variable.value, 3.0)
def testDispatcher(self, TermVisitor): variable1 = ConstantBuilder().build() term_visitor = TermVisitor() variable1.dispatch(term_visitor) self.assertEqual(term_visitor.visit_constant.call_count, 1)
def testHasNameSetNegative(self): variable1 = ConstantBuilder().build() name_set = ['z', 'y'] self.assertFalse(variable1.has_name_set(name_set))
def testGivenExpressionWithConstantWhenAddZeroThenNoConstantIsAdded(self): expression = ExpressionBuilder().default_constant().build() expression_original = expression.clon() zero = ConstantBuilder().value(0).build() expression.add_term(zero) self.assertEqual(str(expression), str(expression_original))
def testGivenExpressionWithOnlyAConstantAsTermWhenGetValueThenConstantValueIsReturned( self): constant = ConstantBuilder().build() expression = ExpressionBuilder().build() expression.add_term(constant) self.assertEqual(expression.get_value_constant(), constant.value)
def testMultiply(self): variable = ConstantBuilder().value(3.0).build() variable.multiply(2.0) self.assertEqual(variable.value, 6.0)
def testClon(self): variable1 = ConstantBuilder().build() variable2 = variable1.clon() self.assertFalse(variable1 is variable2) self.assertTrue(variable1.equal(variable2))
def testGivenEmptyEquationWhenAddConstantThenItIsAddedInBothSides(self): equation = EquationBuilder().build() term = ConstantBuilder().build() equation.add(term) self.assertEqual(equation.get_value_constant(Side.left), term.value) self.assertEqual(equation.get_value_constant(Side.right), term.value)
def testGivenEmptyEquationWhenAddConstantSideRightThenItIsAddedInRightSide( self): equation = EquationBuilder().build() term = ConstantBuilder().value(1.0).build() equation.add_side(Side.right, term) self.assertEqual(equation.get_value_constant(Side.right), term.value)
def testGivenExpressionWithConstantAndVariableWhenGetValueThenConstantValueIsReturned( self): constant = ConstantBuilder().build() expression = ExpressionBuilder().term( constant).default_variable().build() self.assertEqual(expression.get_value_constant(), constant.value)
def one_equals_x(): return EquationBuilder().right_term( VariableBuilder().name('x').value(1).build()).left_term( ConstantBuilder().value(1).build()).build()