/// <summary> /// Creates one by one matrix containing x /// </summary> /// <param name="x"></param> public ArrayMatrix(Complex x) { RowCount = 1; ColumnCount = 1; Values = new ArrayList(1); Values.Add(new ArrayList(1)); ((ArrayList) Values[0]).Add(x); }
/// <summary> /// Inits square matrix /// </summary> /// <param name="n"></param> public ArrayMatrix(int n) { RowCount = n; ColumnCount = n; Values = new ArrayList(n); for (var i = 0; i < n; i++) { Values.Add(new ArrayList(n)); for (var j = 0; j < n; j++) ((ArrayList) Values[i]).Add(Complex.Zero); } }
/// <summary> /// Creates matrix from 2-d Complex array. /// </summary> /// <param name="values"></param> public ArrayMatrix(Complex[,] values) { if (null == values) { Values = new ArrayList(); ColumnCount = 0; RowCount = 0; } RowCount = (int) values.GetLongLength(0); ColumnCount = (int) values.GetLongLength(1); Values = new ArrayList(RowCount); for (var i = 0; i < RowCount; i++) { Values.Add(new ArrayList(ColumnCount)); for (var j = 0; j < ColumnCount; j++) ((ArrayList) Values[i]).Add(values[i, j]); } }
/// <summary> /// Returns the shortest path between two given vertices i and j as /// int array. /// </summary> /// <param name="P">Path matrix as returned from Floyd().</param> /// <param name="i">One-based index of start vertex.</param> /// <param name="j">One-based index of end vertex.</param> /// <returns></returns> public static ArrayList FloydPath(ArrayMatrix P, int i, int j) { if (!P.IsSquare()) throw new ArgumentException("Path matrix must be square."); else if (!P.IsReal()) throw new ArgumentException("Adjacence matrices are expected to be real."); var path = new ArrayList(); path.Add(i); //int borderliner = 0; //int n = P.Size()[0] + 1; // shortest path cannot have more than n vertices! while (P[i, j] != 0) { i = Convert.ToInt32(P[i, j]); path.Add(i); //borderliner++; //if (borderliner == n) // throw new FormatException("P was not a Floyd path matrix."); } path.Add(j); return path; }
/// <summary> /// Creates real matrix from String, e.g. "1,0;0,1" gives the 2 by 2 identity matrix. /// Not fast, but easy to use, if matrices are to be entered by hand or read from text files. /// </summary> /// <param name="matrix">Matrix coded as String. Lines are separated by a semicolon, column elements by a comma.</param> public ArrayMatrix(String matrix_string) { // remove spaces matrix_string = matrix_string.Replace(" ", ""); // split String into rows, use ';' as separator var rows = matrix_string.Split(new char[] {';'}); // init Values, RowCount, ColumnCount RowCount = rows.Length; Values = new ArrayList(RowCount); ColumnCount = 0; for (var i = 0; i < RowCount; i++) Values.Add(new ArrayList()); for (var i = 1; i <= RowCount; i++) { var curcol = rows[i - 1].Split(new char[] {','}); for (var j = 1; j <= curcol.Length; j++) this[i, j] = new Complex(Convert.ToDouble(curcol[j - 1])); } }
/// <summary> /// Creates column vector from double array. /// </summary> /// <param name="values"></param> public ArrayMatrix(double[] values) { if (values == null) { Values = new ArrayList(); ColumnCount = 0; RowCount = 0; } RowCount = values.Length; ColumnCount = 1; Values = new ArrayList(RowCount); for (var i = 0; i < RowCount; i++) { Values.Add(new ArrayList(1)); ((ArrayList) Values[i]).Add(new Complex(values[i])); } }
/// <summary> /// Creates column vector from Complex array. /// </summary> /// <param name="values"></param> public ArrayMatrix(IList<Complex> values) { if (values == null) { Values = new ArrayList(); ColumnCount = 0; RowCount = 0; } RowCount = values.Count; ColumnCount = 1; Values = new ArrayList(RowCount); for (var i = 0; i < RowCount; i++) { Values.Add(new ArrayList(1)); ((ArrayList) Values[i]).Add(values[i]); } }
static void Main(string[] args) { //Arraylists Console.WriteLine("arraylists"); ArrayList a1 = new ArrayList(); a1.Add(5); a1.Add("raju"); a1.Add(2.33); a1.Add('d'); foreach (var val in a1) { Console.WriteLine(val); } a1.Remove(2.33); foreach (var val in a1) { Console.WriteLine(val); } Console.WriteLine("***********"); //non generic Queue Console.WriteLine("Queue"); Queue qlist = new Queue(); qlist.Enqueue(1); qlist.Enqueue("raj"); qlist.Enqueue('r'); Console.WriteLine("removed element is" + qlist.Dequeue()); foreach (var val in qlist) { Console.WriteLine(val); } Console.WriteLine("***********"); Console.WriteLine("Stack"); Stack slist = new Stack(); slist.Push(23); slist.Push("hello"); slist.Push(23.122); Console.WriteLine(slist.Pop()); Console.WriteLine(slist.Peek()); foreach (var s in slist) { Console.WriteLine(s); } Console.WriteLine("***********"); Console.WriteLine("Hash table"); Hashtable ht = new Hashtable(); ht.Add(1, "raj"); ht.Add("a", 1000); ht.Add("t1", 30); foreach (var h in ht.Keys) { Console.WriteLine(h); Console.WriteLine(ht[h]); } Console.WriteLine("***********"); Console.WriteLine("List"); List <int> list = new List <int>(); list.Add(10); list.Add(10); list.Add('a'); list.Add(34); foreach (int val in list) { Console.WriteLine(val); } Console.WriteLine("***********"); HashSet <int> hs = new HashSet <int>(); Console.WriteLine("Hash set"); hs.Add(10); hs.Add(20); hs.Add(47); foreach (int val in hs) { Console.WriteLine(val); } Console.WriteLine("***********"); Console.WriteLine(" generic queue"); Queue <string> gqlist = new Queue <string>(); gqlist.Enqueue("1"); gqlist.Enqueue("arun"); gqlist.Enqueue("c"); Console.WriteLine(gqlist.Dequeue()); foreach (var val in gqlist) { Console.WriteLine(val); } Stack <float> gslist = new Stack <float>(); gslist.Push(23); gslist.Push(12.234f); gslist.Push(23.123f); Console.WriteLine(gslist.Pop()); Console.WriteLine(gslist.Peek()); foreach (var s in gslist) { Console.WriteLine(s); } Console.WriteLine("***********"); Console.WriteLine("Dictionary"); Dictionary <int, string> dt = new Dictionary <int, string>(); dt.Add(1, "Raj"); dt.Add(2, "radha"); dt.Add(3, "lakshmi"); foreach (KeyValuePair <int, string> kl in dt) { Console.WriteLine(kl.Key); Console.WriteLine(kl.Value); } Console.ReadLine(); Console.ReadLine(); }