File size: 9,022 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'''
Owner: AerospaceResearch.net
About: This module hosts the functions used for finding roots of a  quartic equation
Note: Please try to maintain proper documentation
Logic Description:
'''

import math
import copy
from operator import itemgetter
from visma.io.checks import getVariables
from visma.io.parser import tokensToString
from visma.functions.structure import Expression
from visma.functions.constant import Constant, Zero
from visma.functions.variable import Variable
from visma.functions.operator import Binary, Plus, Minus, Sqrt
from visma.simplify.simplify import simplifyEquation, moveRTokensToLTokens
from visma.config.values import ROUNDOFF
from visma.solvers.polynomial.cubic import getRootsCubic


def getRootsQuartic(coeffs):
    """ Applies an implementation of Determinant Method (https://en.wikipedia.org/wiki/Cubic_function) on the coefficients
        of the Quartic equation to get roots

        Arguments:
            coeffs {list} -- list of coefficients of the equation

        Returns:
            roots {list} -- list of roots of quartic equation
            (each element of roots {list} is a list of two elements, where 1st one denotes real part & second part shows imaginary part)
            animation {list} -- list of equation solving process
            comments {list} -- list of comments in equation solving process
    """
    from visma.solvers.polynomial.roots import squareRootComplex
    roots = []
    animations = []
    comments = []
    a = coeffs[4]
    b = coeffs[3]
    c = coeffs[2]
    d = coeffs[1]
    e = coeffs[0]

    f = c - (3*(b**2)/8)
    g = d + (b**3)/8 - (b*c/2)
    h = e - (3*(b**4)/256) + ((b**2)*c/16) - (b*d/4)
    animations += [[]]
    comments += [['Value of determinants [f, g, h] are ' + str(f) + ', ' + str(g) + ', ' + str(h)]]

    reducedCubicEquation = [-(g*g)/64, (f*f - 4*h)/16, (f/2), 1]
    rootsReducedCubic, _, _ = getRootsCubic(reducedCubicEquation)
    animations += [[]]
    comments += [['Can be solved using a cubic equation with coefficients ' + str(1) + ', ' + str((f/2)) + ', ' + str((f*f - 4*h)/16) + ' and ' + str(-(g*g)/64)]]
    for i in range(3):
        rootsReducedCubic[i][0] = round(rootsReducedCubic[i][0], ROUNDOFF)
        rootsReducedCubic[i][1] = round(rootsReducedCubic[i][1], ROUNDOFF)
    if (rootsReducedCubic[0][1] == 0 and rootsReducedCubic[1][1] == 0 and rootsReducedCubic[2][1] == 0):
        animations += [[]]
        comments += [['Depending on imaginary roots exist for cubic, roots are be determined as: ']]
        if (rootsReducedCubic[0][0] >= 0 and rootsReducedCubic[1][0] >= 0 and rootsReducedCubic[2][0] >= 0):
            rootsReducedCubicSorted = sorted(rootsReducedCubic, key=itemgetter(0))
            p = math.sqrt(rootsReducedCubicSorted[1][0])
            q = math.sqrt(rootsReducedCubicSorted[2][0])
            r = -g/(8*p*q)
            s = b/(4*a)
            valueRealX1 = p + q + r - s
            valueImaginaryX1 = 0
            roots.append([valueRealX1, valueImaginaryX1])

            valueRealX2 = p - q - r - s
            valueImaginaryX2 = 0
            roots.append([valueRealX2, valueImaginaryX2])

            valueRealX3 = - p + q - r - s
            valueImaginaryX3 = 0
            roots.append([valueRealX3, valueImaginaryX3])

            valueRealX4 = - p - q + r - s
            valueImaginaryX4 = 0
            roots.append([valueRealX4, valueImaginaryX4])
        else:
            rootsReducedCubicSorted = []
            for x in rootsReducedCubic:
                if x[0] < 0:
                    rootsReducedCubicSorted.append(x)
            for x in rootsReducedCubic:
                if x[0] > 0:
                    rootsReducedCubicSorted.append(x)
            p = [0, math.sqrt(abs(rootsReducedCubicSorted[0][0]))]
            q = [0, math.sqrt(abs(rootsReducedCubicSorted[1][0]))]
            pq = -1 * p[1] * q[1]
            r = -g/(8*pq)
            s = b/(4*a)

            valueRealX1 = r - s
            valueImaginaryX1 = p[1] + q[1]
            roots.append([valueRealX1, valueImaginaryX1])

            valueRealX2 = - r - s
            valueImaginaryX2 = p[1] - q[1]
            roots.append([valueRealX2, valueImaginaryX2])

            valueRealX3 = - r - s
            valueImaginaryX3 = - p[1] + q[1]
            roots.append([valueRealX3, valueImaginaryX3])

            valueRealX4 = r - s
            valueImaginaryX4 = - p[1] - q[1]
            roots.append([valueRealX4, valueImaginaryX4])
    else:
        #   Imaginary part exists in reduced Cubic equation
        rootsReducedCubicSorted = []
        for x in rootsReducedCubic:
            if x[1] != 0:
                rootsReducedCubicSorted.append(x)
        for x in rootsReducedCubic:
            if x[1] == 0:
                rootsReducedCubicSorted.append(x)
        p = squareRootComplex(rootsReducedCubicSorted[0])
        q = squareRootComplex(rootsReducedCubicSorted[1])
        if p[1] > 0 and p[0] < 0:
            p, q = q, p
        pq = p[0]*p[0] + p[1]*p[1]
        r = -g/(8*pq)
        s = b/(4*a)

        valueRealX1 = 2*p[0] + r - s
        valueImaginaryX1 = 0
        roots.append([valueRealX1, valueImaginaryX1])

        valueRealX2 = - r - s
        valueImaginaryX2 = 2*p[1]
        roots.append([valueRealX2, valueImaginaryX2])

        valueRealX3 = - r - s
        valueImaginaryX3 = -2*p[1]
        roots.append([valueRealX3, valueImaginaryX3])

        valueRealX4 = -2*p[0] + r - s
        valueImaginaryX4 = 0
        roots.append([valueRealX4, valueImaginaryX4])
    return roots, animations, comments


