Solve system of linear equations in Spark - scala

I have a system of linear equations in the form of Ax = b to solve in Spark.
A is n by n
b is n by 1
I representA in the form of IndexedRowMatrix or RowMatrix and b in the form of DenseMatrix or DenseVector.
How can I solve this system to calculate the x vector?
If the suggested solution is Cholesky Decomposition, would you please guide me through doing it as it is not part of the public API ? For example if the original matrix A is:
1,2,3,4
2,1,5,6
3,5,1,7
4,6,7,1
and b is:
5,6,7,8
What is passed as argument to the solve method ?
Any other solution other than inversing A would be very helpful.

I don't know if the question is still relevant to you or not, but another solution to the question is to invert the coefficient matrix and then multiply the inverted matrix to the vector b. There are many matrix inversion algorithm. One such algorithm can be found in the following paper
SPIN: A Fast and Scalable Matrix Inversion Method in Apache Spark
You can find the complete code on GitHub link also.
Cheers!!

Related

SVD-like matrix transformation (rank normal form)

I am implementing a research paper in MATLAB and have encountered a matrix transformation that I don't know how to get done in MATLAB.
Here it is,
P*L*Q = [I O]
where P,Q are transformation matrices, L is the given matrix, and I,O are identity and zero matrices respectively.
Can anyone help me get this done in MATLAB through some function or an algorithm so I can implement this through my code?
I would say that the easiest way would be to use the in-build function svd.
https://www.mathworks.com/help/matlab/ref/svd.html?s_tid=gn_loc_drop

Matlab: Reduce second order matrix differential equation to standard eigenproblem

I want to obtain the natural frequencies of a simple mechanical system with mass matrix M and stiffness matrix K (.mat-file -> Download):
Mx''(t)+Kx(t)=0 (x= Position).
It means basically, that I have to solve det(K-w^2*M)=0. But how can I solve it in Matlab (or if necessary reduce it to a standard eigenvalue problem and solve it then)? The matrices are definitely solvable with Abaqus (FEM Software), but I have to solve it in Matlab.
I tried the following without success: det(K-w^2*M)=0 => det(M^-1*K-w^2*I)=0 (I := unity matrix)
But solving this eigenvalue problem with
sqrt(eigs(K*M^-1))
delivers wrong values and the warning:
"Matrix is singular to working precision.
In matlab.internal.math.mpower.viaMtimes (line 35)"
Other wrong values can be obtained via det(K-w^2*M)=0 => det(I/(w^2)-M*K^-1)=0:
1./sqrt(eigs(M*K^-1))
Any hint would help me. Thanks in advance.
As #Arpi mentioned, you actually want to solve the generalized eigenvalue problem:
K*x = w^2*M*x
Since your matrices K and M are apparently singular (or just one of them), it is not possible to use eigs, but you have to use eig:
V = eig(K,M);
w = sqrt(V);

Numerical Instability Kalman Filter in MatLab

