parameters: (self,leftsideeqs,rightsideeqs)
return_statement: return reducedeqs, [solution]
docstring: solve a linear system inside the program since the matrix cannot be reduced so easily
docstring_summary: solve a linear system inside the program since the matrix cannot be reduced so easily
docstring_tokens: solve a linear system inside the program since the matrix cannot be reduced so easily
function: def reduceBothSidesInverseMatrix(self,leftsideeqs,rightsideeqs):
          """solve a linear system inside the program since the matrix cannot be reduced so easily
          """
          allmonomsleft = set()
          for peq in leftsideeqs:
              allmonomsleft = allmonomsleft.union(set(peq.monoms))
          allmonomsleft = list(allmonomsleft)
          allmonomsleft.sort()
          if __builtin__.sum(allmonomsleft[0]) == 0:
              allmonomsleft.pop(0)
          if len(leftsideeqs) < len(allmonomsleft):
              raise self.CannotSolveError('left side has too few equations for the number of variables %d<%d'%(len(leftsideeqs),len(allmonomsleft)))
          
          systemcoeffs = []
          for ileft,left in enumerate(leftsideeqs):
              coeffs = [S.Zero]*len(allmonomsleft)
              rank = 0
              for c,m in left.iter_terms():
                  if __builtin__.sum(m) > 0:
                      if c != S.Zero:
                          rank += 1
                      coeffs[allmonomsleft.index(m)] = c
              systemcoeffs.append((rank,ileft,coeffs))
          # ideally we want to try all combinations of simple equations first until we arrive to linearly independent ones.
          # However, in practice most of the first equations are linearly dependent and it takes a lot of time to prune all of them,
          # so start at the most complex
          systemcoeffs.sort(key=lambda e: -e[0])
          # sort left and right in the same way
          leftsideeqs = [leftsideeqs[ileft] for rank,ileft,coeffs in systemcoeffs]
          rightsideeqs = [rightsideeqs[ileft] for rank,ileft,coeffs in systemcoeffs]
  
          A = zeros((len(allmonomsleft),len(allmonomsleft)))
          Asymbols = []
          for i in range(A.shape[0]):
              Asymbols.append([Symbol('gconst%d_%d'%(i,j)) for j in range(A.shape[1])])
          solution = None
          for eqindices in combinations(range(len(leftsideeqs)),len(allmonomsleft)):
              for i,index in enumerate(eqindices):
                  for k in range(len(allmonomsleft)):
                      A[i,k] = systemcoeffs[index][2][k]
              det = self.det_bareis(A,*self.pvars)
              if det == S.Zero:
                  continue
              solution = AST.SolverMatrixInverse(A=A,Asymbols=Asymbols)
              self.usinglapack = True
              solution.checkforzeros = [self.removecommonexprs(det,onlygcd=False,onlynumbers=True)]
              Aadj=A.adjugate() # too big to be useful for now, but can be used to see if any symbols are always 0
              break
          if solution is None:
              raise self.CannotSolveError('failed to find %d linearly independent equations'%len(allmonomsleft))
          reducedeqs = []
          for i in range(len(allmonomsleft)):
              var=S.One
              for k,kpower in enumerate(allmonomsleft[i]):
                  if kpower != 0:
                      var *= leftsideeqs[0].symbols[k]**kpower
              pright = S.Zero
              for k in range(len(allmonomsleft)):
                  if Aadj[i,k] != S.Zero:
                      pright += Asymbols[i][k] * (rightsideeqs[eqindices[k]].as_basic()-leftsideeqs[eqindices[k]].coeff())
              reducedeqs.append([var,pright.expand()])
          othereqindices = set(range(len(leftsideeqs))).difference(set(eqindices))
          for i in othereqindices:
              # have to multiply just the constant by the determinant
              neweq = rightsideeqs[i].as_basic()
              for c,m in leftsideeqs[i].iter_terms():
                  if __builtin__.sum(m) > 0:
                      neweq -= c*reducedeqs[allmonomsleft.index(m)][1]
                  else:
                      neweq -= c
              reducedeqs.append([S.Zero,neweq])
          return reducedeqs, [solution]
