Source code for sympy.physics.vector.dyadic

[docs]classDyadic(object):"""A Dyadic object. See: http://en.wikipedia.org/wiki/Dyadic_tensor Kane, T., Levinson, D. Dynamics Theory and Applications. 1985 McGraw-Hill A more powerful way to represent a rigid body's inertia. While it is more complex, by choosing Dyadic components to be in body fixed basis vectors, the resulting matrix is equivalent to the inertia tensor. """def__init__(self,inlist):""" Just like Vector's init, you shouldn't call this unless creating a zero dyadic. zd = Dyadic(0) Stores a Dyadic as a list of lists; the inner list has the measure number and the two unit vectors; the outerlist holds each unique unit vector pair. """self.args=[]ifinlist==0:inlist=[]whilelen(inlist)!=0:added=0fori,vinenumerate(self.args):if((str(inlist[0][1])==str(self.args[i][1]))and(str(inlist[0][2])==str(self.args[i][2]))):self.args[i]=(self.args[i][0]+inlist[0][0],inlist[0][1],inlist[0][2])inlist.remove(inlist[0])added=1breakifadded!=1:self.args.append(inlist[0])inlist.remove(inlist[0])i=0# This code is to remove empty parts from the listwhilei<len(self.args):if((self.args[i][0]==0)|(self.args[i][1]==0)|(self.args[i][2]==0)):self.args.remove(self.args[i])i-=1i+=1def__add__(self,other):"""The add operator for Dyadic. """other=_check_dyadic(other)returnDyadic(self.args+other.args)def__and__(self,other):"""The inner product operator for a Dyadic and a Dyadic or Vector. Parameters ========== other : Dyadic or Vector The other Dyadic or Vector to take the inner product with Examples ======== >>> from sympy.physics.vector import ReferenceFrame, outer >>> N = ReferenceFrame('N') >>> D1 = outer(N.x, N.y) >>> D2 = outer(N.y, N.y) >>> D1.dot(D2) (N.x|N.y) >>> D1.dot(N.y) N.x """fromsympy.physics.vector.vectorimportVector,_check_vectorifisinstance(other,Dyadic):other=_check_dyadic(other)ol=Dyadic(0)fori,vinenumerate(self.args):fori2,v2inenumerate(other.args):ol+=v[0]*v2[0]*(v[2]&v2[1])*(v[1]|v2[2])else:other=_check_vector(other)ol=Vector(0)fori,vinenumerate(self.args):ol+=v[0]*v[1]*(v[2]&other)returnoldef__div__(self,other):"""Divides the Dyadic by a sympifyable expression. """returnself.__mul__(1/other)__truediv__=__div__def__eq__(self,other):"""Tests for equality. Is currently weak; needs stronger comparison testing """ifother==0:other=Dyadic(0)other=_check_dyadic(other)if(self.args==[])and(other.args==[]):returnTrueelif(self.args==[])or(other.args==[]):returnFalsereturnset(self.args)==set(other.args)def__mul__(self,other):"""Multiplies the Dyadic by a sympifyable expression. Parameters ========== other : Sympafiable The scalar to multiply this Dyadic with Examples ======== >>> from sympy.physics.vector import ReferenceFrame, outer >>> N = ReferenceFrame('N') >>> d = outer(N.x, N.x) >>> 5 * d 5*(N.x|N.x) """newlist=[vforvinself.args]fori,vinenumerate(newlist):newlist[i]=(sympify(other)*newlist[i][0],newlist[i][1],newlist[i][2])returnDyadic(newlist)def__ne__(self,other):returnnotself.__eq__(other)def__neg__(self):returnself*-1def_latex(self,printer=None):ar=self.args# just to shorten thingsiflen(ar)==0:returnstr(0)ol=[]# output list, to be concatenated to a stringmlp=VectorLatexPrinter()fori,vinenumerate(ar):# if the coef of the dyadic is 1, we skip the 1ifar[i][0]==1:ol.append(' + '+mlp.doprint(ar[i][1])+r"\otimes "+mlp.doprint(ar[i][2]))# if the coef of the dyadic is -1, we skip the 1elifar[i][0]==-1:ol.append(' - '+mlp.doprint(ar[i][1])+r"\otimes "+mlp.doprint(ar[i][2]))# If the coefficient of the dyadic is not 1 or -1,# we might wrap it in parentheses, for readability.elifar[i][0]!=0:arg_str=mlp.doprint(ar[i][0])ifisinstance(ar[i][0],Add):arg_str='(%s)'%arg_strifarg_str.startswith('-'):arg_str=arg_str[1:]str_start=' - 'else:str_start=' + 'ol.append(str_start+arg_str+mlp.doprint(ar[i][1])+r"\otimes "+mlp.doprint(ar[i][2]))outstr=''.join(ol)ifoutstr.startswith(' + '):outstr=outstr[3:]elifoutstr.startswith(' '):outstr=outstr[1:]returnoutstrdef_pretty(self,printer=None):e=selfclassFake(object):baseline=0defrender(self,*args,**kwargs):ar=e.args# just to shorten thingssettings=printer._settingsifprinterelse{}ifprinter:use_unicode=printer._use_unicodeelse:fromsympy.printing.pretty.pretty_symbologyimport(pretty_use_unicode)use_unicode=pretty_use_unicode()mpp=printerifprinterelseVectorPrettyPrinter(settings)iflen(ar)==0:returnunicode(0)bar=u("\N{CIRCLED TIMES}")ifuse_unicodeelse"|"ol=[]# output list, to be concatenated to a stringfori,vinenumerate(ar):# if the coef of the dyadic is 1, we skip the 1ifar[i][0]==1:ol.extend([u(" + "),mpp.doprint(ar[i][1]),bar,mpp.doprint(ar[i][2])])# if the coef of the dyadic is -1, we skip the 1elifar[i][0]==-1:ol.extend([u(" - "),mpp.doprint(ar[i][1]),bar,mpp.doprint(ar[i][2])])# If the coefficient of the dyadic is not 1 or -1,# we might wrap it in parentheses, for readability.elifar[i][0]!=0:ifisinstance(ar[i][0],Add):arg_str=mpp._print(ar[i][0]).parens()[0]else:arg_str=mpp.doprint(ar[i][0])ifarg_str.startswith(u("-")):arg_str=arg_str[1:]str_start=u(" - ")else:str_start=u(" + ")ol.extend([str_start,arg_str,u(" "),mpp.doprint(ar[i][1]),bar,mpp.doprint(ar[i][2])])outstr=u("").join(ol)ifoutstr.startswith(u(" + ")):outstr=outstr[3:]elifoutstr.startswith(" "):outstr=outstr[1:]returnoutstrreturnFake()def__rand__(self,other):"""The inner product operator for a Vector or Dyadic, and a Dyadic This is for: Vector dot Dyadic Parameters ========== other : Vector The vector we are dotting with Examples ======== >>> from sympy.physics.vector import ReferenceFrame, dot, outer >>> N = ReferenceFrame('N') >>> d = outer(N.x, N.x) >>> dot(N.x, d) N.x """fromsympy.physics.vector.vectorimportVector,_check_vectorother=_check_vector(other)ol=Vector(0)fori,vinenumerate(self.args):ol+=v[0]*v[2]*(v[1]&other)returnoldef__rsub__(self,other):return(-1*self)+otherdef__rxor__(self,other):"""For a cross product in the form: Vector x Dyadic Parameters ========== other : Vector The Vector that we are crossing this Dyadic with Examples ======== >>> from sympy.physics.vector import ReferenceFrame, outer, cross >>> N = ReferenceFrame('N') >>> d = outer(N.x, N.x) >>> cross(N.y, d) - (N.z|N.x) """fromsympy.physics.vector.vectorimport_check_vectorother=_check_vector(other)ol=Dyadic(0)fori,vinenumerate(self.args):ol+=v[0]*((other^v[1])|v[2])returnoldef__str__(self,printer=None):"""Printing method. """ar=self.args# just to shorten thingsiflen(ar)==0:returnstr(0)ol=[]# output list, to be concatenated to a stringfori,vinenumerate(ar):# if the coef of the dyadic is 1, we skip the 1ifar[i][0]==1:ol.append(' + ('+str(ar[i][1])+'|'+str(ar[i][2])+')')# if the coef of the dyadic is -1, we skip the 1elifar[i][0]==-1:ol.append(' - ('+str(ar[i][1])+'|'+str(ar[i][2])+')')# If the coefficient of the dyadic is not 1 or -1,# we might wrap it in parentheses, for readability.elifar[i][0]!=0:arg_str=VectorStrPrinter().doprint(ar[i][0])ifisinstance(ar[i][0],Add):arg_str="(%s)"%arg_strifarg_str[0]=='-':arg_str=arg_str[1:]str_start=' - 'else:str_start=' + 'ol.append(str_start+arg_str+'*('+str(ar[i][1])+'|'+str(ar[i][2])+')')outstr=''.join(ol)ifoutstr.startswith(' + '):outstr=outstr[3:]elifoutstr.startswith(' '):outstr=outstr[1:]returnoutstrdef__sub__(self,other):"""The subtraction operator. """returnself.__add__(other*-1)def__xor__(self,other):"""For a cross product in the form: Dyadic x Vector. Parameters ========== other : Vector The Vector that we are crossing this Dyadic with Examples ======== >>> from sympy.physics.vector import ReferenceFrame, outer, cross >>> N = ReferenceFrame('N') >>> d = outer(N.x, N.x) >>> cross(d, N.y) (N.x|N.z) """fromsympy.physics.vector.vectorimport_check_vectorother=_check_vector(other)ol=Dyadic(0)fori,vinenumerate(self.args):ol+=v[0]*(v[1]|(v[2]^other))returnol_sympystr=__str___sympyrepr=_sympystr__repr__=__str____radd__=__add____rmul__=__mul__

[docs]defexpress(self,frame1,frame2=None):"""Expresses this Dyadic in alternate frame(s) The first frame is the list side expression, the second frame is the right side; if Dyadic is in form A.x|B.y, you can express it in two different frames. If no second frame is given, the Dyadic is expressed in only one frame. Calls the global express function Parameters ========== frame1 : ReferenceFrame The frame to express the left side of the Dyadic in frame2 : ReferenceFrame If provided, the frame to express the right side of the Dyadic in Examples ======== >>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols >>> N = ReferenceFrame('N') >>> q = dynamicsymbols('q') >>> B = N.orientnew('B', 'Axis', [q, N.z]) >>> d = outer(N.x, N.x) >>> d.express(B, N) cos(q)*(B.x|N.x) - sin(q)*(B.y|N.x) """fromsympy.physics.vector.functionsimportexpressreturnexpress(self,frame1,frame2)