public static double[] Skew(double[] vec, double theta) { double cos = System.Math.Cos(theta); double sin = System.Math.Sin(theta); double[,] m = new double[, ] { { cos, 0.0, 0.0 }, { sin, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } }; NcvMatrix mat = new NcvMatrix(m); return(mat.Multiply(vec)); }
public static double[] Rotate(double[] vec, double alpha) { double cos = System.Math.Cos(alpha); double sin = System.Math.Sin(alpha); double[,] m = new double[, ] { { cos, -sin, 0.0 }, { sin, cos, 0.0 }, { 0.0, 0.0, 1.0 } }; NcvMatrix mat = new NcvMatrix(m); return(mat.Multiply(vec)); }
public static NcvMatrix del2(NcvMatrix m) { int rows = m.rows(); int cols = m.cols(); /* IV: Compute Laplace operator using Neuman condition */ /* IV.1: Deal with corners */ NcvMatrix Lu = new NcvMatrix(cols, rows); int rows_1 = rows - 1; int rows_2 = rows - 2; int cols_1 = cols - 1; int cols_2 = cols - 2; Lu[0, 0] = (m[0, 1] + m[1, 0]) * 0.5 - m[0, 0]; Lu[cols_1, 0] = (m[cols_2, 0] + m[cols_1, 1]) * 0.5 - m[cols_1, 0]; Lu[0, rows_1] = (m[1, rows_1] + m[0, rows_2]) * 0.5 - m[0, rows_1]; Lu[cols_1, rows_1] = (m[cols_2, rows_1] + m[cols_1, rows_2]) * 0.5 - m[cols_1, rows_1]; /* IV.2: Deal with left and right columns */ for (int y = 1; y <= rows_2; y++) { Lu[0, y] = (2 * m[1, y] + m[0, y - 1] + m[0, y + 1]) * 0.25 - m[0, y]; Lu[cols_1, y] = (2 * m[cols_2, y] + m[cols_1, y - 1] + m[cols_1, y + 1]) * 0.25 - m[cols_1, y]; } /* IV.3: Deal with top and bottom rows */ for (int x = 1; x <= cols_2; x++) { Lu[x, 0] = (2 * m[x, 1] + m[x - 1, 0] + m[x + 1, 0]) * 0.25 - m[x, 0]; Lu[x, rows_1] = (2 * m[x, rows_2] + m[x - 1, rows_1] + m[x + 1, rows_1]) * 0.25 - m[x, rows_1]; } /* IV.4: Compute interior */ for (int y = 1; y <= rows_2; y++) { for (int x = 1; x <= cols_2; x++) { Lu[x, y] = (m[x - 1, y] + m[x + 1, y] + m[x, y - 1] + m[x, y + 1]) * 0.25 - m[x, y]; } } return(Lu); }
public static NcvMatrix diag(NcvVector v, int k) { int n = v.n; int order = n + Math.Abs(k); NcvMatrix m = new NcvMatrix(order, order); for (int i = 0; i < n; i++) { if (k <= 0) { m[i, i - k] = v[i]; } else { m[i + k, i] = v[i]; } } return(m); }
public override bool Equals(object obj) { NcvMatrix other = (NcvMatrix)obj; if (this.Rows != other.Rows || this.Cols != other.Cols) { return(false); } for (int y = 0; y < this.rows(); y++) { for (int x = 0; x < this.cols(); x++) { if (this[x, y] != other[x, y]) { return(false); } } } return(true); }
public NcvMatrix(NcvMatrix M) { data = new double[M.rows(), M.cols()]; copy(M); }
public static NcvMatrix inv(NcvMatrix original) { NcvMatrix inv = new NcvMatrix(original.cols(), original.rows()); return(inv.inverse(original)); }
// sets this Matrix to the inverse of the original Matrix // and returns this. public NcvMatrix inverse(NcvMatrix original) { if (original.rows() < 1 || original.rows() != original.cols() || rows() != original.rows() || rows() != cols()) { return(this); } int n = rows(); copy(new NcvMatrix(n)); // make identity matrix if (rows() == 1) { this[0, 0] = 1 / original[0, 0]; return(this); } NcvMatrix b = new NcvMatrix(original); for (int i = 0; i < n; i++) { // find pivot double mag = 0; int pivot = -1; for (int j = i; j < n; j++) { double mag2 = Math.Abs(b[j, i]); if (mag2 > mag) { mag = mag2; pivot = j; } } // no pivot (error) if (pivot == -1 || mag == 0) { return(this); } // move pivot row into position if (pivot != i) { double temp; for (int j = i; j < n; j++) { temp = b[i, j]; this[i, j] = b[pivot, j]; b[pivot, j] = temp; } for (int j = 0; j < n; j++) { temp = this[i, j]; this[i, j] = this[pivot, j]; this[pivot, j] = temp; } } // normalize pivot row mag = b[i, i]; for (int j = i; j < n; j++) { b[i, j] = b[i, j] / mag; } for (int j = 0; j < n; j++) { this[i, j] = this[i, j] / mag; } // eliminate pivot row component from other rows for (int k = 0; k < n; k++) { if (k == i) { continue; } double mag2 = b[k, i]; for (int j = i; j < n; j++) { b[k, j] = b[k, j] - mag2 * b[i, j]; } for (int j = 0; j < n; j++) { this[k, j] = this[k, j] - mag2 * this[i, j]; } } } return(this); }