Basically I am solving the diffusion equation in 3D using FFT and one of the ways to parallelise this is to decompose the 3D FFT in 2D FFTs.
As described in this paper: https://cmb.ornl.gov/members/z8g/csproject-report.pdf
The way to decompose a 3d fft would be by doing:
2d fft in xy direction
global transpose
1d fft in z direction
Basically, my problem is that I am not sure how to do this global transpose (as I assume it's transposing a 3d array I suppose). Anyone has came accross this? Thanks a lot.
Think of a 3d cube with nx*ny*nz elements. The 3d FFT of these elements is mathematically 3 stages of 1-d FFTs, one along each axis:
Do ny*nz transforms along the X axis, each transform handles nx elements
nx*nz transforms along the Y axis
nx*ny transforms along the Z axis
More generally, an N-dimensional FFT (N>1) is composed of many (N-1)-dimensional FFTs along that axis.
If the signal is real and you have an FFT that can return the half spectrum, then stage 1 would be about half as expensive (real FFT is cheaper), the remaining stages need to be complex, but they only need to have about half as many transforms. So the cost is roughly half.
If your 1d FFT can read input elements that are strided and pack the output into a contiguous buffer, then you end up doing a transposition at each stage.
This is how kissfft performs multi-dimensional FFTs.
P.S. When I need to get a mental pictures of higher dimensions, I think of:
sheets of paper with matrices of numbers (2d), in folders of numbered papers (3d), in numbered filing cabinets (4d), in numbered rooms (5d), in numbered buildings (6d), and so on ... So I can visualize the "filing cabinet" dimension
The "global transposition" mentioned in the paper is not a mathematical operation, but a rearrangement of data between the distributed memory machines.
The data calculated on one machine in step 1 has to be transferred to all other machines, vice versa, for step to. It has nothing to do with a matrix transposition.
Related
I was transforming an object with a matrix A using Object3D.applyMatrix4 and I found that at one point it didn't preserve an eigen vector's direction.
So I tried animating interpolation between Identity Matrix I and A and I found this:
How could the transformation be not continuous?
Linear interpolation of rotation matrices isn't mathematically sound. The vectors composing a rotation matrix need to be unit length.. or at least stay a consistent length.
Imagine a clock with a hand at 12, and a hand at 6.
If you Linearly interpolate the point at the tip of the 12 oclock hand, to the tip of the 6oclock hand, the point travels in a straight line from top of the clock to the bottom.
To interpolate the rotation represented by a 4x4 matrix, you can convert the rotations of the matrices, to quaternions, and .slerp (spherical linear interpolate) between those quaternions, then convert back to a matrix.
And then linearly interpolate the object.position. (although again.. this assumes linear motion between keyframes).
Now in the case that the rotation is small, you can get away with linearly interpolating the matrix, but you will need to orthonormalize it at each step, to reshape the mesh into one that has consistent length vectors that are orthogonal to each other. That isn't that hard.. you use a combination of dot products, multiplies and adds of the vectors forming the matrix rows (or columns, i forget) to orthonormalize the matrix. But its more of a pain, and less accurate than just using quaternions and .slerp.
#manthrax 's answer pointed out the fundamental problem of interpolating a matrix linearly which I wasn't aware of at the time and he was right about that. But the real problem was that Object3D.applyMatrix4 wasn't the right function for explicitly defining local matrix. I tried setting Object3D.matrix property directly and it worked. And the linear interpolation (although I shouldn't do that) became continuous.
In most graphics libraries I've seen, there's some function that returns the determinant from 3x3 and 4x4 matrices, but I have no idea when you'd actually need to use the determinant in 3D computer graphics.
What are some examples of using a determinant in 3D graphics programming?
Off the top of my head...
If the determinant is 0 then the matrix cannot be inverted, which can be useful to know.
If the determinant is negative, then objects transformed by the matrix will reversed as if in a mirror (left handedness becomes right handedness and vice-versa)
For 3x3 matrices, the volume of an object will be multiplied by the determinant when it is transformed by the matrix. Knowing this could be useful for determining, for example, the level of detail / number of polygons to use when rendering an object.
In 3D vector graphics
there are used 4x4 homogenuous transform matrices and we need booth direct and inverse matrices which can be computed by (sub)determinants. But for orthogonal matrices there are faster and more accurate methods like
full pseudo inverse matrix.
Many intersection tests use determinants (or can be converted to use them) especially for quadratic equations (ellipsoids,...) for example:
ray and ellipsoid intersection accuracy improvement
as Matt Timmermans suggested you can decide if your matrix is invertible or left/right handed which is useful to detect errors in matrices (accuracy degradation) or porting skeletons in between formats or engines etc.
And I am sure there area lot of other uses for it in vector math (IIRC IGES use them for rotational surfaces, cross product is determinant,...)
The incircle test is a key primitive for computing Voronoi diagrams and Delaunay triangulations. It is given by the sign of a 4x4 determinant.
(picture from https://www.cs.cmu.edu/~quake/robust.html)
I have an array of 3D paired points in two different coordinate spaces (A and B).
Given points are not coplanar, how do I compute a non-affine transformation matrix which is able to transform a point from A into B?
I have managed to do this in 2D (using a homography), but can't work out how to make it work in 3D. A quick code example would be much appreciated if possible. :)
The approach described in this post will generalize to three dimensions: If you know the coordinates of five points in both coordinate systems, then you can use them to compute a 4×4 projective transformation matrix for this, which will be unique except for a scale factor which is without geometric relevance.
I've included variations of the required code for 2D in various posts, written for sage, and there is also the JavaScript example mentioned along with the description. Any of these could be adapted to the 3D case, but if you also want to change programming language, then you might be better off implementing the formula directly, keeping in mind that the adjoint may serve as alternative for the inverse of a matrix in several locations.
Here are some details on the generalization to 3D:
Use a 4×4 system of linear equations, with the homogenous coordinates of four points on the left and a fifth point on the right hand side.
Use the four solution variables to scale these four columns in order to obtain the transformation matrix.
(as before)
(as before)
(as before)
(as before)
Divide the first three coordinates of the homogenous coordinate vector by the fourth coordinate to obtain dehomogenized coordinates.
Givens
1- X,y,and Z the world co-ordinate system
2-i,j,k another co-ordinate system.
3-the cosines in which each of i,j, and k make with the X,Y,Z.
problem
how to rotate the i,j,k system about i or j or k??
If you have the cosines of the angles formed by pairing each of i,j,k with each of xhat, yhat, and zhat (nine angles altogether), you have the makings for the direction cosine matrix. For example, see http://www.ae.illinois.edu/~tbretl/ae403/handouts/06-dcm.pdf (or just google direction cosine matrix). The direction cosine matrix is just another name for a transformation or rotation matrix.
Be careful, though!
There is no single standard scheme. You need to know that this is the case and read the literature carefully.
Are you rotating the object or transforming coordinates? Rotation and transformation are conjugate operations. Some people (many people!) use the term 'rotation matrix' when they mean 'transformation matrix', and vice versa.
Do you represent vectors as column vectors or row vectors? Here there is a lot more consistency; most people use column vectors rather than row vectors for things like positions, velocities, etc. BUT there are very good reasons to use row vectors (or column vectors if you are one of those contrarians) for things that properly belong in the dual space.
Quaternions have even more ambiguity of representation than matrices. There's nothing wrong with that (I use quaternions all the time), but you do have to beware of these ambiguities when you read a paper or book, look at someone else's code, or exchange data.
Finally, matrices and quaternions are only two of many charts on SO(3). There are lots of ways to represent rotations in 3-space.
You can first create either a rotation matrix or a quaternion. Then you use that to transform your vectors.
You can find the code to create a rotation matrix or a quaternion in pretty much any 3d maths library.
If I recall correctly you calculated the rotation quaternion as(assuming normalized axis):
q.x=axis.x*sin(alpha)
q.y=axis.y*sin(alpha)
q.y=axis.z*sin(alpha)
q.w=cos(alpha)
I'm trying to design an implementation of Vector Quantization as a c++ template class that can handle different types and dimensions of vectors (e.g. 16 dimension vectors of bytes, or 4d vectors of doubles, etc).
I've been reading up on the algorithms, and I understand most of it:
here and here
I want to implement the Linde-Buzo-Gray (LBG) Algorithm, but I'm having difficulty figuring out the general algorithm for partitioning the clusters. I think I need to define a plane (hyperplane?) that splits the vectors in a cluster so there is an equal number on each side of the plane.
[edit to add more info]
This is an iterative process, but I think I start by finding the centroid of all the vectors, then use that centroid to define the splitting plane, get the centroid of each of the sides of the plane, continuing until I have the number of clusters needed for the VQ algorithm (iterating to optimize for less distortion along the way). The animation in the first link above shows it nicely.
My questions are:
What is an algorithm to find the plane once I have the centroid?
How can I test a vector to see if it is on either side of that plane?
If you start with one centroid, then you'll have to split it, basically by doubling it and slightly moving the points apart in an arbitrary direction. The plane is just the plane orthogonal to that direction.
But you don't need to compute that plane.
More generally, the region (i) is defined as the set of points which are closer to the centroid c_i than to any other centroid. When you have two centroids, each region is a half space, thus separated by a (hyper)plane.
How to test on a vector x to see on which side of the plane it is? (that's with two centroids)
Just compute the distance ||x-c1|| and ||x-c2||, the index of the minimum value (1 or 2) will give you which region the point x belongs to.
More generally, if you have n centroids, you would compute all the distances ||x-c_i||, and the centroid x is closest to (i.e., for which the distance is minimal) will give you the region x is belonging to.
I don't quite understand the algorithm, but the second question is easy:
Let's call V a vector which extends from any point on the plane to the point-in-question. Then the point-in-question lies on the same side of the (hyper)plane as the normal N iff V·N > 0