Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add curl and divergence functions to vectors #3021

Closed
jasongrout opened this issue Apr 25, 2008 · 31 comments
Closed

add curl and divergence functions to vectors #3021

jasongrout opened this issue Apr 25, 2008 · 31 comments

Comments

@jasongrout
Copy link
Member

Make curl work if the vector has 2 or 3 components. Is there a higher-dimensional analogue for curl?

CC: @eviatarbach

Component: calculus

Author: Robert Bradshaw

Branch/Commit: 7b49240

Reviewer: Eviatar Bach, Samuel Lelièvre, Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/3021

@jasongrout
Copy link
Member Author

comment:1

(After some discussion on IRC) It seems that Sage is really lacking in the differential geometry area. If some good work is done there, this request will likely automatically be satisfied.

A short term solution would be to write a vector field class.

@ncalexan
Copy link
Mannequin

ncalexan mannequin commented Jun 14, 2009

comment:2
# 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

@jasongrout
Copy link
Member Author

comment:3

See #5506 for a collection of things to add to a symbolic vectors class

@robertwb
Copy link
Contributor

comment:4

There is a 7-dim curl. Why doesn't the generic vector one work?

@jasongrout
Copy link
Member Author

comment:5

What generic curl (I don't think it's in Sage right now)? Or are you saying we should just add curl to generic vectors? I agree; no reason to add this to just the callable symbolic vectors class (what was I thinking?).

@jasongrout jasongrout changed the title add curl and divergence functions to symbolic vectors add curl and divergence functions to vectors May 11, 2010
@robert-marik
Copy link
Mannequin

robert-marik mannequin commented Feb 19, 2012

comment:7

Also

 j = Fx.derivative(z) - Fz.derivative(x)

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@robertwb
Copy link
Contributor

robertwb commented May 1, 2014

Branch: u/robertwb/ticket/3021

@robertwb
Copy link
Contributor

robertwb commented May 1, 2014

New commits:

4e34d0dAdd divergence and curl to vectors.

@robertwb
Copy link
Contributor

robertwb commented May 1, 2014

Commit: 4e34d0d

@eviatarbach
Copy link

comment:13

I was just going to work on this today!

I haven't tested the code, but looks good. Just a few things:

  1. Is there any reason why you're converting the variables to their string representations in ._variables()?
  2. The parameter in Expression.gradient is called variables; I think it would be good to switch vars to variables for consistency.
  3. I think there should be test(s) for .curl() with the variables parameter.
  4. I believe the raise TypeError, "curl only defined for 3 dimensions" syntax is deprecated, and that the string should be passed as an argument.

@slel
Copy link
Member

slel commented May 1, 2014

comment:14

Nitpicking on the error message in div: I would change it from "variable list must be equal to the dimension of self" to "number of variables must equal dimension of self".

About point 4 in eviatarbach's comment:

  • A reference for the change in exception raising syntax is PEP-3109.
  • the ValueError in div on follows the new syntax. By contrast, both the TypeError and ValueError in curl follow the old syntax.

@slel
Copy link
Member

slel commented May 1, 2014

comment:15

More nitpicking:

  • Use "Return" instead of "Returns" in docstrings, see PEP 0257:

    The docstring is a phrase ending in a period. It prescribes the
    function or method's effect as a command ("Do this", "Return that"),
    not as a description; e.g. don't write "Returns the pathname ...".

  • Should there be a docstring and tests for the _variables function?

  • In examples for div, remove extra space before w in R.<x,y,z, w> = QQ[]

@eviatarbach
Copy link

comment:16

Another thought:

The extracting of variables from each element and then sorting by name looks scary, and can give unexpected results (if your vector is (x1, x10, x2), I believe you would get 3 for the divergence instead of 1 as you might expect, since '10' comes before '2' in the alphanumeric sort). Unless there's a better solution, I think we should have it so that the variables list has to always be given explicitly.

@jasongrout
Copy link
Member Author

comment:17

If your vectors are callable symbolic vectors, you should be able to get the list of arguments from the base ring (since you've already explicitly given an order to the variables):

sage: f(x,y,z)=(x*y,y*z,z^2)
sage: f
(x, y, z) |--> (x*y, y*z, z^2)
sage: type(f)
<class 'sage.modules.vector_callable_symbolic_dense.Vector_callable_symbolic_dense'>
sage: f.base_ring().arguments()
(x, y, z)

@eviatarbach
Copy link

comment:18

That's a great idea. I think we should have that for callable vectors.

For the record, Mathematica and Maple both have default coordinate systems, which is how they deal with this issue.

@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 18, 2014

Changed commit from 4e34d0d to f353c94

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 18, 2014

Branch pushed to git repo; I updated commit sha1. New commits:

f353c94Missing doctest.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 20, 2014

Changed commit from f353c94 to 8ab298e

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jun 20, 2014

Branch pushed to git repo; I updated commit sha1. New commits:

ae901d1Merge tag '6.2' into div-curl
8ab298eAddress reviewer comments.

@robertwb
Copy link
Contributor

comment:22

I've addressed all the comments.

@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@kcrisman
Copy link
Member

comment:24

I was going to look at this, but looks like Eviatar and Samuel are on top, so just think of this as a ping :)

@robertwb
Copy link
Contributor

comment:25

Replying to @kcrisman:

I was going to look at this, but looks like Eviatar and Samuel are on top, so just think of this as a ping :)

Feel free to look at this yourself :)

@tscrim
Copy link
Collaborator

tscrim commented Oct 22, 2014

comment:26

My 2 cents, I'd have curl also take 2-dim inputs and return a vector. It might also be a good idea for a way (likely another method) which takes a 2-dim input and return a scalar as a shorthand (a la Green's theorem).

+1 to getting these (fundamental) methods into Sage.

@robertwb
Copy link
Contributor

comment:27

Are there any enhancements to this with-patch, 7-year-old ticket that simply can't be deferred 'till later so we can finally get this in?

@tscrim
Copy link
Collaborator

tscrim commented Feb 11, 2015

Changed branch from u/robertwb/ticket/3021 to u/tscrim/curl_divergence-3021

@tscrim
Copy link
Collaborator

tscrim commented Feb 11, 2015

Changed commit from 8ab298e to 7b49240

@tscrim
Copy link
Collaborator

tscrim commented Feb 11, 2015

Author: Robert Bradshaw

@tscrim
Copy link
Collaborator

tscrim commented Feb 11, 2015

comment:28

I added the 2-dim input. If you're happy with my changes, then I think we can set this to a positive review.


New commits:

0de71acMerge branch 'u/robertwb/ticket/3021' of trac.sagemath.org:sage into u/tscrim/curl_divergence-3021
7b49240Added 2-dim curl which returns a scalar value.

@tscrim
Copy link
Collaborator

tscrim commented Feb 11, 2015

Reviewer: Eviatar Bach, Samuel Lelièvre, Travis Scrimshaw

@vbraun
Copy link
Member

vbraun commented Feb 17, 2015

Changed branch from u/tscrim/curl_divergence-3021 to 7b49240

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants