private void UpdateWeightVector(int i1, int i2, double a1, double a2) { double alpha1, alpha2; alpha1 = m_alpha[i1]; alpha2 = m_alpha[i2]; m_weight.Add((y(i1) * (a1 - alpha1)) * x(i1)); m_weight.Add((y(i2) * (a2 - alpha2)) * x(i2)); }
private void CalculateWeight(ExampleSet t_Set) { for (int i = 0; i < l; i++) { m_weight.Add((t_Set.Examples[i].Label.Id * m_Alpha[i]) * t_Set.Examples[i].X); } }
public void Addテスト() { var x = new SparseVector <double>(0.0d); var y = new SparseVector <double>(0.0d); x.Add(1.0d); x.Add(2.0d); y.Add(3.0d); y.Add(4.0d); y.Add(5.0d); x.Add(y); Assert.AreEqual(5, x.Length); var expected = 1.0d; foreach (var d in x) { Assert.AreEqual(expected, d); expected += 1.0d; } }
public void IndexedPropertyGetAccessorReturnsWhatWasSavedThroughAddMethod() { // arrange var originalVector = SparseVectorHelper.GenerateRandomVector(1000, 0.7, () => SparseVectorHelper.RandomInInterval(-100, 100, 2)); var sv = new SparseVector <double>(); // act foreach (var element in originalVector) { sv.Add(element.Key, element.Value); } // assert foreach (var element in originalVector) { Assert.AreEqual(element.Value, sv[element.Key]); } }
public void IndexedPropertyGetAccessorReturnsWhatWasSavedThroughAddMethod() { // arrange var originalVector = SparseVectorHelper.GenerateRandomVector(1000, 0.7, () => SparseVectorHelper.RandomInInterval(-100, 100, 2)); var sv = new SparseVector<double>(); // act foreach (var element in originalVector) { sv.Add(element.Key, element.Value); } // assert foreach (var element in originalVector) { Assert.AreEqual(element.Value, sv[element.Key]); } }
static void Main(string[] args) { // create SparseVector Console.WriteLine("Create SparseVector ..."); SparseVector <string> vec = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(1, "a"), new IdxDat <string>(3, "b"), new IdxDat <string>(4, "c"), new IdxDat <string>(6, "d") }); Console.WriteLine(vec); // add more items Console.WriteLine("Add more items ..."); // ... at the end vec.Add("E"); vec.AddRange(new string[] { "F", "G" }); // ... at specific places vec.AddRange(new IdxDat <string>[] { new IdxDat <string>(2, "AB"), new IdxDat <string>(10, "H") }); vec[11] = "i"; Console.WriteLine(vec); // get items Console.WriteLine("Get items ..."); Console.WriteLine(vec[1]); Console.WriteLine(vec.TryGet(2, "missing")); Console.WriteLine(vec.TryGet(5, "missing")); // set items Console.WriteLine("Set items ..."); vec[2] = "ab"; vec[10] = "h"; Console.WriteLine(vec); // check for items Console.WriteLine("Check for items ..."); Console.WriteLine(vec.ContainsAt(2)); Console.WriteLine(vec.ContainsAt(5)); // get first and last non-empty index Console.WriteLine("Get first and last non-empty index ..."); Console.WriteLine(vec.FirstNonEmptyIndex); Console.WriteLine(vec.LastNonEmptyIndex); // get first and last item Console.WriteLine("Get first and last item ..."); Console.WriteLine(vec.First); Console.WriteLine(vec.Last); // create another SparseVector Console.WriteLine("Create another SparseVector ..."); SparseVector <string> vec2 = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(0, "!"), new IdxDat <string>(2, "@"), new IdxDat <string>(3, "#"), new IdxDat <string>(5, "$") }); Console.WriteLine(vec2); // concatenate Console.WriteLine("Concatenate ..."); vec.Append(vec2, vec.LastNonEmptyIndex + 1); Console.WriteLine(vec); vec2.Append(vec, vec2.LastNonEmptyIndex + 1); Console.WriteLine(vec2); // get number of items Console.WriteLine("Get number of items ..."); Console.WriteLine(vec.Count); // remove item Console.WriteLine("Remove item ..."); vec.RemoveAt(2); Console.WriteLine(vec); // directly access to items Console.WriteLine("Directly access to items ..."); int idx = vec.GetDirectIdx(3); Console.WriteLine(idx); vec.SetDirect(idx, "bbb"); Console.WriteLine(vec); Console.WriteLine(vec.GetIdxDirect(idx)); Console.WriteLine(vec.GetDatDirect(idx)); Console.WriteLine(vec.GetDirect(idx)); vec.RemoveDirect(idx); Console.WriteLine(vec); // perform unary operation Console.WriteLine("Perform unary operation ..."); vec.PerformUnaryOperation(delegate(string item) { return(item.ToUpper()); }); Console.WriteLine(vec); // merge Console.WriteLine("Merge ..."); vec.Merge(vec2, delegate(string a, string b) { return(string.Format("{0}+{1}", a, b)); }); Console.WriteLine(vec); // purge Console.WriteLine("Purge items ..."); vec.PurgeAt(1); Console.WriteLine(vec); vec.PurgeAt(1); Console.WriteLine(vec); Console.WriteLine(); // *** SparseMatrix *** Console.WriteLine("*** SparseMatrix ***"); Console.WriteLine(); // create SparseMatrix Console.WriteLine("Create SparseMatrix ..."); SparseMatrix <string> matrix = new SparseMatrix <string>(); matrix[0] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(1, "a"), new IdxDat <string>(3, "b"), new IdxDat <string>(4, "c") }); matrix[2] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(2, "d"), new IdxDat <string>(4, "e"), new IdxDat <string>(5, "f") }); matrix[3] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(0, "g"), new IdxDat <string>(3, "h"), new IdxDat <string>(5, "i") }); matrix[4] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(1, "j"), new IdxDat <string>(2, "k"), new IdxDat <string>(4, "l") }); Console.WriteLine(matrix.ToString("E")); // get rows Console.WriteLine("Get rows ..."); Console.WriteLine(matrix[0]); Console.WriteLine(matrix[3]); // set rows Console.WriteLine("Set rows ..."); matrix[1] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(0, "j"), new IdxDat <string>(3, "k") }); matrix[2] = null; matrix[4] = null; Console.WriteLine(matrix.ToString("E")); // count rows Console.WriteLine("Count rows ..."); Console.WriteLine("{0} != {1}", matrix.GetRowCount(), matrix.GetLastNonEmptyRowIdx() + 1); // trim rows Console.WriteLine("Trim rows ..."); matrix.TrimRows(); Console.WriteLine(matrix.ToString("E")); // add more items Console.WriteLine("Add more items ..."); matrix[0].Add("*"); matrix[3].AddRange(new IdxDat <string>[] { new IdxDat <string>(1, "!"), new IdxDat <string>(2, "?"), new IdxDat <string>(4, "&") }); matrix[2] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(2, "d"), new IdxDat <string>(4, "e"), new IdxDat <string>(5, "f") }); Console.WriteLine(matrix.ToString("E")); // get items Console.WriteLine("Get items ..."); Console.WriteLine(matrix[0, 1]); Console.WriteLine(matrix[2, 2]); Console.WriteLine(matrix[2][4]); Console.WriteLine(matrix.TryGet(2, 4, "missing")); Console.WriteLine(matrix.TryGet(2, 6, "missing")); // set items Console.WriteLine("Set items ..."); matrix[0, 1] = "l"; matrix[2, 3] = "m"; matrix[3][4] = "n"; Console.WriteLine(matrix.ToString("E")); // check for items Console.WriteLine("Check for items ..."); Console.WriteLine(matrix.ContainsAt(0, 1)); Console.WriteLine(matrix.ContainsAt(1, 1)); Console.WriteLine(matrix.Contains("c")); Console.WriteLine(matrix.Contains("C")); int rowIdx = -1, colIdx = -1; matrix.IndexOf("c", ref rowIdx, ref colIdx); Console.WriteLine("{0}, {1}", rowIdx, colIdx); // check for rows and columns Console.WriteLine("Check for rows and columns ..."); Console.WriteLine(matrix.ContainsColAt(0)); Console.WriteLine(matrix.ContainsColAt(100)); Console.WriteLine(matrix.ContainsRowAt(0)); Console.WriteLine(matrix.ContainsRowAt(100)); // get first and last non-empty row and column index Console.WriteLine("Get first and last non-empty row and column index ..."); Console.WriteLine(matrix.GetFirstNonEmptyRowIdx()); Console.WriteLine(matrix.GetLastNonEmptyRowIdx()); Console.WriteLine(matrix.GetFirstNonEmptyColIdx()); Console.WriteLine(matrix.GetLastNonEmptyColIdx()); // get first and last item in row Console.WriteLine("Get first and last item in row ..."); Console.WriteLine(matrix[0].First); Console.WriteLine(matrix[3].Last); // create another SparseMatrix Console.WriteLine("Create another SparseMatrix ..."); SparseMatrix <string> matrix2 = new SparseMatrix <string>(); matrix2[0] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(0, "A"), new IdxDat <string>(2, "B"), new IdxDat <string>(3, "C") }); matrix2[2] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(1, "D"), new IdxDat <string>(3, "E") }); matrix2[3] = new SparseVector <string>(new IdxDat <string>[] { new IdxDat <string>(0, "G"), new IdxDat <string>(1, "H"), new IdxDat <string>(2, "I") }); Console.WriteLine(matrix2.ToString("E")); // concatenate Console.WriteLine("Concatenate ..."); matrix.AppendCols(matrix2, matrix.GetLastNonEmptyColIdx() + 1); Console.WriteLine(matrix.ToString("E")); // remove items Console.WriteLine("Remove items ..."); matrix.RemoveAt(0, 1); matrix.RemoveAt(3, 5); Console.WriteLine(matrix.ToString("E")); // directly access to items Console.WriteLine("Directly access to items ..."); idx = matrix[0].GetDirectIdx(4); Console.WriteLine(idx); Console.WriteLine(matrix[0].GetDirect(idx)); matrix[0].SetDirect(idx, "C"); Console.WriteLine(matrix[1].GetDirect(0)); matrix[1].RemoveDirect(0); Console.WriteLine(matrix.ToString("E")); // get properties Console.WriteLine("Get properties ..."); Console.WriteLine("{0:0.00}%", matrix.GetSparseness(matrix.GetLastNonEmptyRowIdx() + 1, matrix.GetLastNonEmptyColIdx() + 1) * 100.0); Console.WriteLine(matrix.IsSymmetric()); Console.WriteLine(matrix.ContainsDiagonalElement()); Console.WriteLine(matrix.CountValues()); // perform unary operation Console.WriteLine("Perform unary operation ..."); matrix.PerformUnaryOperation(delegate(string item) { return(item.ToUpper()); }); Console.WriteLine(matrix.ToString("E")); // merge Console.WriteLine("Merge ..."); matrix.Merge(matrix2, delegate(string a, string b) { return(string.Format("{0}+{1}", a, b)); }); Console.WriteLine(matrix.ToString("E")); // clear row and column Console.WriteLine("Clear row and column ..."); matrix.RemoveRowAt(2); matrix.RemoveColAt(1); Console.WriteLine(matrix.ToString("E")); // purge row and column Console.WriteLine("Purge row and column ..."); matrix.PurgeRowAt(2); matrix.PurgeColAt(1); Console.WriteLine(matrix.ToString("E")); // get column copy Console.WriteLine("Get column copy ..."); Console.WriteLine(matrix.GetColCopy(0)); // transpose Console.WriteLine("Transpose ..."); Console.WriteLine(matrix.GetTransposedCopy().ToString("E")); // set diagonal Console.WriteLine("Set diagonal ..."); matrix.SetDiagonal(matrix.GetLastNonEmptyColIdx() + 1, "X"); Console.WriteLine(matrix.ToString("E")); // make symmetric Console.WriteLine("Make symmetric ..."); matrix.Symmetrize(delegate(string a, string b) { return(string.Format("{0}+{1}", a, b)); }); Console.WriteLine(matrix.ToString("E")); }
/// <summary> /// フィールドの特徴を生成します。 /// </summary> /// <param name="context"></param> /// <returns>フィールドの特徴</returns> public SparseVector <double> GetNext(FieldContext context) { var vector = new SparseVector <double>(0.0d); var param = new ErasedPotentialSlimeAnalyzer.Param(); param.ErasedSlimes = context.UsingSlimes; var players = new[] { Player.Index.First, Player.Index.Second }; foreach (var player in players) { if (!this.OwnConfig.BothPlayer && player != context.OperationPlayer) { continue; } var erasedPotentialCount = 0; var slimeCount = 0; foreach (var slime in context.UsingSlimes) { param.TargetSlime = slime; if (this.OwnConfig.TargetFeatue[Feature.ErasedPotentialCount]) { // 他の色を消すと消える個数 erasedPotentialCount += ErasedPotentialSlimeAnalyzer.Analyze(context, player, param); } if (this.OwnConfig.TargetFeatue[Feature.SlimeCount]) { // フィールドのスライム数 slimeCount += SlimeCountHelper.GetSlimeCount(context, player, slime); } } if (this.OwnConfig.TargetFeatue[Feature.ErasedPotentialCount]) { vector.Add(erasedPotentialCount); } if (this.OwnConfig.TargetFeatue[Feature.SlimeCount]) { vector.Add(slimeCount); } if (this.OwnConfig.TargetFeatue[Feature.ObstructionCount]) { // フィールドのおじゃまスライム数 var obstructionCount = SlimeCountHelper.GetSlimeCount(context, player, Slime.Obstruction); vector.Add(obstructionCount); } if (this.OwnConfig.TargetFeatue[Feature.NoticeObstruction]) { // 予告おじゃまスライム数 var noticeObstruction = ObstructionSlimeHelper.ObstructionsToCount(context.ObstructionSlimes[(int)player]); vector.Add(noticeObstruction); } if (this.OwnConfig.TargetFeatue[Feature.HeightDiff]) { // 高低差 var hDiff = DifferenceHeightAnalyzer.Analyze(context, player); vector.Add(hDiff); } if (this.OwnConfig.TargetFeatue[Feature.UpperCount]) { // 上部スライム数 var upperCount = 0; foreach (var u in UpperUnits) { foreach (var i in UpperIndexes) { upperCount += FieldContextHelper.ExistsSlime(context, player, FieldContextConfig.MaxHiddenUnitIndex + u, i) ? 1 : 0; } } vector.Add(upperCount); } if (this.OwnConfig.TargetFeatue[Feature.DangerCount]) { // 左から3番目のスライム数 var dangerCount = 0; foreach (var u in DangerUnits) { foreach (var i in DangerIndexes) { dangerCount += FieldContextHelper.ExistsSlime(context, player, FieldContextConfig.MaxHiddenUnitIndex + u, i) ? 1 : 0; } } vector.Add(dangerCount); } if (this.OwnConfig.TargetFeatue[Feature.Chain]) { // 起こりうる最大連鎖数 var chain = 0; if (player == context.OperationPlayer) { chain = this.ChainAnalyzer.Analyze(context, player); } vector.Add(chain); } } ; return(vector); }
public void TestOrthoNormal() { List<IVector> Rcols; List<IVector> Acols = MatrixUtils.GetColumnVectors(A); List<IVector> vstarlist = Orthogonalization.Orthogonalize(Acols, out Rcols); for (int i = 0; i < Rcols.Count; ++i) { int count = 0; for (int j = 0; j < Rcols[i].Dimension; ++j) { count += (System.Math.Abs(Rcols[i][j]) > 1e-10) ? 1 : 0; } Assert.Equal(i + 1, count); } for (int i = 0; i < Acols.Count; ++i) { IVector pi = new SparseVector(Acols[i].Dimension); for (int j = 0; j < vstarlist.Count; ++j) { pi = pi.Add(vstarlist[j].Multiply(Rcols[i][j])); } for (int d = 0; d < Acols[i].Dimension; ++d) { Assert.Equal(Acols[i][d], pi[d]); } Assert.True(Acols[i].Equals(pi)); } List<double> norms; List<IVector> qlist = VectorUtils.Normalize(vstarlist, out norms); for (int i = 0; i < qlist.Count - 1; ++i) { for (int j = i + 1; j < vstarlist.Count; ++j) { Assert.True(qlist[i].Multiply(qlist[j]) < 1e-10); } Assert.True(qlist[i].Norm(2) - 1 < 1e-10); } for (int i = 0; i < vstarlist.Count; ++i) { Assert.Equal(norms[i], vstarlist[i].Norm(2)); } IMatrix R = MatrixUtils.GetMatrix(Rcols); foreach (int r in R.RowKeys) { R[r] = R[r].Multiply(norms[r]); } for (int i = 0; i < A.RowCount; ++i) { for (int j = 0; j < A.ColCount; ++j) { Assert.Equal(R[i][j], Rcols[j][i] * norms[i]); } } }
static void Main(string[] args) { // create SparseVector Console.WriteLine("Create SparseVector ..."); SparseVector<string> vec = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(1, "a"), new IdxDat<string>(3, "b"), new IdxDat<string>(4, "c"), new IdxDat<string>(6, "d") }); Console.WriteLine(vec); // add more items Console.WriteLine("Add more items ..."); // ... at the end vec.Add("E"); vec.AddRange(new string[] { "F", "G" }); // ... at specific places vec.AddRange(new IdxDat<string>[] { new IdxDat<string>(2, "AB"), new IdxDat<string>(10, "H") }); vec[11] = "i"; Console.WriteLine(vec); // get items Console.WriteLine("Get items ..."); Console.WriteLine(vec[1]); Console.WriteLine(vec.TryGet(2, "missing")); Console.WriteLine(vec.TryGet(5, "missing")); // set items Console.WriteLine("Set items ..."); vec[2] = "ab"; vec[10] = "h"; Console.WriteLine(vec); // check for items Console.WriteLine("Check for items ..."); Console.WriteLine(vec.ContainsAt(2)); Console.WriteLine(vec.ContainsAt(5)); // get first and last non-empty index Console.WriteLine("Get first and last non-empty index ..."); Console.WriteLine(vec.FirstNonEmptyIndex); Console.WriteLine(vec.LastNonEmptyIndex); // get first and last item Console.WriteLine("Get first and last item ..."); Console.WriteLine(vec.First); Console.WriteLine(vec.Last); // create another SparseVector Console.WriteLine("Create another SparseVector ..."); SparseVector<string> vec2 = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(0, "!"), new IdxDat<string>(2, "@"), new IdxDat<string>(3, "#"), new IdxDat<string>(5, "$") }); Console.WriteLine(vec2); // concatenate Console.WriteLine("Concatenate ..."); vec.Append(vec2, vec.LastNonEmptyIndex + 1); Console.WriteLine(vec); vec2.Append(vec, vec2.LastNonEmptyIndex + 1); Console.WriteLine(vec2); // get number of items Console.WriteLine("Get number of items ..."); Console.WriteLine(vec.Count); // remove item Console.WriteLine("Remove item ..."); vec.RemoveAt(2); Console.WriteLine(vec); // directly access to items Console.WriteLine("Directly access to items ..."); int idx = vec.GetDirectIdx(3); Console.WriteLine(idx); vec.SetDirect(idx, "bbb"); Console.WriteLine(vec); Console.WriteLine(vec.GetIdxDirect(idx)); Console.WriteLine(vec.GetDatDirect(idx)); Console.WriteLine(vec.GetDirect(idx)); vec.RemoveDirect(idx); Console.WriteLine(vec); // perform unary operation Console.WriteLine("Perform unary operation ..."); vec.PerformUnaryOperation(delegate(string item) { return item.ToUpper(); }); Console.WriteLine(vec); // merge Console.WriteLine("Merge ..."); vec.Merge(vec2, delegate(string a, string b) { return string.Format("{0}+{1}", a, b); }); Console.WriteLine(vec); // purge Console.WriteLine("Purge items ..."); vec.PurgeAt(1); Console.WriteLine(vec); vec.PurgeAt(1); Console.WriteLine(vec); Console.WriteLine(); // *** SparseMatrix *** Console.WriteLine("*** SparseMatrix ***"); Console.WriteLine(); // create SparseMatrix Console.WriteLine("Create SparseMatrix ..."); SparseMatrix<string> matrix = new SparseMatrix<string>(); matrix[0] = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(1, "a"), new IdxDat<string>(3, "b"), new IdxDat<string>(4, "c") }); matrix[2] = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(2, "d"), new IdxDat<string>(4, "e"), new IdxDat<string>(5, "f") }); matrix[3] = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(0, "g"), new IdxDat<string>(3, "h"), new IdxDat<string>(5, "i") }); matrix[4] = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(1, "j"), new IdxDat<string>(2, "k"), new IdxDat<string>(4, "l") }); Console.WriteLine(matrix.ToString("E")); // get rows Console.WriteLine("Get rows ..."); Console.WriteLine(matrix[0]); Console.WriteLine(matrix[3]); // set rows Console.WriteLine("Set rows ..."); matrix[1] = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(0, "j"), new IdxDat<string>(3, "k") }); matrix[2] = null; matrix[4] = null; Console.WriteLine(matrix.ToString("E")); // count rows Console.WriteLine("Count rows ..."); Console.WriteLine("{0} != {1}", matrix.GetRowCount(), matrix.GetLastNonEmptyRowIdx() + 1); // trim rows Console.WriteLine("Trim rows ..."); matrix.TrimRows(); Console.WriteLine(matrix.ToString("E")); // add more items Console.WriteLine("Add more items ..."); matrix[0].Add("*"); matrix[3].AddRange(new IdxDat<string>[] { new IdxDat<string>(1, "!"), new IdxDat<string>(2, "?"), new IdxDat<string>(4, "&") }); matrix[2] = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(2, "d"), new IdxDat<string>(4, "e"), new IdxDat<string>(5, "f") }); Console.WriteLine(matrix.ToString("E")); // get items Console.WriteLine("Get items ..."); Console.WriteLine(matrix[0, 1]); Console.WriteLine(matrix[2, 2]); Console.WriteLine(matrix[2][4]); Console.WriteLine(matrix.TryGet(2, 4, "missing")); Console.WriteLine(matrix.TryGet(2, 6, "missing")); // set items Console.WriteLine("Set items ..."); matrix[0, 1] = "l"; matrix[2, 3] = "m"; matrix[3][4] = "n"; Console.WriteLine(matrix.ToString("E")); // check for items Console.WriteLine("Check for items ..."); Console.WriteLine(matrix.ContainsAt(0, 1)); Console.WriteLine(matrix.ContainsAt(1, 1)); Console.WriteLine(matrix.Contains("c")); Console.WriteLine(matrix.Contains("C")); int rowIdx = -1, colIdx = -1; matrix.IndexOf("c", ref rowIdx, ref colIdx); Console.WriteLine("{0}, {1}", rowIdx, colIdx); // check for rows and columns Console.WriteLine("Check for rows and columns ..."); Console.WriteLine(matrix.ContainsColAt(0)); Console.WriteLine(matrix.ContainsColAt(100)); Console.WriteLine(matrix.ContainsRowAt(0)); Console.WriteLine(matrix.ContainsRowAt(100)); // get first and last non-empty row and column index Console.WriteLine("Get first and last non-empty row and column index ..."); Console.WriteLine(matrix.GetFirstNonEmptyRowIdx()); Console.WriteLine(matrix.GetLastNonEmptyRowIdx()); Console.WriteLine(matrix.GetFirstNonEmptyColIdx()); Console.WriteLine(matrix.GetLastNonEmptyColIdx()); // get first and last item in row Console.WriteLine("Get first and last item in row ..."); Console.WriteLine(matrix[0].First); Console.WriteLine(matrix[3].Last); // create another SparseMatrix Console.WriteLine("Create another SparseMatrix ..."); SparseMatrix<string> matrix2 = new SparseMatrix<string>(); matrix2[0] = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(0, "A"), new IdxDat<string>(2, "B"), new IdxDat<string>(3, "C") }); matrix2[2] = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(1, "D"), new IdxDat<string>(3, "E") }); matrix2[3] = new SparseVector<string>(new IdxDat<string>[] { new IdxDat<string>(0, "G"), new IdxDat<string>(1, "H"), new IdxDat<string>(2, "I") }); Console.WriteLine(matrix2.ToString("E")); // concatenate Console.WriteLine("Concatenate ..."); matrix.AppendCols(matrix2, matrix.GetLastNonEmptyColIdx() + 1); Console.WriteLine(matrix.ToString("E")); // remove items Console.WriteLine("Remove items ..."); matrix.RemoveAt(0, 1); matrix.RemoveAt(3, 5); Console.WriteLine(matrix.ToString("E")); // directly access to items Console.WriteLine("Directly access to items ..."); idx = matrix[0].GetDirectIdx(4); Console.WriteLine(idx); Console.WriteLine(matrix[0].GetDirect(idx)); matrix[0].SetDirect(idx, "C"); Console.WriteLine(matrix[1].GetDirect(0)); matrix[1].RemoveDirect(0); Console.WriteLine(matrix.ToString("E")); // get properties Console.WriteLine("Get properties ..."); Console.WriteLine("{0:0.00}%", matrix.GetSparseness(matrix.GetLastNonEmptyRowIdx() + 1, matrix.GetLastNonEmptyColIdx() + 1) * 100.0); Console.WriteLine(matrix.IsSymmetric()); Console.WriteLine(matrix.ContainsDiagonalElement()); Console.WriteLine(matrix.CountValues()); // perform unary operation Console.WriteLine("Perform unary operation ..."); matrix.PerformUnaryOperation(delegate(string item) { return item.ToUpper(); }); Console.WriteLine(matrix.ToString("E")); // merge Console.WriteLine("Merge ..."); matrix.Merge(matrix2, delegate(string a, string b) { return string.Format("{0}+{1}", a, b); }); Console.WriteLine(matrix.ToString("E")); // clear row and column Console.WriteLine("Clear row and column ..."); matrix.RemoveRowAt(2); matrix.RemoveColAt(1); Console.WriteLine(matrix.ToString("E")); // purge row and column Console.WriteLine("Purge row and column ..."); matrix.PurgeRowAt(2); matrix.PurgeColAt(1); Console.WriteLine(matrix.ToString("E")); // get column copy Console.WriteLine("Get column copy ..."); Console.WriteLine(matrix.GetColCopy(0)); // transpose Console.WriteLine("Transpose ..."); Console.WriteLine(matrix.GetTransposedCopy().ToString("E")); // set diagonal Console.WriteLine("Set diagonal ..."); matrix.SetDiagonal(matrix.GetLastNonEmptyColIdx() + 1, "X"); Console.WriteLine(matrix.ToString("E")); // make symmetric Console.WriteLine("Make symmetric ..."); matrix.Symmetrize(delegate(string a, string b) { return string.Format("{0}+{1}", a, b); }); Console.WriteLine(matrix.ToString("E")); }