public void SetT(Tnr <τ, α>?t, params int[] inxs) { Tnr <τ, α>?tnr = this; if (t != null) { int n = inxs.Length - 1; for (int i = 0; i < n; ++i) { if (!tnr.TryGetValue(inxs[i], out tnr)) // Crucial line: out tnr becomes the subtensor if found, if not it is created. { tnr = new Tnr <τ, α>(tnr !, 6); //new Tensor<τ,α>(Structure, tnr!.Rank - 1, tnr, 6); tnr.Superior !.AddSubTnr(inxs[i], tnr); } } var dict = (TnrBase <Tnr <τ, α> >)tnr; // tnr is now the proper subtensor. t.Superior = tnr; // Crucial: to make the added tensor truly a part of this tensor, we must set proper superior and structure. t.Strc = Strc; dict[inxs[n]] = t; } else { for (int i = 0; i < inxs.Length; ++i) { if (!tnr.TryGetValue(inxs[i], out tnr)) // t is null and the corresponding substructure does not exist. Leave as is. { return; } } tnr.Superior !.Remove(inxs[inxs.Length - 1]); } // Corresponding tensor exists. Remove. }
/// <summary>Vector getting/setting indexer. Use tnr[1f, 3] = null to remove an entry, should it exist.</summary> /// <param name="overloadDummy">Type float of first dummy argument specifies that we know we will be getting/setting a Vector.</param> /// <param name="inxs">A set of indices specifiying which Vector we want to set/get. The set length must reach exactly to Vector rank.</param> /// <remarks> <see cref="TestRefs.TensorVectorIndexer"/> </remarks> public Vec <τ, α>?this[Vec <τ, α> overloadDummy, params int[] inx] { get { Tnr <τ, α>?tnr = this; int n = inx.Length - 1; for (int i = 0; i < n; ++i) { if (!tnr.TryGetValue(inx[i], out tnr)) { return(null); } } if (tnr.TryGetValue(inx[n], out tnr)) // No problem with null. { return((Vec <τ, α>)tnr); // Same. } else { return(null); } } set { Tnr <τ, α>?tnr = this; if (value != null) { int n = inx.Length - 1; // Entry one before last chooses tensor, last chooses vector. for (int i = 0; i < n; ++i) { if (tnr.TryGetValue(inx[i], out Tnr <τ, α>?tnr2)) { tnr = tnr2; } else // Tensor does not exist in an intermediate rank. { tnr = new Tnr <τ, α>(tnr, 4); //new Tensor<τ,α>(Structure, tnr.Rank - 1, tnr, 4); tnr.Superior !.Add(inx[i], tnr); } } var dict = (TnrBase <Tnr <τ, α> >)tnr; // Tnr now refers to either a prexisting R2 tensor or a freshly created one. value.Superior = tnr; // Crucial: to make the added vector truly a part of this tensor, we must set proper superior and structure. value.Strc = Strc; dict[inx[n]] = value; } // We do not check that the value is a vector beforehand. It is assumed that the user used indexer correctly. else { int n = inx.Length; // Last entry chooses vector. for (int i = 0; i < n; ++i) { if (!tnr.TryGetValue(inx[i], out tnr)) { return; } } tnr.Superior !.Remove(inx[n - 1]); } } // Vector.Superior.Remove }
/// <summary>Tensor getting/setting indexer.</summary> /// <param name="overloadDummy">Type uint of first dummy argument specifies that we know we will be getting/setting a Tensor.</param> /// <param name="inxs">A set of indices specifiying which Tensor we want to set/get. The set length must not reach all the way out to scalar rank.</param> public Tnr <τ, α>?this[Tnr <τ, α> overloadDummy, params int[] inxs] { get { Tnr <τ, α>?tnr = this; for (int i = 0; i < inxs.Length; ++i) { if (!tnr.TryGetValue(inxs[i], out tnr)) { return(null); } } return(tnr); } // No problem with tnr being null. We return above. set { Tnr <τ, α>?tnr = this; if (value != null) { int n = inxs.Length - 1; for (int i = 0; i < n; ++i) { if (!tnr.TryGetValue(inxs[i], out tnr)) // Crucial line: out tnr becomes the subtensor if found, if not it is created { tnr = new Tnr <τ, α>(tnr !, 6); //new Tensor<τ,α>(Structure, tnr!.Rank - 1, tnr, 6); tnr.Superior !.AddSubTnr(inxs[i], tnr); } } var dict = (TnrBase <Tnr <τ, α> >)tnr; // tnr is now the proper subtensor. value.Superior = tnr; // Crucial: to make the added tensor truly a part of this tensor, we must set proper superior and structure. value.Strc = Strc; dict[inxs[n]] = value; } else { for (int i = 0; i < inxs.Length; ++i) { if (!tnr.TryGetValue(inxs[i], out tnr)) { return; } } tnr.Superior !.Remove(inxs[inxs.Length - 1]); } } }
public Vec <τ, α> GetNonNullVec(params int[] inx) { Tnr <τ, α>?tnr = this; int n = inx.Length - 1; for (int i = 0; i < n; ++i) { if (!tnr.TryGetValue(inx[i], out tnr)) { throw new NullReferenceException("Expected a non-null tensor at specified location."); } } if (tnr.TryGetValue(inx[n], out tnr)) { return((Vec <τ, α>)tnr); } else { throw new NullReferenceException("Expected a non-null vector at specified location."); }; }
public Vec <τ, α>?GetV(params int[] inx) { Tnr <τ, α>?tnr = this; int n = inx.Length - 1; for (int i = 0; i < n; ++i) { if (!tnr.TryGetValue(inx[i], out tnr)) { return(null); } } if (tnr.TryGetValue(inx[n], out tnr)) // No problem with null. { return((Vec <τ, α>)tnr); // Same. } else { return(null); } }
public Tnr <τ, α> GetNonNullTnr(params int[] inxs) { Tnr <τ, α>?tnr = this; for (int i = 0; i < inxs.Length; ++i) { if (!tnr.TryGetValue(inxs[i], out tnr)) { throw new NullReferenceException("Expected a non-null tensor at specified location."); } } return(tnr); }
public Tnr <τ, α>?GetT(params int[] inxs) { Tnr <τ, α>?tnr = this; for (int i = 0; i < inxs.Length; ++i) { if (!tnr.TryGetValue(inxs[i], out tnr)) { return(null); } } return(tnr); }
[MaybeNull] // Getter may return null. public virtual τ this[params int[] inx] { get { α alg = new α(); Tnr <τ, α>?tnr = this; int n = inx.Length - 2; for (int i = 0; i < n; ++i) { if (!tnr.TryGetValue(inx[i], out tnr)) { return(alg.Zero); } } if (tnr.TryGetValue(inx[n], out tnr)) // No probelm with null. { var vec = (Vec <τ, α>)tnr; // Same. if (vec.Scals.TryGetValue(inx[n + 1], out τ val)) { return(val); } } α alg2 = new α(); return(alg.Zero); } set { Tnr <τ, α>?tnr = this; Tnr <τ, α>?tnr2; // Temporary to avoid null problem below. Vec <τ, α> vec; α alg = new α(); if (!alg.IsZero(value)) { if (inx.Length > 1) // At least a 2nd rank tensor. { int n = inx.Length - 2; for (int i = 0; i < n; ++i) // This loop is entered only for a 3rd rank tensor or above. { if (tnr.TryGetValue(inx[i], out tnr2)) { tnr = tnr2; } else { tnr = new Tnr <τ, α>(tnr, 6); //new Tensor<τ,α>(Structure, tnr.Rank - 1, tnr, 6); tnr.Superior !.Add(inx[i], tnr); } } if (tnr.TryGetValue(inx[n], out tnr2)) // Does vector exist? { vec = (Vec <τ, α>)tnr2; } else { vec = new Vec <τ, α>(Strc, tnr, 4); tnr.Add(inx[n], vec); } vec.Scals[inx[n + 1]] = value; } } else { int n = inx.Length - 1; for (int i = 0; i < n; ++i) { if (!tnr.TryGetValue(inx[i], out tnr)) { return; } } vec = (Vec <τ, α>)tnr; vec.Scals.Remove(inx[n]); } } }