public static double[] Subtract(double[] aStore, int aOffset, int aStride, double[] bStore, int bOffset, int bStride, int dimension) { double[] store = new double[dimension]; Blas1.dCopy(aStore, aOffset, aStride, store, 0, 1, dimension); Blas1.dAxpy(-1.0, bStore, bOffset, bStride, store, 0, 1, dimension); return(store); }
// Apply a Householder transfrom defined by v to the vector x (which may be // a column of a matrix, if H is applied from the left, or a row of a matrix // if H is applied from the right). On exit v is unchanged, x is changed. // We have H = I - \beta v v^T, so H x = x - (\beta v^T x) v. public static void ApplyHouseholderReflection( double[] uStore, int uOffset, int uStride, double[] yStore, int yOffset, int yStride, int count ) { double s = Blas1.dDot(uStore, uOffset, uStride, yStore, yOffset, yStride, count); Blas1.dAxpy(-s, uStore, uOffset, uStride, yStore, yOffset, yStride, count); }
public static double[] Multiply(double alpha, double[] store, int offset, int stride, int dimension) { double[] product = new double[dimension]; Blas1.dAxpy(alpha, store, offset, stride, product, 0, 1, dimension); return(product); }
// inverts the matrix in place // the in-place-ness makes this a bit confusing public static void GaussJordanInvert(double[] store, int dimension) { // keep track of row exchanges int[] ps = new int[dimension]; // iterate over dimensions for (int k = 0; k < dimension; k++) { // look for a pivot in the kth column on any lower row int p = k; double q = MatrixAlgorithms.GetEntry(store, dimension, dimension, k, k); for (int r = k + 1; r < dimension; r++) { double s = MatrixAlgorithms.GetEntry(store, dimension, dimension, r, k); if (Math.Abs(s) > Math.Abs(q)) { p = r; q = s; } } ps[k] = p; // if no non-zero pivot is found, the matrix is singular and cannot be inverted if (q == 0.0) { throw new DivideByZeroException(); } // if the best pivot was on a lower row, swap it into the kth row if (p != k) { Blas1.dSwap(store, k, dimension, store, p, dimension, dimension); } // divide the pivot row by the pivot element, so the diagonal element becomes unity MatrixAlgorithms.SetEntry(store, dimension, dimension, k, k, 1.0); Blas1.dScal(1.0 / q, store, k, dimension, dimension); // add factors to the pivot row to zero all off-diagonal elements in the kth column for (int r = 0; r < dimension; r++) { if (r == k) { continue; } double a = MatrixAlgorithms.GetEntry(store, dimension, dimension, r, k); MatrixAlgorithms.SetEntry(store, dimension, dimension, r, k, 0.0); Blas1.dAxpy(-a, store, k, dimension, store, r, dimension, dimension); } } // unscramble exchanges for (int k = dimension - 1; k >= 0; k--) { int p = ps[k]; if (p != k) { Blas1.dSwap(store, dimension * p, 1, store, dimension * k, 1, dimension); } } }