Exemplo n.º 1
0
    def combineLikeTermsWithoutSymCo(self):
        if len(self.summation) == 0:
            return
        zeroMult = copy.deepcopy(self.summation[0]).setZero()
        i = 0
        while i < len(self.summation):
            currentTerm = self.summation[i]
            j = i + 1
            while j < len(self.summation):
                testTerm = self.summation[j]
                #print("current term: ", currentTerm.getTensorCos(), type(currentTerm.getTensorCos()), "test term: ", testTerm.getTensorCos(), type(testTerm.getTensorCos()))
                if type(currentTerm.getTensorCos()
                        ) is TensorCoefficients and type(
                            testTerm.getTensorCos()) is TensorCoefficients:

                    if currentTerm.combEq(testTerm) and (
                            currentTerm.getSymbolCo()
                            == testTerm.getSymbolCo()) and (
                                currentTerm.getTensorCos()
                                == testTerm.getTensorCos()):
                        self.summation.pop(j)
                        if currentTerm.getSign() == testTerm.getSign(
                        ):  # if same sign add coefficients
                            currentTerm.setNumCo(currentTerm.getNumCo() +
                                                 testTerm.getNumCo())
                            #self.summation.pop(j)
                        else:  # subtract coefficients
                            currentTerm.setNumCo(currentTerm.getNumCo() -
                                                 testTerm.getNumCo())
                            if currentTerm.getNumCo() < Fraction(0):
                                currentTerm.setNumCo(
                                    abs(currentTerm.getNumCo()))
                                currentTerm.swapSign()
                            elif currentTerm.getNumCo() == Fraction(
                                    0):  # get rid of BOTH and start again
                                self.summation.pop(i)
                                i += -1
                                #self.summation.pop(j-1)
                                j = len(
                                    self.summation
                                )  # this term is gone so we no longer test against it
                            #else:
                            #self.summation.pop(j)
                    else:
                        j += 1
                else:
                    raise TypeError("not tensor cos")
            i += 1
        if len(self.summation) == 0:
            self.addTerm(zeroMult)
Exemplo n.º 2
0
 def __repr__(self):
     strx = ""
     if self.showSign:
         strx += self.sign
     if self.numCo != Fraction(1):
         strx += repr(self.numCo)
     if self.symCo != SymbolCo():
         strx += repr(self.symCo)
     return strx
Exemplo n.º 3
0
 def remZeroTerms(self):
     if len(self.summation) == 0:
         return
     zeroMult = copy.deepcopy(self.summation[0]).setZero()
     for summy in copy.deepcopy(self.summation):
         if summy.getNumCo() == Fraction(0):
             self.removeTerm(summy)
     if len(self.summation) == 0:
         self.addTerm(zeroMult)
     return
Exemplo n.º 4
0
 def cleanCancellations(self):
     if len(self.cos) == 0:
         return
     i = 0
     while i < len(self.getCos()):
         j = i+1
         while j < len(self.getCos()):
             if self.getCos()[i].combEq(self.getCos()[j]):
                 if self.getCos()[i].getSign() == self.getCos()[j].getSign():
                     self.getCos()[i] = self.getCos()[i]*Coefficient("+", Fraction(2))
                     self.getCos().pop(j)
                 else:
                     self.getCos().pop(j)
                     self.getCos().pop(i)
                     i += -1
                     j = len(self.getCos())
             else:
                 j += 1
         i += 1
     if len(self.cos) == 0:
         self.cos.append(Coefficient("+", Fraction(0)))
         self.isZero = True
Exemplo n.º 5
0
 def __init__(self, sign="+", numCo=None, symCo=None):
     self.sign = sign
     if numCo is None:
         self.numCo = Fraction(1)
     else:
         self.numCo = numCo
     if symCo is None:
         self.symCo = SymbolCo()
     else:
         self.symCo = symCo
     self.showSign = False
     if self.sign == "-":
         self.showSign = True
Exemplo n.º 6
0
 def combineLikeTerms(self):
     if len(self.summation) == 0:
         return
     zeroMult = copy.deepcopy(self.summation[0]).setZero()
     i = 0
     for summy in self.summation:
         summy.combineCoefficients()
         summy.setSums()
     while i < len(self.summation):
         currentTerm = self.summation[i]
         j = i + 1
         while j < len(self.summation):
             testTerm = self.summation[j]
             if currentTerm.combEq(testTerm):
                 self.summation.pop(j)
                 if currentTerm.getSign() == testTerm.getSign(
                 ):  # if same sign add coefficients
                     currentTerm.setTensorCos(currentTerm.getTensorCos() +
                                              testTerm.getTensorCos())
                     #self.summation.pop(j)
                 else:  # subtract coefficients
                     currentTerm.setTensorCos(currentTerm.getTensorCos() -
                                              testTerm.getTensorCos())
                     if currentTerm.getTensorCos() == TensorCoefficients(
                         [Coefficient("-", Fraction(1))]):
                         currentTerm.setTensorCos([Coefficient()])
                         currentTerm.swapSign()
                     if currentTerm.getTensorCos().getIsZero(
                     ):  # get rid of BOTH and start again
                         self.summation.pop(i)
                         i += -1
                         #self.summation.pop(j-1)
                         j = len(
                             self.summation
                         )  # this term is gone so we no longer test against it
                     #else:
                     #    self.summation.pop(j)
             else:
                 j += 1
         i += 1
     if len(self.summation) == 0:
         self.addTerm(zeroMult)
