/// <summary>No null checks.</summary> /// <param name="t">Source tensor (can be R1).</param> /// <param name="newSup">Copy's new superior.</param> /// <param name="inx">Copy's index inside new superior.</param> /// <param name="xCap">Extra capacity of all copied (sub)tensors (beyond existing Count).</param> /// <typeparam name="τ">Numeric type.</typeparam> /// <typeparam name="α">Arithmetic type.</typeparam> internal static Tnr <τ, α> CopyAsSubTnrβ <τ, α>(this Tnr <τ, α> t, Tnr <τ, α> newSup, int inx, int xCap = 0) where τ : notnull, IEquatable <τ>, IComparable <τ> where α : IAlgebra <τ>, new() { Assume.True(t.Dim == newSup.Strc[newSup.StrcInx + 1], () => "Source tensor's dimension does not equal the dimension of slot where copied subtensor will be placed."); if (t is Vec <τ, α> v) { return(v.CopyAsSubVecβ(newSup, inx, xCap)); } else { var nst = newSup.SubTnr <τ, α>(inx, t.Count + xCap); if (t.Rank == 2) { foreach (var(i, st) in t) { var sv = (Vec <τ, α>)st; sv.CopyAsSubVecβ <τ, α>(nst, i, xCap); } } else { foreach (var(i, st) in t) { st.CopyAsSubTnrβ(nst, i, xCap); } } return(nst); } }
/// <summary>No null checks.</summary> /// <param name="sup">Direct superior.</param> /// <param name="inx">Index inside superior.</param> /// <param name="cap">Initial capacity.</param> /// <typeparam name="τ">Numeric type.</typeparam> /// <typeparam name="α">Arithmetic type.</typeparam> internal static Vec <τ, α> SubVecβ <τ, α>(this Tnr <τ, α> sup, int inx, int cap = 6) where τ : notnull, IEquatable <τ>, IComparable <τ> where α : IAlgebra <τ>, new() { Assume.True(sup.Rank == 2, () => "Vector's superior rank not 2. You can only create a subvec with a rank 2 superior."); var vec = new Vec <τ, α>(sup, cap); sup[Vec <τ, α> .V, inx] = vec; return(vec); }
public void Assume_does_not_throw_on_error() { try { Assume.True(false, "Assumption failed"); } catch (AssertException) { Assert.Fail("Not expected to have an assertion failure on Assume"); } Assert.Pass(); }
/// <summary>No null checks.</summary> /// <param name="v">Copy source.</param> /// <param name="newSup">The copied vector's superior.</param> /// <param name="inx">New index inside superior.</param> /// <param name="xCap">Extra capacity of copied vector (beyond the number of elements).</param> /// <typeparam name="τ">Numeric type.</typeparam> /// <typeparam name="α">Arithmetic type.</typeparam> internal static Vec <τ, α> CopyAsSubVecβ <τ, α>(this Vec <τ, α> v, Tnr <τ, α> newSup, int inx, int xCap = 0) where τ : notnull, IEquatable <τ>, IComparable <τ> where α : IAlgebra <τ>, new() { Assume.True(v.Dim == newSup.Strc[newSup.StrcInx + 1], () => "Source vector's dimension does not equal the dimension of slot where copied subvector will be placed."); var copy = SubVec <τ, α>(newSup, inx, v.Count + xCap); // New superior's rank will be checked here. foreach (var(i, s) in v.Scals) { copy.Add(i, s); } return(copy); }
/// <summary>Subtracts vec2 from vec1. Modifies vec1, does not destroy vec2.</summary> /// <param name="vec1">Minuend. Is modified, use return as result.</param> /// <param name="vec2">Subtrahend. Is not destroyed.</param> /// <remarks><see cref="TestRefs.VectorSub"/></remarks> public static Vec <τ, α>?SubInto <τ, α>(this Vec <τ, α>?vec1, Vec <τ, α>?vec2) where τ : notnull, IEquatable <τ>, IComparable <τ> where α : IAlgebra <τ>, new() { if (vec2 == null) { return(vec1); } if (vec1 == null) { vec1 = TopVec <τ, α>(vec2); } Assume.True(vec1.Dim == vec2.Dim, () => "The dimensions of vectors do not match on subtraction."); return(SubIntoß(vec1, vec2)); }
/// <summary>No null checks.</summary> /// <param name="sup">Direct superior.</param> /// <param name="inx">Index inside superior.</param> /// <param name="cap">Capacity of internal dictionary.</param> /// <typeparam name="τ">Numeric type.</typeparam> /// <typeparam name="α">Arithmetic type.</typeparam> internal static Tnr <τ, α> SubTnrβ <τ, α>(this Tnr <τ, α> sup, int inx, int cap = 6) where τ : notnull, IEquatable <τ>, IComparable <τ> where α : IAlgebra <τ>, new() { Assume.True(sup.Rank > 1, () => "Superior's rank too low. Cannot create a subtensor on a vector."); if (sup.Rank > 2) { var tnr = new Tnr <τ, α>(sup, cap); sup[Tnr <τ, α> .T, inx] = tnr; return(tnr); } else { return(sup.SubVecβ(inx, cap)); } }
public void Initialize_should_copy_from_nominals() { Assume.True(Template.IsImmutable(typeof(PImmutable))); var asm = new PImmutable(); Dictionary <string, object> values = new Dictionary <string, object>() { { "Component", asm }, { "ErrorCode", 0xdead }, }; StatusBuilder sb = new StatusBuilder(); Activation.Initialize(sb, values); Assert.Equal(0xdead, sb.ErrorCode); Assert.Equal(asm, sb.Component); }
public static Vec <τ, α>?SubTop <τ, α>(this Vec <τ, α>?v1, Vec <τ, α>?v2) where τ : notnull, IEquatable <τ>, IComparable <τ> where α : IAlgebra <τ>, new() { if (v1 == null) { if (v2 == null) { return(null); } else { return(TopVec <τ, α>(v2).SubIntoß(v2)); } } else if (v2 == null) { return(TopVec <τ, α>(v1).SumIntoß(v1)); } Assume.True(v1.Dim == v2.Dim, () => "The dimensions of vectors do not match on subtraction."); return(SubTopß(v1, v2)); }
public static Vec <τ, α>?SumTop <τ, α>(this Vec <τ, α>?vec1, Vec <τ, α>?vec2) where τ : notnull, IEquatable <τ>, IComparable <τ> where α : IAlgebra <τ>, new() { if (vec1 == null) { if (vec2 == null) { return(null); } else { var newVec = TopVec <τ, α>(vec2); return(newVec.SumIntoß(vec2)); } } else if (vec2 == null) { return(TopVec <τ, α>(vec1).SumIntoß(vec1)); } Assume.True(vec1.Dim == vec2.Dim, () => "The dimensions of vectors do not match on addition."); return(SumTopß(vec1, vec2)); }
public IEnumerable <Tnr <τ, α> > EnumerateRank(int rankInx) { Assume.True(rankInx > 1, () => "This method applies only to ranks that hold pure tensors."); if (Rank > rankInx + 1) { foreach (var subTnr in Recursion(this)) { yield return(subTnr); } } else { foreach (var int_subTnr in this) { yield return(int_subTnr.Value); } } IEnumerable <Tnr <τ, α> > Recursion(Tnr <τ, α> src) { foreach (var int_tnr in src) { if (int_tnr.Value.Rank > rankInx + 1) { foreach (var subTnr in Recursion(int_tnr.Value)) { yield return(subTnr); } } else { foreach (var int_subTnr in int_tnr.Value) { yield return(int_subTnr.Value); } } } } }
/// <summary>Creates a non-top vector from an array span. Adds it to its specified superior at the specified index.</summary> /// <param name="span">Array span of values.</param> /// <param name="sup">Direct superior with an existing structure.</param> /// <param name="inx">Index inside superior.</param> /// <typeparam name="τ">Numeric type.</typeparam> /// <typeparam name="α">Arithmetic type.</typeparam> public static Vec <τ, α>?SubVecFromSpan <τ, α>(Span <τ> span, Tnr <τ, α> sup, int inx) where τ : notnull, IEquatable <τ>, IComparable <τ> where α : IAlgebra <τ>, new() { Assume.True(sup.Rank == 2, () => "Vector's superior rank not 2. You can only create a subvector with a rank 2 superior."); α alg = new α(); var vec = new Vec <τ, α>(sup, span.Length); for (int i = 0; i < span.Length; ++i) { if (!alg.IsZero(span[i])) { vec.Add(i, span[i]); } } if (vec.Count > 0) // Created vector is not empty. { sup[Vec <τ, α> .V, inx] = vec; return(vec); } else { return(null); } }
/// <summary>Creates a top tensor (null superior) with specified structure and initial capacity. Rank is assigned as the length of structure array.</summary> /// <param name="strc">Specifies dimension of each rank.</param> /// <param name="cap">Initially assigned memory.</param> /// <typeparam name="τ">Numeric type.</typeparam> /// <typeparam name="α">Arithmetic type.</typeparam> public static Tnr <τ, α> TopTnr <τ, α>(List <int> strc, int cap = 6) where τ : notnull, IEquatable <τ>, IComparable <τ> where α : IAlgebra <τ>, new() { Assume.True(strc.Count > 1, () => "For creating tensors of rank 1 use Vector's constructor."); return(new Tnr <τ, α>(strc, cap)); }