Determine elements of a matrix when sum of rows and columns are given - matrix

There is 4x4 matrix with all 4 diagonal elements zero. All other elements are non negative integers. Sum of all 4 rows and 4 columns are known individually. Is it possible to determine the remaining 12 elements of the matrix? Eg
0 1 1 0 sum=2
2 0 0 1 sum=3
4 1 0 0 sum=5
0 1 6 0 sum=7
sum=6 sum=3 sum=7 sum=1
Any guidance will be very helpful.
Thanks

The matrix is
0 a12 a13 a14
a21 0 a23 a24
a31 a32 0 a34
a41 a42 a43 0
The problem is to solve a set of linear equations:
a12 + a13 + a14 = c1
a21 + a23 + a24 = c2
and so on. We have 12 variables and 8 equations (4 for the rows and 4 for the columns). To solve a linear equation system in 12 variables, we generally need 12 equations. Since the number of equations is lesser, the system will not have a unique solution. It may have infinitely many solutions.

The matrix is
0 a12 a13 a14
a21 0 a23 a24
a31 a32 0 a34
a41 a42 a43 0
The problem is to solve a set of linear equations:
a12 + a13 + a14 = r1
a21 + a23 + a24 = r2
a31 + a32 + a34 = r3
a41 + a43 + a44 = r4
a21 + a31 + a41 = c1
a12 + a32 + a42 = c2
a13 + a23 + a43 = c3
a14 + a34 + a44 = c4
Thus you need to solve an equation of the form Ax = b with A consisting of only 0 and 1 coefficients. Use Gauss Elimination and Euclidian Algorithm to find integer Matrices S, D, T such that D is in Diagonal form and SDT = A. If you do not know how to do this search the web for Smith normal form algorithm.
Then
SDTx = Ax = b
Thus
DTx = S-1Ax = S-1b
Since D is in diagonal form you can check if you can solve
Dy = S-1b
for y. You also find a base for the (Homogenous) solution space. This in turn can then be used to cut down the complexity in the search for the positive solutions of the original equation.

Related

Pulling information from two URIs using Hadoop