Exemplo n.º 7
0
 def simplify(self):
     if len(self.cos) == 0:
         return
     numCo = Fraction(0)
     i = 0
     while i < len(self.cos):
         el = self.cos[i]
         if el.isJustNum():
             if el.getSign() == "-":
                 numCo = numCo - el.getNumCo()
             else:
                 numCo = numCo + el.getNumCo()
             self.cos.pop(i)
         else:
             i += 1
     #for el in copy.deepcopy(self.cos):
     #    if el.isJustNum():
     #        if el.getSign() == "-":
     #            numCo = numCo - el.getNumCo()
     #        else:
     #            numCo = numCo + el.getNumCo()
     #        self.cos.remove(el)
     if numCo != Fraction(0):
         if numCo.isNeg():
             self.cos.append(Coefficient("-", abs(numCo)))
         else:
             self.cos.append(Coefficient("+", numCo))
     i = 0
     while i < len(self.cos):
         j = i+1
         while j < len(self.cos):
             if self.cos[i] == self.cos[j]:
                 self.cos[i] = self.cos[i]*Coefficient("+", Fraction(2))
                 self.cos.pop(j)
             elif self.cos[i].combEq(self.cos[j]):
                 self.cos.pop(j)
                 self.cos.pop(i)
                 i += -1
                 j = len(self.cos)
             else:
                 j += 1
         i += 1
     s = 0
     while s < len(self.cos):
         q = s+1
         while q < len(self.cos):
             if self.cos[s].getSymCo() == self.cos[q].getSymCo():
                 if self.cos[s].getSign() == self.cos[q].getSign():
                     self.cos[s].setNumCo(self.cos[s].getNumCo() + self.cos[q].getNumCo())
                 else:
                     self.cos[s].setNumCo(self.cos[s].getNumCo() - self.cos[q].getNumCo())
                     if self.cos[s].getNumCo().isNeg():
                         self.cos[s].setNumCo(abs(self.cos[s].getNumCo()))
                         self.cos[s].changeSign()
                 self.cos.pop(q)
                 if self.cos[s].getNumCo() == Fraction(0):
                     self.cos.pop(s)
                     s += -1
                     q = len(self.cos)
             else:
                 q += 1
         s += 1
     if len(self.cos) == 0:
         self.cos.append(Coefficient("+", Fraction(0)))
         self.isZero = True
Exemplo n.º 8
0
 def isJustSymb(self):
     if (self.symCo != SymbolCo()) and (self.numCo == Fraction(1)):
         return True
     else:
         return False
Exemplo n.º 9
0
 def gcf(self, node):
     if not node.isSum():
         return
     sums = node.getElement().getSums()
     if len(sums) == 1 or len(sums) == 0:
         return
     for summy in sums:
         summy.factorNumFromTensorCo()
         summy.factorSymFromTensorCo()
     otherTerm = MultGroup("+")
     added = False
     symCo = copy.deepcopy(sums[0].getSymbolCo())
     tensors = copy.deepcopy(sums[0].getTensors())
     variations = copy.deepcopy(sums[0].getVariations())
     etas = copy.deepcopy(sums[0].getEtas())
     deltas = copy.deepcopy(sums[0].getDeltas())
     partials = copy.deepcopy(sums[0].getPartials())
     tensorCos = copy.deepcopy(sums[0].getTensorCos())
     numCosList = list()
     for sum1 in sums:
         numCosList.append(sum1.getNumCo())
     numCo = min(numCosList)
     inAll = True
     for frac in numCosList:
         if frac % numCo != Fraction(0):
             inAll = False
     if inAll:
         otherTerm.addNumCo(numCo)
         added = True
         for sum2 in sums:
             sum2.setNumCo(sum2.getNumCo() / numCo)
     inAll = True
     for sum1 in sums:
         if not symCo == sum1.getSymbolCo():
             inAll = False
     if inAll:
         otherTerm.setSymCo(symCo)
         added = True
         for sum2 in sums:
             sum2.setSymCo(SymbolCo())
     inAll = True
     for sum1 in sums:
         if not tensorCos == sum1.getTensorCos():
             inAll = False
     if inAll:
         otherTerm.addTensorCos(tensorCos)
         added = True
         for sum2 in sums:
             sum2.setTensorCos(TensorCoefficients([Coefficient()]))
     for tensor in tensors:
         inAll = True
         for sum1 in sums:
             if tensor not in sum1.getTensors():
                 inAll = False
         if inAll:
             otherTerm.addTensors(tensor)
             added = True
             for sum2 in sums:
                 sum2.removeTensor(tensor)
     for variation in variations:
         inAll = True
         for sum1 in sums:
             if variation not in sum1.getVariations():
                 inAll = False
         if inAll:
             otherTerm.addVariations(variation)
             added = True
             for sum2 in sums:
                 sum2.removeVar(variation)
     for delta in deltas:
         inAll = True
         for sum1 in sums:
             if delta not in sum1.getDeltas():
                 inAll = False
         if inAll:
             otherTerm.addDeltas(delta)
             added = True
             for sum2 in sums:
                 sum2.removeDelta(delta)
     for eta in etas:
         inAll = True
         for sum1 in sums:
             if eta not in sum1.getEtas():
                 inAll = False
         if inAll:
             otherTerm.addEtas(eta)
             added = True
             for sum2 in sums:
                 sum2.removeEta(eta)
     for partial in partials:
         inAll = True
         for sum1 in sums:
             if partial not in sum1.getPartials():
                 inAll = False
         if inAll:
             otherTerm.addPartials(partial)
             added = True
             for sum2 in sums:
                 sum2.removePartial(partial)
     if added:
         node.setRight(EquationNode(Summation([otherTerm])))
         node.setLeft(EquationNode(node.getElement()))
         node.setElement(Sign("*"))
Exemplo n.º 10
0
 def isZero(self):
     if (len(self.summation) == 1) and (self.summation[0].getNumCo()
                                        == Fraction(0)):
         return True
     else:
         return False