Inverse of a spark RowMatrix - scala

I am trying to inverse a spark rowmatrix. The function I am using is below.
def computeInverse(matrix: RowMatrix): BlockMatrix = {
val numCoefficients = matrix.numCols.toInt
val svd = matrix.computeSVD(numCoefficients, computeU = true)
val indexed_U = new IndexedRowMatrix(svd.U.rows.zipWithIndex.map(r => new IndexedRow(r._2, r._1)))
val invS = DenseMatrix.diag(new DenseVector(svd.s.toArray.map(x => if(x == 0) 0 else math.pow(x,-1))))
val V_inv = svd.V.multiply(invS)
val inverse = indexed_U.multiply(V_inv.transpose)
inverse.toBlockMatrix.transpose
}
The logic I am implementing is through SVD. An explanation of the process is
U, Σ, V = svd(A)
A = U * Σ * V.transpose
A.inverse = (U * Σ * V.transpose).inverse
= (V.transpose).inverse * Σ.inverse * U.inverse
Now U and V are orthogonal matrix
Therefore,
M * M.transpose = 1
Applying the above,
A.inverse = V * Σ.inverse * U.transpose
Let V * Σ.inverse be X
A.inverse = X * U.transpose
Now, A * B = ((A * B).transpose).transpose
= (B.transpose * A.transpose).transpose
Applying the same, to keep U as a row matrix, not a local matrix
A.inverse = X * U.transpose
= (U.transpose.transpose * X.transpose).transpose
= (U * X.transpose).transpose
The problem is with the input row matrix. For example
1, 2, 3
4, 5, 6
7, 8, 9
10,11,12
the inverse from the above code snippet and on using python numpy is different. I am unable to find out why is it so? Is it because of some underlying assumption made during svd calculation? Any help will be greatly appreciated. Thanks.

The above code works properly. The reason I was getting this error was that I made the RowMatrix with a RDD[Vector]. Now, in spark things are sorted column wise to form a matrix, whereas in the case of numpy, array is converted row wise to a matrix
Array(1,2,3,4,5,6,7,8,9)
In Spark
1 4 7
2 5 8
3 6 9
In python, it is interpreted as
1 2 3
4 5 6
7 8 9
So, the test case was failing :|

Related

System of Boolean Equations

I am working on solving a system of boolean equations. Specifically, I am trying to find the values of bit vectors S1...S3 and/or C1...C3 such that their XOR results are given in the table below (in hexadecimal values). Any ideas?
So we have six 32-digit sequences we want to determine, for a total of 192 unknown hexadecimal digits. We can focus on just the first digits of each to illustrate how we could try to solve for the others.
Let's call the first digits of S1, S2 and S3 a, b and c, respectively. Let's also call the first digits of C1, C2 and C3 x, y and z, respectively. Then we have the following equations, where * stands for XOR:
a * x = E b * x = A c * x = 7
a * y = 2 b * y = 6 c * y = B
a * z = 1 b * z = 5 c * z = 8
Let us note some properties of XOR. First, it is associative. That is, A XOR (B XOR C) is always equal to (A XOR B) XOR C. Second, it is commutative. That is, A XOR B is always equal to B XOR A. Also, A XOR A is always the "false" vector (0) for any A. A XOR FALSE is always A where FALSE stands for the "false" vector (0). These facts let us do algebra to solve for variables and substitute to solve. We can solve for c first, substitute in and simplify to get:
a * x = E b * x = A z * x = F
a * y = 2 b * y = 6 z * y = 3
a * z = 1 b * z = 5 c = 8 * z
We can do z next:
a * x = E b * x = A y * x = C
a * y = 2 b * y = 6 z = 3 * y
a * y = 2 b * y = 6 c = 8 * z
We found a couple of our equations are redundant. We expected that if the system were not inconsistent since we had nine equations in six unknowns. Let's continue with y:
a * x = E b * x = A y = C * x
a * x = E b * x = A z = 3 * y
a * x = E b * x = A c = 8 * z
We find now that we have even more unhelpful equations. Now we are in trouble, though: we only have five distinct equalities and six unknowns. This means that our system is underspecified and we will have multiple solutions. We can pick one or list them all. Let us continue with x:
a * b = 4 x = b * A y = C * x
a * b = 4 x = b * A z = 3 * y
a * b = 4 x = b * A c = 8 * z
What this means we have one solution for every solution to the equation a * b = 4. How many solutions are there? Well, there must be 16, one for every value of a. Here is a table:
a: 0 1 2 3 4 5 6 7 8 9 A B C D E F
b: 4 5 6 7 0 1 2 3 C D E F 8 9 A B
We can fill in the rest of the table using the other equations we determined. Each row will then be a solution.
You can continue this process for each of the 32 places in the hexadecimal sequences. For each position, you will find:
there are multiple solutions, as we found for the first position
there is a unique solution, if you end up with six useful equations
there are no solutions, if you find one of the equations becomes a contradiction upon substitution (e.g., A = 3).
If you find a position that has no solutions, then the whole system has no solutions. Otherwise, you'll have a number of solutions that is the product of the nonzero numbers of solutions for each of the 32 positions.

MATLAB math operation with the power function is giving me very off results (giving me extremely small numbers)

