File size: 4,675 Bytes
389d072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from visma.io.parser import tokensToString
from visma.functions.structure import Expression
from visma.functions.constant import Constant
from visma.functions.variable import Variable
from visma.functions.operator import Plus, Minus, Multiply
from visma.simplify.simplify import simplify
from visma.utils.integers import gcd, factors
from visma.utils.polynomials import syntheticDivision


def factorize(tokens):

    tokens, availableOperations, token_string, animation, comments = simplify(tokens)

    tokens, animNew, commentsNew = (factorizeTokens(tokens))

    animation.extend(animNew)
    comments.append(commentsNew)
    token_string = tokensToString(tokens)

    return tokens, availableOperations, token_string, animation, comments


def factorizeTokens(tokens):

    coeffs, var = getPolyCoeffs(tokens)
    gcf, roots, polynomial = factor(coeffs)
    if roots != []:
        tokens = []
        comment = "The real roots of the above polynomial are "
        for root in roots:
            comment += r"$" + str(root) + r"\ ,\ " + r"$"
        if gcf != 1:
            tokens.append(Constant(float(gcf)))
            tokens.append(Multiply())
        for root in roots:
            expression = Expression()
            expression.tokens.append(Variable(1, var, 1))
            if root > 0:
                expression.tokens.append(Minus())
                expression.tokens.append(Constant(float(root)))
            elif root < 0:
                expression.tokens.append(Plus())
                expression.tokens.append(Constant(float(-root)))
            tokens.append(expression)
            tokens.append(Multiply())
        if polynomial != [1]:
            expression = Expression()
            degree = len(polynomial) - 1
            for i, coeff in enumerate(polynomial):
                if i == degree:
                    if coeff > 0:
                        expression.tokens.append(Plus())
                        expression.tokens.append(Constant(float(coeff)))
                    elif coeff < 0:
                        expression.tokens.append(Minus())
                        expression.tokens.append(Constant(float(-coeff)))
                elif coeff > 0:
                    expression.tokens.append(Plus())
                    expression.tokens.append(Variable(coeff, var, degree - i))
                elif coeff < 0:
                    expression.tokens.append(Minus())
                    expression.tokens.append(Variable(-coeff, var, degree - i))
            if isinstance(expression.tokens[0], Plus):
                expression.tokens.pop(0)
            tokens.append(expression)
        else:
            tokens.pop()
    else:
        comment = None
    animation = [tokens]
    comments = [comment]
    return tokens, animation, comments


def getPolyCoeffs(tokens):
    degree = 0
    for token in tokens:
        if isinstance(token, Variable) and token.power[0] > degree:
            degree = token.power[0]
            var = token.value[0]

    coeffs = [0]*int(degree + 1)
    for i, token in enumerate(tokens):
        if isinstance(token, Variable):
            coeffs[int(degree - token.power[0])] = int(token.coefficient)
            if tokens[i-1].value == '-':
                coeffs[int(degree - token.power[0])] *= -1
        elif isinstance(token, Constant):
            coeffs[int(degree)] = int(token.value)
            if tokens[i-1].value == '-':
                coeffs[int(degree)] *= -1
    return coeffs, var


def factor(coefficients):
    gcf = gcd(coefficients)
    coefficients = [coefficient / gcf for coefficient in coefficients]
    polynomial = coefficients
    roots = []
    while extractRoots(polynomial) is not False:
        root, quotient = extractRoots(polynomial)
        roots.append(root)
        polynomial = quotient
    polynomial = [int(term) for term in polynomial]
    roots.sort()
    return gcf, roots, polynomial


def extractRoots(coefficients):
    factorsLeading = factors(coefficients[0])
    factorsConstant = factors(coefficients[-1])
    roots = possibleRoots(factorsConstant, factorsLeading)
    for root in roots:
        quotient, remainder = syntheticDivision(coefficients, root)
        if remainder == 0:
            return root, quotient
    return False


def possibleRoots(listA, listB):
    roots = []
    listA = [float(i) for i in listA]
    listB = [float(i) for i in listB]
    for i in listA:
        for j in listB:
            roots.extend([i / j, -i / j])
    roots = removeDuplicates(roots)
    return roots


def removeDuplicates(extraRoots):
    roots = []
    for x in extraRoots:
        if x not in roots:
            roots.append(x)
    return roots