Let's say I've got one giant sparse matrix in HDFS at matrixX and another at matrixY and I want to do a giant matrix multiplication and write to matrixY. Can I draw from two different URIs in Hadoop? How do I do this? All the examples I've seen have one input directory and one output directory.
You can read from multiple sources as below:
MultipleInputs.addInputPath(jobConf, MultipleInputs.addInputPath(jobConf,
new Path(MatixX),
TextInputFormat.class,
ColumnReaderMapper.class);
MultipleInputs.addInputPath(jobConf,
new Path(MatirxY),
TextInputFormat.class,
RowReaderMapper.class);
Lets say you have to matrix A and B:
a11 a12 a13
a21 a22 a23
a31 a32 a33
b11 b12 b13
b21 b22 b23
b31 b32 b33
A * B = summation (row of a * column of b)
How about storing Matrix A as row major and matrix b as column major. Now you can set keys as row number in RowReader and Column Number in Columnreader in next mapper and send it to the same reducer which will do the summation and write the output. You can do many other optimizations but that should be the first start.

Method for interpolating value at the midpoint of a square, given the values, first and second derivatives at the corners?

All the practical examples of spatial interpolation I've been able to find work by sampling additional surrounding points to estimate the derivatives. But is there a simpler method if the derivatives are already known—and if you only need the value (and derivatives) for the single point at the center of the known points?
To illustrate: suppose for each of the points (1, 1), (-1, 1), (-1, -1), and (1, -1) you know f(x, y), f'(x), f''(x), f'(y), and f''(y) — and you want interpolated values at (0, 0) for f(x, y), f'(x), f''(x), f'(y), and f''(y).
First of all the problem as posed does not make sense. In multi-variable calculus we don't have derivatives, we have partial derivatives. Lots of them.
Suppose you have the value, first partial derivatives and second partial derivatives at the corners. So at each corner we know the value, the partial by x, the partial by y, the second partial by x by x, the second partial by x by y, and the second partial by y by y. We have 6 pieces of data per corner, for 24 pieces of data total.
Next what we do is try to fit this to an appropriate polynomial. 24 terms, that would be, a0 + a1 x + a2 y + a3 x^2 + a4 x y + a5 y^2 + a6 x^3 + a7 x^2 y + a8 x y^2 + a9 y^3 + a10 x^4 + a11 x^3 y + a12 x^2 y^2 + a13 x y^3 + a14 y^4 + a15 x^5 + a16 x^4 y + a17 x^3 y^2 + a18 x^2 y^3 + a18 x y^4 + a19 y^5 + a20 x^6 + a21 x^4 y^2 + a22 x^2 y^4 + a23 y^6. (I had to leave out some 6th power terms because I was hitting that 24 limit.)
If you calculate that out, matching up all of those values against all of those points you get 24 equations in 24 variables. Solve and you get all of the coefficients to use. Plug in the value (0, 0) and you have your interpolation.
Straightforward, tedious, and not for the faint of heart.

Divide and Conquer Matrix Multiplication

I am having trouble getting divide and conquer matrix multiplication to work. From what I understand, you split the matrices of size nxn into quadrants (each quadrant is n/2) and then you do:
C11 = A11⋅ B11 + A12 ⋅ B21
C12 = A11⋅ B12 + A12 ⋅ B22
C21 = A21 ⋅ B11 + A22 ⋅ B21
C22 = A21 ⋅ B12 + A22 ⋅ B22
My output for divide and conquer is really large and I'm having trouble figuring out the problem as I am not very good with recursion.
example output:
Original Matrix A:
4 0 4 3
5 4 0 4
4 0 4 0
4 1 1 1
A x A
Classical:
44 3 35 15
56 20 24 35
32 0 32 12
29 5 21 17
Divide and Conquer:
992 24 632 408
1600 272 720 1232
512 0 512 384
460 17 405 497
Could someone tell me what I am doing wrong for divide and conquer? All my matrices are int[][] and classical method is the traditional 3 for loop matrix multiplication
You are recursively calling divideAndConquer in the wrong way. What your function does is square a matrix. In order for divide and conquer matrix multiplication to work, it needs to be able to multiply two potentially different matrixes together.
It should look something like this:
private static int[][] divideAndConquer(int[][] matrixA, int[][] matrixB){
if (matrixA.length == 2){
//calculate and return base case
}
else {
//make a11, b11, a12, b12 etc. by dividing a and b into quarters
int[][] c11 = addMatrix(divideAndConquer(a11,b11),divideAndConquer(a12,b21));
int[][] c12 = addMatrix(divideAndConquer(a11,b12),divideAndConquer(a12,b22));
int[][] c21 = addMatrix(divideAndConquer(a21,b11),divideAndConquer(a22,b21));
int[][] c22 = addMatrix(divideAndConquer(a21,b12),divideAndConquer(a22,b22));
//combine result quarters into one result matrix and return
}
}
Some debugging approaches to try:
Try some very simple test matrices as input (e.g. all zeros, with a one or a few strategic ones). You may see a pattern in the "failures" that will show you where your error(s) are.
Make sure your "classical" approach is giving you correct answers. For small matrices, you can use Woflram Alpha on-line to test answers: http://www.wolframalpha.com/examples/Matrices.html
To debug recursion: add printf() statements at the entry and exit of your function, including the invocation arguments. Run your test matrix, write the output to a log file, and open the log file with a text editor. Step through each case, writing your notes alongside in the editor making sure it's working correctly at each step. Add more printf() statements and run again if needed.
Good luck with the homework!
Could someone tell me what I am doing wrong for divide and conquer?
Yes:
int[][] a = divideAndConquer(topLeft);
int[][] b = divideAndConquer(topRight);
int[][] c = divideAndConquer(bottomLeft);
int[][] d = divideAndConquer(bottomRight);
int[][] c11 = addMatrix(classical(a,a),classical(b,c));
int[][] c12 = addMatrix(classical(a,b),classical(b,d));
int[][] c21 = addMatrix(classical(c,a),classical(d,c));
int[][] c22 = addMatrix(classical(c,b),classical(d,d));
You are going through an extra multiplication step here: you shouldn't be calling both divideAndConquer() and classical().
What you are effectively doing is:
C11 = (A11^2)⋅(B11^2) + (A12^2)⋅(B21^2)
C12 = (A11^2)⋅(B12^2) + (A12^2)⋅(B22^2)
C21 = (A21^2)⋅(B11^2) + (A22^2)⋅(B21^2)
C22 = (A21^2)⋅(B12^2) + (A22^2)⋅(B22^2)
which is not correct.
First, remove the divideAndConquer() calls, and replace a/b/c/d by topLeft/topRight/etc.
See if it gives you the proper results.
Your divideAndConquer() method needs a pair of input parameters, so you can use A*B. Once you get that working, get rid of the calls to classical(), and use divideAndConquer() instead. (or save them for matrices that are not a multiple of 2 in length.)
You might find the Wiki article on Strassen's algorithm helpful.

Accessing a matrix in Matlab

Assume user input data as below. I define my matrix is cost. The matrix i created is 3 by 3 matrix. So the matrix should form like this:
cost = [c11 c12 c13
c21 c22 c23
c31 c32 c33]
Since i want to display set of row, i do it like this :
c1 = cost(1,:); % it will become c1 = c11 c12 c13
c2 = cost(2,:); % it will become c2 = c21 c22 c23
c3 = cost(3,:); % it will become c3 = c31 c32 c33
Then i want the value in the matrix. I do it like this.
c11 = cost(1,1);
c12 = cost(1,2);
c13 = cost(1,3);
c21 = cost(2,1);
c22 = cost(2,2);
c23 = cost(2,3);
c31 = cost(3,1);
c32 = cost(3,2);
c33 = cost(3,3);
So this is the equation that i want to use for this type of matrix.
lambda =
((8*c13*c23*c33*Pdt)+(4*c12*c23*c33)+(4*c13*c22*c33)+(4*c13*c23*c32)) ./ (4*c23*c33)+(4*c13*c33)+(4*c13*c23));
So my problem is, if i want to make 4 by 3 matrix, and it would generate a matrix like this:
cost = [c11 c12 c13
c21 c22 c23
c31 c32 c33
c41 c42 c43]
The equation that i want to use for this matrix(4 by 3) is quite different. So how im gonna do it? Do i need to use if else statement? or do while? Can anyone help me solve this? Can anyone create the code?
Why do you explicitly create the variables c11, c12, ...? Surely it would be easier to just access the matrix in your equation like this:
lambda =
((8*cost(1,3)*cost(2,3)*cost(3,3)*Pdt)+(4*cost(1,2)*cost(2,3)*cost(3,3)+(4*cost(1,3)*cost(2,2)*c(3,3))+(4*cost(1,3)*cost(2,3)*cost(3,2)) ./
(4*cost(2,3)*cost(3,3))+(4*cost(1,3)*cost(3,3))+(4*cost(1,3)*cost(2,3)));
For your question, yes, just use a simple if statment, like this:
if size(cost,1) == 3
%equation for matrix size 3x3
else
%equation for matriz size 4x3

Projective transformation fitting

Given set of points in 3D ( X = (x1, x2, x3), Y = (y1, y2, y3) ), how can I fit transformation from X to Y?
As far as I know this is called projective transformation.
Here is example of X and Y.
Blue and red lines in X are parallel, but they are not parallel in Y.
Projective transformations in 3d have an associated 4x4 matrix (modulo a constant multiplication). You can find the matrix with least square fitting.
Well. I found some useful information:
This transformation is non-linear and it is not possible to represent non-linear transformation with a matrix. There are some tricks such as using homogenous coordinates. but it doesn't make all non-linear transformations representable using matrices.
However, approximating a nonlinear function by a linear function is possible.
So, the task is to find best fitting linear transformation, right?
There is a simple solution using linear regression.
Say the transformation matrix is named A and has dimensions 3x3. And say you have N vectors (points) in 3D before and after the transformation - so you have matrices X and Y of 3 rows and N columns. Then the transformation is:
Y = A X + B
where B is a vector of length 3 and specifies the shift. You can rewrite the matrix multiplication using indices:
y[i,j] = sum(k=1..3)(a[i,k] * x[k,j]) + b[i]
for i = 1..3 and j = 1 .. N. So, you have 12 unknown variables (a, b), and 3 * N equations. For N >= 4, you simply find the best solution using linear regression.
For example, in R it is very easy:
# input data
X = matrix(c(c(0, 0, 0), c(1, 0, 0), c(0, 1, 0), c(0, 1, 1)), nrow = 3)
Y = matrix(c(c(1, 0, 1), c(2, 0, 1), c(1, 1, 1), c(1, 1, 2)), nrow = 3)
# expected transformation: A is identity matrix, b is [1, 0, 1]
N = dim(Y)[2]
# transform data for regression
a1 = rbind(t(X), matrix(rep(0, 3*2*N), ncol = 3))
a2 = rbind(matrix(rep(0, 3*N), ncol = 3), t(X), matrix(rep(0, 3*N), ncol = 3))
a3 = rbind(matrix(rep(0, 3*2*N), ncol = 3), t(X))
b1 = rep(1:0, c(N, 2*N))
b2 = rep(c(0, 1, 0), each = N)
b3 = rep(0:1, c(2*N, N))
y = as.vector(t(Y))
# do the regression
summary(lm(y ~ 0 + a1 + a2 + a3 + b1 + b2 + b3))
And the output is:
[...]
Coefficients:
Estimate Std. Error t value Pr(>|t|)
a11 1.000e+00 NA NA NA
a12 -2.220e-16 NA NA NA
a13 -3.612e-32 NA NA NA
a21 7.850e-17 NA NA NA
a22 1.000e+00 NA NA NA
a23 -1.743e-32 NA NA NA
a31 0.000e+00 NA NA NA
a32 0.000e+00 NA NA NA
a33 1.000e+00 NA NA NA
b1 1.000e+00 NA NA NA
b2 -7.850e-17 NA NA NA
b3 1.000e+00 NA NA NA
Residual standard error: NaN on 0 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: NaN
F-statistic: NaN on 12 and 0 DF, p-value: NA
as expected.

Resources