public static GenericMatrix <T> operator *(GenericMatrix <T> first, GenericMatrix <T> second) { //making sure first matrix columns and second matrix rows have the same length, exception will be thrown in case of mismatch if (first.columns != second.rows) { throw new ArgumentException("Matrices do not have equal length"); } GenericMatrix <T> third = new GenericMatrix <T>(first.rows, second.columns); for (int rows = 0; rows < third.columns; rows++) { for (int columns = 0; columns < first.rows; columns++) { dynamic value = 0; for (int modifier = 0; modifier < second.rows; modifier++) { value += (dynamic)first[columns, modifier] * (dynamic)second[modifier, rows]; } third[columns, rows] = value; } } return(third); }
static void Main(string[] args) { //Create new Matrix var genericMatrix = new GenericMatrix <int>(2, 2); genericMatrix[0, 0] = 1; genericMatrix[0, 1] = 2; genericMatrix[1, 0] = 3; genericMatrix[1, 1] = 4; var secondGenericMatrix = new GenericMatrix <int>(2, 2); secondGenericMatrix[0, 0] = 5; secondGenericMatrix[0, 1] = 6; secondGenericMatrix[1, 0] = 7; secondGenericMatrix[1, 1] = 8; var result = genericMatrix * secondGenericMatrix; Console.WriteLine(result); //Expected result of matrix multiplication //19 22 //43 50 //Test overloaded true/false operator var newGenericMatrix = new GenericMatrix <int>(2, 2); genericMatrix[0, 0] = 0; genericMatrix[0, 1] = 0; genericMatrix[1, 0] = 3; genericMatrix[1, 1] = 4; if (newGenericMatrix) { Console.WriteLine("There is zero values in the Matrix!"); } else { Console.WriteLine("There is NO zero values in the Matrix!"); } if (result) { Console.WriteLine("There is zero values in the Matrix!"); } else { Console.WriteLine("There is NO zero values in the Matrix!"); } //Print Attributes Type matrixType = typeof(GenericMatrix <Type>); var allAttributes = matrixType.GetCustomAttributes(false); //matrixType return type is "object[]". Console.WriteLine(string.Join(Environment.NewLine, allAttributes)); }
public GenericMatrix(GenericMatrix <T, S> source) { //Copy constructor m_rowstart = source.m_rowstart; m_columnstart = source.m_columnstart; nr = source.nr; nc = source.nc; initState(); }
public static GenericMatrix <T> operator -(GenericMatrix <T> first, GenericMatrix <T> second) { //making sure first matrix and second matrix rows & columns have the same length, exception will be thrown in case of mismatch if (first.rows != second.rows || first.columns != second.columns) { throw new ArgumentException("Matrices do not have equal length"); } GenericMatrix <T> third = new GenericMatrix <T>(first.rows, second.columns); for (int rows = 0; rows < third.rows; rows++) { for (int columns = 0; columns < third.columns; columns++) { third[rows, columns] = (dynamic)first[rows, columns] - (dynamic)second[rows, columns]; } } return(third); }
static void Main() { GenericMatrix <int> first = new GenericMatrix <int>(5, 5); GenericMatrix <int> second = new GenericMatrix <int>(5, 5); Random random = new Random(); for (int rows = 0; rows < first.Rows; rows++) { for (int columns = 0; columns < first.Columns; columns++) { first[rows, columns] = random.Next(-150, 150); second[rows, columns] = random.Next(-150, 150); } } Console.WriteLine("Matrix 1:"); Console.WriteLine(first); Console.WriteLine("Matrix 2:"); Console.WriteLine(second); Console.WriteLine("Result after addition:"); Console.WriteLine(first + second); Console.WriteLine("Result upon substraction:"); Console.WriteLine(first - second); Console.WriteLine("Matrices multiplication:"); Console.WriteLine(first * second); if (first) { Console.WriteLine("Not empty"); } else { Console.WriteLine("Empty"); } }
static void Main() { GenericMatrix<int> first = new GenericMatrix<int>(5, 5); GenericMatrix<int> second = new GenericMatrix<int>(5, 5); Random random = new Random(); for (int rows = 0; rows < first.Rows; rows++) { for (int columns = 0; columns < first.Columns; columns++) { first[rows, columns] = random.Next(-150, 150); second[rows, columns] = random.Next(-150, 150); } } Console.WriteLine("Matrix 1:"); Console.WriteLine(first); Console.WriteLine("Matrix 2:"); Console.WriteLine(second); Console.WriteLine("Result after addition:"); Console.WriteLine(first + second); Console.WriteLine("Result upon substraction:"); Console.WriteLine(first - second); Console.WriteLine("Matrices multiplication:"); Console.WriteLine(first * second); if (first) { Console.WriteLine("Not empty"); } else { Console.WriteLine("Empty"); } }
static void Main(string[] args) { // Jagged arrays // Lower triangular matrix int NR = 10; int NC = 10; double[][] lowerTriangular = new double[NR][]; for (int j = 0; j < NC; j++) { lowerTriangular[j] = new double[j + 1]; } // Generic arrays for starters NR = 4; NC = 4; //MatrixTwoArrayImpl<double> myMatrixStructure = new MatrixTwoArrayImpl<double>(NR, NC); UpperTriangularImpl <double> myMatrixStructure = new UpperTriangularImpl <double>(NR, NC); GenericMatrix <double, MatrixTwoArrayImpl <double> > myMatrix = new GenericMatrix <double, MatrixTwoArrayImpl <double> >(NR, NC); myMatrix.extendedPrint(); for (int i = myMatrix.MinRowIndex; i <= myMatrix.MaxRowIndex; i++) { for (int j = myMatrix.MinColumnIndex; j <= myMatrix.MaxColumnIndex; j++) { myMatrix[i, j] = -1; } } myMatrix.extendedPrint(); MatrixOneArrayImpl <double> myMatrixStructure2 = new MatrixOneArrayImpl <double>(NR, NC); GenericMatrix <double, MatrixOneArrayImpl <double> > myMatrix2 = new GenericMatrix <double, MatrixOneArrayImpl <double> >(NR, NC); myMatrix2.extendedPrint(); for (int i = myMatrix2.MinRowIndex; i <= myMatrix2.MaxRowIndex; i++) { for (int j = myMatrix2.MinColumnIndex; j <= myMatrix2.MaxColumnIndex; j++) { myMatrix2[i, j] = 2; } } myMatrix2[myMatrix2.MinRowIndex, myMatrix2.MinColumnIndex] = 99; myMatrix2[myMatrix2.MaxRowIndex, myMatrix2.MaxColumnIndex] = 98; myMatrix2.extendedPrint(); // Algebra and matrix manipulation int J = 3; Vector <double> a = new Vector <double>(J, 1, 1.0); Vector <double> b = new Vector <double>(J, 1, 2.0); Vector <double> c = new Vector <double>(J, 1, 1.0); Vector <double> r = new Vector <double>(J, 1, 0.0); // Right-hand side for (int i = r.MinIndex; i <= r.MaxIndex; i++) { r[i] = 1.0; } r[2] = -1.0; /* r[1] = 4.0; * r[2] = 10.0; * r[3] = 8.0;*/ Console.WriteLine("LU stuff"); LUTridiagonalSolver mySolver = new LUTridiagonalSolver(a, b, c, r); Vector <double> result = mySolver.solve(); Console.WriteLine("Solution"); result.extendedPrint(); // Array int startIndex = -1; Array <double> arr = new Array <double>(10, startIndex); for (int j = arr.MinIndex; j <= arr.MaxIndex; j++) { arr[j] = (double)(j); } arr[arr.MinIndex] = 99.98; // arr.extendedPrint(); // Matrix Matrix <int> mat = new Matrix <int>(4, 4, 2, 1); mat[mat.MinRowIndex, mat.MinColumnIndex] = 99; mat.initCells(3); mat[mat.MinRowIndex, mat.MinColumnIndex] = 98; mat.extendedPrint(); for (int i = mat.MinRowIndex; i <= mat.MaxRowIndex; i++) { for (int j = mat.MinColumnIndex; j <= mat.MaxColumnIndex; j++) { mat[i, j] = i * j; } } mat[mat.MinRowIndex, mat.MinColumnIndex] = 98; mat.extendedPrint(); // Vectors Vector <double> vec = new Vector <double>(10); for (int j = vec.MinIndex; j <= vec.MaxIndex; j++) { vec[j] = (double)(vec.MaxIndex - j); } vec.print(); Vector <double> vec2 = vec + vec; vec2.print(); vec2 = vec - vec; vec2.print(); vec2 = vec - 3.0; vec2.print(); vec2 = 2.0 + vec; vec2.print(); // Numeric Matrices, initial tests NumericMatrix <double> mat2 = new NumericMatrix <double>(4, 4); mat2.initCells(1.0); mat2.extendedPrint(); for (int i = mat2.MinRowIndex; i <= mat2.MaxRowIndex; i++) { for (int j = mat2.MinColumnIndex; j <= mat2.MaxColumnIndex; j++) { mat2[i, j] = i * j; } } Console.WriteLine("Matrix multiplication..."); Vector <double> vec3 = new Vector <double>(4, 1, 2.0); Vector <double> vec4 = mat2 * vec3; vec4.print(); NumericMatrix <double> mat4 = mat2 * mat2; mat4.extendedPrint(); // Numeric Matrices, testing accuracy /* int rows = 10; * int columns = 10; * int startRow = 1; * int startColumn = 1; * NumericMatrix<double> A = new NumericMatrix<double>(rows, columns, startRow, startColumn); * A[A.MinRowIndex, A.MinColumnIndex] = 1.0; A[A.MinRowIndex, A.MinColumnIndex + 1] = 2.0; * A[A.MinRowIndex + 1, A.MinColumnIndex] = 3.0; A[A.MinRowIndex + 1, A.MinColumnIndex + 1] = 4.0; * // A.extendedPrint(); * * int si = 1; // Start index * Vector<double> x = new Vector<double>(10, si); * x[si] = 1.0; * x[si + 1] = 2.0; * * Vector<double> y = A * x; * // y.extendedPrint(); * * NumericMatrix<double> B = A * A; * // B.extendedPrint(); * * B = B * B; * B = B * B; * // B.extendedPrint(); * * // Modify rows and columns of matrices * Vector<double> r1 = new Vector<double>(10, 1, 3.3); * //r1.extendedPrint(); * // A.Row(A.MinRowIndex, r1); * // A.Column(A.MaxColumnIndex, r1); * A[A.MinRowIndex, A.MinColumnIndex] = 00.0001; * A[A.MaxRowIndex, A.MaxColumnIndex] = -99.99; * A[A.MaxRowIndex, A.MinColumnIndex] = 88.88; * A.extendedPrint(); * * // Slices * Vector<double> vecSlice = A.getRow(A.MaxRowIndex); * vecSlice.extendedPrint(); * vecSlice = A.getColumn(A.MinColumnIndex); * vecSlice.extendedPrint(); * * int[, ,] tensor = new int[3, 3, 3]; * for (int i = 0; i < tensor.GetLength(0); i++) * for (int j = 0; j < tensor.GetLength(1); j++) * for (int k = 0; k < tensor.GetLength(2); k++) * tensor[i, j, k] = i * j * k; * * /* for (int i = 0; i < td.GetLength(0); i++) * for (int j = 0; j < td.GetLength(0); j++) * for (int k = 0; k < td.GetLength(0); k++) * Console.Write(td[i, j, k]); Console.Write(", ");*/ //A.Column(2, r1); // Tensors int nrows = 2; int ncols = 2; int ndepth = 10; Tensor <double> myTensor = new Tensor <double>(nrows, ncols, ndepth); // myTensor[myTensor.MinThirdIndex] = B; for (int j = myTensor.MinThirdIndex + 1; j <= myTensor.MaxThirdIndex; j++) { // myTensor[j] = A*A; } for (int j = myTensor.MinThirdIndex + 1; j <= myTensor.MaxThirdIndex; j++) { // myTensor[j].extendedPrint(); } }
//-------------------------------------------------------------------- internal GenericMatrixListDimension(GenericMatrix <T> Matrix_in) : this(Matrix_in, 0) { return; }
//-------------------------------------------------------------------- internal GenericMatrixListDimension(GenericMatrix <T> Matrix_in, int Min_in) { this._matrix = Matrix_in; this._min = Min_in; return; }
private static void Main() { GenericMatrix<int> firstMatrix = new GenericMatrix<int>(3, 2); firstMatrix[0, 0] = 1; firstMatrix[0, 1] = 2; firstMatrix[1, 0] = 3; firstMatrix[1, 1] = 4; firstMatrix[2, 0] = 5; firstMatrix[2, 1] = 6; GenericMatrix<int> secondMatrix = new GenericMatrix<int>(3, 2); secondMatrix[0, 0] = 1; secondMatrix[0, 1] = 2; secondMatrix[1, 0] = 0; secondMatrix[1, 1] = 4; secondMatrix[2, 0] = 5; secondMatrix[2, 1] = 6; //Checking the operators for adding, substracting and multiplying GenericMatrix<int> resultFromAdding = firstMatrix + secondMatrix; GenericMatrix<int> resultFromSubstracting = firstMatrix - secondMatrix; GenericMatrix<int> resultFromMultiplication = firstMatrix * secondMatrix; //Showing the attribute at runtime Type type = typeof(MainClass); object[] attributes = type.GetCustomAttributes(false); foreach (object attribute in attributes) { VersionAttribute version = (VersionAttribute)attribute; Console.WriteLine(version.Version); } //Checking the true operator if (secondMatrix) { Console.WriteLine("SecondMatrix Contains Zero"); } else { Console.WriteLine("SecondMatrix Not Contains Zero"); } //Console.WriteLine(matrix[2,1]); //matrix[1, 1] = 5; //matrix[0, 1] = 3; //matrix[1, 1] = 4; Point3D newpoint = new Point3D(); newpoint.X = 5; newpoint.Y = 8; newpoint.Z = 1; //The overrided ToString() prints us the points on the console //newpoint.ToString(); //This is the static point with coordinates 5,5,5 to test it because 0,0,0 is by default and it will //aways work //Point3D.ZeroPoint.ToString(); Point3D ad = new Point3D(4, 5, 6); //This Class creates List of paths Path newPath = new Path(); newPath.SequenceOfPoints.Add(newpoint); newPath.SequenceOfPoints.Add(Point3D.ZeroPoint); newPath.SequenceOfPoints.Add(ad); //This class Saves the currentPath in text file, you can type the name and it will appear in the //Files folder "../../Files" in the project // PathStorage.SavePaths(newPath, "mitko.doc"); // This will load a coordinate file from the same directory - Files and will split and initialize // them and will save them in SavedPaths.txt automaticly //PathStorage.LoadPaths(); //This is Class that calculate the difference between two points //DistanceBetweenPoints.DistanceBetweenPoint(newpoint, Point3D.ZeroPoint); GenericList<int> test = new GenericList<int>(5); test.Add(5); test.Add(34); test.Remove(0); test.Insert(2, 1); test.Add(4); test.Insert(3, 1); test.Insert(4, 1); test.Add(8); //Auto grow functionality is turned on from now on test.Add(9); test.Add(10); //Gets the minValue in the GenericList test.Min(); test.Max(); //this is for testing the object states //Console.WriteLine(test[0] + " " + test[1] + " " + test[2] + " " + test[3]); //testing the ToString() //Console.WriteLine(test[0].ToString()); test.Contains(2); test.Contains(100); }