Applying pseudo-inverse give unwanted result - numpy

I have to apply inversion to the equation DX=G.
solving for the equationis not simple as X=D-1G, This is because the D matrix is not a square matix. So I have to do the matix calculation as X=(DTD)-1(DTG) pseudo inverse.
I wrote the code for this as,
X=np.matmul(np.linalg.inv(np.matmul(np.transpose(D),D)),np.matmul(np.transpose(D),G))
but this is giving me unwanted results with random noise at the output image1 unwrapped, at the same time while employing the following code,
X=np.matmul(np.matmul(np.transpose(D),D),np.matmul(np.transpose(D),G))
ie., removing the inverse X=(DTD)(DTG) is giving the kind of desired result which is smooth image2, what is happening???? Is there something that I should take care of.

Related

How is this Alpha Blending equation to understand?

So I was reading a Document about Displacement Mappings and Surface Blendings and came across this equation which is supposed to be a Alpha-Blending equation:
while v1,...,vn are supposed to be the value vector and w1,....,wn the weight vector (is how the document describes it).
To tell what my interpretation of this equation is, is that considering n being the number of surfaces we are trying to blend together the value vectors are supposed to represent as the name says the value (probably color related?) of each surface and the weight vector basically describing the value preference of each surface (so the higher the weight value the more we would see the color of that one surface after the blend). The multiplication and division part is something what i do not fully understand (just interpreting it as the 'it just works like that' part of the equation)
I couldn't find any similar equation anywhere so far so I figured out that either I didn't search deep enough or I am not understanding something that is supposed to be very obvious and I wanted to make sure that fully understand this equation for further read in the document which bases on this idea.

finding pivot point of two 3D transformations

I need to find out what the degrees of freedom are between two arbitrary geometries that may be linked to eachother. for instance a hinge consisting of two parts. I can simulate the motion of the two parts, and I figured that if I fix one of the parts in place, i can deduce what the axes and point of rotation is for the second moving part is from the transformation in each timestep.
I run into some difficulties calculating this (my vector algebra is ok, my (numpy) math skills less so)
How I see it is I have two 4x4 transformation matrices for each timestep, the previous position/orientation of the moving part (A) and the current position/orientation (A')
then the point of rotation can be found by by calculating the transformation matrix B that transforms A into A' which is I believe
B = inverse(A) * A'
and then find the point that does not change under transformation by B:
x = Bx
Is my thinking correct and if so, how do I solve this equation?

Why is tf.transpose so important in a RNN?

I've been reading the docs to learn TensorFlow and have been struggling on when to use the following functions and their purpose.
tf.split()
tf.reshape()
tf.transpose()
My guess so far is that:
tf.split() is used because inputs must be a sequence.
tf.reshape() is used to make the shapes compatible (Incorrect shapes tends to be a common problem / mistake for me). I used numpy for this before. I'll probably stick to tf.reshape() now. I am not sure if there is a difference between the two.
tf.transpose() swaps the rows and columns from my understanding. If I don't use tf.transpose() my loss doesn't go down. If the parameter values are incorrect the loss doesn't go down. So the purpose of me using tf.transpose() is so that my loss goes down and my predictions become more accurate.
This bothers me tremendously because I'm using tf.transpose() because I have to and have no understanding why it's such an important factor. I'm assuming if it's not used correctly the inputs and labels can be in the wrong position. Making it impossible for the model to learn. If this is true how can I go about using tf.transpose() so that I am not so reliant on figuring out the parameter values via trial and error?
Question
Why do I need tf.transpose()?
What is the purpose of tf.transpose()?
Answer
Why do I need tf.transpose()? I can't imagine why you would need it unless you coded your solution from the beginning to require it. For example, suppose I have 120 student records with 50 stats per student and I want to use that to try and make a linear association with their chance of taking 3 classes. I'd state it like so
c = r x m
r = records, a matrix with a shape if [120x50]
m = the induction matrix. it has a shape of [50x3]
c = the chance of all students taking one of three courses, a matrix with a shape of [120x3]
Now if instead of making m [50x3], we goofed and made m [3x50], then we'd have to transpose it before multiplication.
What is the purpose of tf.transpose()?
Sometimes you just need to swap rows and columns, like above. Wikipedia has a fantastic page on it. The transpose function has some excellent properties for matrix math function, like associativeness and associativeness with the inverse function.
Summary
I don't think I've ever used tf.transpose in any CNN I've written.

Fitting curves to a set of points

Basically, I have a set of up to 100 co-ordinates, along with the desired tangents to the curve at the first and last point.
I have looked into various methods of curve-fitting, by which I mean an algorithm with takes the inputted data points and tangents, and outputs the equation of the cure, such as the gaussian method and interpolation, but I really struggled understanding them.
I am not asking for code (If you choose to give it, thats acceptable though :) ), I am simply looking for help into this algorithm. It will eventually be converted to Objective-C for an iPhone app, if that changes anything..
EDIT:
I know the order of all of the points. They are not too close together, so passing through all points is necessary - aka interpolation (unless anyone can suggest something else). And as far as I know, an algebraic curve is what I'm looking for. This is all being done on a 2D plane by the way
I'd recommend to consider cubic splines. There is some explanation and code to calculate them in plain C in Numerical Recipes book (chapter 3.3)
Most interpolation methods originally work with functions: given a set of x and y values, they compute a function which computes a y value for every x value, meeting the specified constraints. As a function can only ever compute a single y value for every x value, such an curve cannot loop back on itself.
To turn this into a real 2D setup, you want two functions which compute x resp. y values based on some parameter that is conventionally called t. So the first step is computing t values for your input data. You can usually get a good approximation by summing over euclidean distances: think about a polyline connecting all your points with straight segments. Then the parameter would be the distance along this line for every input pair.
So now you have two interpolation problem: one to compute x from t and the other y from t. You can formulate this as a spline interpolation, e.g. using cubic splines. That gives you a large system of linear equations which you can solve iteratively up to the desired precision.
The result of a spline interpolation will be a piecewise description of a suitable curve. If you wanted a single equation, then a lagrange interpolation would fit that bill, but the result might have odd twists and turns for many sets of input data.

