I have to representing a matrix as a list of the matrix rows with the term
like this [[a,b],[c,d]] with representing numbers in Peano notation.
I have to obtain a row of matrix
ow(X,N,C): C is the N-th row of matrix X. and the column of matrix
column(X,N,C): C es the N-th column of matrix X.
alse this One to decompose a matrix in its first column and the rest of the matrix
(which is exactly the same matrix but without the first column):
first_column(X,C,R): matrix X is formed by a first column C in
front of matrix R.
Could somebody help me?
Peano' notation simplifies expressing recursive algorithms.
I assume matrix indexing is 0 based, and will show you how to get the simplest task of your assignment.
row([Row|_], 0, Row).
row([_|Rows], succ(N), Row) :- row(Rows, N, Row).
test (get the second row, with index 1):
?- row([[a,b], [c,d]], succ(0), R).
R = [c, d] ;
false.
This shows the essential ingredients you'll use to answer the other two tasks.
Related
I run into this question because I will apply the preconditioner to my iterative method.
Matlab says [L, U, P] = ilu(A, setup) gives L, U, and a permutation matrix P. I wanna know this P is either column or row permutation matrix. In other words, is PA=LU or AP = LU? Does it matter if I use ilutp or Crout method?
I looked into Matlab carefully and just found the LU function gives row permutation matrix. My guess P is a row permutation matrix because Q is the letter we normally use for column permutation.
Let M be an n x n matrix with each entry equal to either 0 or 1. Let m[i][j]
denote the entry in row i and column j. A diagonal entry is one of the
form m[i][i] for some i. Swapping rows i and j of the matrix M denotes the following action:
we swap the values m[i][k] and m[j][k] for k = 1, 2 ..... n. Swapping two columns
is defined analogously We say that M is re arrangeable if it is possible to swap some of the pairs of rows and some of the pairs of columns (in any sequence) so that,
after all the swapping, all the diagonal entries of M are equal to 1.
(a) Give an example of a matrix M that is not re arrangeable, but for
which at least one entry in each row and each column is equal to !.
(b) Give a polynomial-time algorithm that determines whether a matrix
M with 0-1 entries is re-arrangeable.
I tried a lot but could not reach to any conclusion please suggest me algorithm for that.
I think this post is on topic here because I think the answer is http://en.wikipedia.org/wiki/Assignment_problem. Consider the job of putting a 1 in column i, for each i. Each row could do some subset of those jobs. If you can find an assignment of rows such that there is a different row capable of putting a 1 in each column then you can make the matrix diagonal by rearranging the rows so that row i puts a 1 on column i.
Suppose that there is an assignment that solves the problem. Paint the cells that hold the 1s for the solution red. Notice that permuting rows leaves a single red cell in each row and in each column. Similarly permuting columns leaves a single red cell in each row and each column. Therefore no matter how much you permute rows and columns I can restore the diagonal by permuting rows. Therefore if there is any solution which places 1s on all the diagonals, no matter how much you try to disguise it by permuting both rows and columns I can restore a diagonal by permuting only rows. Therefore the assignment algorithm fails to solve this problem exactly when there is no solution, for example if the only 1s are in the top row and the leftmost column.
Given a 15*15 symmetric matrix, each row containing all the numbers from 1 to 15 and each column containing all the numbers from 1 to 15, how do you go on to prove that all the diagonal elements will be different?
I tried to prove that no two diagonal elements will be same, but couldn't come up with anything solid. Even tried it for 5*5 matrix, but nothing I could come up with to prove it.
Any help would be appreciated!
This is a problem of symmetric latin squares. The first observation (which requires a short proof) is that each of the numbers 1 to 15 occur an even number of times in the off-diagonal positions. Since 15 is odd, this means that each number must occur at least once in the diagonal positions. But there are only 15 diagonal positions and so each number must occur exactly once in the diagonal positions.
If by 'prove' you mean demonstrate for a particular matrix, see below. If by 'prove' you mean mathematically prove, well, all diagonal matrices are symmetric matrices, and a diagonal matrix isn't required to have unique elements, so not all symmetric matrices have unique elements on the diagonal.
One way to test a particular matrix is to make a new array containing all the diagonal elements, then eliminate duplicates in that array, and test the length. Another is to take each diagonal element and compare it against those elements on the diagonal with a higher index. Here's the latter with some pseudocode using 0 based arrays
unique = TRUE
for i = 0 to 14 {
value = matrix[i][i]
for j = i+1 to 14 // doesn't loop if i+1 > 14
if (value == matrix[j][j])
unique = FALSE
}
ADDED: The OP points out I missed the restriction on the contents of each row and column! All symmetric NxN matrices consisting of N unique values with no duplicated values in each row and column must have an antidiagonal consisting of only one value, by the definition of symmetry. If N is odd, the resulting matrix has a single element that is in both the diagonal and antidiagonal (and of course, if N is even, no element is in common). Given this, you can see the diagonal values must differ in each position from the antidiagonal, except in the common element. Combine that with the requirement that each row and each column has N values, and you'll see that the diagonal element must be different for each row. This isn't formal, but I hope it helps.
We can assume the given matrix is m * m, and we should fill the matrix with m distinct numbers: N1, N2 ... Nm.
Because each element should show up in each column/row once, for each number, it will show up n) times in the matrix.
Because it is symmetric, each number will show up x (even) times in the upper section above the diagonal or x (even) times in the lower section below the diagonal. In this way, in addition to the diagonal, each number will show up 2 * x (x is even) times in the matrix.
Therefore, if the given m is odd, each number should show up one more time in the diagonal; if the given is even, we don't require each number show up on the diagonal cause 2 * x is already even.
We are given an N dimensional matrix of order [m][m][m]....n times where value position contains the value sum of its index..
For example in 6x6 matrix A, value at position A[3][4] will be 7.
We have to find out the total number of counts of elements greater than x. For 2 dimensional matrix we have following approach:
If we know the one index say [i][j] {i+j = x} then we create a diagonal by just doing [i++][j--] of [i--][j++] with constraint that i and j are always in range of 0 to m.
For example in two dimensional matrix A[6][6] for value A[3][4] (x = 7), diagonal can be created via:
A[1][6] -> A[2][5] -> A[3][4] -> A[4][3] -> A[5][2] -> A[6][2]
Here we have converted our problem into another problem which is count the element below the diagonal including the diagonal.
We can easily count in O(m) complexity instead spending O(m^2) where 2 is order of matrix.
But if we consider N dimensional matrix, how we will do it, because in N dimensional matrix if we know the index of that location,
where sum of index is x say A[i1][i2][i3][i4]....[in] times.
Then there may be multiple diagonal which satisfy that condition, say by doing i1-- we can increment any of {i2, i3, i4....in}
So, above used approach for 2 dimensional matrix become useless here... because there is only two variable quantity i1 and i2 is present.
Please help me to find solution
For 2D: count of the elements below diagonal is triangular number.
For 3D: count of the elements below diagonal plane is tetrahedral number
Note that Kth tetrahedral number is the sum of the first K triangular numbers.
For nD: n-simplexial (I don't know exact english term) number (is sum of first (n-1)-simplexial numbers).
The value of Kth n-simplexial is
S(k, n) = k * (k+1) * (k+2).. (k + n - 1) / n! = BinomialCoefficient(k+n-1, n)
Edit: this method works "as is" for limited values of X below main anti-diagonal (hyper)plane.
Generating function approach:
Let's we have polynom
A(s)=1+s+s^2+s^3+..+s^m
then it's nth power
B(s) = An(s) has important property: coefficient of kth power of s is the number of ways to compose k from n summands. So the sum of nth to kth coefficients gives us the count of the elements below kth diagonal
For a 2-dimensional matrix, you converted the problem into another problem, which is count the elements below the diagonal including the diagonal.
Try and visualize it for a 3-d matrix. In case of a 3-dimensional matrix, the problem will be reduced to another problem, which is to count the elements below the diagonal plane including the diagonal
We have three sets S1, S2, S3. I need to find x,y,z such that
x E S1
y E S2
z E S3
let min denote the minimum value out of x,y,z
let max denote the maximum value out of x,y,z
The range denoted by max-min should be the MINIMUM possible value
Of course, the full-bruteforce solution described by IVlad is simple and therefore, easier and faster to write, but it's complexity is O(n3).
According to your algorithm tag, I would like to post a more complex algorithm, that has a O(n2) worst case and O(nlogn) average complexity (almost sure about this, but I'm too lazy to make a proof).
Algorithm description
Consider thinking about some abstract (X, Y, Z) tuple. We want to find a tuple that has a minimal distance between it's maximum and minimum element. What we can say at this point is that distance is actually created by our maximum element and minimum element. Therefore, the value of element between them really doesn't matter as long as it really lies between the maximum and the minimum.
So, here is the approach. We allocate some additional set (let's call it S) and combine every initial set (X, Y, Z) into one. We also need an ability to lookup the initial set of every element in the set we've just created (so, if we point to some element in S, let's say S[10] and ask "Where did this guy come from?", our application should answer something like "He comes from Y).
After that, let's sort our new set S by it's keys (this would be O(n log n) or O(n) in some certain cases)
Determining the minimal distance
Now the interesting part comes. What we want to do is to compute some artificial value, let's call it minimal distance and mark it as d[x], where x is some element from S. This value refers to the minimal max - min distance which can be achived using the elements that are predecessors / successors of current element in the sequence.
Consider the following example - this is our S set(first line shows indexes, second - values and letters X, Y and Z refer to initial sets):
0 1 2 3 4 5 6 7
------------------
1 2 4 5 8 10 11 12
Y Z Y X Y Y X Z
Let's say we want to compute that our minimal distance for element with index 4. In fact, that minimal distance means the best (x, y, z) tuple that can be built using the selected element.
In our case (S[4]), we can say that our (x, y, z) pair would definitely look like (something, 8, something), because it should have the element we're counting the distance for (pretty obvious, hehe).
Now, we have to fill the gaps. We know that elements we're seeking for, should be from X and Z. And we want those elements to be the best in terms of max - min distance. There is an easy way to select them.
We make a bidirectional run (run left, the run right from current element) seeking for the first element-not-from-Y. In this case we would seek for two nearest elements from X and Z in two directions (4 elements total).
This finding method is what we need: if we select the first element of from X while running (left / right, doesn't matter), that element would suit us better than any other element that follows it in terms of distance. This happens because our S set is sorted.
In case of my example (counting the distance for element with index number 4), we would mark elements with indexes 6 and 7 as suitable from the right side and elements with indexes 1 and 3 from the left side.
Now, we have to test 4 cases that can happen - and take the case so that our distance is minimal. In our particular case we have the following (elements returned by the previous routine):
Z X Y X Z
2 5 8 11 12
We should test every (X, Y, Z) tuple that can be built using these elements, take the tuple with minimal distance and save that distance for our element. In this example, we would say that (11, 8, 12) tuple has the best distance of 4. So, we store d[5] = 4 (5 here is the element index).
Yielding the result
Now, when we know how to find the distance, let's do it for every element in our S set (this operation would take O(n2) in the worst case and better time - something like O(nlogn) in average).
After we have that distance value for every element in our set, just select the element with minimal distance and run our distance counting algorithm (which is described above) for it once again, but now save the (-, -, -) tuple. It would be the answer.
Pseudocode
Here is comes the pseudocode, I tried to make it easy to read, but it's implementation would be more complex, because you'll need to code set lookups *("determine set for element"). Also note that determine tuple and determine distance routines are basically the same, but the second yields the actual tuple.
COMBINE (X, Y, Z) -> S
SORT(S)
FOREACH (v in S)
DETERMINE_DISTANCE(v, S) -> d[v]
DETERMINE_TUPLE(MIN(d[v]))
P.S
I'm pretty sure that this method could be easily used for (-, -, -, ... -) tuple seeking, still resulting in good algorithmic complexity.
min = infinity (really large number in practice, like 1000000000)
solution = (-, -, -)
for each x E S1
for each y E S2
for each z E S3
t = max(x, y, z) - min(x, y, z)
if t < min
min = t
solution = (x, y, z)