/// <summary> /// Returns a tuple with the index and values of the first element pair of two vectors of the same size satisfying a predicate, or null if none is found. /// Zero elements may be skipped on sparse data structures if allowed (default). /// </summary> public Tuple <int, T, TOther> Find2 <TOther>(Func <T, TOther, bool> predicate, Vector <TOther> other, Zeros zeros = Zeros.AllowSkip) where TOther : struct, IEquatable <TOther>, IFormattable { return(Storage.Find2(other.Storage, predicate, zeros)); }
/// <summary> /// Returns true if at least one element satisfies a predicate. /// Zero elements may be skipped on sparse data structures if allowed (default). /// </summary> public bool Exists(Func <T, bool> predicate, Zeros zeros = Zeros.AllowSkip) { return(Storage.Find(predicate, zeros) != null); }
/// <summary> /// Applies a function to update the status with each value pair of two vectors and returns the resulting status. /// </summary> public TState Fold2 <TOther, TState>(Func <TState, T, TOther, TState> f, TState state, Vector <TOther> other, Zeros zeros = Zeros.AllowSkip) where TOther : struct, IEquatable <TOther>, IFormattable { return(Storage.Fold2(other.Storage, f, state, zeros)); }
/// <summary> /// Returns a tuple with the index and value of the first element satisfying a predicate, or null if none is found. /// Zero elements may be skipped on sparse data structures if allowed (default). /// </summary> public Tuple <int, T> Find(Func <T, bool> predicate, Zeros zeros = Zeros.AllowSkip) { return(Storage.Find(predicate, zeros)); }
/// <summary> /// Applies a function to each value of this vector and replaces the value in the result vector. /// The index of each value (zero-based) is passed as first argument to the function. /// If forceMapZero is not set to true, zero values may or may not be skipped depending /// on the actual data storage implementation (relevant mostly for sparse vectors). /// </summary> public void MapIndexedConvert <TU>(Func <int, T, TU> f, Vector <TU> result, Zeros zeros = Zeros.AllowSkip) where TU : struct, IEquatable <TU>, IFormattable { Storage.MapIndexedTo(result.Storage, f, zeros, zeros == Zeros.Include ? ExistingData.AssumeZeros : ExistingData.Clear); }
/// <summary> /// Applies a function to each value pair of two vectors and replaces the value in the result vector. /// </summary> public void Map2(Func <T, T, T> f, Vector <T> other, Vector <T> result, Zeros zeros = Zeros.AllowSkip) { Storage.Map2To(result.Storage, other.Storage, f, zeros, ExistingData.Clear); }
/// <summary> /// Returns true if all element pairs of two vectors of the same size satisfy a predicate. /// Zero elements may be skipped on sparse data structures if allowed (default). /// </summary> public bool ForAll2 <TOther>(Func <T, TOther, bool> predicate, Vector <TOther> other, Zeros zeros = Zeros.AllowSkip) where TOther : struct, IEquatable <TOther>, IFormattable { return(Storage.Find2(other.Storage, (x, y) => !predicate(x, y), zeros) == null); }
/// <summary> /// Applies a function to each value of this vector and replaces the value with its result. /// The index of each value (zero-based) is passed as first argument to the function. /// If forceMapZero is not set to true, zero values may or may not be skipped depending /// on the actual data storage implementation (relevant mostly for sparse vectors). /// </summary> public void MapIndexedInplace(Func <int, T, T> f, Zeros zeros = Zeros.AllowSkip) { Storage.MapIndexedInplace(f, zeros); }
internal override void MapIndexedToUnchecked <TU>(VectorStorage <TU> target, Func <int, T, TU> f, Zeros zeros = Zeros.AllowSkip, ExistingData existingData = ExistingData.Clear) { var sparseTarget = target as SparseVectorStorage <TU>; if (sparseTarget != null) { var indices = new List <int>(); var values = new List <TU>(); if (zeros == Zeros.Include || !Zero.Equals(f(0, Zero))) { int k = 0; for (int i = 0; i < Length; i++) { var item = k < ValueCount && (Indices[k]) == i?f(i, Values[k++]) : f(i, Zero); if (!Zero.Equals(item)) { values.Add(item); indices.Add(i); } } } else { for (int i = 0; i < ValueCount; i++) { var item = f(Indices[i], Values[i]); if (!Zero.Equals(item)) { values.Add(item); indices.Add(Indices[i]); } } } sparseTarget.Indices = indices.ToArray(); sparseTarget.Values = values.ToArray(); sparseTarget.ValueCount = values.Count; return; } var denseTarget = target as DenseVectorStorage <TU>; if (denseTarget != null) { if (existingData == ExistingData.Clear) { denseTarget.Clear(); } if (zeros == Zeros.Include || !Zero.Equals(f(0, Zero))) { int k = 0; for (int i = 0; i < Length; i++) { denseTarget.Data[i] = k < ValueCount && (Indices[k]) == i ? f(i, Values[k++]) : f(i, Zero); } } else { CommonParallel.For(0, ValueCount, 4096, (a, b) => { for (int i = a; i < b; i++) { denseTarget.Data[Indices[i]] = f(Indices[i], Values[i]); } }); } return; } // FALL BACK base.MapIndexedToUnchecked(target, f, zeros, existingData); }
/// <summary> /// Returns true if all elements satisfy a predicate. /// Zero elements may be skipped on sparse data structures if allowed (default). /// </summary> public bool ForAll(Func <T, bool> predicate, Zeros zeros = Zeros.AllowSkip) { return(Storage.Find(x => !predicate(x), zeros) == null); }
public IEnumerable <(int row, int column, double value)> EnumerateElementsIndexed(Zeros zeros = Zeros.AllowSkip) { switch (zeros) { case Zeros.AllowSkip: { for (int i = 0; i < m; ++i) { var srcIndicesLength = count[i]; if (srcIndicesLength > 0) { var srcIndices = indices[i]; var items_i = items[i]; for (int jj = 0; jj < srcIndices.Length; ++jj) { yield return(i, srcIndices[jj], items_i[jj]);
internal override TState Fold2Unchecked <TOther, TState>(VectorStorage <TOther> other, Func <TState, T, TOther, TState> f, TState state, Zeros zeros = Zeros.AllowSkip) { var sparseOther = other as SparseVectorStorage <TOther>; if (sparseOther != null) { int[] otherIndices = sparseOther.Indices; TOther[] otherValues = sparseOther.Values; int otherValueCount = sparseOther.ValueCount; TOther otherZero = BuilderInstance <TOther> .Vector.Zero; if (zeros == Zeros.Include) { int p = 0, q = 0; for (int i = 0; i < Length; i++) { var left = p < ValueCount && Indices[p] == i ? Values[p++] : Zero; var right = q < otherValueCount && otherIndices[q] == i ? otherValues[q++] : otherZero; state = f(state, left, right); } } else { int p = 0, q = 0; while (p < ValueCount || q < otherValueCount) { if (q >= otherValueCount || p < ValueCount && Indices[p] < otherIndices[q]) { state = f(state, Values[p], otherZero); p++; } else if (p >= ValueCount || q < otherValueCount && Indices[p] > otherIndices[q]) { state = f(state, Zero, otherValues[q]); q++; } else { Debug.Assert(Indices[p] == otherIndices[q]); state = f(state, Values[p], otherValues[q]); p++; q++; } } } return(state); } var denseOther = other as DenseVectorStorage <TOther>; if (denseOther != null) { TOther[] otherData = denseOther.Data; int k = 0; for (int i = 0; i < otherData.Length; i++) { if (k < ValueCount && Indices[k] == i) { state = f(state, Values[k], otherData[i]); k++; } else { state = f(state, Zero, otherData[i]); } } return(state); } return(base.Fold2Unchecked(other, f, state, zeros)); }
internal override void Map2ToUnchecked(VectorStorage <T> target, VectorStorage <T> other, Func <T, T, T> f, Zeros zeros = Zeros.AllowSkip, ExistingData existingData = ExistingData.Clear) { var processZeros = zeros == Zeros.Include || !Zero.Equals(f(Zero, Zero)); var denseTarget = target as DenseVectorStorage <T>; var denseOther = other as DenseVectorStorage <T>; if (denseTarget == null && (denseOther != null || processZeros)) { // The handling is effectively dense but we're supposed to push // to a sparse target. Let's use a dense target instead, // then copy it normalized back to the sparse target. var intermediate = new DenseVectorStorage <T>(target.Length); Map2ToUnchecked(intermediate, other, f, zeros, ExistingData.AssumeZeros); intermediate.CopyTo(target, existingData); return; } if (denseOther != null) { T[] targetData = denseTarget.Data; T[] otherData = denseOther.Data; int k = 0; for (int i = 0; i < otherData.Length; i++) { if (k < ValueCount && Indices[k] == i) { targetData[i] = f(Values[k], otherData[i]); k++; } else { targetData[i] = f(Zero, otherData[i]); } } return; } var sparseOther = other as SparseVectorStorage <T>; if (sparseOther != null && denseTarget != null) { T[] targetData = denseTarget.Data; int[] otherIndices = sparseOther.Indices; T[] otherValues = sparseOther.Values; int otherValueCount = sparseOther.ValueCount; if (processZeros) { int p = 0, q = 0; for (int i = 0; i < targetData.Length; i++) { var left = p < ValueCount && Indices[p] == i ? Values[p++] : Zero; var right = q < otherValueCount && otherIndices[q] == i ? otherValues[q++] : Zero; targetData[i] = f(left, right); } } else { if (existingData == ExistingData.Clear) { denseTarget.Clear(); } int p = 0, q = 0; while (p < ValueCount || q < otherValueCount) { if (q >= otherValueCount || p < ValueCount && Indices[p] < otherIndices[q]) { targetData[Indices[p]] = f(Values[p], Zero); p++; } else if (p >= ValueCount || q < otherValueCount && Indices[p] > otherIndices[q]) { targetData[otherIndices[q]] = f(Zero, otherValues[q]); q++; } else { Debug.Assert(Indices[p] == otherIndices[q]); targetData[Indices[p]] = f(Values[p], otherValues[q]); p++; q++; } } } return; } var sparseTarget = target as SparseVectorStorage <T>; if (sparseOther != null && sparseTarget != null) { var indices = new List <int>(); var values = new List <T>(); int[] otherIndices = sparseOther.Indices; T[] otherValues = sparseOther.Values; int otherValueCount = sparseOther.ValueCount; int p = 0, q = 0; while (p < ValueCount || q < otherValueCount) { if (q >= otherValueCount || p < ValueCount && Indices[p] < otherIndices[q]) { var value = f(Values[p], Zero); if (!Zero.Equals(value)) { indices.Add(Indices[p]); values.Add(value); } p++; } else if (p >= ValueCount || q < otherValueCount && Indices[p] > otherIndices[q]) { var value = f(Zero, otherValues[q]); if (!Zero.Equals(value)) { indices.Add(otherIndices[q]); values.Add(value); } q++; } else { var value = f(Values[p], otherValues[q]); if (!Zero.Equals(value)) { indices.Add(Indices[p]); values.Add(value); } p++; q++; } } sparseTarget.Indices = indices.ToArray(); sparseTarget.Values = values.ToArray(); sparseTarget.ValueCount = values.Count; return; } // FALL BACK base.Map2ToUnchecked(target, other, f, zeros, existingData); }
internal virtual TState Fold2Unchecked <TOther, TState>(VectorStorage <TOther> other, Func <TState, T, TOther, TState> f, TState state, Zeros zeros = Zeros.AllowSkip) where TOther : struct, IEquatable <TOther>, IFormattable { for (int i = 0; i < Length; i++) { state = f(state, At(i), other.At(i)); } return(state); }
public TState Fold2 <TOther, TState>(VectorStorage <TOther> other, Func <TState, T, TOther, TState> f, TState state, Zeros zeros = Zeros.AllowSkip) where TOther : struct, IEquatable <TOther>, IFormattable { if (other == null) { throw new ArgumentNullException("other"); } if (Length != other.Length) { throw new ArgumentException(Resource.ArgumentVectorsSameLength, "other"); } return(Fold2Unchecked(other, f, state, zeros)); }