public static SparseCompressedRowMatrixStorage <T> OfDiagonalInit(int rows, int columns, Func <int, T> init) { var storage = new SparseCompressedRowMatrixStorage <T>(rows, columns); var rowPointers = storage.RowPointers; var columnIndices = new List <int>(); var values = new List <T>(); for (int i = 0; i < Math.Min(rows, columns); i++) { rowPointers[i] = values.Count; var x = init(i); if (!Zero.Equals(x)) { values.Add(x); columnIndices.Add(i); } } storage.ColumnIndices = columnIndices.ToArray(); storage.Values = values.ToArray(); storage.ValueCount = values.Count; return(storage); }
public void CanMapSubMatrixToSame(Matrix <T> matrix) { T one = Matrix <T> .Build.One; // Full Range - not forced Matrix <T> target = Matrix <T> .Build.SameAs(matrix); matrix.Storage.MapSubMatrixIndexedTo(target.Storage, (i, j, x) => x, 0, 0, matrix.RowCount, 0, 0, matrix.ColumnCount, Zeros.AllowSkip, ExistingData.Clear); Assert.That(target, Is.EqualTo(matrix), "Full Range - not forced"); matrix.Storage.MapSubMatrixIndexedTo(target.Storage, (i, j, x) => Zero.Equals(x) ? Zero : one, 0, 0, matrix.RowCount, 0, 0, matrix.ColumnCount, Zeros.AllowSkip, ExistingData.Clear); Assert.That(target.Enumerate().All(x => Zero.Equals(x) || one.Equals(x)), Is.True); if (matrix.Storage.IsFullyMutable) { // Full Range - forced target = Matrix <T> .Build.SameAs(matrix); matrix.Storage.MapSubMatrixIndexedTo(target.Storage, (i, j, x) => x, 0, 0, matrix.RowCount, 0, 0, matrix.ColumnCount, Zeros.Include, ExistingData.Clear); Assert.That(target, Is.EqualTo(matrix), "Full Range - forced"); matrix.Storage.MapSubMatrixIndexedTo(target.Storage, (i, j, x) => Zero.Equals(x) ? Zero : one, 0, 0, matrix.RowCount, 0, 0, matrix.ColumnCount, Zeros.Include, ExistingData.Clear); Assert.That(target.Enumerate().All(x => Zero.Equals(x) || one.Equals(x)), Is.True); } }
/// <summary> /// Returns an ID within the range of the bucket's Low and High range. /// The optional parameter forceBit1 is for our unit tests. /// This works because the bucket low-high range will always be a power of 2! /// </summary> public static ID RandomIDWithinBucket(KBucket bucket, bool forceBit1 = false) { // Simple case: // High = 1000 // Low = 0010 // We want random values between 0010 and 1000 // Low and High will always be powers of 2. var lowBits = new ID(bucket.Low).Bytes.Bits().Reverse(); var highBits = new ID(bucket.High).Bytes.Bits().Reverse(); // We randomize "below" this High prefix range. int highPrefix = highBits.TakeWhile(b => !b).Count() + 1; // Up to the prefix of the Low range. // This sets up a mask of 0's for the LSB's in the Low prefix. int lowPrefix = lowBits.TakeWhile(b => !b).Count(); // RandomizeBeyond is little endian for "bits after" so reverse high/low prefixes. ID id = Zero.RandomizeBeyond(Constants.ID_LENGTH_BITS - highPrefix, Constants.ID_LENGTH_BITS - lowPrefix, forceBit1); // The we add the low range. id = new ID(bucket.Low + id.Value); return(id); }
void TransposeToUnchecked(SparseCompressedRowMatrixStorage <T> target) { var rowPointers = target.RowPointers; var columnIndices = new List <int>(); var values = new List <T>(); for (int j = 0; j < ColumnCount; j++) { rowPointers[j] = values.Count; var index = j * RowCount; for (int i = 0; i < RowCount; i++) { if (!Zero.Equals(Data[index + i])) { values.Add(Data[index + i]); columnIndices.Add(i); } } } rowPointers[ColumnCount] = values.Count; target.ColumnIndices = columnIndices.ToArray(); target.Values = values.ToArray(); }
void CopySubMatrixToUnchecked(DiagonalMatrixStorage <T> target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount) { if (sourceRowIndex - sourceColumnIndex != targetRowIndex - targetColumnIndex) { if (Data.Any(x => !Zero.Equals(x))) { throw new NotSupportedException(); } target.ClearUnchecked(targetRowIndex, rowCount, targetColumnIndex, columnCount); return; } var beginInclusive = Math.Max(sourceRowIndex, sourceColumnIndex); var endExclusive = Math.Min(sourceRowIndex + rowCount, sourceColumnIndex + columnCount); if (endExclusive > beginInclusive) { var beginTarget = Math.Max(targetRowIndex, targetColumnIndex); Array.Copy(Data, beginInclusive, target.Data, beginTarget, endExclusive - beginInclusive); } }
// FUNCTIONAL COMBINATORS: MAP public override void MapInplace(Func <T, T> f, Zeros zeros) { var indices = new List <int>(); var values = new List <T>(ValueCount); if (zeros == Zeros.Include || !Zero.Equals(f(Zero))) { var k = 0; for (var i = 0; i < Length; i++) { var item = k < ValueCount && Indices[k] == i?f(Values[k++]) : f(Zero); if (!Zero.Equals(item)) { values.Add(item); indices.Add(i); } } } else { for (var i = 0; i < ValueCount; i++) { var item = f(Values[i]); if (!Zero.Equals(item)) { values.Add(item); indices.Add(Indices[i]); } } } Indices = indices.ToArray(); Values = values.ToArray(); ValueCount = values.Count; }
/// <summary> /// 取负数 /// </summary> /// <param name="left"></param> /// <returns></returns> public static MinuteTime operator -(MinuteTime left) { return(Zero.Minus(left)); }
public void ZeroMinusZero() { var zero = new Zero(); Assert.AreEqual(zero, zero.Minus(zero)); }
public void OneMinusNegativeTwo() { var one = new Zero().Next(); var negTwo = one.Next().Invert(); Assert.AreEqual(one.Next().Next(), one - negTwo); }
public void NegativeOneMinusNegativeOne() { var zero = new Zero(); var negOne = zero.Previous(); Assert.AreEqual(zero, negOne - negOne); }
public static Donnees.Individu MapIndividu(Zero.POCO.Donnees.Individu indiv, UnitOfWork uow) { var individu = new Donnees.Individu(uow); individu.Oid = indiv.id; individu.Sexe = indiv.Sexe; individu.IdQuest = indiv.IdQuest; return individu; }
public void TwoToString() { var two = new Zero().Next().Next(); Assert.AreEqual("++", two.ToString()); }
public void Visit(Zero zero) { // return false; }
internal override void MapIndexedToUnchecked <TU>(VectorStorage <TU> target, Func <int, T, TU> f, Zeros zeros, ExistingData existingData) { 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); }
public static T ZeroValue <T>() => Zero <T> .Invoke();
/*Functionality Added to make the calculator work with Keyboard NumberPad * When the correct Key is detected it will fire off the corresponding button press*/ private void Window_KeyDownPreview(object sender, KeyEventArgs e) { switch (e.Key) { case Key.NumPad0: Zero.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.NumPad1: One.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.NumPad2: Two.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.NumPad3: Three.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.NumPad4: Four.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.NumPad5: Five.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.NumPad6: Six.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.NumPad7: Seven.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.NumPad8: Eight.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.NumPad9: Nine.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.Add: Plus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.Subtract: Minus.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.Multiply: Multiply.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.Divide: Divide.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; case Key.Enter: Equals.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); break; } }
/// <summary> /// Sets the element without range checking. /// </summary> /// <param name="row"> The row of the element. </param> /// <param name="column"> The column of the element. </param> /// <param name="value"> The value to set the element to. </param> /// <remarks>WARNING: This method is not thread safe. Use "lock" with it and be sure to avoid deadlocks.</remarks> public override void At(int row, int column, T value) { var index = FindItem(row, column); if (index >= 0) { // Non-zero item found in matrix if (Zero.Equals(value)) { // Delete existing item RemoveAtIndexUnchecked(index, row); } else { // Update item Values[index] = value; } } else { // Item not found. Add new value if (Zero.Equals(value)) { return; } index = ~index; var valueCount = RowPointers[RowPointers.Length - 1]; // Check if the storage needs to be increased if ((valueCount == Values.Length) && (valueCount < ((long)RowCount * ColumnCount))) { // Value array is completely full so we increase the size // Determine the increase in size. We will not grow beyond the size of the matrix var size = Math.Min(Values.Length + GrowthSize(), (long)RowCount * ColumnCount); if (size > int.MaxValue) { throw new NotSupportedException(Resources.TooManyElements); } Array.Resize(ref Values, (int)size); Array.Resize(ref ColumnIndices, (int)size); } // Move all values (with a position larger than index) in the value array to the next position // move all values (with a position larger than index) in the columIndices array to the next position Array.Copy(Values, index, Values, index + 1, valueCount - index); Array.Copy(ColumnIndices, index, ColumnIndices, index + 1, valueCount - index); // Add the value and the column index Values[index] = value; ColumnIndices[index] = column; // add 1 to all the row indices for rows bigger than rowIndex // so that they point to the correct part of the value array again. for (var i = row + 1; i < RowPointers.Length; i++) { RowPointers[i] += 1; } } }
public string Visit(Zero node) { return "\t\tldc.i4.0\n"; } public string Visit(One node) {
public void NegativeFourToString() { var negFour = new Zero().Previous().Previous().Previous().Previous(); Assert.AreEqual("----", negFour.ToString()); }
public void NegativeOneToString() { var negOne = new Zero().Previous(); Assert.AreEqual("-", negOne.ToString()); }
public static Donnees.Vague MapVague(Vague vague, UnitOfWork uow, int nBInstance=0,int iteration=1,Zero.DataLayer.Donnees.Vague vagueBD=null) { Zero.DataLayer.Donnees.Vague vag; if (vagueBD == null) { vag = new Donnees.Vague(uow); vag.Annee = vague.Annee; vag.Libelle = vague.Libelle; vag.Mois = vague.Mois; vag.Numero = vague.Numero; vag.Rang = vague.Rang; } else vag = vagueBD; var i = 0; var listeSuportPresse = new XPCollection<XpoSupportPresse>(uow); var listeSuportTv = new XPCollection<XpoSupportTV>(uow); var listeSuportRadio = new XPCollection<XpoSupportRadio>(uow); foreach (var signalitique in vague.Signalitique.Skip((iteration-1)*200).Take(nBInstance)) {//TODO: rappel jai essayer de deviser les sauvegarde en 200 a la fois i++; var sig = MapSignalitique(signalitique, uow); foreach (var AudPresse in signalitique.AudienceJournals) { var aud = MapAudJournal(AudPresse,uow, listeSuportPresse); sig.AudienceJournals.Add(aud); } foreach (var AudTV in signalitique.AudienceTVs) { var aud = MapAudTV(AudTV, uow,listeSuportTv); sig.AudienceTVs.Add(aud); } foreach (var AudRad in signalitique.AudienceRadios) { var aud = MapAudRD(AudRad, uow,listeSuportRadio); sig.AudienceRadios.Add(aud); } vag.Signalitiques.Add(sig); } return vag; }
public bool Visit(Zero zero) { return(false); }
public override IEnumerable <T> EnumerateNonZero() { return(Data.Where(x => !Zero.Equals(x))); }
public void OneMinusOne() { var zero = new Zero(); var one = zero.Next(); Assert.AreEqual(zero, one.Minus(one)); }
public void OneToString() { var one = new Zero().Next(); Assert.AreEqual("+", one.ToString()); }
public override IEnumerable <T> EnumerateNonZero() { return(Values.Take(ValueCount).Where(x => !Zero.Equals(x))); }
public void TwoMinusOne() { var one = new Zero().Next(); var two = one.Next(); Assert.AreEqual(one, two.Minus(one)); }
internal override void Map2ToUnchecked(VectorStorage <T> target, VectorStorage <T> other, Func <T, T, T> f, Zeros zeros, ExistingData existingData) { 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); }
public void FourMinusTwo() { var two = new Zero().Next().Next(); var four = two.Next().Next(); Assert.AreEqual(two, four.Minus(two)); }
public void NegativeOneMinusOne() { var one = new Zero().Next(); var negOne = one.Invert(); Assert.AreEqual(negOne.Previous(), negOne - one); }
public void NegativeThreeMinusTwo() { var two = new Zero().Next().Next(); var negThree = two.Next().Invert(); Assert.AreEqual(negThree.Previous().Previous(), negThree - two); }
void MapSubMatrixIndexedToUnchecked <TU>(DenseColumnMajorMatrixStorage <TU> target, Func <int, int, T, TU> f, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, Zeros zeros, ExistingData existingData) where TU : struct, IEquatable <TU>, IFormattable { var processZeros = zeros == Zeros.Include || !Zero.Equals(f(0, 1, Zero)); if (existingData == ExistingData.Clear && !processZeros) { target.ClearUnchecked(targetRowIndex, rowCount, targetColumnIndex, columnCount); } if (processZeros) { CommonParallel.For(0, columnCount, Math.Max(4096 / rowCount, 32), (a, b) => { int sourceColumn = sourceColumnIndex + a; int targetColumn = targetColumnIndex + a; for (int j = a; j < b; j++) { int targetIndex = targetRowIndex + (j + targetColumnIndex) * target.RowCount; int sourceRow = sourceRowIndex; int targetRow = targetRowIndex; for (int i = 0; i < rowCount; i++) { target.Data[targetIndex++] = f(targetRow++, targetColumn, sourceRow++ == sourceColumn ? Data[sourceColumn] : Zero); } sourceColumn++; targetColumn++; } }); } else { if (sourceRowIndex > sourceColumnIndex && sourceColumnIndex + columnCount > sourceRowIndex) { // column by column, but skip resulting zero columns at the beginning int columnInit = sourceRowIndex - sourceColumnIndex; int offset = (columnInit + targetColumnIndex) * target.RowCount + targetRowIndex; int step = target.RowCount + 1; int count = Math.Min(columnCount - columnInit, rowCount); for (int k = 0, j = offset; k < count; j += step, k++) { target.Data[j] = f(targetRowIndex + k, targetColumnIndex + columnInit + k, Data[sourceRowIndex + k]); } } else if (sourceRowIndex < sourceColumnIndex && sourceRowIndex + rowCount > sourceColumnIndex) { // row by row, but skip resulting zero rows at the beginning int rowInit = sourceColumnIndex - sourceRowIndex; int offset = targetColumnIndex * target.RowCount + rowInit + targetRowIndex; int step = target.RowCount + 1; int count = Math.Min(columnCount, rowCount - rowInit); for (int k = 0, j = offset; k < count; j += step, k++) { target.Data[j] = f(targetRowIndex + rowInit + k, targetColumnIndex + k, Data[sourceColumnIndex + k]); } } else { int offset = targetColumnIndex * target.RowCount + targetRowIndex; int step = target.RowCount + 1; var count = Math.Min(columnCount, rowCount); for (int k = 0, j = offset; k < count; j += step, k++) { target.Data[j] = f(targetRowIndex + k, targetColumnIndex + k, Data[sourceRowIndex + k]); } } } }
public static AudienceTV MapAudTV(Zero.POCO.Audiences.AudienceTV Adj, UnitOfWork uow, XPCollection<XpoSupportTV> supports) { var audience = new Donnees.AudienceTV(uow); audience.Oid = Adj.id != default(Guid) ? Adj.id : Guid.NewGuid(); audience.NumeroQuertdheure = Adj.NumeroQuertdheure; audience.NumeroJour = Adj.NumeroJour; var support = supports.First(e => e.Code == Adj.SupportTV.Code); audience.SupportTV = support; return audience; }