public void MatrixArrayConversion() { // start with a .NET Array double[,] A = new double[,] { { 0, 1, 2 }, { 3, 4, 5 } }; // Convert it to a Matrix RectangularMatrix B = new RectangularMatrix(A); Assert.IsTrue(B.RowCount == A.GetLength(0)); Assert.IsTrue(B.ColumnCount == A.GetLength(1)); for (int r = 0; r < B.RowCount; r++) { for (int c = 0; c < B.ColumnCount; c++) { Assert.IsTrue(B[r, c] == A[r, c]); } } // Convert that Matrix back to an Array double[,] C = B.ToArray(); Assert.IsTrue(C.Rank == 2); Assert.IsTrue(C.GetLength(0) == B.RowCount); Assert.IsTrue(C.GetLength(1) == B.ColumnCount); for (int r = 0; r < B.RowCount; r++) { for (int c = 0; c < B.ColumnCount; c++) { Assert.IsTrue(C[r, c] == A[r, c]); } } }
static MixedMatrixTest() { R = new RectangularMatrix(2, 3); R[0, 0] = 0; R[0, 1] = 1; R[0, 2] = 2; R[1, 0] = 3; R[1, 1] = 4; R[1, 2] = 5; M = new SquareMatrix(3); M[0, 0] = 0; M[0, 1] = 1; M[0, 2] = 2; M[1, 0] = 3; M[1, 1] = 4; M[1, 2] = 5; M[2, 0] = 6; M[2, 1] = 7; M[2, 2] = 8; S = new SymmetricMatrix(3); M[0, 0] = 0; M[0, 1] = 1; M[0, 2] = 2; M[1, 1] = 3; M[1, 2] = 4; M[2, 2] = 5; }
private RectangularMatrix Division(RectangularMatrix first, double second) { RectangularMatrix divMatrix = new RectangularMatrix(first.RowCount, first.ColumnCount); for (int i = 0; i < divMatrix.RowCount; i++) { for (int j = 0; j < divMatrix.ColumnCount; j++) { divMatrix[i, j] = first[i, j] / second; } } return divMatrix; }
public Network(int windowSize, int imagesNumber, double learningCoefficient, double maxError, int maxIterations) { this.windowSize = windowSize; this.imagesNumber = imagesNumber; this.learningCoefficient = learningCoefficient; this.maxError = maxError; this.maxIterations = maxIterations; weightMatrix1 = new RectangularMatrix(imagesNumber, windowSize + imagesNumber); CreateRandomMatrix(weightMatrix1); weightMatrix2 = new RowVector(imagesNumber); CreateRandomRowVector(weightMatrix2); contextNeurons = new ColumnVector(imagesNumber); }
// Solve a linear system using QR decomposition. This isn't really the obvious place for this algorithm, but since // it's used by both BivariableSample and UncertainMeasurementSample, it needs to be centralized somewhere and here // is as good a place as any. Probably it should be moved in to RectangularMatrixAlgorithms and it should // work directly on the underlying array storage for increased performance. internal static void SolveLinearSystem(RectangularMatrix A, ColumnVector b, out ColumnVector a, out SymmetricMatrix C) { Debug.Assert(A.RowCount == b.Dimension); Debug.Assert(A.ColumnCount <= A.RowCount); // Our problem is to "solve" A a = b, where a is the vector of coefficients. QR gives the least squares // solution that minimizes | A a - y |. QRDecomposition QR = A.QRDecomposition(); a = QR.Solve(b); // The covariance matrix C = (A^T A)^{-1} = (R^T R)^{-1}. This is a matrix multiplication plus an inversion. // A faster way is to use the direct solution // for k = n ... 1 // C_{kk} = R_{kk}^{-1} \left[ R_{kk}^{-1} - \sum_{j=k+1}^{n} R_{kj} C_{kj} \right] // for i = k-1 ... 1 // C_{ik} = -R_{ii}^{-1} \left[ \sum_{j=i+1}^{k} R_{ij} C_{jk} + \sum_{j=k+1}^{n} R_{ij} C_{kj} \right] // end // end // This is detailed in Ake Bjorck, "Numerical Methods for Least Squares Problems", pp. 118-120 C = new SymmetricMatrix(a.Dimension); RectangularMatrix R = QR.RMatrix; double c; // used for storage so we don't call bounds-checking accessors each time for (int k = C.Dimension - 1; k >= 0; k--) { c = 1.0 / R[k, k];; for (int j = k + 1; j < C.Dimension; j++) { c -= R[k, j] * C[k, j]; } C[k, k] = c / R[k, k]; for (int i = k - 1; i >= 0; i--) { c = 0.0; for (int j = i + 1; j <= k; j++) { c += R[i, j] * C[j, k]; } for (int j = k + 1; j < C.Dimension; j++) { c += R[i, j] * C[k, j]; } C[i, k] = -c / R[i, i]; } } }
/* * /// <summary> * /// Multiplies any real, rectangular matrix by a column vector. * /// </summary> * /// <param name="A">The matrix.</param> * /// <param name="x">The column vector.</param> * /// <returns>The column vector Ax.</returns> * /// <remarks> * /// <para>Matrix multiplication is an O(N<sup>2</sup>) process.</para> * /// </remarks> * public static ColumnVector operator * (RectangularMatrixBase A, ColumnVector x) { * if (A == null) throw new ArgumentNullException("A"); * if (x == null) throw new ArgumentNullException("x"); * if (A.RowCount != x.Dimension) throw new DimensionMismatchException(); * ColumnVector Ax = new ColumnVector(A.ColumnCount); * for (int i = 0; i < A.ColumnCount; i++) { * for (int j = 0; j < A.RowCount; j++) { * Ax[i] += A[i, j] * x[j]; * } * } * return (Ax); * } */ /// <summary> /// Multiplies any real, rectangular matrix by a real constant. /// </summary> /// <param name="alpha">The constant.</param> /// <param name="A">The matrix.</param> /// <returns>The product matrix.</returns> public static RectangularMatrix operator *(double alpha, AnyRectangularMatrix A) { if (A == null) { throw new ArgumentNullException(nameof(alpha)); } RectangularMatrix B = new RectangularMatrix(A.RowCount, A.ColumnCount); for (int i = 0; i < A.RowCount; i++) { for (int j = 0; j < A.ColumnCount; j++) { B[i, j] = alpha * A[i, j]; } } return(B); }
//Method that calculates the calibration parameters void calibrazione(double[] X_kinect, double[] Y_kinect, double[] Z_kinect) { //Grid coordinates double[] X_Ref_B = new double[12] { -517.5, -172.5, 172.5, 517.5, -517.5, -172.5, 172.5, 517.5, -517.5, -172.5, 172.5, 517.5 };// coordinate X dei punti della griglia di riferimento (origine posta al centro della griglia stessa) double[] Y_Ref_B = new double[12] { 345.0, 345.0, 345.0, 345.0, 0.0, 0.0, 0.0, 0.0, -345.0, -345.0, -345.0, -345.0 };// coordinate Y dei punti della griglia di riferimento (origine posta al centro della griglia stessa) double[] Z_Ref_B = new double[12]; Sample XKINECT = new Sample(X_kinect); Sample YKINECT = new Sample(Y_kinect); Sample ZKINECT = new Sample(Z_kinect); // barycenter of Kinect coordinates double X_Bar = XKINECT.Mean; double Y_Bar = YKINECT.Mean; double Z_Bar = ZKINECT.Mean; //Kinect barycentric coordinates contaneirs double[] X_Kinect_Bar = new double[12]; double[] Y_Kinect_Bar = new double[12]; double[] Z_Kinect_Bar = new double[12]; //Kinect barycentric coordinates for (int i = 0; i < 12; i++) { X_Kinect_Bar[i] = X_kinect[i] - X_Bar; Y_Kinect_Bar[i] = Y_kinect[i] - Y_Bar; Z_Kinect_Bar[i] = Z_kinect[i] - Z_Bar; } //Modello rotazione + 2 parametri di scala Yoss=AX+b oppure B=Yoss-b=AX X=B*A^-1 RectangularMatrix A = new RectangularMatrix(36, 5); ColumnVector B = new ColumnVector(36); ColumnVector bb = new ColumnVector(36); ColumnVector Yoss = new ColumnVector(36); ColumnVector X = new ColumnVector(5); SquareMatrix X1 = new SquareMatrix(5); ColumnVector X2 = new ColumnVector(5); for (int i = 0; i < 12; i++) { Yoss[3 * i] = X_Ref_B[i]; Yoss[3 * i + 1] = Y_Ref_B[i]; Yoss[3 * i + 2] = Z_Ref_B[i]; A[3 * i, 0] = Y_Kinect_Bar[i]; A[3 * i, 1] = -Z_Kinect_Bar[i]; A[3 * i, 2] = 0.0; A[3 * i, 3] = X_Kinect_Bar[i]; A[3 * i, 4] = 0.0; A[3 * i + 1, 0] = -X_Kinect_Bar[i]; A[3 * i + 1, 1] = 0.0; A[3 * i + 1, 2] = Z_Kinect_Bar[i]; A[3 * i + 1, 3] = 0.0; A[3 * i + 1, 4] = Y_Kinect_Bar[i]; A[3 * i + 2, 0] = 0.0; A[3 * i + 2, 1] = X_Kinect_Bar[i]; A[3 * i + 2, 2] = -Y_Kinect_Bar[i]; A[3 * i + 2, 3] = 0.0; A[3 * i + 2, 4] = 0.0; bb[3 * i] = X_Kinect_Bar[i]; bb[3 * i + 1] = Y_Kinect_Bar[i]; bb[3 * i + 2] = Z_Kinect_Bar[i]; B[3 * i] = X_Ref_B[i] - X_Kinect_Bar[i]; B[3 * i + 1] = Y_Ref_B[i] - Y_Kinect_Bar[i]; B[3 * i + 2] = Z_Ref_B[i] - Z_Kinect_Bar[i]; } X1 = (SquareMatrix)(A.Transpose() * A); X2 = (A.Transpose() * B); X = (X1.Inverse()) * X2; a = X[0]; b = X[1]; c = X[2]; lambdaX = X[3]; lambdaY = X[4]; this.sw9.Write("{0:#####.0000} {1:#####.0000} {2:#####.0000} {3:#####.0000} {4:#####.0000} ", a, b, c, lambdaX, lambdaY); this.sw9.WriteLine(); }
public void PC() { Random rng = new Random(1); double s = 1.0 / Math.Sqrt(2.0); MultivariateSample MS = new MultivariateSample(2); RectangularMatrix R = new RectangularMatrix(1000, 2); for (int i = 0; i < 1000; i++) { double r1 = 2.0 * rng.NextDouble() - 1.0; double r2 = 2.0 * rng.NextDouble() - 1.0; double x = r1 * 4.0 * s - r2 * 9.0 * s; double y = r1 * 4.0 * s + r2 * 9.0 * s; R[i, 0] = x; R[i, 1] = y; MS.Add(x, y); } Console.WriteLine("x {0} {1}", MS.Column(0).Mean, MS.Column(0).Variance); Console.WriteLine("y {0} {1}", MS.Column(1).Mean, MS.Column(1).Variance); Console.WriteLine("SVD"); SingularValueDecomposition SVD = R.SingularValueDecomposition(); for (int i = 0; i < SVD.Dimension; i++) { Console.WriteLine("{0} {1}", i, SVD.SingularValue(i)); ColumnVector v = SVD.RightSingularVector(i); Console.WriteLine(" {0} {1}", v[0], v[1]); } Console.WriteLine("PCA"); PrincipalComponentAnalysis PCA = MS.PrincipalComponentAnalysis(); Console.WriteLine("Dimension = {0} Count = {1}", PCA.Dimension, PCA.Count); for (int i = 0; i < PCA.Dimension; i++) { PrincipalComponent PC = PCA.Component(i); Console.WriteLine(" {0} {1} {2} {3}", PC.Index, PC.Weight, PC.VarianceFraction, PC.CumulativeVarianceFraction); RowVector v = PC.NormalizedVector(); Console.WriteLine(" {0} {1}", v[0], v[1]); } // reconstruct SquareMatrix U = SVD.LeftTransformMatrix(); SquareMatrix V = SVD.RightTransformMatrix(); double x1 = U[0, 0] * SVD.SingularValue(0) * V[0, 0] + U[0, 1] * SVD.SingularValue(1) * V[0, 1]; Console.WriteLine("x1 = {0} {1}", x1, R[0, 0]); double y1 = U[0, 0] * SVD.SingularValue(0) * V[1, 0] + U[0, 1] * SVD.SingularValue(1) * V[1, 1]; Console.WriteLine("y1 = {0} {1}", y1, R[0, 1]); double x100 = U[100, 0] * SVD.SingularValue(0) * V[0, 0] + U[100, 1] * SVD.SingularValue(1) * V[0, 1]; Console.WriteLine("x100 = {0} {1}", x100, R[100, 0]); double y100 = U[100, 0] * SVD.SingularValue(0) * V[1, 0] + U[100, 1] * SVD.SingularValue(1) * V[1, 1]; Console.WriteLine("y100 = {0} {1}", y100, R[100, 1]); ColumnVector d1 = U[0,0] * SVD.SingularValue(0) * SVD.RightSingularVector(0) + U[0, 1] * SVD.SingularValue(1) * SVD.RightSingularVector(1); Console.WriteLine("d1 = ({0} {1})", d1[0], d1[1]); ColumnVector d100 = U[100, 0] * SVD.SingularValue(0) * SVD.RightSingularVector(0) + U[100, 1] * SVD.SingularValue(1) * SVD.RightSingularVector(1); Console.WriteLine("d100 = ({0} {1})", d100[0], d100[1]); Console.WriteLine("compare"); MultivariateSample RS = PCA.TransformedSample(); IEnumerator<double[]> RSE = RS.GetEnumerator(); RSE.MoveNext(); double[] dv1 = RSE.Current; Console.WriteLine("{0} {1}", dv1[0], dv1[1]); Console.WriteLine("{0} {1}", U[0, 0], U[0, 1]); RSE.Dispose(); }
private RectangularMatrix GenerateRandomMatrix(int rd, int cd) { RectangularMatrix M = new RectangularMatrix(rd, cd); Random rng = new Random(1); for (int r = 0; r < rd; r++) { for (int c = 0; c < cd; c++) { M[r,c] = 2.0 * rng.NextDouble() - 1.0; } } return (M); }
private void CreateRandomMatrix(RectangularMatrix weightMatrix) { Random rand = new Random(); for (int i = 0; i < weightMatrix.RowCount; i++) { for (int j = 0; j < weightMatrix.ColumnCount; j++) { weightMatrix[i, j] = rand.NextDouble() * 0.01; } } }
// Method that calculates the calibration parameters public void Calibration(double[] X_Kinect, double[] Y_Kinect, double[] Z_Kinect) { double[] X_Ref_B = new double[dim]; double[] Y_Ref_B = new double[dim]; double[] Z_Ref_B = new double[dim]; if (colonne_griglia_calibrazione % 2 == 0 && righe_griglia_calibrazione % 2 == 0) { for (int i = 0; i < righe_griglia_calibrazione; i++) { for (int j = 0; j < colonne_griglia_calibrazione; j++) { X_Ref_B[colonne_griglia_calibrazione*i + j] = (-colonne_griglia_calibrazione/2 + j)*step + step/2; Y_Ref_B[colonne_griglia_calibrazione*i + j] = (righe_griglia_calibrazione/2 - i)*step - step/2; } } } if (colonne_griglia_calibrazione % 2 == 0 && righe_griglia_calibrazione % 2 != 0) { for (int i = 0; i < righe_griglia_calibrazione; i++) { for (int j = 0; j < colonne_griglia_calibrazione; j++) { X_Ref_B[colonne_griglia_calibrazione * i + j] = (-colonne_griglia_calibrazione / 2 + j) * step + step / 2; Y_Ref_B[colonne_griglia_calibrazione * i + j] = ((righe_griglia_calibrazione-1)/ 2 - i) * step; } } } if (colonne_griglia_calibrazione % 2 != 0 && righe_griglia_calibrazione % 2 == 0) { for (int i = 0; i < righe_griglia_calibrazione; i++) { for (int j = 0; j < colonne_griglia_calibrazione; j++) { X_Ref_B[colonne_griglia_calibrazione*i + j] = (-(colonne_griglia_calibrazione-1)/2 + j)*step; Y_Ref_B[colonne_griglia_calibrazione*i + j] = (righe_griglia_calibrazione/2 - i)*step - step/2; } } } if (colonne_griglia_calibrazione % 2 != 0 && righe_griglia_calibrazione % 2 != 0) { for (int i = 0; i < righe_griglia_calibrazione; i++) { for (int j = 0; j < colonne_griglia_calibrazione; j++) { X_Ref_B[colonne_griglia_calibrazione * i + j] = (-(colonne_griglia_calibrazione - 1) / 2 + j) * step; Y_Ref_B[colonne_griglia_calibrazione * i + j] = ((righe_griglia_calibrazione - 1) / 2 - i) * step; } } } // Grid coordinates for (int k = 0; k < dim; k++) { this.Win.sw5.WriteLine("{0:#####.0000} {1:#####.0000} {2:#####.0000} ", X_Ref_B[k], Y_Ref_B[k], Z_Ref_B[k]); } this.Win.sw5.WriteLine(); Sample XKINECT = new Sample(X_Kinect); Sample YKINECT = new Sample(Y_Kinect); Sample ZKINECT = new Sample(Z_Kinect); // barycenter of Kinect coordinates double X_Bar = XKINECT.Mean; double Y_Bar = YKINECT.Mean; double Z_Bar = ZKINECT.Mean; //Kinect barycentric coordinates contaneirs double[] X_Kinect_Bar = new double[dim]; double[] Y_Kinect_Bar = new double[dim]; double[] Z_Kinect_Bar = new double[dim]; //Kinect barycentric coordinates for (int i = 0; i < dim; i++) { X_Kinect_Bar[i] = X_Kinect[i] - X_Bar; Y_Kinect_Bar[i] = Y_Kinect[i] - Y_Bar; Z_Kinect_Bar[i] = Z_Kinect[i] - Z_Bar; this.Win.sw3.WriteLine("{0:#####.0000} {1:#####.0000} {2:#####.0000} ", X_Kinect_Bar[i], Y_Kinect_Bar[i], Z_Kinect_Bar[i]); } this.Win.sw3.WriteLine(); //Modello rotazione + 2 parametri di scala Yoss = AX+b oppure B = Yoss-bb = AX X = B*A^-1 RectangularMatrix A = new RectangularMatrix(dim * 3, 5); ColumnVector B = new ColumnVector(dim * 3); ColumnVector bb = new ColumnVector(dim * 3); ColumnVector Yoss = new ColumnVector(dim * 3); ColumnVector X = new ColumnVector(5); SquareMatrix X1 = new SquareMatrix(5); ColumnVector X2 = new ColumnVector(5); for (int i = 0; i < dim; i++) { Yoss[3 * i ] = X_Ref_B[i]; Yoss[3 * i + 1] = Y_Ref_B[i]; Yoss[3 * i + 2] = Z_Ref_B[i]; A[3 * i, 0] = Y_Kinect_Bar[i]; A[3 * i, 1] = -Z_Kinect_Bar[i]; A[3 * i, 2] = 0.0; A[3 * i, 3] = X_Kinect_Bar[i]; A[3 * i, 4] = 0.0; A[3 * i + 1, 0] = -X_Kinect_Bar[i]; A[3 * i + 1, 1] = 0.0; A[3 * i + 1, 2] = Z_Kinect_Bar[i]; A[3 * i + 1, 3] = 0.0; A[3 * i + 1, 4] = Y_Kinect_Bar[i]; A[3 * i + 2, 0] = 0.0; A[3 * i + 2, 1] = X_Kinect_Bar[i]; A[3 * i + 2, 2] = -Y_Kinect_Bar[i]; A[3 * i + 2, 3] = 0.0; A[3 * i + 2, 4] = 0.0; bb[3 * i] = X_Kinect_Bar[i]; bb[3 * i + 1] = Y_Kinect_Bar[i]; bb[3 * i + 2] = Z_Kinect_Bar[i]; B[3 * i] = X_Ref_B[i] - X_Kinect_Bar[i]; B[3 * i + 1] = Y_Ref_B[i] - Y_Kinect_Bar[i]; B[3 * i + 2] = Z_Ref_B[i] - Z_Kinect_Bar[i]; } X1 = (SquareMatrix)(A.Transpose() * A); X2 = (A.Transpose() * B); X = (X1.Inverse()) * X2; a = X[0]; b = X[1]; c = X[2]; lambdaX = X[3]; lambdaY = X[4]; this.Win.sw12.Write("{0:#####.0000} {1:#####.0000} {2:#####.0000} {3:#####.0000} {4:#####.0000} ", a, b, c, lambdaX, lambdaY); this.Win.sw12.WriteLine(); this.Win.textBlock8.Text = string.Format("Calibration parameters:\na = {0} \nb = {1} \nc = {2} \nlambdaX = {3} \nlambdaY = {4}", a, b, c, lambdaX, lambdaY); SquareMatrix ROT = new SquareMatrix(3); ROT[0, 0] = 1 + lambdaX; ROT[0, 1] = a; ROT[0, 2] = -b; ROT[1, 0] = -a; ROT[1, 1] = 1 + lambdaY; ROT[1, 2] = c; ROT[2, 0] = b; ROT[2, 1] = -c; ROT[2, 2] = 1; }
private void TeachNeuralNetwork(ImageRectangle[] rectangles, BackgroundWorker worker, DoWorkEventArgs doWorkEvent, bool test) { int N = n * m * 3; RectangularMatrix weightMatrix = new RectangularMatrix(N, p); Random rand = new Random(); for (int i = 0; i < N; i++) { for (int j = 0; j < p; j++) { weightMatrix[i, j] = rand.NextDouble() * 0.1; } } RectangularMatrix secondWeightMatrix = weightMatrix.Transpose(); double totalError = e + 1; int totalIterationNumber = 0; state = new CurrentState(); RowVector[] vectors = new RowVector[rectangles.Length]; for (int i = 0; i < rectangles.Length; i++) { vectors[i] = new RowVector(((ImageRectangle)rectangles.GetValue(i)).GetVector()); } while (totalError > e && totalIterationNumber < iterationNumber) { totalError = 0; for (int i = 0; i < rectangles.Length; i++) { RowVector xVector = vectors[i]; RowVector yVector = xVector * weightMatrix; RowVector xSecondVector = yVector * secondWeightMatrix; RowVector deltaXVector = xSecondVector - xVector; weightMatrix = weightMatrix - a * xVector.Transpose() * deltaXVector * secondWeightMatrix.Transpose(); secondWeightMatrix = secondWeightMatrix - a * yVector.Transpose() * deltaXVector; } for (int i = 0; i < rectangles.Length; i++) { RowVector xVector = vectors[i]; RowVector yVector = xVector * weightMatrix; RowVector xSecondVector = yVector * secondWeightMatrix; RowVector deltaXVector = xSecondVector - xVector; for (int j = 0; j < deltaXVector.ColumnCount; j++) { totalError += deltaXVector[0, j] * deltaXVector[0, j]; } } totalIterationNumber++; state.CurentError = totalError; state.IterationNumber = totalIterationNumber; if (!test) { worker.ReportProgress(0, state); if (worker.CancellationPending) { doWorkEvent.Cancel = true; break; } } } for (int i = 0; i < rectangles.Length; i++) { RowVector xVector = vectors[i]; RowVector yVector = xVector * weightMatrix; RowVector xSecondVector = yVector * secondWeightMatrix; ((ImageRectangle)rectangles.GetValue(i)).SetPixelMatrixFromVector(xSecondVector.ToArray()); } state.Compressing = CalculateCompressing(rectangles.Length, N); state.FirstWeightMatrix = GetMatrixString(weightMatrix); state.SecondWeightMatrix = GetMatrixString(secondWeightMatrix); }
private string GetMatrixString(RectangularMatrix matrix) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < matrix.RowCount; i++) { RowVector rowVector = matrix.Row(i); string elements =String.Join(", ", rowVector.ToArray().Select(element => Math.Round(element, 4).ToString()).ToArray()); builder.Append("[" + elements + "]" + Environment.NewLine); } return builder.ToString(); }
private void LearnImage(Image image) { int[] vector = image.ToVector(); ColumnVector X = new ColumnVector(vector.Length); for (int i = 0; i < X.RowCount; i++) { X[i] = vector[i]; } ColumnVector b = W * X; ColumnVector a = b - X; RectangularMatrix first = a * a.Transpose(); double second = (X.Transpose() * X) - (X.Transpose() * W * X); W = W + Division(first, second); for (int i = 0; i < W.ColumnCount; i++) { W[i, i] = 0; } }
private void FillMatrixWithZeros(RectangularMatrix matrix) { for (int i = 0; i < matrix.RowCount; i++) { for (int j = 0; j < matrix.ColumnCount; j++) { matrix[i, j] = 0.0; } } }
public void Bell1() { RectangularMatrix A = new RectangularMatrix(new double[,] { {1.0, 0.0, 0.0, 0.0, 2.0}, {0.0, 0.0, 3.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 4.0, 0.0, 0.0, 0.0} }); //A = A.Transpose(); SingularValueDecomposition svd = A.SingularValueDecomposition(); Console.WriteLine("values"); for (int i = 0; i < svd.Dimension; i++) { Console.WriteLine(svd.SingularValue(i)); } Console.WriteLine("left"); WriteMatrix(svd.LeftTransformMatrix()); Console.WriteLine("right"); WriteMatrix(svd.RightTransformMatrix()); Console.WriteLine("product"); WriteMatrix(svd.LeftTransformMatrix().Transpose() * A * svd.RightTransformMatrix()); }
public void Bug7686() { // This SVD failed with a IndexOutOfBoundsException because we didn't handle SVD for cols > rows and didn't check for this condition on entry. RectangularMatrix A = new RectangularMatrix(new double[,] { {-418.746, 310.726, 313.969, 1}, {-418.746, 337.451, 229.786, 1}, {-305.253, 321.895, 304.895, 1} }); var SVD = A.SingularValueDecomposition(); }
public void RectangularMatrixEquality() { RectangularMatrix A = new RectangularMatrix(new double[,] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 } }); RectangularMatrix AC = A.Copy(); RectangularMatrix B = 2.0 * A; // Equality operator Assert.IsTrue(A == AC); Assert.IsTrue(AC == A); Assert.IsFalse(A == B); Assert.IsFalse(B == A); Assert.IsFalse(A == null); Assert.IsFalse(null == A); Assert.IsTrue((RectangularMatrix) null == (RectangularMatrix) null); // Inequality operator Assert.IsFalse(A != AC); Assert.IsFalse(AC != A); Assert.IsTrue(A != B); Assert.IsTrue(B != A); Assert.IsTrue(A != null); Assert.IsTrue(null != A); Assert.IsFalse((RectangularMatrix) null != (RectangularMatrix) null); // Equals method Assert.IsTrue(A.Equals(AC)); Assert.IsFalse(A.Equals(B)); Assert.IsFalse(A.Equals(new object())); Assert.IsFalse(A.Equals(null)); }
private RectangularMatrix CreateScalarMatrix(double value, int size) { RectangularMatrix scalarMatrix = new RectangularMatrix(size, size); for (int i = 0; i < size; i++) { scalarMatrix[i, i] = value; } return scalarMatrix; }
public void RectangularMatrixNorms() { RectangularMatrix Z = new RectangularMatrix(3, 4); Assert.IsTrue(Z.OneNorm() == 0.0); Assert.IsTrue(Z.InfinityNorm() == 0.0); Assert.IsTrue(Z.FrobeniusNorm() == 0.0); RectangularMatrix A = GenerateRandomMatrix(4, 5); Assert.IsTrue(A.OneNorm() > 0.0); Assert.IsTrue(A.InfinityNorm() > 0.0); Assert.IsTrue(A.FrobeniusNorm() > 0.0); RectangularMatrix B = GenerateRandomMatrix(5, 6); Assert.IsTrue(B.OneNorm() > 0.0); Assert.IsTrue(B.InfinityNorm() > 0.0); Assert.IsTrue(B.FrobeniusNorm() > 0.0); // Frobenius norm is sub-multiplicative RectangularMatrix P = A * B; Assert.IsTrue(P.FrobeniusNorm() <= A.FrobeniusNorm() * B.FrobeniusNorm()); }
public void Learn(BackgroundWorker backgroundWorker, DoWorkEventArgs e, double[] sequence, bool? showIteration) { ColumnVector[] learningMatrix = createLearningMatrix(sequence); double[] etalons = createEtalons(sequence); backgroundResult = new BackgroundResult(); backgroundResult.IsComplete = false; double totalError; int iterations = 0; do { // learn for (int i = 0; i < learningMatrix.Length; i++) { ColumnVector X = ConcatVertically(learningMatrix[i], contextNeurons); ColumnVector Xn = Normalize(X); double norm = X.FrobeniusNorm(); ColumnVector Y1 = weightMatrix1 * Xn; double Y2 = weightMatrix2 * Y1; double gamma2 = (Y2 * norm) - etalons[i]; RowVector gamma1 = gamma2 * weightMatrix2; RowVector a = learningCoefficient * gamma1; RectangularMatrix b = Xn * a; weightMatrix1 = weightMatrix1 - b.Transpose(); weightMatrix2 = weightMatrix2 - ((gamma2 * learningCoefficient) * Y1).Transpose(); contextNeurons = Y1; } totalError = 0; // calculate total error for (int i = 0; i < learningMatrix.Length; i++) { ColumnVector X = ConcatVertically(learningMatrix[i], contextNeurons); ColumnVector Xn = Normalize(X); double norm = X.FrobeniusNorm(); ColumnVector Y1 = weightMatrix1 * Xn; double Y2 = weightMatrix2 * Y1; double gamma2 = Y2 * norm - etalons[i]; totalError += Math.Pow(gamma2, 2); } backgroundResult.IterationNumber = iterations; backgroundResult.Error = totalError; backgroundResult.IsComplete = false; if (showIteration.Equals(true)) { backgroundWorker.ReportProgress(0, backgroundResult); Thread.Sleep(200); if (backgroundWorker.CancellationPending) { e.Cancel = true; break; } } iterations++; } while (totalError >= maxError && iterations <= maxIterations); backgroundResult.IterationNumber = iterations; backgroundResult.Error = totalError; backgroundResult.IsComplete = true; backgroundWorker.ReportProgress(0, backgroundResult); }
public Network(int width, int height) { W = new RectangularMatrix(width * height, width * height); FillMatrixWithZeros(W); }