def quarticRoots(lTokens, rTokens):
    '''Used to get roots of a quartic equation
    This functions also translates roots {list} into final result of solution

    Argument:
        lTokens {list} -- list of LHS tokens
        rTokens {list} -- list of RHS tokens

    Returns:
        lTokens {list} -- list of LHS tokens
        rTokens {list} -- list of RHS tokens
        {empty list}
        token_string {string} -- final result stored in a string
        animation {list} -- list of equation solving process
        comments {list} -- list of comments in equation solving process
    '''
    from visma.solvers.polynomial.roots import getCoefficients

    animations = []
    comments = []
    lTokens, rTokens, _, _, animNew1, commentNew1 = simplifyEquation(lTokens, rTokens)
    animations.extend(animNew1)
    comments.extend(commentNew1)
    if len(rTokens) > 0:
        lTokens, rTokens = moveRTokensToLTokens(lTokens, rTokens)
    coeffs = getCoefficients(lTokens, rTokens, 4)
    for i, lTok in enumerate(lTokens):
        if not isinstance(lTok, Binary):
            lTokens[i] /= Constant(coeffs[4])
    for i, rTok in enumerate(rTokens):
        if not isinstance(rTok, Binary):
            rTokens[i] /= Constant(coeffs[4])
    coeffs = getCoefficients(lTokens, rTokens, 4)
    roots, animNew2, commentnew2 = getRootsQuartic(coeffs)
    animations.extend(animNew2)
    comments.extend(commentnew2)
    var = getVariables(lTokens)
    lTokens = []
    rTokens = []
    for _, root in enumerate(roots):
        tokens2 = []
        expression2 = Expression(coefficient=1, power=1)
        variable = Variable(1, var[0], 1)
        tokens2.append(variable)
        binary = Binary()
        if root[1] == 0:
            if root[0] < 0:
                root[0] *= -1
                binary.value = '+'
            else:
                binary.value = '-'
            tokens2.append(binary)
            constant = Constant(round(root[0], ROUNDOFF), 1)
            tokens2.append(constant)
        else:
            binary.value = '-'
            tokens2.append(binary)
            expressionResult = Expression(coefficient=1, power=1)
            tokensResult = []
            real = Constant(round(root[0], ROUNDOFF), 1)
            tokensResult.append(real)
            imaginary = Constant(round(root[1], ROUNDOFF), 1)
            if imaginary.value < 0:
                tokensResult.append(Minus())
                imaginary.value = abs(imaginary.value)
                tokensResult.append(imaginary)
            else:
                tokensResult.append(Plus())
                tokensResult.append(imaginary)
            tokensResult.append(Binary('*'))
            sqrt = Sqrt(Constant(2, 1), Constant(-1, 1))
            tokensResult.append(sqrt)
            expressionResult.tokens = tokensResult
            tokens2.append(expressionResult)
        expression2.tokens = tokens2
        lTokens.extend([expression2, Binary('*')])
    rTokens = [Zero()]
    lTokens.pop()
    tokenToStringBuilder = copy.deepcopy(lTokens)
    tokLen = len(lTokens)
    equalTo = Binary()
    equalTo.scope = [tokLen]
    equalTo.value = '='
    tokenToStringBuilder.append(equalTo)
    tokenToStringBuilder.extend(rTokens)
    token_string = tokensToString(tokenToStringBuilder)
    animations.append(copy.deepcopy(tokenToStringBuilder))
    comments.append([])
    return lTokens, rTokens, [], token_string, animations, comments