void ProcessMatrix(RectangularMatrix source, Func <double, double> func) { for (int i = 0; i < source.RowCount; i++) { for (int j = 0; j < source.ColumnCount; j++) { source[i, j] = func(source[i, j]); } } }
public static RectangularMatrix ToMatrix(this double[] source) { var matrix = new RectangularMatrix(source.Length, 1); for (int i = 0; i < matrix.RowCount; i++) { matrix[i, 0] = source[i]; } return(matrix); }
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 Bug56() { RectangularMatrix M = new RectangularMatrix(new double[, ] { { 44.6667, -392.0000, -66.0000 }, { -392.0000, 3488.0000, 504.0001 }, { -66.0000, 504.0001, 216.0001 } }); //M = M.Transpose; SingularValueDecomposition S = M.SingularValueDecomposition(); }
public void MixedVectorArithmetic() { // outer product RectangularMatrix CR = C * R; Assert.IsTrue(CR.RowCount == C.Dimension); Assert.IsTrue(CR.ColumnCount == R.Dimension); // inner product double x = R * M * C; }
public static RectangularMatrix ProcessWithFunction(this RectangularMatrix source, Func <double, double> func) { for (int i = 0; i < source.RowCount; i++) { for (int j = 0; j < source.ColumnCount; j++) { source[i, j] = func(source[i, j]); } } return(source); }
List <double> GetErrorDelta(RectangularMatrix newErros, RectangularMatrix lastError) { List <double> list = new List <double>(); for (int i = 0; i < newErros.RowCount; i++) { var delta = newErros[i, 0] - lastError[i, 0]; list.Add(delta); } return(list); }
public static void PrintMatrix(RectangularMatrix matrix) { for (int i = 0; i < matrix.RowCount; i++) { for (int j = 0; j < matrix.ColumnCount; j++) { Console.Write(matrix[i, j] + " "); } Console.WriteLine(); } Console.WriteLine(); }
public void BigSVD() { RectangularMatrix R = GenerateRandomMatrix(500, 100); Stopwatch s = Stopwatch.StartNew(); SingularValueDecomposition SVD = R.SingularValueDecomposition(); s.Stop(); Console.WriteLine(s.Elapsed); Console.WriteLine(SVD.ConditionNumber); }
public void AnyRectangularMatrixOperations() { AnyRectangularMatrix M1 = R; AnyRectangularMatrix M2 = -R; RectangularMatrix MS = M1 + M2; Assert.IsTrue(MS == 0.0 * M1); RectangularMatrix MD = M1 - M2; Assert.IsTrue(MD == 2.0 * M1); }
public void MatrixSelfMultiplication() { RectangularMatrix A = GenerateRandomMatrix(3, 4); SymmetricMatrix AAT = A.MultiplySelfByTranspose(); Assert.IsTrue(TestUtilities.IsNearlyEqual(AAT, A * A.Transpose)); SymmetricMatrix ATA = A.MultiplyTransposeBySelf(); Assert.IsTrue(TestUtilities.IsNearlyEqual(ATA, A.Transpose * A)); }
public void RectangularMatrixAccess() { // create a matrix via outer product ColumnVector cv = new ColumnVector(new double[] { 1, 2 }); RowVector rv = new RowVector(new double[] { 3, 4, 5 }); RectangularMatrix M = cv * rv; // check dimensions Assert.IsTrue(M.RowCount == cv.Dimension); Assert.IsTrue(M.ColumnCount == rv.Dimension); // check values for (int r = 0; r < M.RowCount; r++) { for (int c = 0; c < M.ColumnCount; c++) { Assert.IsTrue(M[r, c] == cv[r] * rv[c]); } } // extract a column ColumnVector mc = M.Column(1); Assert.IsTrue(mc.Dimension == M.RowCount); for (int i = 0; i < mc.Dimension; i++) { Assert.IsTrue(mc[i] == M[i, 1]); } // extract a row RowVector mr = M.Row(1); Assert.IsTrue(mr.Dimension == M.ColumnCount); for (int i = 0; i < mr.Dimension; i++) { Assert.IsTrue(mr[i] == M[1, i]); } // test clone RectangularMatrix MC = M.Copy(); Assert.IsTrue(MC.RowCount == M.RowCount); Assert.IsTrue(MC.ColumnCount == M.ColumnCount); // test equality of clone Assert.IsTrue(MC == M); Assert.IsFalse(MC != M); // test independence of clone MC[0, 0] += 1.0; Assert.IsFalse(MC == M); Assert.IsTrue(MC != M); }
private static void PrintMatrix(RectangularMatrix M) { for (int r = 0; r < M.RowCount; r++) { for (int c = 0; c < M.ColumnCount; c++) { Console.Write(String.Format("{0,12:g8} ", M[r, c])); } Console.WriteLine(String.Empty); } Console.WriteLine("--"); }
public static double[] ConicFit(IList <Point> points) { Contract.Requires(points != null); Contract.Requires(points.Count >= 6); Contract.Ensures(Contract.Result <double[]>() != null); // construct the design matrix parts var d1 = new RectangularMatrix(points.Count, 3); var d2 = new RectangularMatrix(points.Count, 3); for (int i = 0; i < points.Count; ++i) { d1[i, 0] = points[i].X * points[i].X; d1[i, 1] = points[i].X * points[i].Y; d1[i, 2] = points[i].Y * points[i].Y; d2[i, 0] = points[i].X; d2[i, 1] = points[i].Y; d2[i, 2] = 1; } var s1 = MultiplyTranspose(d1, d1); var s2 = MultiplyTranspose(d1, d2); var s3 = MultiplyTranspose(d2, d2); var c1 = new SquareMatrix(3); c1[0, 2] = 2; c1[1, 1] = -1; c1[2, 0] = 2; var m = c1.Inverse() * (s1 - s2 * s3.Inverse() * s2.Transpose()); ColumnVector a1 = null; var eigen = m.Eigensystem(); for (int i = 0; i < eigen.Dimension; ++i) { var evec = eigen.Eigenvector(i); var cond = 4 * evec[0].Re * evec[2].Re - evec[1].Re * evec[1].Re; if (cond > 0) { a1 = new ColumnVector(evec[0].Re, evec[1].Re, evec[2].Re); } } Debug.Assert(a1 != null); var a2 = -s3.Inverse() * s2.Transpose() * a1; var conic = a1.Concat(a2).ToArray(); return(conic); }
public void simulateFFT(out ColumnVector Z, out ColumnVector volterra) { int n_T = grid.get_timeNmbrStep(); //Z_i defined in A.1 Z = new ColumnVector(n_T); RectangularMatrix Z_k = new RectangularMatrix(2, n_T); volterra = new ColumnVector(n_T); //DateTime starttestM = DateTime.Now; //for (int zz = 1; zz <= 1.0E4; zz++) //{ // SimulateCorrelated(Z, Z_k); //} //TimeSpan timeExecutiontestM = DateTime.Now - starttestM; SimulateCorrelated(Z, Z_k);//Simulate 3 Correlated Gaussians Z_i; Z_k_{1,i}; Z_k_{2,i} i:= 0, ..., n_T //Gamma for Convolution double[] Zdouble = Z.ToArray(); double[] convolution; DateTime starttest = DateTime.Now; //for (int zz = 1; zz <= 1.0E4; zz++) // alglib.convr1d(Gamma.ToArray(), n_T, Z.ToArray(), n_T, out convolution2); //TimeSpan timeExecutiontest = DateTime.Now - starttest; alglib.convr1d(Gamma.ToArray(), n_T, Z.ToArray(), n_T, out convolution); //DateTime starttestM = DateTime.Now; //for (int zz = 1; zz <= 1.0E5; zz++) //{ //} //TimeSpan timeExecutiontestM = DateTime.Now - starttestM; //var convolutionM = new Double1DConvolution(Z, Gamma.Count()); //DoubleVector convolution = convolutionM.Convolve(Gamma); for (int i = 1; i <= volterra.Count(); i++) { double v = convolution[i - 1]; for (int k = 1; k < Math.Min(i, kappa); k++) { v += Z_k[k - 1, i - k]; } volterra[i - 1] = v; } }
// //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //Use ClassCleanup to run code after all tests in a class have run //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // //Use TestInitialize to run code before running each test //[TestInitialize()] //public void MyTestInitialize() //{ //} // //Use TestCleanup to run code after each test has run //[TestCleanup()] //public void MyTestCleanup() //{ //} // #endregion 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); }
public static void VectorsAndMatrices() { ColumnVector v = new ColumnVector(0.0, 1.0, 2.0); ColumnVector w = new ColumnVector(new double[] { 1.0, -0.5, 1.5 }); SquareMatrix A = new SquareMatrix(new double[, ] { { 1, -2, 3 }, { 2, -5, 12 }, { 0, 2, -10 } }); RowVector u = new RowVector(4); for (int i = 0; i < u.Dimension; i++) { u[i] = i; } Random rng = new Random(1); RectangularMatrix B = new RectangularMatrix(4, 3); for (int r = 0; r < B.RowCount; r++) { for (int c = 0; c < B.ColumnCount; c++) { B[r, c] = rng.NextDouble(); } } SquareMatrix AI = A.Inverse(); PrintMatrix("A * AI", A * AI); PrintMatrix("v + 2.0 * w", v + 2.0 * w); PrintMatrix("Av", A * v); PrintMatrix("B A", B * A); PrintMatrix("v^T", v.Transpose); PrintMatrix("B^T", B.Transpose); Console.WriteLine($"|v| = {v.Norm()}"); Console.WriteLine($"sqrt(v^T v) = {Math.Sqrt(v.Transpose * v)}"); UnitMatrix I = UnitMatrix.OfDimension(3); PrintMatrix("IA", I * A); Console.WriteLine(v == w); Console.WriteLine(I * A == A); }
public void SvdOfRankOneMatrix() { // Create a rank-1 matrix ColumnVector v = new ColumnVector(1.0, 2.0, 3.0, 4.0); RectangularMatrix A = v * v.Transpose(); // Only the first singular value should be non-zero SingularValueDecomposition SVD = A.SingularValueDecomposition(); Console.WriteLine(SVD.SingularValue(0)); for (int i = 1; i < SVD.Dimension; i++) { Console.WriteLine(SVD.SingularValue(i)); Assert.IsTrue(SVD.SingularValue(i) < TestUtilities.TargetPrecision * SVD.SingularValue(0)); } }
/// <summary> /// /// </summary> /// <param name="args"></param> static void Main(string[] args) { RowVector u = new RowVector(4); for (int i = 0; i < u.Dimension; i++) { u[i] = i; } Random rng = new Random(); RectangularMatrix B = new RectangularMatrix(4, 3); for (int r = 0; r < B.RowCount; r++) { for (int c = 0; c < B.ColumnCount; c++) { B[r, c] = rng.Next(10) + 1; } } // Linear combinations of PrintMatrix(" v ", v); PrintMatrix(" 2w ", 2.0 * w); PrintMatrix(" v+2w ", v + 2.0 * w); PrintMatrix(" v + 2 w ", v + 2.0 * w); PrintMatrix(" A ", A); PrintMatrix(" B ", B); // Matrix-vector multiplication PrintMatrix(" Av ", A * v); // Matrix multiplication PrintMatrix(" B A ", B * A); SquareMatrix invA = A.Inverse(); // This should print a unit matrix PrintMatrix(" invA", invA); PrintMatrix("A * invA", A * invA); Console.WriteLine("Hello World!"); }
public void GetColumnsTest() { AnyRectangularMatrix matrix = this.ThreeByThreeSquare; IEnumerable <int> columnIndices = new[] { 0, 2 }; AnyRectangularMatrix expected = new RectangularMatrix(new[, ] { { 0.0, 2.0 }, { 3.0, 5.0 }, { 0.0, 0.0 } }); AnyRectangularMatrix actual = Utilities.GetColumns(matrix, columnIndices); for (int r = 0; r < expected.RowCount; r++) { for (int c = 0; c < expected.ColumnCount; c++) { Assert.AreEqual(expected, actual); } } }
public static AnyRectangularMatrix GetColumns(AnyRectangularMatrix matrix, IEnumerable <int> columnIndices) { RectangularMatrix target = new RectangularMatrix(matrix.RowCount, columnIndices.Count()); int columnIndex = 0; foreach (int col in columnIndices) { for (int r = 0; r < matrix.RowCount; r++) { target[r, columnIndex] = matrix[r, col]; } columnIndex++; } return(target); }
public void simulate(out ColumnVector Z, out ColumnVector volterra) { int n_T = grid.get_timeNmbrStep(); //Z_i defined in A.1 Z = new ColumnVector(n_T); RectangularMatrix Z_k = new RectangularMatrix(2, n_T); SimulateCorrelated(Z, Z_k);//Simulate 3 Correlated Gaussians Z_i; Z_k_{1,i}; Z_k_{2,i} i:= 0, ..., n_T //Calculation of Matrix M SquareMatrix M = new SquareMatrix(n_T); for (int i = 1; i <= n_T; i++) { for (int j = 1; j <= Math.Min(kappa, i); j++) { M[i - 1, j - 1] = Z_k[j - 1, i - j]; } for (int j = kappa + 1; j <= i; j++) { M[i - 1, j - 1] = Z[i - j]; } } volterra = new ColumnVector(n_T); //Vector of b* ColumnVector b = new ColumnVector(n_T); for (int i = 1; i <= n_T; i++) { if (i <= kappa) { b[i - 1] = 1; } else { double b_ = ((Math.Pow(i, H + 0.5) - Math.Pow(i - 1, H + 0.5)) / (H + 0.5)); b[i - 1] = b_ / Math.Pow(n_T, H - 0.5); } } // 1-simulation of volterra volterra = M * b; }
//Simulate the three COrrelated Processus Z ,Z_1 and Z_2 public void SimulateCorrelated(ColumnVector Z, RectangularMatrix Z_k) { double n_T = grid.get_timeNmbrStep(); for (int i = 0; i < n_T; i++) { ColumnVector vectorG = new ColumnVector(3);// vectorG[0] = simulator.Next(); vectorG[1] = simulator.Next(); vectorG[2] = simulator.Next(); ColumnVector ZVector = choleskyCorrel * vectorG; //Adjusting the Variance Z[i] = Math.Sqrt(var_0) * ZVector[0]; Z_k[0, i] = Math.Sqrt(var_1) * ZVector[1]; //Z_{i,1} Z_k[1, i] = Math.Sqrt(var_2) * ZVector[2]; //Z_{i,2} } }
public RectangularMatrix ScaliarMultiplication(RectangularMatrix source, RectangularMatrix multyplicator, Func <double, double> func) { if (source.RowCount != multyplicator.RowCount || source.ColumnCount != multyplicator.ColumnCount) { throw new InvalidOperationException("Cant mutiplicate"); } var sourceClone = source.Copy(); for (int i = 0; i < sourceClone.RowCount; i++) { for (int j = 0; j < sourceClone.ColumnCount; j++) { var multy = func == null ? multyplicator[i, j] : func(multyplicator[i, j]); sourceClone[i, j] = sourceClone[i, j] * multy; } } return(sourceClone); }
public void RandomRectangularSVD() { for (int c = 1; c < 64; c += 11) { Console.WriteLine(c); RectangularMatrix R = GenerateRandomMatrix(64, c); SingularValueDecomposition SVD = R.SingularValueDecomposition(); Assert.IsTrue(SVD.RowCount == R.RowCount); Assert.IsTrue(SVD.ColumnCount == SVD.ColumnCount); Assert.IsTrue(SVD.Dimension == SVD.ColumnCount); // U has right dimensions and is orthogonal SquareMatrix U = SVD.LeftTransformMatrix; Assert.IsTrue(U.Dimension == R.RowCount); Assert.IsTrue(TestUtilities.IsNearlyEqual(U.Transpose * U, UnitMatrix.OfDimension(U.Dimension))); // V has right dimensions and is orthogonal SquareMatrix V = SVD.RightTransformMatrix; Assert.IsTrue(V.Dimension == R.ColumnCount); Assert.IsTrue(TestUtilities.IsNearlyEqual(V.Transpose * V, UnitMatrix.OfDimension(V.Dimension))); // The transforms decompose the matrix with the claimed singular values RectangularMatrix S = U.Transpose * R * V; for (int i = 0; i < SVD.Contributors.Count; i++) { SingularValueContributor t = SVD.Contributors[i]; Assert.IsTrue(t.SingularValue >= 0.0); Assert.IsTrue(TestUtilities.IsNearlyEqual(S[i, i], t.SingularValue)); Assert.IsTrue(TestUtilities.IsNearlyEqual(R * t.RightSingularVector, t.SingularValue * t.LeftSingularVector)); } // We can reconstruct the original matrix from the claimed singular values RectangularMatrix R2 = new RectangularMatrix(SVD.RowCount, SVD.ColumnCount); foreach (SingularValueContributor t in SVD.Contributors) { R2 += t.SingularValue * t.LeftSingularVector * t.RightSingularVector.Transpose; } Assert.IsTrue(TestUtilities.IsNearlyEqual(R, R2)); } }
private static SquareMatrix ToSquareMatrix(RectangularMatrix source) { Contract.Requires(source != null); Contract.Requires(source.RowCount == source.ColumnCount); Contract.Ensures(Contract.Result <SquareMatrix>() != null); Contract.Ensures(Contract.Result <SquareMatrix>().Dimension == source.RowCount); var n = source.RowCount; var result = new SquareMatrix(n); for (int row = 0; row < n; ++row) { for (int col = 0; col < n; ++col) { result[row, col] = source[row, col]; } } return(result); }
public void SvdOfRankOneMatrix() { // Create a rank-1 matrix ColumnVector v = new ColumnVector(1.0, 2.0, 3.0, 4.0); RectangularMatrix A = v * v.Transpose; SingularValueDecomposition SVD = A.SingularValueDecomposition(); // The rank should be 1 Assert.IsTrue(SVD.Rank == 1); // Only the first singular value should be non-zero double w = SVD.Contributors[0].SingularValue; Assert.IsTrue(w > 0.0); for (int i = 1; i < SVD.Contributors.Count; i++) { Assert.IsTrue(SVD.Contributors[i].SingularValue < TestUtilities.TargetPrecision * w); } }
public RectangularMatrix GetWightsInputHiddenRandom(int lastNeuronsCount, int nextNeuronsCount) { var List = new List <decimal>(); var diapason = (1 / Math.Sqrt(nextNeuronsCount)); for (decimal i = -0.99999999M; i <= 0.99999999M; i += 0.00002M) { List.Add(i); } RectangularMatrix matrix = new RectangularMatrix(nextNeuronsCount, lastNeuronsCount); var rand = new Random(); for (int i = 0; i < nextNeuronsCount; i++) { for (int j = 0; j < lastNeuronsCount; j++) { var index = rand.Next(0, List.Count - 1); matrix[i, j] = (double)List[index]; List.RemoveAt(index); } } return(matrix); }
public void RectangularQRDecomposition() { RectangularMatrix M = GenerateRandomMatrix(30, 10); QRDecomposition QRD = M.QRDecomposition(); Assert.IsTrue(QRD.RowCount == M.RowCount); Assert.IsTrue(QRD.ColumnCount == M.ColumnCount); SquareMatrix Q = QRD.QMatrix; Assert.IsTrue(Q.Dimension == M.RowCount); Assert.IsTrue(TestUtilities.IsNearlyEqual(Q * Q.Transpose, UnitMatrix.OfDimension(Q.Dimension))); RectangularMatrix R = QRD.RMatrix; Assert.IsTrue(R.RowCount == M.RowCount); Assert.IsTrue(R.ColumnCount == M.ColumnCount); RectangularMatrix QR = Q * R; Assert.IsTrue(TestUtilities.IsNearlyEqual(QR, M)); }
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]); } } }