N x N identity matrix in MATLAB - algorithm

I am having difficulty creating a generic N x N identity matrix in Matlab.
I am given a system where
Ai,j =
{1, if i does not equal j
{n, if i = j}
You are asked to compute this when the value of the identity matrix n = 10, n = 20.
What I don't see is how to apply matrix indexing here. That is easy enough to do, but how do I account for the given linear system?

There is a builtin function for creating a unit matrix called eye.
have a look at the documentation http://au.mathworks.com/help/matlab/ref/eye.html?requestedDomain=au.mathworks.com
Also, ones(n,m) creates a matrix of ones.
For a square matrix use (n-1)*eye(n) + ones(n) and for non-square
(n-1)*eye(n, m) + ones(n, m)

Related

Making a customizable LCG that travels backward and forward

How would i go about making an LCG (type of pseudo random number generator) travel in both directions?
I know that travelling forward is (a*x+c)%m but how would i be able to reverse it?
I am using this so i can store the seed at the position of the player in a map and be able to generate things around it by propogating backward and forward in the LCG (like some sort of randomized number line).
All LCGs cycle. In an LCG which achieves maximal cycle length there is a unique predecessor and a unique successor for each value x (which won't necessarily be true for LCGs that don't achieve maximal cycle length, or for other algorithms with subcycle behaviors such as von Neumann's middle-square method).
Suppose our LCG has cycle length L. Since the behavior is cyclic, that means that after L iterations we are back to the starting value. Finding the predecessor value by taking one step backwards is mathematically equivalent to taking (L-1) steps forward.
The big question is whether that can be converted into a single step. If you're using a Prime Modulus Multiplicative LCG (where the additive constant is zero), it turns out to be pretty easy to do. If xi+1 = a * xi % m, then xi+n = an * xi % m. As a concrete example, consider the PMMLCG with a = 16807 and m = 231-1. This has a maximal cycle length of m-1 (it can never yield 0 for obvious reasons), so our goal is to iterate m-2 times. We can precalculate am-2 % m = 1407677000 using readily available exponentiation/mod libraries. Consequently, a forward step is found as xi+1 = 16807 * xi % 231-1, while a backwards step is found as xi-1 = 1407677000 * xi % 231-1.
ADDITIONAL
The same concept can be extended to generic full-cycle LCGs by casting the transition in matrix form and doing fast matrix exponentiation to come up with the equivalent one-stage transform. The matrix formulation for xi+1 = (a * xi + c) % m is Xi+1 = T · Xi % m, where T is the matrix [[a c],[0 1]] and X is the column vector (x, 1) transposed. Multiple iterations of the LCG can be quickly calculated by raising T to any desired power through fast exponentiation techniques using squaring and halving the power. After noticing that powers of matrix T never alter the second row, I was able to focus on just the first row calculations and produced the following implementation in Ruby:
def power_mod(ary, mod, power)
return ary.map { |x| x % mod } if power < 2
square = [ary[0] * ary[0] % mod, (ary[0] + 1) * ary[1] % mod]
square = power_mod(square, mod, power / 2)
return square if power.even?
return [square[0] * ary[0] % mod, (square[0] * ary[1] + square[1]) % mod]
end
where ary is a vector containing a and c, the multiplicative and additive coefficients.
Using this with power set to the cycle length - 1, I was able to determine coefficients which yield the predecessor for various LCGs listed in Wikipedia. For example, to "reverse" the LCG with a = 1664525, c = 1013904223, and m = 232, use a = 4276115653 and c = 634785765. You can easily confirm that the latter set of coefficients reverses the sequence produced by using the original coefficients.

degenerate in simplex method

I am reading about linear programming using simplex method in book Introduction to Algorithm Design and analysis.
I am having difficulty in understanding text. Here is text snippet.
The principal advantage of the standard form lies in the simple
mechanism it provides for identifying extreme points of the feasible
region. For the general case of a problem with m equations in n
unknowns (n ≥ m), n − m variables need to be set to zero to get a
system of m equations in m unknowns. If the system obtained has a
unique solution—as any nondegenerate system of linear equations with
the number of equations equal to the number of unknowns does—we have a
basic solution; its coordinates set to zero before solving the system
are called nonbasic, and its coordinates obtained by solving the
system are called basic.
My questions on above text are
What is nondegenerate system of linear equations?
What does author mean by unique solution here in above context?
As author says, "For the general case of a problem with m equations in n unknowns (n ≥ m), (n − m) variables need to be set to zero to get a system of m equations in m unknowns." So when you set (n-m) variables as zero, you will have a system of m equations with m variables.
Let me explain with an example and basic math. Suppose that m = 2, and the system of equations you have is :
x + y = 2 & x - y = 0
So this system of equations has "unique solution", where (x = 1 & y = 1)
Now suppose that m = 3, and the system of equations you have is :
x + y + z = 3, x + y - z = 1, & x + y = 2
In this case, any point with z = 1 and satisfying equation x + y = 2 will satisfy all the equations. For example, (x = 1, y = 1, & z = 1), (x = 2, y = 0, & z = 1), and (x = 4, y = - 2, & z = 1). So here you have more than one solution to this system of equations, so you will say that this system of equations has "multiple solutions".
Now let us talk about degeneracy. Assuming that you get unique solution to the system of equations, If you solve system of m equations with m variables, you will get numerical value for all m variables. If any of these m variables have their numerical value equal to zero, you will say that solution is degenerate. By non-degenerate, author means that all of the variables have non-zero value in solution. Degeneracy tends to increase the number of simplex iterations before reaching the optimal solution.
Now let us talk a little about simplex method. Suppose you have set (n-m) out of n variables as zero (as author says), and you get an unique non-degenerate solution. You say this solution as basic solution. But their is catch here, you don't know which of (n-m) variables out of n variables have to be set as zero to find optimal solution. In theory, you will have n_combinatorial_(n-m) basic solutions to evaluate. This is where simplex method helps. It helps you in identifying optimal set of variables to be set as zero in order to find optimal solution. Once you have optimally identified variables to be set as zero, and your reduced system of equations have unique solution, then solution to this system of equations is the optimal solution.
I hope it helps :-)

Diagonalizable Matrix in MATLAB

Is there a way to generate N x N random diagonalizable matrix in MATLAB? I tried as following:
N = 10;
A = diag(rand(N,N))
but it is giving me an N x 1 matrix. I also need the matrix to be symmetric.
Assuming that you are considering real-valued matrices: Every real symmetric matrix is diagonalizable. You can therefore randomly generate some matrix A, e.g. by using A = rand(N, N), and then symmetrize it, e.g. by
A = A + A'
For complex matrices the condition for diagonalizability is that the matrix is normal. If A is an arbitrary square random matrix, you can normalize it by
A = A * A'
All full-rank matrices are diagonalizable by SVD or eigen-decomposition.
If you want a random symmetric matrix...
N = 5
V = rand(N*(N+1)/2, 1)
M = triu(ones(N))
M(M==1) = V
M = M + tril(M.',-1)
#DavidEisenstat is right. I tried his example. Sorry for the false statement. Here's a true statement that is relevant specifically to your situation, but is not as general: Random matrices are virtually guaranteed to be diagonalizable.

Algorithm for orthogonal polynomials

and thank you for the attention you're paying to my question :)
My question is about finding an (efficient enough) algorithm for finding orthogonal polynomials of a given weight function f.
I've tried to simply apply the Gram-Schmidt algorithm but this one is not efficient enough. Indeed, it requires O(n^2) integrals. But my goal is to use this algorithm in order to find Hankel determinants of a function f. So a "direct" computation wich consists in simply compute the matrix and take its determinants requires only 2*n - 1 integrals.
But I want to use the theorem stating that the Hankel determinant of order n of f is a product of the n first leading coefficients of the orthogonal polynomials of f. The reason is that when n gets larger (say about 20), Hankel determinant gets really big and my goal is to divided it by an other big constant (for n = 20, the constant is of order 10^103). My idea is then to "dilute" the computation of the constant in the product of the leading coefficients.
I hope there is a O(n) algorithm to compute the n first orthogonal polynomials :) I've done some digging and found nothing in that direction for general function f (f can be any smooth function, actually).
EDIT: I'll precise here what the objects I'm talking about are.
1) A Hankel determinant of order n is the determinant of a square matrix which is constant on the skew diagonals. Thus for example
a b c
b c d
c d e
is a Hankel matrix of size 3 by 3.
2) If you have a function f : R -> R, you can associate to f its "kth moment" which is defined as (I'll write it in tex) f_k := \int_{\mathbb{R}} f(x) x^k dx
With this, you can create a Hankel matrix A_n(f) whose entries are (A_n(f)){ij} = f{i+j-2}, that is something of the like
f_0 f_1 f_2
f_1 f_2 f_3
f_2 f_3 f_4
With this in mind, it is easy to define the Hankel determinant of f which is simply
H_n(f) := det(A_n(f)). (Of course, it is understood that f has sufficient decay at infinity, this means that all the moments are well defined. A typical choice for f could be the gaussian f(x) = exp(-x^2), or any continuous function on a compact set of R...)
3) What I call orthogonal polynomials of f is a set of polynomials (p_n) such that
\int_{\mathbb{R}} f(x) p_j(x) p_k(x) is 1 if j = k and 0 otherwize.
(They are called like that since they form an orthonormal basis of the vector space of polynomials with respect to the scalar product
(p|q) = \int_{\mathbb{R}} f(x) p(x) q(x) dx
4) Now, it is basic linear algebra that from any basis of a vector space equipped with a scalar product, you can built a orthonormal basis thanks to the Gram-Schmidt algorithm. This is where the n^2 integrations comes from. You start from the basis 1, x, x^2, ..., x^n. Then you need n(n-1) integrals for the family to be orthogonal, and you need n more in order to normalize them.
5) There is a theorem saying that if f : R -> R is a function having sufficient decay at infinity, then we have that its Hankel determinant H_n(f) is equal to
H_n(f) = \prod_{j = 0}^{n-1} \kappa_j^{-2}
where \kappa_j is the leading coefficient of the j+1th orthogonal polynomial of f.
Thank you for your answer!
(PS: I tagged octave because I work in octave so, with a bit of luck (but I doubt it), there is a built-in function or a package already done managing this kind of think)
Orthogonal polynomials obey a recurrence relation, which we can write as
P[n+1] = (X-a[n])*P[n] - b[n-1]*P[n-1]
P[0] = 1
P[1] = X-a[0]
and we can compute the a, b coefficients by
a[n] = <X*P[n]|P[n]> / c[n]
b[n-1] = c[n-1]/c[n]
where
c[n] = <P[n]|P[n]>
(Here < | > is your inner product).
However I cannot vouch for the stability of this process at large n.

Maximum Subrectangle for very special matrices

While working on an image processing task I have come across the following problem: There are n points in the unit square with coordinates $x_i$ and $y_i$, each assigned with a positive or negative weight $w_i$. Find a rectangle such that the sum of all weights of those points lying within the rectangle is positive and maximal.
By defining a proper grid, the problem can be rephrased as finding a submatrix in an n-by-n matrix A whose sum of elements is maximal. This is also known as the "maximal subrectangle problem" and has been discussed on SO before. While a brute force approach has a run-time of O(n^5), there is a kind of tricky solution with a run-time of O(n^3). It utilizes a solution for the corresponding one-dimensional problem, called "maximal subarray problem", with an O(n) run-time.
I have implemented both algorithms in R and can solve 100s of points in a few seconds. But with thousands of points it will be much too slow, probably even when outsourcing the loops to some Fortran or C code.
Now look at the matrix A. When assuming (w/o loss of generality) that all points have different x- or y-coordinates, A has a special form: In each row and column of A there is exactly one non-zero element. For matrices with this special property I assume there should be an algorithm performing the task in O(n^2) time, or even better.
Here is an example with the optimal rectangle added:
set.seed(723)
N <- 50; w <- rnorm(N)
x <- runif(N); y <- runif(N)
clr <- ifelse (w >= 0, "blue", "red")
plot(x, y, pch = 20, col = clr, xlim = c(0, 1), ylim = c(0, 1))
rect(0.075, 0.45, 0.31, 0.95, border="gray")
You see that there can be red, ie. negative, points in the optimal rectangle. It also shows that it will not suffice to solve the one-dimensional cases for the x- and y-coordinates.
I will translate the standard solution into Fortran, but I would surely like to have a more efficient algorithm at hand.
These guys (found from the wiki page) claim to have a simpler sub-cubic solution for the 2-dimensional case. It may be the one you're already aware of.
See the accepted answer for "Maximum sum subrectangle in a sparse matrix". For an nxn matrix with m non-zero elements, the solution there takes O(nm log n) time. So, for you, since you have exactly n non-zero elements, this would give O(n^2 log n) time. Probably you'll be able to handle cases with n being 50 times larger or more, vs. the standard O(n^3) solution.
The best I can do is O(n^2 log n).
If we look at the n+1 choose 2 calls made by Kadane's 2D algorithm to Kadane's 1D algorithm on an input of your type, all but O(n) successive pairs are on 1D arrays that differ only in one element. I'm going to present a divide-and-conquer variant of Kadane's 1D; by caching the outcomes of each recursive call, only the O(log n) that involve the changed array element have to be recomputed, reducing the (amortized) running time of the inner loop from Theta(n) to Theta(log n).
def maxsubarray(arr, a, b):
# this function returns a 4-tuple
# element 0 is the max over intervals of the form [i, j)
# element 1 is the max over intervals of the form [i, b)
# element 2 is the max over intervals of the form [a, j)
# element 3 is the max over intervals of the form [a, b), i.e., sum(arr[a:b])
n = b - a
if n == 0:
return (0, 0, 0, 0)
elif n == 1:
x = arr[a]
y = max(x, 0)
return (y, y, y, x)
else:
m = a + n // 2
l = maxsubarray(arr, a, m)
r = maxsubarray(arr, m, b)
return (max(l[0], r[0], l[1] + r[2]),
max(r[1], l[1] + r[3]),
max(l[2], l[3] + r[2]),
l[3] + r[3])

Resources