Maxima: how to plot a 2D and 3D vector? - vector

What is the simplest way to plot and 2D and 3D vectors using wxMaxima?
I have searched around, but all the solutions seem too complicated just for a single plot of simple vector, is that possible?
I would like to see 3 vectors at the same time in a 3D space in order to visualize a shape, but all options I have seen around seem to make this simple task not so trivial.

Does it help?
load("draw");
draw3d(vector([0, 0, 0], [100, 0, 0]),
vector([0, 0, 0], [0, 100, 0]),
vector([0, 0, 0], [0, 0, 100]));

Related

How to make a diagonal tensor and why doesn't Tensorflow linalg.tensor_diag do that?

What I would consider a diagonal tensor is a tensor t of shape (d1, ..., dr) which is all zero except when the components are equal.
So t[i,j,k,l] = 0 unless i == j == k == l.
A function to create such a tensor should take in a shape (d1, ..., dr) and a vector [a1, ..., ak] of length min(d1, ..., dr), placing these values along the diagonal.
I would like to do this in Tensorflow, and the most relevant function I could find was tf.linalg.tensor_diag, but it doesn't do what I want. For instance, the diagonal input is a tensor, and the output tensor always has twice the rank, and so it can never output tensors of odd rank.
The documentation says "Given a diagonal, this operation returns a tensor with the diagonal and everything else padded with zeros", but I don't know how to square that with its actual behavior.
My question is two parts:
What is the best way in TF to do create what I am calling a diagonal tensor. Is there another name for this?
Why does linalg.tensor_diag work like this? What is the intended use?
Here is an example output:
>>> tf.linalg.tensor_diag([1,2],[3,4]])
<tf.Tensor: shape=(2, 2, 2, 2), dtype=int32, numpy=
array([[[[1, 0],
[0, 0]],
[[0, 2],
[0, 0]]],
[[[0, 0],
[3, 0]],
[[0, 0],
[0, 4]]]], dtype=int32)>```
So this is a little tricky to think about but I'll try to explain the thinking.
If you do tf.linalg.tensor_diag([1,2,3,4]) this is intuitively gives a matrix with that diagonal:
[[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]]
Notice you went from rank 1 to rank 2 doing this, the rank doubled. So to "diagonalize" it's going to end up doubling the rank.
Now your question, tf.linalg.tensor_diag([[1,2],[3,4]]) What you're passing in is a matrix so rank 2
[[1, 2],
[3, 4]]
But now, how should this be diagonalized? So it's rank 2 and following the pattern means we'll end up with something of rank 4. In the previous example diagonalize sort of "pulled up" the vector into the higher rank. And each step of "pulling up" took a single value from the diagonal and put it there.
So this matrix will also be "pulled up" and each step of the way leaving a value. So it's going to make 4 squares of [[0,0],[0,0]] and drop the value in each one. This would give us
[[1,0],
[0,0]]
[[0,2],
[0,0]]
[[0,0],
[3,0]]
[[0,0],
[0,4]]
Lastly things will be "grouped" if they were originally (like [1,2] idk how better to say this) so that gives the final result of
[
[
[[1,0],
[0,0]] ,
[[0,2],
[0,0]]
],
[
[[0,0],
[3,0]] ,
[[0,0],
[0,4]]
]
]
Which indeed gives us a rank 4 result 👍
Note: You may want to look into the other diag function for more of you're trying to do

Interpolation in a distorted box

I want to interpolate in a distorted box. As We have 8 points around a distorted box (p0, p1, p2, ..., p7), if we find the transformation matrix which transform this box to a box with points ((0, 0, 0), (0, 0, 1), (0, 1, 1), (0, 1, 0), (1, 0, 0), (1, 0, 1), (1, 1, 1), (1, 1, 0) ), the interpolation can be done easily. In other words, If we find a transformation from a distorted box to a normal box which length, width and height of the box are equal to 1, the interpolation can be done very simple. Anyone has any idea about interpolating in a distorted box or finding transformation from a distorted box to a normal box?
Not answering the original question since in the comment you said that you simply wanted to interpolate a function inside the cube, using the values at the 8 vertices.
So in order to do that, you can reason as follow:
1) Split the cube in 6 tetrahedra
2) Find the tetrahedron that contains the point you want to interpolate
3) An irregular tetrahedron can be easily mapped to a regular one, that is you can easily obtain the generalized tetrahedral coordinates of a point. Check eq. 9-11 here.
4) Once you have the tetrahedral coordinates of your point, the interpolation is trivial (see previous link).
This is the easiest way I can think of, the big downside is that there are 13 ways to split a cube in tetrahedras, and this choice will produce (slightly) different results, especially if the cube is heavily deformed. You should aim for a delaunay tetrahedralization of the cube to minimize this effect.
Also notice that the interpolated function defined in this way is continuous across the faces of the tetrahedra (but not differentiable).
You can apply the inverse of a scaling matrix to the cube where vx, vy and vz
are the cube's spacial extents.

How can I calculate a multi-axis SCNVector4 rotation for a SCNNode?

The SCNNode take a rotation using a SCNVector4, which has an angle (w) and a magnitude how that angle applies to each axis (x, y, z). For example, to rotate 45 degrees around the x-axis I'd create a SCNVector4 like this:
SCNVector4Make(1.0f, 0, 0, DEG2RAD(45))
What I'd like to do is rotate it across all three axis, for example: 45 degrees on the x-axis, 15 degrees on the y-axis and -135 degress across the z-axis. Does anyone know the math to calculate the final SCNVector4?
Instead of rotation property, use eulerAngles and specify angle for each axis
You'll need to generate an SCNVector4 for each of the rotations, and then multiply them. Note that the order of operations matters!
http://www.cprogramming.com/tutorial/3d/rotationMatrices.html has a pretty good writeup of the math. Any OpenGL reference that deals with rotation matrices is worth a look too.
If you're not animating the rotation, it might be cleaner to just set the transform matrix directly, like:
node.transform = CATransform3DRotate(CATransform3DRotate(CATransform3DRotate(node.transform, xAngle, 1, 0, 0), yAngle, 0, 1, 0), zAngle, 0, 0, 1);
Do you ask for rotation matrix or how to simply rotate in general? If the second is correct then for example:
[node runAction:[SCNAction rotateByX:0 y:M_PI z:0 duration:0]];

Color Plot by order of points in list - Mathematica

I've got a list of three dimensional points, ordered by time. Is there a way to plot the points so that I can get a visual representation that also includes information on where in the list the point occurred? My initial thought is to find a way to color the points by the order in which they were plotted.
ListPlot3D drapes a sheet over the points, with no regard to the order which they were plotted.
ListPointPlot just shows the points, but gives no indication as to the order in which they were plotted. It's here that I am thinking of coloring the points according to the order in which they appear in the list.
ListLinePlot doesn't seem to have a 3D cousin, unlike a lot of the other plotting functions.
You could also do something like
lst = RandomReal[{0, 3}, {20, 3}];
Graphics3D[{Thickness[0.005],
Line[lst,
VertexColors ->
Table[ColorData["BlueGreenYellow"][i], {i,
Rescale[Range[Length[lst]]]}]]}]
As you did not provide examples, I made up some by creating a 3d self-avoiding random walk:
Clear[saRW3d]
saRW3d[steps_]:=
Module[{visited},
visited[_]=False;
NestList[
(Function[{randMove},
If[
visited[#+randMove]==False,
visited[#+randMove]=True;
#+randMove,
#
]
][RandomChoice[{{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}]])&,
{0,0,0},
steps
]//DeleteDuplicates
]
(this is sort of buggy but does the job; it produces a random walk in 3d which avoids itself, ie, avoids revisiting the same place in subsequent steps).
Then we produce 100000 steps like this
dat = saRW3d[100000];
this is like I understood your data points to be. We then make these change color depepnding on which step it is:
datpairs = Partition[dat, 2, 1];
len = Length#datpairs;
dressPoints[pts_, lspec_] := {RGBColor[(N#First#lspec)/len, 0, 0],
Line#pts};
datplt = MapIndexed[dressPoints, datpairs];
This can also be done all at once like the other answers
datplt=MapIndexed[
{RGBColor[(N#First##2)/Length#dat, 0, 0], Line##1} &,
Partition[dat, 2, 1]
]
but I tend to avoid this sort of constructions because I find them harder to read and modify.
Finally plot the result:
Graphics3D[datplt]
The path gets redder as time advances.
If this is the sort of thing you're after, I can elaborate.
EDIT: There might well be easier ways to do this...
EDIT2: Show a large set of points to demonstrate that this is very useful to see the qualitative trend in time in cases where arrows won't scale easily.
EDIT3: Added the one-liner version.
I think Heike's method is best, but she made it overly complex, IMHO. I would use:
Graphics3D[{
Thickness[0.005],
Line[lst,
VertexColors ->
ColorData["SolarColors"] /# Rescale#Range#Length#lst ]
}]
(acl's data)
Graphics3D#(Arrow /# Partition[RandomInteger[{0, 10}, {10, 3}], 2, 1])
As to your last question: If you want to have a kind of ListLinePlot3D instead of a ListPointPlot you could simply do the following:
pointList =
Table[{t, Sin[t] + 5 Sin[t/10], Cos[t] + 5 Cos[t/10],
t + Cos[t/10]}, {t, 0, 100, .5}];
ListPointPlot3D[pointList[[All, {2, 3, 4}]]] /. Point -> Line
Of course, in this way you can't set line properties so you have to change the rule a bit if you want that:
ListPointPlot3D[pointList[[All, {2, 3, 4}]]] /.
Point[a___] :> {Red, Thickness[0.02], Line[a]}
or with
ListPointPlot3D[pointList[[All, {2, 3, 4}]]] /.
Point[a___] :> {Red, Thickness[0.002], Line[a], Black, Point[a]}
But then, why don't you use just Graphics3D and a few graphics primitives?

Sketching solution curves for differential equations

I have a few differential equations that I'd like to draw solutions for, for a variety of start values N_0
Here are the equations:
dN\dt= bN^2 - aN
dN\dt = bN^2 (1 - N\K) - aN
How would I go about it?
I don't really care about the language is used. In terms of dedicated math I have mathematica and matlab on my computer. I've got access to maple. I have to do more of this stuff, and I'd like to have examples from any language, as it'll help me figure out which one I want to use and learn it.
I'll pretend the first one cannot be solved analytically so as to show how one would go about playing with a general ODE in mathematica.
Define
p1[n0_, a_, b_, uplim_: 10] :=(n /. First#NDSolve[
{n'[t] == b*n[t]^2 - a*n[t], n[0] == n0},n, {t, 0, uplim}]
which returns the solution of the ODE, i.e., a = p1[.1, 2., 3.] and then e.g. a[.3] tells you n(.3). One can then do something like
Show[Table[ans = p1[n0, 1, 1];
Plot[ans[t], {t, 0, 10}, PlotRange \[Rule] Full],
{n0, 0, 1, .05}], PlotRange \[Rule] {{0, 5}, {0, 1}}]
which plots a few solutions with different initial values:
or, to gain some insight into the solutions, one can interactively manipulate the values of a, b and n0:
Manipulate[
ans = p1[n0, a, b];
Plot[ans[t], {t, 0, 10},PlotRange -> {0, 1}],
{{n0, .1}, 0, 1},
{{a, 1}, 0, 2},
{{b, 1}, 0, 2}]
which gives something like
with the controls active (i.e. you move them and the plot changes; try it live to see what I mean; note that you can set parameters for which the initial conditions gives diverging solutions).
Of course this can be made arbitrarily more complicated. Also in this particular case this ODE is easy enough to integrate analytically, but this numerical approach can be applied to generic ODEs (and many PDEs, too).
Adding to the several good answers, if you just want a quick sketch of an ODE's solutions for many starting values, for guidance, you can always do a one-line StreamPlot. Suppose a==1 and b==1, and dy/dx == x^2 - x.
StreamPlot[{1, x^2 - x}, {x, -3, 3}, {y, -3, 3}]
StreamStyle -> "Line" will give you just lines, no arrows.
In Mathematica you use NDSolve (unless it can be solved analytically, in which case you use DSolve. So for your first equation I tried:
b = 1.1; a = 2;
s = NDSolve[{n'[t] == b n[t]^2 - a n[t], n[0] == 1}, n, {t, 0, 10}];
Plot[Evaluate[n[t] /. s], {t, 1, 10}, PlotRange -> All]
I didn't know what to use for a, b or N0, but I got this result:
If you're happy to solve the equations numerically, MATLAB has a set of ODE solvers that might be useful. Check out the documentation for the ode45 function here.
The general approach is to define an "ode function" that describes the right-hand-side of the differential equations. You then pass this function, along with initial conditions and an integration range to the ode solvers.
One attractive feature of this type of approach is that it extends in a straight-forward way to complex systems of coupled ODE's.
Hope this helps.

Resources