"""A few practical conventions common to all printers."""importrefromcollections.abcimportIterablefromsympy.core.functionimportDerivative_name_with_digits_p=re.compile(r'^([^\W\d_]+)(\d+)$',re.U)
[docs]defsplit_super_sub(text):"""Split a symbol name into a name, superscripts and subscripts The first part of the symbol name is considered to be its actual 'name', followed by super- and subscripts. Each superscript is preceded with a "^" character or by "__". Each subscript is preceded by a "_" character. The three return values are the actual name, a list with superscripts and a list with subscripts. Examples ======== >>> from sympy.printing.conventions import split_super_sub >>> split_super_sub('a_x^1') ('a', ['1'], ['x']) >>> split_super_sub('var_sub1__sup_sub2') ('var', ['sup'], ['sub1', 'sub2']) """ifnottext:returntext,[],[]pos=0name=Nonesupers=[]subs=[]whilepos<len(text):start=pos+1iftext[pos:pos+2]=="__":start+=1pos_hat=text.find("^",start)ifpos_hat<0:pos_hat=len(text)pos_usc=text.find("_",start)ifpos_usc<0:pos_usc=len(text)pos_next=min(pos_hat,pos_usc)part=text[pos:pos_next]pos=pos_nextifnameisNone:name=partelifpart.startswith("^"):supers.append(part[1:])elifpart.startswith("__"):supers.append(part[2:])elifpart.startswith("_"):subs.append(part[1:])else:raiseRuntimeError("This should never happen.")# Make a little exception when a name ends with digits, i.e. treat them# as a subscript too.m=_name_with_digits_p.match(name)ifm:name,sub=m.groups()subs.insert(0,sub)returnname,supers,subs
defrequires_partial(expr):"""Return whether a partial derivative symbol is required for printing This requires checking how many free variables there are, filtering out the ones that are integers. Some expressions do not have free variables. In that case, check its variable list explicitly to get the context of the expression. """ifisinstance(expr,Derivative):returnrequires_partial(expr.expr)ifnotisinstance(expr.free_symbols,Iterable):returnlen(set(expr.variables))>1returnsum(nots.is_integerforsinexpr.free_symbols)>1