ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 12 Feb 2019 18:54:13 -0600Mapping 2-dimensional matrix index to a listhttp://ask.sagemath.org/question/45436/mapping-2-dimensional-matrix-index-to-a-list/Hi all,
I have a following matrix:
mat = matrix([[1,5,7],[3,10,12],[0,5,3]])
[ 1 5 7]
[ 3 10 12]
[ 0 5 3]
I got 3 parts from the matrix:
A = mat[[0,1,2],[0]]
[1]
[3]
[0]
B = mat[[1,2],[1,2]]
[10 12]
[ 5 3]
C = mat[[0],[1,2]]
[5 7]
To know that these 3 parts are not overlapping to each other, I have an idea to map the original matrix to 1-dimension array as IDs for each cell:
tt = copy(mat)
row = mat.nrows()
col = mat.ncols()
for x in range(row):
for y in range(col):
tt[x,y] = x*col+y
sage: tt
[0 1 2]
[3 4 5]
[6 7 8]
Then again I go over all coordinate that I took for A, B and C to collect these mapped ID.
Regarding to A,
cells_A = []
for i in range(0, 3):
for j in range(0, 1):
cells_A.append(tt[i, j])
sage: cells_A
[0, 3, 6]
Regarding to B:
cells_B = []
for i in range(1, 3):
for j in range(1, 3):
cells_B.append(tt[i, j])
sage: cells_B
[4, 5, 7, 8]
Regarding to C, similarly we have:
sage: cells_C = [1,2]
If size of union from these 3 cells is equal to the total size of A, B, and C, then I conclude no overlapping among them.
area = sum([A.nrows()*A.ncols(), B.nrows()*B.ncols(), C.nrows()*C.ncols()])
if len(set.union(*[set(cells_A), set(cells_B), set(cells_C)])) == area:
print("No overlapping parts")
However, this way requires lots of work and **SLOW**. Is there any already SageMath's Matrix feature supporting some steps above, especially a way to improve/avoid mapping index?imnvshTue, 12 Feb 2019 18:54:13 -0600http://ask.sagemath.org/question/45436/Coordinate Transformshttp://ask.sagemath.org/question/9966/coordinate-transforms/Is there something in sage that does the same thing that `CoordinateTransform` and `TransformedField` in Mathematica 9 ?
The idea is that `CoordinateTransform` is given some coordinates, e.g. (r,th) and asked to transform them from "polar" to "cartesian", thus gives the expression of the cartesian coordinates in terms of the polar coordinates, e.g.
(x(r,th), y(r,th)) = (r*cos(th), r*sin(th))
Obvioulsly, it also works with other coordinates systems.
`TransformedField` makes the transformation between a scalar, vector, or tensor field in, say, cartesian coordinates, to spherical coordinates.
These actions are not very complicated, nor difficult to implement when needed, but they are also very common.
Thanks.
references :
* http://reference.wolfram.com/mathematica/ref/CoordinateTransform.html
* http://reference.wolfram.com/mathematica/ref/TransformedField.htmlConvenient TruthSun, 31 Mar 2013 13:04:28 -0500http://ask.sagemath.org/question/9966/Plotting families (depending on integer parameter(s)) of curvilinear coordinate and isolevel lines and surfaces, resp. for functional and parametric surfaces, and for 3-variate scalar fields and volume deformations in 3Dhttp://ask.sagemath.org/question/29836/plotting-families-depending-on-integer-parameters-of-curvilinear-coordinate-and-isolevel-lines-and-surfaces-resp-for-functional-and-parametric/To begin with a warning: I am a very experienced mathematician but a complete – 'greenhorn' – beginner in SAGE.
I would like to be able to plot in the same 3D plot FAMILIES OF PARAMETRIC CURVES IN 3D (x(i)=x(t,i),y=y(t,i),z=z(t,i)) plotted via e.g. parametric_plot3D DEPENDING ON A NON-NEGATIVE INTEGER PARAMETER i in range(I+1) where the coordinate functions x,y,z are defined as functions of t by me and are not necessarily predefined SAGE functions, and where I can vary the non-negative integer I without varying the code.
I would like to do the same for FAMILIES OF FUNCTIONAL SURFACES (z=f(x,y,i), DEPENDING ON A NON-NEGATIVE INTEGER PARAMETER i) using e.g. ParametricSurface, as well as TO DO THE SAME for FAMILIES OF PARAMETRIC SURFACES ( (fx(u,v,i),fy(u,v,i),fz(u,v,i)) ) via using e.g. parametric_plot3D DEPENDING ON A NON-NEGATIVE INTEGER PARAMETER i in range(I+1.
I would also like to be able to do the same for FAMILIES OF IMPLICITLY DEFINED SURFACES IN 3D DEPENDING ON A NON-NEGATIVE INTEGER PARAMETER i, using e.g. implicit_plot3d ( (f(x,y,z)-C(i)==0, I in range (I+1)), C being a 1-dimensional array (in general algorithmic language) with entries – real numbers in the range of the values of the real-valued f), with the possibility of custom definition of f, I and C.
Examining the SAGE tutorials and reference manuals, I have so far been able to find a model solution of this problem only for planar curves in 2D using plot() and only for SAGE-predefined curves (namely, polynomial curves):
http://sage.maa.org/home/pub/140/
sage: lotsa_plots = sum([plot(x^n,(x,0,1),color=rainbow(5)[n]) for n in range(5)])
Already in this simple 2D case, if the SAGE-predefined function x^n in the above example be replaced by a custom-defined function f(x,n) , it is not clear to me (a greenhorn!) how to extend the above instance to plotting together the 2D graphs of f(x,n), color=rainbow(N)[n]) for n in range(N), where one should be able to vary N withouth modifying the SAGE code.
I think that providing examples solving the above tasks in the SAGE tutorials/manuals (or developing SAGE in this direction if the tasks are currently SAGE-unsolvable!) is of key importance FOR A GREAT MANY OF THE USERS (OR WANNABE USERS) OF SAGE WHO ARE EXPERIENCED THEORETICIANS BUT ARE NOT EXPERIENCED PROGRAMMERS.
To facilitate the eventual answering of these questions, I herewith propose a simple but comprehensive example. IF YOU PROVIDE COMPLETE ANSWERS CONCERNING THIS EXAMPLE, YOU WILL HAVE ANSWERED ALL OF THE ABOVE QUESTIONS TOGETHER. Moreover, your answer would certainly merit to be included among the examples in the next version(s) of the SAGE tutorial[(s) and/or reference manual(s).
Consider the 3D scalar field (argument is a 3D vector, value is a real scalar between 0 and 1)
F(x,y,z)=exp(-x^2)*((cos(pi*y))^2)*(1/(1+z^2)), -1<=x,y,z<=1
Design a 3D-plot of F (different from ray tracing a grey-scale colored 3D point cloud and different from constructing and plotting several isosurfaces), namely, by using the
Curvilinear coordinates in 3D induced by F:
Consider the positive integers l,m,n and knots
-1==x_0<x_1<...<x_l==1,
-1==y_0<y_1<...<y_m==1,
-1==z_0<x_1<...<x_n==1.
Plot in 3D:
the x-coordinate lines F(x,y_j,z_k), j in range(m+1), k in range(n+1), rgbcolor=(1,0,0), thickness=3;
the y-coordinate lines F(x_i,y,z_k), i in range(l+1), k in range(n+1), rgbcolor=(0,1,0), thickness=3;
the z-coordinate lines F(x_i,y_j,z), j in range(m+1), k in range(n+1), rgbcolor=(0,0,1), thickness=3;
the (x,y)-coordinate surfaces F(x,y,z_k), k in range(n+1), rgbcolor=(1/2,1/2,0), opacity=0.5;
the (y,z)-coordinate surfaces F(x_i,y,z), i in range(l+1), rgbcolor=(0,1/2,1/2), opacity=0.5;
the (x,z)-coordinate surfaces F(x,y_j,z), j in range(m+1), rgbcolor=(1/2,0,1/2), opacity=0.5;
in a common plot P (equal to the sum of the plots of the separate coordinate lines and surfaces as described above), and then execute the SAGE command
P.show(viewer='jmol')
where l,m,n can be varied as input WITHOUT otherwise CHANGING THE SAGE CODE of the plotting.
Analogously, of interest is an isolevel/isosurface plot of F, as follows:
Consider the positive integer J and the knots
0==c_0<x_1<...<c_K==1, where [0,1] is the range of values taken by F.
Plot in 3D the isolevel surfaces (isosurfaces)
F(x,y,z)-c_I=0, I in range(K+1), color=rainbow(K)[I] , opacity=0.5
using (A) the default implicit_plot3d method and (B) the 'smooth-triangle' method for Tachyon (the normals are very easy to compute),
then sum the plots into P and execute again
P.show(viewer='jmol')
So far I can only plot and visualize (with my preferences being to use jmol for the latter) single 3D-curves (via parametric_plot3d), single functional surfaces (via ParametricSurface) and single parametric surfaces (via parametric_plot3d), as well as single implicitly defined surfaces (via implicit_plot3d) but so far my efforts to organize loops in i,j,k and I to plot the above-sai 3d-geometric objects in a common plot have failed.
Typically, in an algorithmic language one would organize the data about the plotting of a single x-coordinate line in a 2-dimensional array Plinex(j,k) using parametric_plot3d for the design of every entry in the array, and then it should be possible to execute something of the type
P=sum(Plinex(j,k), j in range(m+1), k in range(n+1))
Analogously, in this hypothetical algorithmic language the data about the plotting of a single (x,y)-coordinate surface should be organized in a 1-dimensional array Psurfxy(k) using ParametricSurface for the design of every entry in the array, and then it should be possible to execute something of the type
P=sum(Psurfxy(k), k in range(n+1))
Also analogously, one should be able to organize the data about the plotting of a single isosurface in a 1-dimensional array Pisosurf(I) using implicit_plot3d (in each of the variants A and B considered above, or at least in variant A only) for the design of every entry in the array, and then it should be possible to execute something of the type
P=sum(Pisosurf(I), I in range(K+1))
The only case mentioned in the title which this example does not cover is the case of plotting curvilinear curves and surfaces for a PARAMETRIC VOLUME DEFORMATION (i.e., (x=fx(u,v,w),y=fy(u,v,w),z=fz(u,v,w)) where (u,v,w) belongs to 3-VARIATE parametric domain), but handling this case would be an easy consequence of the case of a scalar field F(x,y,z) considered above.
Unfortunately, being a newbie in SAGE, I do not know how to write all this in SAGE.
CAN YOU PLEASE HELP WITH THIS? MANY PEOPLE WOULD BE THANKFUL FOR YOUR REPLY, I am sure...shao-linuxThu, 08 Oct 2015 09:22:59 -0500http://ask.sagemath.org/question/29836/Chart vs ambient coördinates for smooth affine varietieshttp://ask.sagemath.org/question/28739/chart-vs-ambient-coordinates-for-smooth-affine-varieties/In SageManifolds, coördinates are defined by means of charts and transition functions. For instance, I understand one can define a sphere by giving two charts and the usual transition arising from stereographic projections. But a sphere can also come with its own coordinates by being defined as a variety. Is there a way (or even a nice way!) to reconcile these two views when working with locally defined objects? For instance, I might have a function on a manifold that vanishes in some way, which when defined on charts is obvious, but when defined on the subvariety one needs to apply the defining equation(s) to see this. Other properties defined via equations in function rings (or other rings of sections of sheaves or bundles) are similar.David RobertsFri, 31 Jul 2015 04:32:26 -0500http://ask.sagemath.org/question/28739/Normalized coordinates for plots/graphics?http://ask.sagemath.org/question/25357/normalized-coordinates-for-plotsgraphics/ Is it possible to use normalized (unit) coordinates for graphics?
I would like to position a text label below the x-axis of a plot. Something like this:
po = plot(...)
po += text( (r'$mylabel$', (.5, -.1))
Where (.5, -.1) are coordinates relative to the bounding box of the plot with the extends (0..1,0..1). So yes, this is a very simple coordinate transformation. Do I have to do it by hand?
And if so, how do I get the actual bounding box of the plot?bitfixMon, 29 Dec 2014 08:46:33 -0600http://ask.sagemath.org/question/25357/Changing basis on a vector spacehttp://ask.sagemath.org/question/10327/changing-basis-on-a-vector-space/Dear community:
I'd like to know if it is possible use a basis other than the canonical. For example
> sage: V = VectorSpace(SR,3)
> sage: V.basis()
> [ (1, 0, 0),
> (0, 1, 0),
> (0, 0, 1) ]
is the canonical basis of `V`, but I want to use say
> [ (1, 1, 0),
> (1, -1, 0),
> (0, 0, 1)]
How could I do that?
**Edit**
More specifically, I'd like to define two different basis on a vector space. Say, a set of coordinate basis and a non-coordinate basis defined over the same vector space... and finally I'd like to express results in either of them.
I'm interested on a change of basis on Differential Forms, but I guess that if you can help me to understand the general problem I can manage the particular one! ChreesDoxSat, 06 Jul 2013 10:04:31 -0500http://ask.sagemath.org/question/10327/Get list of coordinates from plot objecthttp://ask.sagemath.org/question/9262/get-list-of-coordinates-from-plot-object/I'm looking for a way to get a plain list of coordinates from a plot object. For instance, after creating an implicit plot of a circle
> p = implicit_plot(x^2+y^2-1,(x,-2,2),(y,-2,2))
I am able to save the image in .pdf or even .svg format (where coordinates are present), which is a very good start. What I really want, though, is a plain list of coordinates of, say, 2000 points lying on that circle. It should be possible to extract some from the .svg file, but is there a more convenient method? Maybe even one where I can set the sample size?
Thank you in advance for any suggestions.AnakondaWed, 22 Aug 2012 00:35:15 -0500http://ask.sagemath.org/question/9262/get coordinates and function value as listhttp://ask.sagemath.org/question/9136/get-coordinates-and-function-value-as-list/ I am quite new in sage as well as in programming, so please if you explain this, please do it thouroughly.
Let's assume we have a simple function like F(x,y)=x+y.
Is it possible to create a list, something like (x,y,F)?
To make clearer a list in the form of (value_of_x_coordinate,value_of_y_coordinate,value_of_function) for instance (1,1,2) or (1,2,3).
If this is possible, how can we transform the coordinates x,y by operating somehow on the list e.g adding on both x and y, a number N. CosmosWed, 04 Jul 2012 09:32:33 -0500http://ask.sagemath.org/question/9136/