def changeIndicesList(self, fromList, toList): if len(fromList) != len(toList): raise TypeError("indices to replace do not match in length!") for i in range(len(fromList)): if not fromList[i].patternEq(toList[i]): raise TypeError( "these indices are not replaceable - they have different summation types" ) toChange = self.getEqualIndices(fromList[i]) for ind in toChange: ind.setIndex(toList[i].getIndex() + "%") for newInd in self.getIndices(): if not (newInd.getIndex() == "\\ "): newInd.changeIndex(newInd.getIndex().replace("%", ""))
def multList(self, list): if len(list) == 0: raise TypeError("list MUST HAVE ELEMENTS!!!") if (len(list) == 1): return list[0] elif len(list) == 2: return list[0] * list[1] else: return list[0] * self.multList(list[1:])
def simplestChangeIndicesList(self, fromList, toList): if len(fromList) != len(toList): raise TypeError("indices to replace do not match in length!") for i in range(len(fromList)): toChange = self.getEqualIndices(fromList[i]) for ind in toChange: ind.setIndex(toList[i].getIndex() + "%") for newInd in self.getIndices(): if not (newInd.getIndex() == "\\ "): newInd.changeIndex(newInd.getIndex().replace("%", ""))
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)
def noPAdd(self, other): self.remZeroTerms() other.remZeroTerms() if other.isZero(): return self if self.isZero(): return other self.distributeSign() other.distributeSign() #self.distributePartials() #other.distributePartials() #self.removeSumsWithinMult() #other.removeSumsWithinMult() if len(self.getPartials()) == 0 and len(other.getPartials()) == 0: return Summation( copy.deepcopy(self.getSums()) + copy.deepcopy(other.getSums())) else: raise TypeError("cannot add/ subtract terms under a derivative")
def noPMul(self, other): self.remZeroTerms() other.remZeroTerms() if self.isZero(): return copy.deepcopy(self) if other.isZero(): return copy.deepcopy(other) if self.getSign() == other.getSign(): sign = "+" else: sign = "-" if len(self.getSums()) == 1: if self.getSums()[0].isOnlyPartials(): other.addPartials(self.getSums()[0].getPartials()) self.getSums()[0].setPartials(list()) for t in range(len(other.getSums())): #other.replaceTerm(term, term*self.getSums()[0]) other.getSums()[t] = other.getSums()[t] * self.getSums()[0] return other if len(other.getSums()) == 1: if other.getSums()[0].isOnlyPartials(): self.addPartials(other.getSums()[0].getPartials()) other.getSums()[0].setPartials(list()) for t in range(len(self.getSums())): #self.replaceTerm(term, term*other.getSums()[0]) self.getSums()[t] = self.getSums()[t] * other.getSums()[0] return self #self.distributePartials() #other.distributePartials() if len(self.getPartials()) == 0 and len(other.getPartials()) == 0: sumsList = list() for sum1 in self.getSums(): for sum2 in other.getSums(): sumsList.append(copy.deepcopy(sum1) * copy.deepcopy(sum2)) return Summation(sumsList, list(), sign) else: raise TypeError("cannot multiply terms under a derivative")
def multOutPartials(self, other): self.remZeroTerms() other.remZeroTerms() if self.getSign() == other.getSign(): sign = "+" else: sign = "-" if len(self.getSums()) == 1: if self.getSums()[0].isOnlyPartials(): other.addPartials(self.getSums()[0].getPartials()) self.getSums()[0].setPartials(list()) for t in range(len(other.getSums())): #other.replaceTerm(term, term*self.getSums()[0]) other.getSums()[t] = other.getSums()[t] * self.getSums()[0] return other if len(other.getSums()) == 1: if other.getSums()[0].isOnlyPartials(): self.addPartials(other.getSums()[0].getPartials()) other.getSums()[0].setPartials(list()) for t in range(len(self.getSums())): #self.replaceTerm(term, term*other.getSums()[0]) self.getSums()[t] = self.getSums()[t] * other.getSums()[0] return self raise TypeError("cannot multiply partials if no partial")
def __init__(self, symbol, sym, partials=None, sign="+", isSymmetric=False, symmetricTensorSymbols=None): self.symbol = symbol if type(sym) is SymmetryProperties: self.symmetry = sym else: raise TypeError() self.isSymmetric = isSymmetric if partials is None: self.partials = list() else: self.partials = partials self.sign = sign #if self.symbol == "h": # self.isSymmetric = True # self.symmetry.setSymmetric(True) if symmetricTensorSymbols is None: self.symTenSym = list() else: self.symTenSym = symmetricTensorSymbols if self.symbol in self.symTenSym: self.isSymmetric = True self.symmetry.setSymmetric(True) self.numTSums = 0 if len(sym.getIndices()) > 1: for i in range(len(sym.getIndices())): for j in range(i + 1, len(sym.getIndices())): if sym.getIndices()[i].sumsWith(sym.getIndices()[j]): sym.getIndices()[i].changeSum(True) sym.getIndices()[j].changeSum(True) self.numTSums += 1 self.numPTSums = 0 if len(sym.getIndices()) > 0 and len(partials) > 0: for p in range(len(partials)): for i in range(len(sym.getIndices())): if partials[p].getIndex().sumsWith(sym.getIndices()[i]): #if not sym.getUpInds()[i].hasSum(): sym.getIndices()[i].changeSum(True) partials[p].getIndex().changeSum(True) self.numPTSums += 1 #else: #raise IndexException("Too many instances of the index " + repr(sym.getUpInds()[i].getSymbol())) #for d in range(len(sym.getDownInds())): # if partials[p].getIndex().sumsWith(sym.getDownInds()[d]): #if not sym.getDownInds()[d].hasSum(): # sym.getDownInds()[d].changeSum(True) # partials[p].getIndex().changeSum(True) # self.numPTSums += 1 #if (not sym.getDownInds()[d].isUp()) and (isSymmetric): # sym.getDownInds()[d].changeHeight() # partials[p].getIndex().changeHeight() #elif (not sym.getDownInds()[d].isUp()): # if len(sym.getUpInds()) > d and sym.getUpInds()[d].getIndex() == Index("\\ "): # ind = copy.deepcopy(sym.getDownInds()[d]) # ind.changeHeight() # sym.getUpInds()[d] = ind # sym.getDownInds()[d] = Index("\\%", 0) # partials[p].getIndex().changeHeight() #else: # raise IndexException("Too many instances of the index " + repr(sym.getDownInds()[d].getSymbol())) self.rank = (len(sym.getIndices()) - 2 * self.numTSums - 2 * self.numPTSums) self.numPartials = len(self.partials) self.ownerHash = self.symbol + str(self.numTSums) + str(self.numPTSums) self.setSums()