I wrote the following code to plot a graph. The math operation for value does not make sense.
I have captured a screenshot. It looks like this.
.
The new_x and new_y values are repectively -3 and 5.0249 in this iteration, but the 'value' calculated is some absurdly small number.
I tried the exact same thing in the command window using the same inputs and it works fine...
for i = 1:length(x_vals)
syms f(y);
f(y) = 3 * x_vals(i)^(7) + 2 * y^(5) - x_vals(i)^(3) + y^(3);
df = diff(f,y);
f_func = matlabFunction(f(y));
f_prime = matlabFunction(df);
a = 4;
tolerance = 10.^-6;
root(i) = Newton(f_func, f_prime, a, tolerance);
%value(i) = G(x_vals(i),root(i));
new_x = double(x_vals(i));
new_y = double(root(i));
value = 3 * new_x^7 + 2 * new_y^5 - new_x^3 + new_y^3;
if (value >= -3 - e && value <= -3 + e)
y_vals(i) = root(i);
z_vals(i) = -3;
end
end

Convert for loop into vector expression

I'm new to MATLAB and learning to use vector expressions instead of verbose for loops. I have a snippet and I was wondering whether it could even be written in a concise vector and if so how would I modify it.
for v = I
X(i, v) = X(i, v) + length(I(I == v));
end
X is 1500x200
I is 3763x1
i can be thought of as a constant
What I'm doing here is this. I contains column indexes of X and I want to increment those locations by the number of times that particular index appeared in I. So after this for loop is done the ith row of X will contain a histogram.
Any other ideas or suggestions to improve my MATLAB coding would also be appreciated.
Here's a couple of ways:
I = randi(10, [50,1]);
X = zeros (1, 10);
for Col = 1 : size (X, 2)
X(1, Col) = sum (I == Col);
end
% X = 7 7 3 3 7 4 5 8 1 5
X = zeros (1, 10);
for Col = I.' % the transpose operation is needed to convert to horizontal!
X(1, Col) += 1;
end
% X = 7 7 3 3 7 4 5 8 1 5
X = zeros (1, 10);
X = accumarray (I, ones (size (I)), size (X))
% X = 7 7 3 3 7 4 5 8 1 5

How Can I Solve Matrix Equation in Matlab

Say I have the following matrix equation
X - B*X*C = D
Where,
X: 3 by 5, to be solved;
B: 3 by 3;
C: 5 by 5;
D: 3 by 5;
Is there any convenient method that I can use for solving the system? fsolve?
In case B or C are invertible, you can check the matrix cookbook section 5.1.10 deals with similar settings:
X * inv(C) - B * X = D * inv(C)
Can be translated to
x = inv( kron( eye, -B ) + kron( inv(C)', eye ) ) * d
where x and d are vector-stack of X and D respectively.
You can use MATLAB's dlyap function:
X = dlyap(B,C,D)

Matlab transition matrix

I tried to find a function of matlab, I found 'tf' but I didn't know how to use it :/
So I am trying to write a code of transition matrix, from:
mat1=[1,1,1;
1,1,0;
1,0,0];
to this one:
mat2=[1,2,3;
0,1,1;
0,0,1]
I think I have to do something like:
a{1} * mat2(1,:) + a{2} * mat2(1,:) + a{3} * mat2(1,:) = mat1(1,:);
a{4} * mat2(2,:) + a{5} * mat2(2,:) + a{6} * mat2(2,:) = mat1(2,:);
a{7} * mat2(3,:) + a{8} * mat2(3,:) + a{9} * mat2(3,:) = mat1(3,:);
find the a{1}, a{2}, .... a{9} that solve these equations, and put it in the columns:
result = [a{1} a{4} a{7};
a{2} a{5} a{8};
a{3} a{6} a{9}];
Is my way good? can someone tell me please how to use the matlab function for creating a transition matrix for my matrices?
This is an example:
1(1,2,3)-1(0,1,1)-1(0,0,1) = (1,1,1)
1(1,2,3)-1(0,1,1)-2(0,0,1) = (1,1,0)
1(1,2,3)-2(0,1,1)-1(0,0,1) = (1,0,0)
then, the result should be:
result = [1 1 1
-1 -1 -2
-1 -2 -1]
now if I take the vector (3, -1, -1) in the basis of B, I got (1,0,0) in the basis of c.
The tf function computes the transfer function model. That does not seem to be in any way related to your problem.
EDITED:
Now I got it, so the result matrix R that you want is actually
R = (M1 * M2^-1)^T
hence
result = (mat1 * inv(mat2))';
where the transposition is simply due to your choice of picking the indices column-first.
However, I must underline that this solution yields
mat1 = result' * mat2;
so R^T is not the transition matrix from M1 to M2, but the transition matrix from M2 to M1.
TF() is transfer function. As in controls. For example, if the function is F(s) = (1/5s^2+2s+1) Numerator = [1] Denominator = [5 2 1] and therefore your transfer function F = tf([1], [5 2 2]). From here you can do a lot of fun engineering stuff like bode(F) and so on.
What I think you are trying to do is: http://www.mathworks.com/matlabcentral/newsreader/view_thread/132415