Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
2015-2 matrix threads
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; namespace Rextester { //Ejemplos MultiThread //Ejercio 2015-2: /* Crear clase Matriz, Vector: overload operator *, + (Suma y Multiplicación) Sumar Matrices: 1 thread por fila Multiplicar Matrices */ public class Matriz{ int[][] matriz; public int Size{ get{ return matriz.Length; } } public int[] this[int index]{ get{ return matriz[index]; } } /* public int this[int fila, int columna]{ get{ return matriz[fila][columna]; } set{ this.matriz[fila][columna] = value; } }*/ public Matriz(int size){ this.matriz = new int[size][]; for(int i = 0; i < size; i++){ this.matriz[i] = new int[size]; for(int j = 0; j < size; j++){ this.matriz[i][j] = 0; } } } public Matriz(int[][] matriz){ this.matriz = new int[matriz.Length][] ; if( matriz.Length != 0){ for(int i = 0; i < matriz.Length; i++){ this.matriz[i] = new int[matriz.Length]; for(int j = 0; j < matriz.Length; j++){ this.matriz[i][j] = matriz[i][j]; } } } else throw new ArgumentException(); } public static Matriz operator + ( Matriz a, Matriz b){ ManualResetEvent[] manualEvents = new ManualResetEvent[a.Size]; for(int i = 0; i < a.Size; i++){ manualEvents[i] = new ManualResetEvent(false); } Matriz matriz; List<Task> tasks = new List<Task>(); if(a.Size != b.Size ) throw new InvalidOperationException("Matrix dimension not equal"); else{ matriz = new Matriz(a.Size); for(int i = 0; i < a.Size; i++){ Console.WriteLine("Sumar fila {0}", i); Thread t = new Thread(SumarFila); t.Start(new Tuple<int,Matriz,Matriz,Matriz,ManualResetEvent>(i,matriz,a,b,manualEvents[i])); } } WaitHandle.WaitAll(manualEvents); return matriz; } private static void SumarFila(object o){ var data = (Tuple<int,Matriz,Matriz,Matriz,ManualResetEvent>)o; int fila = data.Item1; Matriz c = data.Item2; Matriz a = data.Item3; Matriz b = data.Item4; ManualResetEvent mre = data.Item5; for(int j = 0; j < a.Size ; j++){ c[fila][j] = a[fila][j] + b[fila][j]; } mre.Set(); } public override string ToString(){ var str = ""; for(int i = 0; i < this.Size; i++){ for(int j = 0; j < this.Size; j++){ str += (this.matriz[i][j].ToString()).PadRight(4); } str += "\n"; } return str; } public int[] Orden { get{ var orden = new List<int>(); foreach( int[] fila in this.matriz ){ orden.AddRange(fila); } orden.Sort(); return orden.ToArray(); } } } public enum VectorType {Columna, Fila}; public class Vector{ private int[] vector; public VectorType type = VectorType.Fila; public int Size{ get{ return vector.Length; } } public Vector(int size){ this.vector = new int[size]; } public Vector(int[] v){ this.vector = new int[v.Length]; for(int j = 0; j < this.Size; j++){ this.vector[j] = v[j]; } } public Vector(int[] v, VectorType type){ this.vector = new int[v.Length]; this.type = type; for(int j = 0; j < this.Size; j++){ this.vector[j] = v[j]; } } public override string ToString(){ var str = ""; for(int i = 0; i < this.Size; i++){ str += this.type == VectorType.Columna ? this.vector[i] + "\n" : (this.vector[i].ToString()).PadRight(3); } return str; } } public class Program { public static void Main(string[] args) { //Your code goes here Matriz A = new Matriz(3); int[][] jaggedArray3 = { new int[] {1,3,5}, new int[] {0,2,4}, new int[] {11,3,0} }; /* int[][] 2darray = new int[3][]; for(int i = 0; i < 3; i++){ 2darray[i] = new int[3]; for(int j = 0; j < size; j++){ 2darray[i][j] = i*j; } }*/ Matriz B = new Matriz(jaggedArray3); Vector v = new Vector(B.Orden); Console.WriteLine(v.ToString()); A = A + B; Console.WriteLine(A.ToString()); A = A + B; Console.WriteLine(A.ToString()); A = A + B; Console.WriteLine(A.ToString()); A = A + B; Console.WriteLine(A.ToString()); //Vector r1,r2; //v.type = columna; //r1 = v*A; //v.type = fila; //r2 = v*A; } } }
run
|
edit
|
history
|
help
0
Welcome
Fórum ➡ What values have the greatest frequency? ♦
Lambda Expression
Create URL Safe and Cryptographically Strong Short GUID
Microsoft Hello World
value tuple
FuncDelegate
Assignment 1
Math 10.95 fracOp (mult div only)
First Class