/// <summary> /// Extracts main diagonal vector of the Matriz as a column vector. /// </summary> /// <returns>Vector diagonal</returns> public Matriz DiagVector() { if (!this.IsSquare()) { throw new InvalidOperationException("Cannot get diagonal of non-square Matriz."); } Matriz v = new Matriz(this.columnCount, 1); for (int i = 1; i <= this.columnCount; i++) { v[i] = this[i, i]; } return v; }
/// <summary> /// Retrieves column with one-based index j. /// </summary> /// <param name="j">Index j of the matrix</param> /// <returns>j-th column of the matrix</returns> public Matriz Column(int j) { Matriz buf = new Matriz(this.rowCount, 1); for (int i = 1; i <= this.rowCount; i++) { buf[i] = this[i, j]; } return buf; }
/// <summary> /// Replaces each Matriz entry z = x + iy with x - iy. /// </summary> /// <returns>Conjugated Matriz.</returns> public Matriz Conjugate() { Matriz m = new Matriz(this.rowCount, this.columnCount); for (int i = 1; i <= this.rowCount; i++) { for (int j = 1; j <= this.columnCount; j++) { m[i, j] = Complex.Conj(this[i, j]); } } return m; }
/// <summary> /// Define operator multiplication with a complex number /// </summary> /// <param name="x">First complex number in the operation</param> /// <param name="a">Second matrix in the operation</param> /// <returns>Matrix result</returns> public static Matriz operator *(Complex x, Matriz a) { Matriz b = new Matriz(a.RowCount, a.ColumnCount); for (int i = 1; i <= a.RowCount; i++) { for (int j = 1; j <= a.ColumnCount; j++) { b[i, j] = a[i, j] * x; } } return b; }
/// <summary> /// Provides a shallow copy of this Matriz in O(m). /// </summary> /// <returns>Matrix clonned</returns> public Matriz Clone() { Matriz a = new Matriz(); a.rowCount = this.rowCount; a.columnCount = this.columnCount; for (int i = 0; i < this.rowCount; i++) { a.values.Add(((ArrayList)this.values[i]).Clone()); } return a; }
/// <summary> /// Creates n by n Matriz filled with ones. /// </summary> /// <param name="n">Number of columns.</param> /// <returns>n by n Matriz filled with ones.</returns> public static Matriz Ones(int n) { Matriz m = new Matriz(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ((ArrayList)m.values[i])[j] = Complex.One; } } return m; }
/// <summary> /// Define operator multiplication /// </summary> /// <param name="a">First matrix in the operation</param> /// <param name="b">Second matrix in the operation</param> /// <returns>Matrix result</returns> public static Matriz operator *(Matriz a, Matriz b) { if (a.ColumnCount != b.RowCount) { throw new ArgumentException("Inner Matriz dimensions must agree."); } Matriz c = new Matriz(a.RowCount, b.ColumnCount); for (int i = 1; i <= a.RowCount; i++) { for (int j = 1; j <= b.ColumnCount; j++) { c[i, j] = Dot(a.Row(i), b.Column(j)); } } return c; }
/// <summary> /// Implements the dot product of two vectors. /// </summary> /// <param name="v">Row or column vector.</param> /// <param name="w">Row or column of other vector.</param> /// <returns>Dot product.</returns> public static Complex Dot(Matriz v, Matriz w) { int m = v.VectorLength(); int n = w.VectorLength(); if (m == 0 || n == 0) { throw new ArgumentException("Arguments need to be vectors."); } else if (m != n) { throw new ArgumentException("Vectors must be of the same length."); } Complex buf = Complex.Zero; for (int i = 1; i <= m; i++) { buf += v[i] * w[i]; } return buf; }
/// <summary> /// Creates m by n Matriz filled with ones. /// </summary> /// <param name="m">Number of rows.</param> /// <param name="n">Number of columns.</param> /// <returns>m by n Matriz filled with ones.</returns> public static Matriz Ones(int m, int n) { Matriz mat = new Matriz(m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { ((ArrayList)mat.values[i])[j] = Complex.One; } } return mat; }
/// <summary> /// Generates diagonal Matriz /// </summary> /// <param name="diag_vector">column vector containing the diag elements</param> /// <returns>Diagonal matrix</returns> public static Matriz Diag(Matriz diag_vector) { int dim = diag_vector.VectorLength(); if (dim == 0) { throw new ArgumentException("diag_vector must be 1xN or Nx1"); } Matriz m = new Matriz(dim, dim); for (int i = 1; i <= dim; i++) { m[i, i] = diag_vector[i]; } return m; }
/// <summary> /// Generates diagonal Matriz /// </summary> /// <param name="diag_vector">column vector containing the diag elements</param> /// <param name="offset">Offset of matrix</param> /// <returns>Diagonal matrix</returns> public static Matriz Diag(Matriz diag_vector, int offset) { int dim = diag_vector.VectorLength(); if (dim == 0) { throw new ArgumentException("diag_vector must be 1xN or Nx1."); } ////if (Math.Abs(offset) >= dim) //// throw new ArgumentException("Absolute value of offset must be less than length of diag_vector."); Matriz m = new Matriz(dim + Math.Abs(offset), dim + Math.Abs(offset)); dim = m.RowCount; if (offset >= 0) { for (int i = 1; i <= dim - offset; i++) { m[i, i + offset] = diag_vector[i]; } } else { for (int i = 1; i <= dim + offset; i++) { m[i - offset, i] = diag_vector[i]; } } return m; }
/// <summary> /// Swaps each Matriz entry A[i, j] with A[j, i]. /// </summary> /// <returns>A transposed Matriz.</returns> public Matriz Transpose() { Matriz m = new Matriz(this.columnCount, this.rowCount); for (int i = 1; i <= this.columnCount; i++) { for (int j = 1; j <= this.rowCount; j++) { m[i, j] = this[j, i]; } } return m; }
/// <summary> /// Retrieves row with one-based index i. /// </summary> /// <param name="i">Define position searching</param> /// <returns>i-th row in the matrix</returns> public Matriz Row(int i) { if (i <= 0 || i > this.rowCount) { throw new ArgumentException("Index exceed matrix dimension."); } ////return (new Matrix((Complex[])((ArrayList)Values[i - 1]).ToArray(typeof(Complex)))).Transpose(); Matriz buf = new Matriz(this.columnCount, 1); for (int j = 1; j <= this.columnCount; j++) { buf[j] = this[i, j]; } return buf; }
public override double[] CrossValidation() { double[] output = new double[2]; List<Kpoint> cpointsCross = new List<Kpoint>(); double SSerror = 0; double sumAllValues = 0; double quad = 0; ; for (int drop = 0; drop < this.NumPoints; drop++) { cpointsCross = new List<Kpoint>(); for (int i = 0; i < this.NumPoints; i++) if (i != drop) cpointsCross.Add(this.Kpoints[i]); this.result = GenerateMatrixCross(this.order, this.npoints - 1, cpointsCross); Kpoint value = this.Kpoints[drop]; double[] inter = this.Interpolate(value.X, value.Y,value.Z, false); value.Zinte = inter[0]; value.SdtError = value.W - value.Zinte; sumAllValues += value.SdtError; quad += (value.Zinte * value.Zinte); SSerror += (value.SdtError * value.SdtError); } output[0] = Math.Sqrt(SSerror / this.npoints); //RMS output[1] = sumAllValues / this.npoints; //Mean Prediction errors this.GenerateMatrix(this.order, this.npoints, this.Kpoints); return output; }
public Matriz GenerateMatrixCross(int order, int num, List<Kpoint> data) { int matrixSize = SerieCoef(order).Length; Matriz matrix = new Matriz(matrixSize); Matriz ZMatrix = new Matriz(matrixSize, 1); double width = extent[2] - extent[0]; double height = extent[3] - extent[1]; double difZ = extentZ[1] - extentZ[0]; for (int v = 0; v < num; v++) { double x = (data[v].X - extent[0]) / width; double y = (data[v].Y - extent[1]) / height; double z = data[v].W; for (int row = 0; row < matrixSize; row++) { for (int col = 0; col < matrixSize; col++) { matrix[row + 1, col + 1] = matrix[row + 1, col + 1] + Seq(num, order, col, row, x, y); } ZMatrix[row + 1, 1] = ZMatrix[row + 1, 1] + Seq(num, order, row, 0, x, y, z); } } Matriz inver = matrix.Inverse(); Matriz ind = matrix * inver; Matriz result = inver * ZMatrix; return result; }
public void GenerateMatrix(int order, int num, double[,] data) { int matrixSize = SerieCoef(order).Length; Matriz matrix = new Matriz(matrixSize); Matriz ZMatrix = new Matriz(matrixSize, 1); double width = this.extent[2] - this.extent[0]; double height = this.extent[3] - this.extent[1]; double difZ = this.extentZ[1] - this.extentZ[0]; for (int v = 0; v < num; v++) { double x = (data[v, 0] - extent[0])/width; double y = (data[v, 1] - extent[1])/height; double z = data[v, 2]; for (int row = 0; row < matrixSize; row++) { for (int col = 0; col < matrixSize; col++) { matrix[row + 1, col + 1] = matrix[row + 1, col + 1] + Seq(num, order, col, row, x, y); } ZMatrix[row + 1, 1] = ZMatrix[row + 1, 1] + Seq(num, order, row, 0, x, y, z); } } Matriz inver = matrix.Inverse(); Matriz ind = matrix * inver; result = inver * ZMatrix; }