示例#1
0
    def substract(a, b):
        """Returns the expression resultant of substracting terms a and b."""

        a = Expression(a, no_vars_intended=True)
        b = Expression(b, no_vars_intended=True)

        if isanumber(a.expression) or isanumber(b.expression):
            raise NonAlgebraicOperationError

        if len(a.terms) > 1 or len(b.terms) > 1:
            raise InvalidOperationError(a, b)

        if TermOperations.getpower(
                a.expression) is not TermOperations.getpower(
                    b.expression) or a.variables != b.variables:
            result = a.expression + '-' + b.expression
            return Expression.beautify(result)

        a_coefficient = a.get_number(0, frac_to_number=True)
        b_coefficient = b.get_number(0, frac_to_number=True)

        result = str(num(a_coefficient) - num(b_coefficient))
        result = if_assign(result == '1', "", result)
        result = if_assign(result == '-1', "-", result)

        result += "".join(a.variables) + '**' + str(
            TermOperations.getpower(a.expression))
        return Expression.beautify(result)
示例#2
0
    def add(a, b, non_algebraic=False):
        """Returns the expression resultant of adding terms a and b."""

        a = Expression(a, no_vars_intended=True)
        b = Expression(b, no_vars_intended=True)

        if isanumber(a.expression) or isanumber(b.expression):

            if non_algebraic is True:
                return num(num(a.expression) + num(b.expression))
            raise NonAlgebraicOperationError

        if len(a.terms) > 1 or len(b.terms) > 1:
            raise InvalidOperationError(a, b)

        if TermOperations.getpower(
                a.expression) is not TermOperations.getpower(
                    b.expression) or a.variables != b.variables:
            operator = if_assign(b.expression.startswith('-'), '', '+')
            return Expression.beautify(a.expression + operator + b.expression)

        a_coefficient = a.get_number(0)
        b_coefficient = b.get_number(0)

        result = str(num(a_coefficient) + num(b_coefficient))
        result = if_assign(result == '1', "", result)
        result = if_assign(result == '-1', "-", result)

        result += "".join(a.variables) + '**' + str(
            TermOperations.getpower(a.expression))
        if result.endswith('**1'):
            result = result.replace('**1', '')
        return Expression.beautify(result)
示例#3
0
    def get_vertex(self):
        """Returns this quadratic's vertex (point intersected by
        axis of symmetry)."""

        a, b, c = self.getabc()

        x = eval('-' + b + '/(2*' + a + ')')
        y = self.evaluate(x)

        return num(str(x)), num(str(y))
示例#4
0
    def __init__(self, equation):

        Equation.__init__(self, equation)
        self.form = self.get_form()
        self.x_coefficient = self.get_x_coefficient()
        self.y_coefficient = self.get_y_coefficient()
        self.slope = self.get_slope()
        self.y_intercept = num(self.solve_for("x", 0))
示例#5
0
    def getpower(a):
        """Returns exponent of term a."""

        if TermOperations.ispowered(a):
            return num(a[a.find('**') + 2:])
        elif isanumber(a):
            return 0
        return 1
示例#6
0
    def solve(self):
        """Returns the solution to this system of equation."""

        if self.is_compatible() is False:
            return None
        if self.get_number_of_solutions() == 'Infinitely many solutions':
            raise InfinitelySolutionsError

        x_coefficient = str(
            num(self.linear_2.x_coefficient) -
            num(self.linear_1.x_coefficient))
        x_coefficient = '' if x_coefficient == '1' else x_coefficient
        equation = x_coefficient + 'x' + '=' + str(
            self.linear_1.y_intercept) + '-' + str(self.linear_2.y_intercept)

        x_value = Equation(equation).solve()

        return self.linear_1.get_point(x_value)
示例#7
0
    def solve(self, debug=False):
        """Returns the solution of the equation as integer or float, depending on what
		the solution is."""

        rhs_no_var_terms = [
            num(term) for term in Expression(
                self.rhs, no_vars_intended=True).get_terms() if isanumber(term)
        ]
        lhs_no_var_terms = [
            num(term) for term in Expression(
                self.lhs, no_vars_intended=True).get_terms() if isanumber(term)
        ]

        rhs_var_terms = [
            term for term in Expression(self.rhs,
                                        no_vars_intended=True).get_terms()
            if any(var in term for var in self.variables)
        ]
        lhs_var_terms = [
            term for term in Expression(self.lhs,
                                        no_vars_intended=True).get_terms()
            if any(var in term for var in self.variables)
        ]

        polynomial = '+'.join(lhs_var_terms) + '-' + '+'.join(rhs_var_terms)
        polynomial = if_assign(polynomial.endswith('-'), polynomial[:-1],
                               polynomial)

        lhs = Polynomial(polynomial)
        coefficient = self.get_number(0, lhs.polynomial)
        result = sum(rhs_no_var_terms) - sum(lhs_no_var_terms)

        if debug:
            print(lhs, '=', result, '-->',
                  str(result) + '/(' + coefficient + ')')

        if coefficient is '0':
            if result == 0:
                return "All real numbers are solutions."
            return "No solutions."

        return num(str((eval(str(result) + '/(' + coefficient + ')'))))
示例#8
0
    def get_roots(self):
        """Returns a list containing the roots of this quadratic."""

        if self._roots_type == RootTypes.ComplexConjugateRoots:
            return [self.bhaskarize()]

        bhask = self.bhaskarize().replace('½', '0.5')

        if self._roots_type == RootTypes.IdenticRealRoots:
            return [num(str(eval(bhask.replace('+/-', '+'))))]

        roots = [
            eval(bhask.replace('+/-', '+')),
            eval(bhask.replace('+/-', '-'))
        ]

        return roots
示例#9
0
    def divide(a, b):
        a = Expression(a)
        b = Expression(b)

        variables = set(a.variables + b.variables)
        power = '**' + str(
            int(TermOperations.getpower(a.expression)) -
            int(TermOperations.getpower(b.expression)))
        power = if_assign(power == '**1', '', power)

        if power == '**0':
            return "/".join(variables)

        a_coefficient = a.get_number(0)
        b_coefficient = b.get_number(0)

        result = str(num(
            int(a_coefficient) / int(b_coefficient))) + "/".join(variables)
        result = if_assign(power != '', '(' + result + ')' + power, result)
        return Expression.beautify(result)
示例#10
0
    def solve_for(self, variable, value, show=False):
        """Solves the equation after changing variable into value.

        Keyword Arguments:

        variable (str): the variable to be replaced by a value.
        value (int): the value to replace the variable by.
        (optional) show (bool) : print the sorted equation."""

        eqtn, value = self.equation, str(value)
        sol_side = self.sort(variable)

        if eqtn[eqtn.find(variable) -
                1] == "(" and self.form is not LinearForms.PointSlope:
            eqtn = eqtn.replace("(" + variable, "*(" + variable)
            sol_side = eqtn[eqtn.find("=") + 1:]

        if show is True:
            self.show_sorted(variable, value, sol_side)

        return num(eval(sol_side.replace(variable, value)))
示例#11
0
    def get_slope(self):
        """Returns the slope of this linear equation."""

        a_points, b_points = self.get_point(1), self.get_point(2)
        return num((a_points[1] - b_points[1]) / (a_points[0] - b_points[0]))