File size: 5,890 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
from visma.functions.structure import Function, Expression
from visma.functions.variable import Variable
from visma.functions.constant import Constant
from visma.functions.operator import Operator, Minus, EqualTo
from visma.simplify.simplify import simplify, simplifyEquation
from visma.io.parser import tokensToString
import copy

##########################
# Simple Solver(for now) #
##########################

# FIXME: try-catch ValueError: negative number cannot be raised to a fractional power
# FIXME: Only single variable in LHS after moveToRHS


def solveFor(lTokens, rTokens, wrtVar):
    """Solve the input equation wrt to selected variable

    Arguments:
        lTokens {list} -- LHS tokens list
        rTokens {list} -- RHS tokens list
        wrtVar {string} -- variable to be solved

    Returns:
        lTokens {list} -- LHS tokens list
        rTokens {list} -- RHS tokens list
        availableOperations {list} -- list of operations
        token_string {string} -- final result in string
        animation {list} -- list of equation solving progress
        comments {list} -- list of solution steps
    """

    lTokens, rTokens, availableOperations, token_string, animation, comments = simplifyEquation(lTokens, rTokens)

    lTokens, rTokens, animNew, commentsNew = solveTokens(lTokens, rTokens, wrtVar)

    tokenToStringBuilder = copy.deepcopy(lTokens)
    tokenToStringBuilder.append(EqualTo())
    tokenToStringBuilder.extend(rTokens)
    token_string = tokensToString(tokenToStringBuilder)

    animation.extend(animNew)
    comments.extend(commentsNew)

    return lTokens, rTokens, availableOperations, token_string, animation, comments


def solveTokens(lTokens, rTokens, wrtVar):
    """Solve the input equation wrt to selected variable after equation simplification

    Arguments:
        lTokens {list} -- LHS tokens list
        rTokens {list} -- RHS tokens list
        wrtVar {string} -- variable to be solved

    Returns:
        lTokens {list} -- LHS tokens list
        rTokens {list} -- RHS tokens list
        animNew {list} -- list of equation solving progress
        commentsNew {list} -- list of solution steps
    """
    animNew = []
    commentsNew = []

    lTokens, rTokens, animation1, comment1 = moveToRHS(lTokens, rTokens, wrtVar)

    if checkOnlyVarTermsInList(lTokens, wrtVar):
        if len(lTokens) == 1:
            lTokens, rTokens, animation2, comment2 = funcInverse(lTokens, rTokens, wrtVar)
        else:
            animation2 = []
            comment2 = [""]
    else:
        animation2 = []
        comment2 = [""]

    animNew.extend([animation1, animation2])
    commentsNew.extend([comment1, comment2])
    return lTokens, rTokens, animNew, commentsNew


def moveToRHS(lTokens, rTokens, wrtVar):
    """Moves all variables which are not equal to wrtVar to RHS

    Arguments:
        lTokens {list} -- LHS tokens list
        rTokens {list} -- RHS tokens list
        wrtVar {string} -- variable to be solved

    Returns:
        lTokens {list} -- LHS tokens list
        rTokens {list} -- RHS tokens list
        animation {list} -- list of equation solving progress
        comments {list} -- list of solution steps
    """

    comment = "Moving "
    i = 0
    while i < len(lTokens):
        if isinstance(lTokens[i], Function) and not isVarInToken(lTokens[i], wrtVar):
            if i-1 >= 0 and isinstance(lTokens[i-1], Operator):
                comment += r"$" + lTokens[i-1].__str__() + r"$"
                if lTokens[i-1].value == '-':
                    lTokens[i-1].value = '+'
                    rTokens.append(lTokens.pop(i-1))
                elif lTokens[i-1].value == '+':
                    lTokens[i-1].value = '-'
                    rTokens.append(lTokens.pop(i-1))
                i -= 1
            elif i == 0:
                rTokens.append(Minus())
            comment += r"$" + lTokens[i].__str__() + r"$"
            rTokens.append(lTokens.pop(i))
            i -= 1
        i += 1
    comment += " to RHS"
    if isinstance(lTokens[0], Operator):
        if lTokens[0].value == '+':
            lTokens.pop(0)
        elif lTokens[0].value == '-':
            lTokens.pop(0)
            lTokens[0].coefficient *= -1
    lTokens, _, _, _, _ = simplify(lTokens)
    rTokens, _, _, _, _ = simplify(rTokens)
    animation = copy.deepcopy(lTokens)
    animation.append(EqualTo())
    animation.extend(rTokens)
    return lTokens, rTokens, animation, [comment]


def funcInverse(lTokens, rTokens, wrtVar):
    """Applies inverse of function of wrtVar to RHS

    Arguments:
        lTokens {list} -- LHS tokens list
        rTokens {list} -- RHS tokens list
        wrtVar {string} -- variable to be solved

    Returns:
        lTokens {list} -- LHS tokens list
        rTokens {list} -- RHS tokens list
        animation {list} -- list of equation solving progress
        comments {list} -- list of solution steps
    """
    if len(lTokens) == 1:
        rExpr = Expression()
        rExpr.tokens.extend(rTokens)
        lToken, rToken, comment = lTokens[0].inverse(rExpr, wrtVar)
    animation = copy.deepcopy(lTokens)
    animation.append(EqualTo())
    animation.append(rToken)
    return [lToken], [rToken], animation, [comment]


def isVarInTokensList(tokens, wrtVar):
    for token in tokens:
        if isVarInToken(token, wrtVar) is True:
            return True
    return False


def checkOnlyVarTermsInList(tokens, wrtVar):  # Rename func
    for token in tokens:
        if isVarInToken(token, wrtVar) is False:
            return False
    return True


def isVarInToken(token, wrtVar):
    if isinstance(token, Constant):
        return False
    elif isinstance(token, Variable):
        if wrtVar in token.value:
            return True
    elif isinstance(token, Expression):
        return isVarInTokensList(token.tokens, wrtVar)
    else:
        return True