ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 17 Jul 2020 17:54:10 +0200Computing gradients of vector fields?https://ask.sagemath.org/question/52529/computing-gradients-of-vector-fields/Premise, I'm not working with special/general relativity, so it may be that the way the formalism is thought in SageMath does not fit perfectly my needs.
I'm trying to define vector and tensor fields in Euclidean space and then differentiate them. For example, I'm interested in calculating the gradient of a vector field, namely, the tensor field
$$
T_{ij} = \frac{dv_i}{dx_j}
$$
. However classes like `EuclideanSpace` only allow me to produce vector fields on which I can compute divergence and curl. Should I just create three scalar fields, one for each component, and go with that? Or are there ways to get `nabla` out as a vector and use it directly to operate on actual vectors and tensors (in which case I could use the tensor product, $T = \nabla \otimes v$)?Okarin86Fri, 17 Jul 2020 17:54:10 +0200https://ask.sagemath.org/question/52529/Lie bracket of derivations over polynomial ringhttps://ask.sagemath.org/question/49881/lie-bracket-of-derivations-over-polynomial-ring/I want to take the Lie bracket of derivations defined for an arbitrary polynomial ring. Using the notation for injecting variables into the global scope:
E.<x0,x1> = QQ[]
M = E.derivation_module()
f=(x1*M.gens()[0])
g=x0*M.gens()[1]
f.bracket(g)
gives `-x0*d/dx0 + x1*d/dx`. But I want to be able to construct vector fields programmatically for an arbitrary number of `x0, x1, x2, ..., xn` so I tried the following:
E = QQ[['x%i'%i for i in range(2)]]
E.inject_variables()
M = E.derivation_module()
f=(x1*M.gens()[0])
g=x0*M.gens()[1]
f.bracket(g)
which fails to take the Lie bracket with `TypeError: unable to convert x1 to a rational` (which causes another error `TypeError: Unable to coerce into background ring.`) ... which looks a bit like something is not right? or is this just not a permissible way to construct derivations in sagemath? or is the only way to do this using SageManifolds?
E = EuclideanSpace(2, coordinates='Cartesian', symbols='x0 x1')
U = E.default_chart()
f = U[2]*U.frame()[1]
g = U[1]*U.frame()[2]
f.bracket(g).display()
gives `-x0 e_x0 + x1 e_x1`
ericWed, 12 Feb 2020 05:58:22 +0100https://ask.sagemath.org/question/49881/Vector valued function: unable to convert to symbolic expressionhttps://ask.sagemath.org/question/47710/vector-valued-function-unable-to-convert-to-symbolic-expression/This has been giving me quite a headache. It seems that I am unable to create even a simple vector-valued function of multiple variables. For example, the following
```
F(s,t)=vector([1,s,s*t])
```
spits out a big traceback ending with
`TypeError: unable to convert (1, s, s*t) to a symbolic expression`.
What's going on here? I know I **can** use substitution instead, but what if I'd prefer a callable function?terrygarciaSun, 01 Sep 2019 19:22:03 +0200https://ask.sagemath.org/question/47710/3D Vector Field Doesn't have Arrowshttps://ask.sagemath.org/question/41898/3d-vector-field-doesnt-have-arrows/I have been trying to plot 3D vector fields, but I keep running into a problem. The graphic does not have arrows instead it is just lines.
Here is the code.
var('x,y,z')
p = plot_vector_field3d((x,y,z), (x, -5, 5), (y, -5, 5), (z, -5, 5))
p.show()
jacksevenSat, 07 Apr 2018 04:01:14 +0200https://ask.sagemath.org/question/41898/Derivation of a sagemanifold vector (possible bug)https://ask.sagemath.org/question/33951/derivation-of-a-sagemanifold-vector-possible-bug/ I'm using sage v7.1 with sagemanifold v0.9 to calculate Lie derivatives.
<h2>The problem</h2>
I define my manifold, with a chart and the vectors which define the symmetry,
M = Manifold(4, 'M', latex_name=r"\mathcal{M}")
X.<t,r,th,ph> = M.chart(r't r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi')
Lx = M.vector_field('Lx')
Lx[:] = [ 0, 0, -cos(ph), cot(th)*sin(ph) ]
Ly = M.vector_field('Ly')
Ly[:] = [ 0, 0, sin(ph), cot(th)*cos(ph) ]
Lz = M.vector_field('Lz')
Lz[:] = [ 0, 0, 0, 1]
Lt = M.vector_field('Lt')
Lt[:] = [ 1, 0, 0, 0]
However, if I call the derivative of a component of the vector like
diff( Lx[3], th )
I get a `TypeError`, because
type( Lx[3] )
<class 'sage.manifolds.coord_func_symb.CoordFunctionSymb'>
and it seems that the function `diff` only acts on SR expressions.
<h2>My bypass</h2>
Although I was able to bypass the situation, it would be nice if a solution was provided *out of the box*, but in case anyone else needs a solution:
I defined my vectors in a Sage way
xi0 = [ 1, 0, 0, 0]
xi1 = [ 0, 0, -cos(ph), cot(th)*sin(ph) ]
xi2 = [ 0, 0, sin(ph), cot(th)*cos(ph) ]
xi3 = [ 0, 0, 0, 1]
and then assigned them to the SageManifolds vector_field
Lx = M.vector_field('Lx')
Lx[:] = xi1
Ly = M.vector_field('Ly')
Ly[:] = xi2
Lz = M.vector_field('Lz')
Lz[:] = xi3
Lt = M.vector_field('Lt')
Lt[:] = xi0
So, when I need the derivative of a component of the vector, I calculate
diff(xi1[3], th)
Cheers.DoxTue, 28 Jun 2016 17:16:13 +0200https://ask.sagemath.org/question/33951/Symbolic vector fieldhttps://ask.sagemath.org/question/33138/symbolic-vector-field/ Is it possible to represent a partial differentiation without specifying what the derivative is acting upon? This would be handy in manipulating vector fields symbolically. For ex., suppose we have a vector field Y = y * d/dx and we'd like to compute the pushforward of Y by some diffeomorphism F. Can one express "d/dx" in the usual mathematical partial differential notation and in a way that Sage can handle?Richard_LTue, 19 Apr 2016 00:53:11 +0200https://ask.sagemath.org/question/33138/Restricting vector field to a subset of a rectangular regionhttps://ask.sagemath.org/question/29440/restricting-vector-field-to-a-subset-of-a-rectangular-region/ I'm trying to restrict a vector field plot to a specific region of the plane. I found the code for the plot_vector_field3d function online, and just modified that accordingly, but I'm having trouble with the 2d case. plot_vector_field always picks points in a rectangular region, but there's a part of the region I want to skip. Here's what I have so far.
x,y = var('x y')
C = circle((0,0),1)
# useful functions
def norm2((x,y)):
"""
Norm squared of `(x,y)`
"""
return x^2 + y^2
def norm((x,y)):
"""
Norm of `(x,y)`
"""
return norm2((x,y)).sqrt()
def dy(x,y):
r = norm((x,y))
theta = arctan(y/x)
dr = cos(theta)*(1-1/r^2)
dtheta = -sin(theta)*(1+1/r^2)
if r<= 1:
return 0
else:
return dr*cos(theta)-r*sin(theta)*dtheta
def dx(x,y):
r = norm((x,y))
theta = arctan(y/x)
dr = cos(theta)*(1-1/r^2)
dtheta = -sin(theta)*(1+1/r^2)
if r<= 1:
return 0
else:
return dr*sin(theta) + r*cos(theta)*dtheta
VF = plot_vector_field((dx(x,y),dy(x,y)),(x,-2,2),(y,-2,2))
show(VF + C,aspect_ratio=1)
I'm not able to upload an image file, but if you run this, I'm trying to get rid of all of the vectors within the circle.
For comparison, here's the working code in 3d. This generates a flow on a torus, with the vectors only coming from points on or in the torus, instead of the usual plot_vector_field3d, which puts the points on an entire cube. dx, dy, and dz are the functions that give the correct vector components.
twopi = 2 * RDF.pi()
DT = [] # discretized tori
n = 20 # points in the boundary
m = 5 #layers in the torus
#Discretizes T0
for k in xrange(m):
for i in xrange(n):
DT.append((2*cos(i*twopi/(n)), 2*sin(i*twopi/(n)),0))
for j in xrange(n/2):
x0 = (2 + cos(j*twopi/(n/2)))*cos(i*twopi/(n))
y0 = (2 + cos(j*twopi/(n/2)))*sin(i*twopi/(n))
z0 = ((k+1)/m)*sin(j*twopi/(n/2))
DT.append((x0,y0,z0))
points = [vector(DT[i]) for i in range(len(DT))]
vectors = [vector((dx(*point), dy(*point), dz(*point))) for point in points]
max_len = max(v.norm() for v in vectors)
scaled_vectors = [v/max_len for v in vectors]
VF = sum([plot(v,color=hue(v.norm())).translate(p) for v,p in zip(scaled_vectors,points)])
jford1906Mon, 14 Sep 2015 22:01:05 +0200https://ask.sagemath.org/question/29440/Unscaled arrows in a vector fieldhttps://ask.sagemath.org/question/27053/unscaled-arrows-in-a-vector-field/ I have a vector field I'm plotting, but the arrows are scaled so small near the singular points, it's unclear which are sinks, sources, and saddles. Is there a way to change the arrow scaling so they are all the same length and the behavior is clearer?
x,y = var('x,y')
VF = plot_vector_field(((x^2-1)*(y^2-1)*x*y, (x^2+(y-3/8)^2-1/64)*(x^2+(y+3/8)^2-1/64)), (x, -1,1), (y, -1,1))
points = point([(0,1/4),(0,1/2),(0,-1/4),(0,-1/2)],color='red', size=25)
VF + pointsjford1906Mon, 08 Jun 2015 19:36:17 +0200https://ask.sagemath.org/question/27053/plot surface plus vector fieldhttps://ask.sagemath.org/question/26056/plot-surface-plus-vector-field/My apologies for posting a (hopefully) simple question. I want to plot a surface with the gradient field in the plane underneath. I can create the surface and vector field separately, but I get an error when I try to show them on the same axes. It seems that the vector field is a different type. Does anyone know how to induce the vector field so it can be plotted as part of a 3d plot?
I'd like to do something like this:
f(x,y)=y/(1-x+y)
P=plot3d(f(x,y),(x,0,1),(y,0,1))
gradf=vector([y/(x-y-1)^2,(1-x)/(x-y-1)^2])
VF=plot_vector_field(gradf,(x,0,1),(y,0,1))
show(P+VF)
Everything but that last step works great, and then I get an error message:
NotImplementedError: 3D plotting not implemented for PlotField defined by a 20 x 20 vector grid
Can anyone suggest a workaround? Thanks for your help!c_williamsFri, 06 Mar 2015 22:45:30 +0100https://ask.sagemath.org/question/26056/Adding arrows in vector fieldshttps://ask.sagemath.org/question/25371/adding-arrows-in-vector-fields/ I'm using plot_vector_field3d, and I'm not getting any arrows at the ends of the vectors. The documentation seems to indicate this would be automatic. Is there something I'm missing?Jeff FordThu, 01 Jan 2015 19:01:48 +0100https://ask.sagemath.org/question/25371/