function_tokens: def reduceBothSidesInverseMatrix ( self , leftsideeqs , rightsideeqs ) : allmonomsleft = set ( ) for peq in leftsideeqs : allmonomsleft = allmonomsleft . union ( set ( peq . monoms ) ) allmonomsleft = list ( allmonomsleft ) allmonomsleft . sort ( ) if __builtin__ . sum ( allmonomsleft [ 0 ] ) == 0 : allmonomsleft . pop ( 0 ) if len ( leftsideeqs ) < len ( allmonomsleft ) : raise self . CannotSolveError ( 'left side has too few equations for the number of variables %d<%d' % ( len ( leftsideeqs ) , len ( allmonomsleft ) ) ) systemcoeffs = [ ] for ileft , left in enumerate ( leftsideeqs ) : coeffs = [ S . Zero ] * len ( allmonomsleft ) rank = 0 for c , m in left . iter_terms ( ) : if __builtin__ . sum ( m ) > 0 : if c != S . Zero : rank += 1 coeffs [ allmonomsleft . index ( m ) ] = c systemcoeffs . append ( ( rank , ileft , coeffs ) ) # ideally we want to try all combinations of simple equations first until we arrive to linearly independent ones. # However, in practice most of the first equations are linearly dependent and it takes a lot of time to prune all of them, # so start at the most complex systemcoeffs . sort ( key = lambda e : - e [ 0 ] ) # sort left and right in the same way leftsideeqs = [ leftsideeqs [ ileft ] for rank , ileft , coeffs in systemcoeffs ] rightsideeqs = [ rightsideeqs [ ileft ] for rank , ileft , coeffs in systemcoeffs ] A = zeros ( ( len ( allmonomsleft ) , len ( allmonomsleft ) ) ) Asymbols = [ ] for i in range ( A . shape [ 0 ] ) : Asymbols . append ( [ Symbol ( 'gconst%d_%d' % ( i , j ) ) for j in range ( A . shape [ 1 ] ) ] ) solution = None for eqindices in combinations ( range ( len ( leftsideeqs ) ) , len ( allmonomsleft ) ) : for i , index in enumerate ( eqindices ) : for k in range ( len ( allmonomsleft ) ) : A [ i , k ] = systemcoeffs [ index ] [ 2 ] [ k ] det = self . det_bareis ( A , * self . pvars ) if det == S . Zero : continue solution = AST . SolverMatrixInverse ( A = A , Asymbols = Asymbols ) self . usinglapack = True solution . checkforzeros = [ self . removecommonexprs ( det , onlygcd = False , onlynumbers = True ) ] Aadj = A . adjugate ( ) # too big to be useful for now, but can be used to see if any symbols are always 0 break if solution is None : raise self . CannotSolveError ( 'failed to find %d linearly independent equations' % len ( allmonomsleft ) ) reducedeqs = [ ] for i in range ( len ( allmonomsleft ) ) : var = S . One for k , kpower in enumerate ( allmonomsleft [ i ] ) : if kpower != 0 : var *= leftsideeqs [ 0 ] . symbols [ k ] ** kpower pright = S . Zero for k in range ( len ( allmonomsleft ) ) : if Aadj [ i , k ] != S . Zero : pright += Asymbols [ i ] [ k ] * ( rightsideeqs [ eqindices [ k ] ] . as_basic ( ) - leftsideeqs [ eqindices [ k ] ] . coeff ( ) ) reducedeqs . append ( [ var , pright . expand ( ) ] ) othereqindices = set ( range ( len ( leftsideeqs ) ) ) . difference ( set ( eqindices ) ) for i in othereqindices : # have to multiply just the constant by the determinant neweq = rightsideeqs [ i ] . as_basic ( ) for c , m in leftsideeqs [ i ] . iter_terms ( ) : if __builtin__ . sum ( m ) > 0 : neweq -= c * reducedeqs [ allmonomsleft . index ( m ) ] [ 1 ] else : neweq -= c reducedeqs . append ( [ S . Zero , neweq ] ) return reducedeqs , [ solution ]