Gradient of a vector

90 views
Skip to first unread message

Alexander Lindsay

unread,
Apr 24, 2020, 2:44:15 PM4/24/20
to sympy
I've been using the capabilities in sympy.vector for generating MMS solutions for verifying FEM in MOOSE. I'm curious whether sympy supports calculation of the gradient of a vector field (e.g. with the result being a tensor)? It doesn't appear to me that it does, but I'm hoping I overlooked something. Below is the sample code I'd like to be able to execute and the corresponding backtrace. If the ability to take the gradient of a vector doesn't exist, I'm curious whether it would be difficult to add? If it's not too much anticipated work, I'd be willing to take a stab at it.

from sympy.vector import gradient, CoordSys3D
R = CoordSys3D('R')
u = R.x*R.y * (R.i + R.j)
gradient(u)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-49-c095794df196> in <module>
      5 u = R.x*R.y * (R.i + R.j)
      6
----> 7 gradient(u)

~/miniconda3/envs/moose-boost/lib/python3.7/site-packages/sympy/vector/operators.py in gradient(scalar_field, coord_sys, doit)
    317
    318         if doit:
--> 319             return (vx * i + vy * j + vz * k).doit()
    320         return vx * i + vy * j + vz * k
    321     else:

~/miniconda3/envs/moose-boost/lib/python3.7/site-packages/sympy/vector/basisdependent.py in doit(self, **hints)
    169         """Calls .doit() on each term in the Dyadic"""
    170         doit_components = [self.components[x].doit(**hints) * x
--> 171                            for x in self.components]
    172         return self._add_func(*doit_components)
    173

~/miniconda3/envs/moose-boost/lib/python3.7/site-packages/sympy/vector/basisdependent.py in <listcomp>(.0)
    169         """Calls .doit() on each term in the Dyadic"""
    170         doit_components = [self.components[x].doit(**hints) * x
--> 171                            for x in self.components]
    172         return self._add_func(*doit_components)
    173

~/miniconda3/envs/moose-boost/lib/python3.7/site-packages/sympy/core/decorators.py in __sympifyit_wrapper(a, b)
     89                 if not hasattr(b, '_op_priority'):
     90                     b = sympify(b, strict=True)
---> 91                 return func(a, b)
     92             except SympifyError:
     93                 return retval

~/miniconda3/envs/moose-boost/lib/python3.7/site-packages/sympy/core/decorators.py in binary_op_wrapper(self, other)
    127                     if f is not None:
    128                         return f(self)
--> 129             return func(self, other)
    130         return binary_op_wrapper
    131     return priority_decorator

~/miniconda3/envs/moose-boost/lib/python3.7/site-packages/sympy/vector/basisdependent.py in __mul__(self, other)
     34     @call_highest_priority('__rmul__')
     35     def __mul__(self, other):
---> 36         return self._mul_func(self, other)
     37
     38     @_sympifyit('other', NotImplemented)

~/miniconda3/envs/moose-boost/lib/python3.7/site-packages/sympy/vector/vector.py in __new__(cls, *args, **options)
    412
    413     def __new__(cls, *args, **options):
--> 414         obj = BasisDependentMul.__new__(cls, *args, **options)
    415         return obj
    416

~/miniconda3/envs/moose-boost/lib/python3.7/site-packages/sympy/vector/basisdependent.py in __new__(cls, *args, **options)
    257         # Make sure incompatible types weren't multiplied
    258         if count > 1:
--> 259             raise ValueError("Invalid multiplication")
    260         elif count == 0:
    261             return Mul(*args, **options)

ValueError: Invalid multiplication


Aaron Meurer

unread,
Apr 24, 2020, 2:46:56 PM4/24/20
to sympy
Hi.

I don't know the answer to your question, as I am not familiar enough
with the vector or tensor modules, but if you don't get an answer here
I would suggest opening an issue about this on the issue tracker.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/29c041d5-1bab-4d54-9bea-fe5ba0d4a02a%40googlegroups.com.

Alan Bromborsky

unread,
Apr 24, 2020, 3:28:06 PM4/24/20
to sy...@googlegroups.com

S.Y. Lee

unread,
Apr 25, 2020, 9:39:32 AM4/25/20
to sympy
SymPy defines gradient only for scalar valued function.
Do you mean that gradient of vector valued function should return other stuff like jacobian?

Nicolas Guarin

unread,
Apr 25, 2020, 5:06:00 PM4/25/20
to sympy
You could check a module that I wrote in Continuum Mechanics


It already has the gradient of a vector for orthogonal coordinates.

Alexander Lindsay

unread,
Apr 27, 2020, 4:09:25 PM4/27/20
to sy...@googlegroups.com
On Sat, Apr 25, 2020 at 6:39 AM S.Y. Lee <syle...@gmail.com> wrote:
SymPy defines gradient only for scalar valued function.
Do you mean that gradient of vector valued function should return other stuff like jacobian?

I would like the gradient of a vector valued function to return the Jacobian yes, or the transpose of the Jacobian, I don't really care. I know different people prefer different conventions.

Thanks Alan and Nicolas for sharing those packages; I will look into them. The ideal solution would be to do everything in sympy in order for us to minimize our number of package dependencies, but if sympy can't do this then we very well might use one of the packages you shared.

Aaron, I will go ahead and open a ticket, thanks.

Alex
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.

Aaron Meurer

unread,
Apr 27, 2020, 7:45:32 PM4/27/20
to sympy
SymPy does support taking derivatives with respect to matrices, via
diff(). Is this what you are looking for?

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CANFcJrGz6KF-JdcBfjRfT4O-dfmhsSmWTvf%3D%3DZO%3DP2ioQZ%3D9rw%40mail.gmail.com.

Alexander Lindsay

unread,
Apr 29, 2020, 6:20:05 PM4/29/20
to sy...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages