Monday, November 3, 2014

#codingexercise
int[,] Transpose (int[,] matrix, int row, int col)
{
   var ret = new int[col, row]
   for (int i = 0; i < row; i++)
     for (int j = 0; j < col; j++)
     {
           ret[j, i] = matrix[i,j]
     }
    return ret;
}

Assert (matrix = Transpose(Transpose(matrix));

We will continue to look at the convergence of Steepest Descent method. Let's first consider the case where ei is an eigenvector with eigenvalue Lambda-e. Then the residual which is A applied to the negative eigenvector ei, is also an eigenvector.  Since we discussed earlier that the position in the next step can be determined from the position in the current step based on step length and direction, we see that when we express the residuals as eigen vectors, the next residual turns out to be zero. This implies that it takes only one step to converge to the exact solution.  The current position lies on one of the axes of the ellipsoid, and so the residual points directly to the center of the ellipsoid. Choosing gradient as  negative eignenvalue gives us instant convergence.

#codingexercise
Matrix multiplication
Int [, ] multiply ( int [,] left, int [,] right)
{
If (left == null || right == null) return null;
Int lc  = left.GetLength (0);
Int lr   = left.GetLength (1);
Int rc  = right.GetLength (0);
Int rr  = right.GetLength (1);
If (lc != rr) return null;
Var ret = new int [lr, rc]();
For int r = 0; r < lr;  r++;
   For int  c = 0; c < rr; c++
     {
         For ( int k = 0; k < lc; k++)
                     Ret [r,c] += left [r,k] × right [k, r];
}
Return ret;

}

1 comment: