Hi Luis, Thank you for both the code and the explanation! I was aware of the limitations of diffover for this (including its name - just "diff" caused clashes with Text::Diff in a downstream module). I'm proposing to rename "diffover" to "numdiff" (for numerical differentiation) in the next version of PDL, any objections?
I considered using diff2 in my quickly-written prototype, but chose diffover so as not to shrink the dim by 1. I am also keen to capture the 3 functions you've made, probably with "partial" going into Ufunc, and gaining an options param that has "difference" of either "forward", "backward", or "centred", or "shrink" using diff2. "div" and "curl" would then "just" be examples in the docs. Does anyone see any difficulties with this? Boyd, I don't feel this would clash with an Advent posting in any important way, but I'm very pleased to hear your thoughts if you disagree :-) Best regards, Ed ________________________________ From: Luis Mochan <moc...@icf.unam.mx> Sent: 20 November 2024 6:05 PM To: Boyd Duffee <boyd.duf...@gmail.com> Cc: pdl-devel <pdl-devel@lists.sourceforge.net>; perldl <pdl-gene...@lists.sourceforge.net> Subject: Re: [Pdl-devel] curl of vector Hi Boyd, About higher dimensions, if you have a vector field with coordinates V_i in D dimensions, you can build a matrix of derivatives M_ij= partial_i V_j where partial_j denotes the partial derivative with respect to the j-th coordinate. You can then take twice its antisymmetric part A_ij=partial_i V_j-partial_j V_i such that A_ij=-A_ji. In one dimension, A_xx=0 In two dimensions A_xx=A_yy=0 and Axy=-Ayx, so there is only one independent component, i.e., the curl in 2D is a scalar, the only independent component of a 2x2 antysymmetric matrix. In three dimensions all diagonal terms are zero and the three above the diagonal equal minus the three below the diagonal, thus there are three independent components, as in a vector. Acually, the curl would be C=(A_yz, Azx, Axy)=(-A_zy, -A_xz, -A_yx). Thus you may think of the curl as the elements of an antisymmetric matrix or as the elements of a vector. In four dimensions A would have 4x4=16 components, the four along the diagonal are zero and the six above the diagonal are minus the six below, so there are 6 independent components. Thus, one cannot identify the curl with a 4 dimensional vector, nor a 4x4 general matrix, but it may still be identified with the antisymmetric matrix. In general, the antisymmetric part of the matrix of derivatives may be identified with the curl in an arbitrary number D of dimensions, but only in 3D can it be identified with a vector and in 2D with a scalar. (Actually, pseudo vector and pseudo scalar). The case of 4D is frequently used in relativistic calculations. For example, the electromagnetic field is the 4D curl of the electromagnetic potentials. Regards, Luis On Wed, Nov 20, 2024 at 05:22:10PM +0000, Boyd Duffee wrote: > Hi Ingo, > > I'm quite interested in what your use case is. It sounds like a great > Advent Calendar entry (simple question, complete answer) if I could base it > on an actual need. People don't get excited about "an exercise" as much as > they do about someone trying to solve a problem, whether it's circulation > in a fluid or some EM field calculation. As any that I could think of would > be rather artificial, can you tell us yours? > > Knowing the use case would also help with giving it a name in case someone > decides that their Christmas project is writing a module that computes div, > grad, curl and whether to name it PDL::Fields or PDL::VectorCalc. > > (I'm now wondering if a curl over a 2D or 5D vector field makes any sense) > > cheers, > Boyd > > On Wed, 20 Nov 2024 at 10:18, Ed . <ej...@hotmail.com> wrote: > > > This (untested) should work, as it is a fairly direct translation of the > > formula: > > > > $pP = $vec->slice('(0)')->diffover; # no mv as x dim already bottom > > $px = $coords->slice('(0)')->diffover; > > $pQ = $vec->slice('(1)')->mv(1,0)->diffover->mv(0,1); > > $py = $coords->slice('(1)')->mv(1,0)->diffover->mv(0,1); > > $pR = $vec->slice('(2)')->mv(2,0)->diffover->mv(0,2); > > $pz = $coords->slice('(2)')->mv(2,0)->diffover->mv(0,2); > > > > $curl = $vec->zeroes; > > $curl->slice('(0)') .= $pR/$py - $pQ/$pz; > > $curl->slice('(1)') .= $pP/$pz - $pR/$px; > > $curl->slice('(2)') .= $pQ/$px - $pP/$py; > > > > The $curl could be a cat of those 3 expressions, with ->mv(-1,0); there's > > probably a clever way to make one copy of each ndarray and then do inplace > > operations with less mving, and the whole thing could become a PP > > operation, but let's see if this is conceptually correct first! > > > > Best regards, > > Ed > > > > ------------------------------ > > *From:* Ed . <ej...@hotmail.com> > > *Sent:* 20 November 2024 9:54 AM > > *To:* perldl <pdl-gene...@lists.sourceforge.net>; pdl-devel < > > pdl-devel@lists.sourceforge.net>; Ingo Schmid <ingo...@gmx.at> > > *Subject:* Re: [Pdl-devel] curl of vector > > > > Hi Ingo, > > > > I'm not aware of any, but I had a quick google to find the formula (I know > > vaguely what divergence and curl are having watched a 3blue1brown video > > about it some time ago). I couldn't find any implementations in Python or > > Fortran. > > > > This ( > > https://openstax.org/books/calculus-volume-3/pages/6-5-divergence-and-curl > > formula > > 6.17) indicates the formula for curl is: > > > > If F=⟨P,Q,R⟩ is a vector field in R3, and Px, Py, Pz, Qy, Qx, Qz, Rz, > > Rx, and Ry all exist, then the curl of F is defined by > > > > curl F = (Ry−Qz)i + (Pz−Rx)j + (Qx−Py)k > > = (∂R/∂y − ∂Q/∂z)i + (∂P/∂z − ∂R/∂x)j + (∂Q/∂x − ∂P/∂y)k > > > > This suggests to me that for a 3D problem, you need a coordinates ndarray > > dim (3,x,y,z) and a vector field ndarray with the same dims. You can do the > > partial differentiation numerically by some mv-ing and then diffover ( > > https://metacpan.org/pod/PDL::Ufunc#diffover), then the notional i,j,k > > above obviously indicate the final components of curl vector at each point. > > More to follow after I've figured out how to do the partial stuff! > > > > Best regards, > > Ed > > > > ------------------------------ > > *From:* Ingo Schmid via pdl-devel <pdl-devel@lists.sourceforge.net> > > *Sent:* 19 November 2024 6:06 PM > > *To:* perldl <pdl-gene...@lists.sourceforge.net>; pdl-devel < > > pdl-devel@lists.sourceforge.net> > > *Subject:* [Pdl-devel] curl of vector > > > > > > Hi, > > > > is there any implementations of calculating the curl of a vector field > > around? > > > > Best wishes > > > > Ingo > > _______________________________________________ > > pdl-devel mailing list > > pdl-devel@lists.sourceforge.net > > https://lists.sourceforge.net/lists/listinfo/pdl-devel > > > _______________________________________________ > pdl-devel mailing list > pdl-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/pdl-devel -- o W. Luis Mochán, | tel:(52)(777)329-1734 /<(*) Instituto de Ciencias Físicas, UNAM | fax:(52)(777)317-5388 `>/ /\ Av. Universidad s/n CP 62210 | (*)/\/ \ Cuernavaca, Morelos, México | moc...@fis.unam.mx /\_/\__/ GPG: 791EB9EB, C949 3F81 6D9B 1191 9A16 C2DF 5F0A C52B 791E B9EB _______________________________________________ pdl-devel mailing list pdl-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pdl-devel
_______________________________________________ pdl-devel mailing list pdl-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pdl-devel