I am trying to run a standard Kalman Filter algorithm to calculate likelihoods, but I keep getting a problema of a non positive definite variance matrix when calculating normal densities.
I've researched a little and seen that there may be in fact some numerical instabitlity; tried some numerical ways to avoid a non-positive definite matrix, using both choleski decomposition and its variant LDL' decomposition.
I am using MatLab.
Does anyone suggest anything?
Thanks.
I have encountered what might be the same problem before when I needed to run a Kalman filter for long periods but over time my covariance matrix would degenerate. It might just be a problem of losing symmetry due to numerical error. One simple way to enforce your covariance matrix (let's call it P) to remain symmetric is to do:
P = (P + P')/2 # where P' is transpose(P)
right after estimating P.
post your code.
As a rule of thumb, if the model is not accurate and the regularization (i.e. the model noise matrix Q) is not sufficiently "large" an underfitting will occur and the covariance matrix of the estimator will be ill-conditioned. Try fine tuning your Q matrix.
The Kalman Filter implemented using the Joseph Form is known to be numerically unstable, as any old timer who once worked with single precision implementation of the filter can tell. This problem was discovered zillions of years ago and prompt a lot of research in implementing the filter in a stable manner. Probably the best well-known implementation is the UD, where the Covariance matrix is factorized as UDU' and the two factors are updated and propagated using special formulas (see Thoronton and Bierman). U is an upper diagonal matrix with "1" in its diagonal, and D is a diagonal matrix.

Linear program with double constraints on the same variable

I have a linear program of the form min(f*x) s.t. A1*x < d1; A2*x < d2. The form with one constraint is implemented in Matlab in command linprog. What command can I use to solve linear program with two constrraints?
I could of course create a block diagonal matrix, and double the size of the variable x, but if there is more efficient way I would like to use it, because the size of the matrix is quite large.
Possibly I don't understand the question right but can't you combine the matrixes A1 und A2 by A = [A1; A2]?
You maybe interested in Dantzig-Wolfe Decomposition algorithm for solving linear programming. It takes advantage of this block diagonal structure. However, I don't think there is an out-of-the box implementation of it in commercial softwares.

fft matrix-vector multiplication

I have to solve in MATLAB a linear system of equations A*x=B where A is symmetric and its elements depend on the difference of the indices: Aij=f(i-j).
I use iterative solvers because the size of A is say 40000x40000. The iterative solvers require to determine the product A*x where x is the test solution. The evaluation of this product turns out to be a convolution and therefore can be done dy means of fast fourier transforms (cputime ~ Nlog(N) instead of N^2). I have the following questions to this problem:
is this convolution circular? Because if it is circular I think that I have to use a specific indexing for the new matrices to take the fft. Is that right?
I find difficult to program the routine for the fft because I cannot understand the indexing I should use. Is there any ready routine which I can use to evaluate by fft directly the product A*x and not the convolution? Actually, the matrix A is constructed of 3x3 blocks and is symmetric. A ready routine for the product A*x would be the best solution for me.
In case that there is no ready routine, could you give me an idea by example how I could construct this routine to evaluate a matrix-vector product by fft?
Thank you in advance,
Panos
Very good and interesting question! :)
For certain special matrix structures, the Ax = b problem can be solved very quickly.
Circulant matrices.
Matrices corresponding to cyclic convolution Ax = h*x (* - is convolution symbol) are diagonalized in
the Fourier domain, and can be solved by:
x = ifft(fft(b)./fft(h));
Triangular and banded.
Triangular matrices and diagonally-dominant banded matrices are solved
efficiently by sparse LU factorization:
[L,U] = lu(sparse(A)); x = U\(L\b);
Poisson problem.
If A is a finite difference approximation of the Laplacian, the problem is efficiently solved by multigrid methods (e.g., web search for "matlab multigrid").
Interesting question!
The convolution is not circular in your case, unless you impose additional conditions. For example, A(1,3) should equal A(2,1), etc.
You could do it with conv (retaining only the non-zero-padded part with option valid), which probably is also N*log(N). For example, let
A = [a b c d
e a b c
f e a b
g f e a];
Then A*x is the same as
conv(fliplr([g f e a b c d]),x,'valid').'
Or more generally, A*x is the same as
conv(fliplr([A(end,1:end-1) A(1,:)]),x,'valid').'
I'd like to add some comments on Pio_Koon's answer.
First of all, I wouldn't advise to follow the suggestion for triangular and banded matrices. The time taken by a call to Matlab's lu() procedure on a large sparse matrix massively overshadows any benefits gained by solving the linear system as x=U\(L\b).
Second, in the Poisson problem you end up with a circulant matrix, therefore you can solve it using the FFT as described. In this specific case, your convolution mask h is a Laplacian, i.e., h=[0 -0.25 0; -0.25 1 -0.25; 0 -0.25 0].