#3021: add curl and divergence functions to symbolic vectors
-------------------------+--------------------------------------------------
 Reporter:  jason        |       Owner:  was       
     Type:  enhancement  |      Status:  new       
 Priority:  major        |   Milestone:  sage-4.0.2
Component:  calculus     |    Keywords:            
 Reviewer:               |      Author:            
   Merged:               |  
-------------------------+--------------------------------------------------

Comment(by ncalexan):

 {{{
 # a possible implementation of div, for irc user hedgehog

 var('x, y, z')
 (x, y, z)
 f1 = x^2 + 2*y; f2 = x^3 + sin(z); f3 = y*z + 2
 F = vector([f1, f2, f3])

 print F(x=0, y=2, z=3)

 def _variables(F):
     # this is a little funky -- we're finding all the variables that occur
     # in the components of F, and somehow choosing an ordering.  There are
     # other (better ways) but I'm not sure what the correct interface is.
     # For now, the user can specify the variables if they choose, just
     # like the gradient method.
     variables = list(set(flatten([ list(f.variables()) for f in F ])))
     variables.sort()
     return variables

 def div(F, variables=None):
     assert len(F) == 3
     if variables is None:
         variables = _variables(F)

     s = 0
     for i in range(len(F)):
         s += F[i].derivative(variables[i])
     return s

 print F
 print div(F)
 print div(F, variables=(y, x, z))

 def curl(F, variables=None):
     assert len(F) == 3
     if variables is None:
         variables = _variables(F)
     assert len(variables) == 3
     x, y, z = variables
     Fx, Fy, Fz = F
     i = Fz.derivative(y) - Fy.derivative(z)
     j = Fz.derivative(z) - Fx.derivative(x)
     k = Fy.derivative(x) - Fz.derivative(y)
     return vector([i, j, k])

 print curl(F)
 print curl(F, variables=(y, x, z))

 # let's assert that div(curl) == 0
 # we need the variables because the ordering is suspect otherwise: for me,
 # sage: _variables(F)
 # [x, y, z]
 # sage: _variables(curl(F))
 # [z, x, y]
 assert div(curl(F, variables=(x, y, z)), variables=(x, y, z)) == 0
 }}}

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/3021#comment:2>
Sage <http://sagemath.org/>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sage-trac" 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/sage-trac?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to