On 02/12/2012 11:25 AM, Matthew Rocklin wrote:
This can be done using Matrix Exprs. Really, Mul is the multilinear function you're looking for.

In [1]: x1,x2,x3 = [MatrixSymbol('x_%d'%i, n, 1) for i in [1,2,3]]

In [2]: y1,y2,y3 = [MatrixSymbol('y_%d'%i, 1, n) for i in [1,2,3]]

In [3]: a1,a2,a3 = symbols('a1,a2,a3')

In [4]: b1,b2,b3 = symbols('b1,b2,b3')

In [5]: (b1*y1+b2*y2+b3*y3) * (a1*x1+a2*x2+a3*x3)
Out[5]: (b₁⋅y₁ + b₃⋅y₃ + b₂⋅y₂)⋅(a₃⋅x₃ + a₂⋅x₂ + a₁⋅x₁)

In [6]: matrixify(expand((b1*y1+b2*y2+b3*y3) * (a1*x1+a2*x2+a3*x3)))
Out[6]:
a₁⋅b₂⋅y₂⋅x₁ + a₃⋅b₁⋅y₁⋅x₃ + a₂⋅b₃⋅y₃⋅x₂ + a₂⋅b₂⋅y₂⋅x₂ + a₂⋅b₁⋅y₁⋅x₂ + a₁⋅b₃⋅y₃
⋅x₁ + a₃⋅b₂⋅y₂⋅x₃ + a₁⋅b₁⋅y₁⋅x₁ + a₃⋅b₃⋅y₃⋅x₃

Notice that there are nine terms in the MatrixAdd. All combinations are accounted for. On Sun, Feb 12, 2012 at 8:04 AM, Alan Bromborsky <[email protected] <mailto:[email protected]>> wrote:

    Is there a way of defining a multilinear function in sympy.
     Consider the following -

    Assume we have a vector class and the function dot(x,y) is defined
    when when x and y are vectors and that vectors are inherited form
    non-commutating symbols.  If x1,x2,y1, and y2 are vectors and
    a1,a2,b1, and b2 are commutating symbols we wish to have the behavior:

       dot(a1*x1+a2*x2,b1*y1+b2*y2) =
    a1*b1*dot(x1,y1)+a1*b2*dot(x1,y2)+a2*b1*dot(x2,y1)+a2*b2*dot(x2,y2)


-- You received this message because you are subscribed to the Google
    Groups "sympy" group.
    To post to this group, send email to [email protected]
    <mailto:[email protected]>.
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:sympy%[email protected]>.
    For more options, visit this group at
    http://groups.google.com/group/sympy?hl=en.


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
I was looking for something where you did not have to specify how many vectors were in the sum and came up with this -

from sympy import *

class vector(Expr):

    is_commutative = False

    def __new__(cls, label, shape=None, **kw_args):
        label = sympify(label)
        obj = Expr.__new__(cls, label, **kw_args)
        obj.label = label
        return obj

    @staticmethod
    def dot(v1,v2):
        v_lst = [str(v1.label),str(v2.label)]
        v_lst.sort()
        return(Symbol(v_lst[0]+v_lst[1]))

    def _sympystr(self, p):
        return p.doprint(self.label)

def dot(x,y):
    x = expand(x)
    y = expand(y)
    #print 'x =',x,x._args,type(x),' y =',y,y._args,type(y)
    if isinstance(x,vector) and isinstance(y,vector):
        return(vector.dot(x,y))
    if isinstance(x,Mul) and isinstance(y,Mul):
        return(x._args[0]*y._args[0]*dot(x._args[1],y._args[1]))
    if isinstance(x,Mul) and isinstance(y,vector):
        return(x._args[0]*dot(x._args[1],y))
    if isinstance(x,vector) and isinstance(y,Mul):
        return(y._args[0]*dot(x,y._args[1]))
    if isinstance(x,(vector,Mul)) and isinstance(y,Add):
        xy = 0
        if isinstance(x,Mul):
            s = x._args[0]
            x = x._args[1]
        else:
            s = 1
        for yarg in y._args:
            xy += dot(x,yarg)
        return(s*xy)
    if isinstance(x,Add) and isinstance(y,(vector,Mul)):
        xy = 0
        if isinstance(y,Mul):
            s = y._args[0]
            y = y._args[1]
        else:
            s = 1
        for xarg in x._args:
            xy += dot(xarg,y)
        return(s*xy)
    if isinstance(x,Add) and isinstance(y,Add):
        xy = 0
        for xarg in x._args:
            for yarg in y._args:
                xy += dot(xarg,yarg)
        return(xy)


v1 = vector('v1')
v2 = vector('v2')
v = v1*2-3*v2
print dot(4*v,4*v1)
print dot(v,v+v1)

Which gives -

32*v1v1 - 48*v1v2
6*v1v1 - 15*v1v2 + 9*v2v2

Now the question is can generate a general mutilinear function class to act as a template for specific multilinear functions? That is one where if f(v1,...,v2) is defined for a all v's in the same class it will be defined for all arguments that are linear combinations of the v's.


--
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to