本文整理汇总了C#中SparseVector.TryGet方法的典型用法代码示例。如果您正苦于以下问题:C# SparseVector.TryGet方法的具体用法?C# SparseVector.TryGet怎么用?C# SparseVector.TryGet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SparseVector
的用法示例。
在下文中一共展示了SparseVector.TryGet方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddInPlace
/// <summary>
/// Adds each element of vec to corresponding element of addTo vector
/// addTo vector is being changed in place
/// </summary>
public static void AddInPlace(SparseVector<double> addTo, SparseVector<double> vec)
{
foreach (var vecIdx in vec.InnerIdx)
{
var num = addTo.TryGet(vecIdx, 0.0);
addTo[vecIdx] = num + vec[vecIdx];
}
}
示例2: DivideInPlace
/// <summary>
/// Divides each element of dividee with corresponding element of divisor in place
/// Dividee is being changed
/// </summary>
public static void DivideInPlace(SparseVector<double> dividee, SparseVector<double> divisor)
{
foreach (var divIdx in divisor.InnerIdx)
{
var num = dividee.TryGet(divIdx, 0.0);
if (num < Double.Epsilon)
continue;
dividee[divIdx] = num/divisor[divIdx];
}
}
示例3: Add
public static SparseVector<double> Add(SparseVector<double> v1, SparseVector<double> v2)
{
var newVector = new SparseVector<double>();
var v1LastI = v1.Count > 0 ? v1.Last.Idx : 0;
var v2LastI = v2.Count > 0 ? v2.Last.Idx : 0;
var limit = Math.Max(v1LastI, v2LastI);
for (int i = 0; i < limit; i++)
{
var sum = v1.TryGet(i, 0.0) + v2.TryGet(i, 0.0);
if (sum > Double.Epsilon)
newVector[i] = sum;
}
return newVector;
}
示例4: Main
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"),
//.........这里部分代码省略.........