What's the meaning of the partial derivatives of B-spline as given by scipy.interpolate.bisplev?

I have a mesh generated from cloudpoint, which could be described as z = f(x,y), so I'm using scipy.interpolate.bisplrep and bisplev, with good results.
bisplev can be used with parameters dx=n and/or dy=n so that the results are derivatives of order n at the evaluated points. I plan to use this to calculate mean and gaussian curvatures (called surfature in Matlab), and that should involve getting the second-order partial derivatives of the survace
The results using one of the partial derivatives at a time, say dx are great, clearly representing the gradient as a "shading" effect, as seen in this image from a human back (code first):
self.spline = inter.bisplrep(self.pointlist[:,1],
self.pointlist[:,0],
self.pointlist[:,2], s=smoothing_factor)
self.mesh_shadow = inter.bisplev(yy.flat, xx.flat, self.spline, dy=1)
So far, so good. The problem is: I can't understand (and can't find any explanation) what's the meaning of the result when I ask for both partial derivatives at the same time, since there isn't any obvious numeric or visual meaning. For example, if I use dx AND dy:
self.mesh_shadow = inter.bisplev(yy.flat, xx.flat, self.spline, dx=1, dy=1)
I get this:
So, I wonder:
What's the mathematical/geometrical meaning of the simultaneous result of first-order partial derivatives of a surface as given by bislplev(..., dx=1, dy=1), if any?
Would there be a way to get the maximum slope (in any direction) from bislplev(..., dx=1, dy=1)?
Are both partial derivatives supposed to be called together, in the first place? I see I can use, say, (..., dx=1, dy=2) and the function seems to produce "valid" results, but would that make any sense?
Every time, the returned value is a (Y,X)-shaped array of single float values (Z or one of its derivative-related values).
Any help?
Thanks for reading
The partial derivative you get with dx=n, dy=m is the mathematical object (or rather, its numerical approximation)
(d/dx)^n (d/dy)^m f(x,y)
You cannot compute the Gaussian curvature just from dx=2,dy=0 and dx=0,dy=2 --- you in general also need also the cross-derivative dx=1,dy=1.
Partial derivatives (d/dx)^n (d/dy)^m f(x,y) are mathematically well-defined. With splines, if you go to too high orders, you should start getting zeros or discontinuities.