public void MapToAutoIncludeZeros(TestVectorStorage aType, TestVectorStorage resultType) { var a = TestData.VectorStorage(aType, new[] { 1.0, 2.0, 0.0, 4.0 }); var result = TestData.VectorStorage<double>(resultType, 4); var expected = new DenseVectorStorage<double>(4, new[] { 0.0, -1.0, 1.0, -3.0 }); a.MapTo(result, u => -u + 1.0, Zeros.AllowSkip); Assert.That(result.Equals(expected)); }
public void Map2ToAutoIncludeZeros(TestVectorStorage aType, TestVectorStorage bType, TestVectorStorage resultType) { var a = TestData.VectorStorage(aType, new[] { 1.0, 2.0, 0.0, 4.0, 0.0, 6.0 }); var b = TestData.VectorStorage(bType, new[] { 11.0, 12.0, 13.0, 0.0, 0.0, 16.0 }); var result = TestData.VectorStorage<double>(resultType, 6); var expected = new DenseVectorStorage<double>(6, new[] { 13.0, 15.0, 14.0, 5.0, 1.0, 23.0 }); a.Map2To(result, b, (u, v) => u + v + 1.0, Zeros.AllowSkip); Assert.That(result.Equals(expected)); }
public void Map2ToForceIncludeZeros(VectorStorageType aType, VectorStorageType bType, VectorStorageType resultType) { var a = Build.VectorStorage(aType, new[] { 1.0, 2.0, 0.0, 4.0, 0.0, 6.0 }); var b = Build.VectorStorage(bType, new[] { 11.0, 12.0, 13.0, 0.0, 0.0, 16.0 }); var result = Build.VectorStorage<double>(resultType, 6); var expected = new DenseVectorStorage<double>(6, new[] { 13.0, 15.0, 14.0, 5.0, 1.0, 23.0 }); a.Map2To(result, b, (u, v) => u + v + 1.0, Zeros.Include); Assert.That(result.Equals(expected)); }
public void MapIndexedToSkipZeros(TestVectorStorage aType, TestVectorStorage resultType) { var a = TestData.VectorStorage(aType, new[] { 1.0, 2.0, 0.0, 4.0 }); var result = TestData.VectorStorage<double>(resultType, 4); var expected = new DenseVectorStorage<double>(4, new[] { -1.0, -2.0, 0.0, -4.0 }); int badValueCount = 0; // one time is OK for zero-check a.MapIndexedTo(result, (i, u) => { if (a.At(i) != u) Interlocked.Increment(ref badValueCount); return -u; }, Zeros.AllowSkip); Assert.That(badValueCount, Is.LessThanOrEqualTo(1)); Assert.That(result.Equals(expected)); }
public void ReturnComplex32VectorStorage(DenseVectorStorage<Complex32> storage) { lock (_lock) { if (!_complex32VectorStoragePool.Contains(storage)) _complex32VectorStoragePool.Add(storage); } }
internal DenseVector(DenseVectorStorage<Complex32> storage) : base(storage) { _length = storage.Length; _values = storage.Data; }
/// <summary> /// Create a new dense vector as a copy of the given other vector. /// This new vector will be independent from the other vector. /// A new memory block will be allocated for storing the vector. /// </summary> public static DenseVector OfVector(Vector <Complex> vector) { return(new DenseVector(DenseVectorStorage <Complex> .OfVector(vector.Storage))); }
/// <summary> /// Create a new dense vector as a copy of the given enumerable. /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// </summary> public static DenseVector OfEnumerable(IEnumerable <Complex> enumerable) { return(new DenseVector(DenseVectorStorage <Complex> .OfEnumerable(enumerable))); }
/// <summary> /// Create a new dense vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// </summary> public static Vector <T> Dense <T>(DenseVectorStorage <T> storage) where T : struct, IEquatable <T>, IFormattable { return(Vector <T> .Build.Dense(storage)); }
/// <summary> /// Create a new dense vector and initialize each value using the provided init function. /// </summary> public static DenseVector Create(int length, Func <int, Complex> init) { return(new DenseVector(DenseVectorStorage <Complex> .OfInit(length, init))); }
/// <summary> /// Create a new dense vector as a copy of the given other vector. /// This new vector will be independent from the other vector. /// A new memory block will be allocated for storing the vector. /// </summary> public static DenseVector OfVector(Vector <float> vector) { return(new DenseVector(DenseVectorStorage <float> .OfVector(vector.Storage))); }
/// <summary> /// Create a new dense vector and initialize each value using the provided init function. /// </summary> public static DenseVector Create(int length, Func <int, double> init) { return(new DenseVector(DenseVectorStorage <double> .OfInit(length, init))); }
/// <summary> /// Create a new dense vector with values sampled from the provided random distribution. /// </summary> public static DenseVector CreateRandom(int size, IContinuousDistribution distribution) { var storage = new DenseVectorStorage<Complex>(size); for (var i = 0; i < storage.Data.Length; i++) { storage.Data[i] = new Complex(distribution.Sample(), distribution.Sample()); } return new DenseVector(storage); }
public void MapIndexedToForceIncludeZeros(VectorStorageType aType, VectorStorageType resultType) { var a = Build.VectorStorage(aType, new[] { 1.0, 2.0, 0.0, 4.0 }); var result = Build.VectorStorage<double>(resultType, 4); var expected = new DenseVectorStorage<double>(4, new[] { 0.0, -1.0, 1.0, -3.0 }); int badValueCount = 0; // one time is OK for zero-check a.MapIndexedTo(result, (i, u) => { if (a.At(i) != u) Interlocked.Increment(ref badValueCount); return -u + 1.0; }, Zeros.Include); Assert.That(badValueCount, Is.LessThanOrEqualTo(1)); Assert.That(result.Equals(expected)); }
public void Map2ToSkipZeros(VectorStorageType aType, VectorStorageType bType, VectorStorageType resultType) { var a = Build.VectorStorage(aType, new[] { 1.0, 2.0, 0.0, 4.0, 0.0, 6.0 }); var b = Build.VectorStorage(bType, new[] { 11.0, 12.0, 13.0, 0.0, 0.0, 16.0 }); var result = Build.VectorStorage<double>(resultType, 6); var expected = new DenseVectorStorage<double>(6, new[] { 12.0, 14.0, 13.0, 4.0, 0.0, 22.0 }); a.Map2To(result, b, (u, v) => u + v, Zeros.AllowSkip); Assert.That(result.Equals(expected)); }
public void MapToForceIncludeZeros(VectorStorageType aType, VectorStorageType resultType) { var a = Build.VectorStorage(aType, new[] { 1.0, 2.0, 0.0, 4.0 }); var result = Build.VectorStorage<double>(resultType, 4); var expected = new DenseVectorStorage<double>(4, new[] { 0.0, -1.0, 1.0, -3.0 }); a.MapTo(result, u => -u + 1.0, Zeros.Include); Assert.That(result.Equals(expected)); }
public void MapToSkipZeros(VectorStorageType aType, VectorStorageType resultType) { var a = Build.VectorStorage(aType, new[] { 1.0, 2.0, 0.0, 4.0 }); var result = Build.VectorStorage<double>(resultType, 4); var expected = new DenseVectorStorage<double>(4, new[] { -1.0, -2.0, 0.0, -4.0 }); a.MapTo(result, u => -u, Zeros.AllowSkip); Assert.That(result.Equals(expected)); }
public void ShareDiff(Tensor other) { switch (this.Location) { case TensorLocation.Cpu: this._diff = other._diff; break; case TensorLocation.Gpu: throw new NotImplementedException(); } }
public DenseVector(int length, Complex32 value) : this(DenseVectorStorage <Complex32> .OfInit(length, i => value)) { }
/// <summary> /// Create a new dense vector as a copy of the given array. /// This new vector will be independent from the array. /// A new memory block will be allocated for storing the vector. /// </summary> public static DenseVector OfArray(double[] array) { return(new DenseVector(DenseVectorStorage <double> .OfVector(new DenseVectorStorage <double>(array.Length, array)))); }
public DenseVector(Vector <Complex32> other) : this(DenseVectorStorage <Complex32> .OfVector(other.Storage)) { }
/// <summary> /// Create a new dense vector as a copy of the given other vector. /// This new vector will be independent from the other vector. /// A new memory block will be allocated for storing the vector. /// </summary> public static DenseVector OfVector(Vector <double> vector) { return(new DenseVector(DenseVectorStorage <double> .OfVector(vector.Storage))); }
public DenseVector(IEnumerable <Complex32> other) : this(DenseVectorStorage <Complex32> .OfEnumerable(other)) { }
/// <summary> /// Set the values of this vector to the given values. /// </summary> /// <param name="values">The array containing the values to use.</param> /// <exception cref="ArgumentNullException">If <paramref name="values"/> is <see langword="null" />.</exception> /// <exception cref="ArgumentException">If <paramref name="values"/> is not the same size as this vector.</exception> public void SetValues(T[] values) { var source = new DenseVectorStorage <T>(Count, values); source.CopyTo(Storage); }
public void DenseVectorStorageOfValue_NegativeLength_ThrowsArgumentException() { Assert.That(() => DenseVectorStorage <double> .OfValue(-1, 42), Throws.TypeOf <ArgumentOutOfRangeException>() .With.Message.Contains("Value must not be negative (zero is ok).")); }
/// <summary> /// Create a new dense vector straight from an initialized vector storage instance. /// The storage is used directly without copying. /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// </summary> public DenseVector(DenseVectorStorage <Complex> storage) : base(storage) { _length = storage.Length; _values = storage.Data; }
return(new DenseVector(DenseVectorStorage <Complex32> .OfIndexedEnumerable(length, enumerable)));
/// <summary> /// Create a new dense vector as a copy of the given array. /// This new vector will be independent from the array. /// A new memory block will be allocated for storing the vector. /// </summary> public static DenseVector OfArray(Complex[] array) { return(new DenseVector(DenseVectorStorage <Complex> .OfVector(new DenseVectorStorage <Complex>(array.Length, array)))); }
/// <summary> /// Create a new dense vector with values sampled from the provided random distribution. /// </summary> public static DenseVector CreateRandom(int length, IContinuousDistribution distribution) { return(new DenseVector(DenseVectorStorage <Complex> .OfInit(length, i => new Complex(distribution.Sample(), distribution.Sample())))); }
/// <summary> /// Create a new dense vector as a copy of the given indexed enumerable. /// Keys must be provided at most once, zero is assumed if a key is omitted. /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// </summary> public static DenseVector OfIndexedEnumerable(int length, IEnumerable <Tuple <int, Complex> > enumerable) { return(new DenseVector(DenseVectorStorage <Complex> .OfIndexedEnumerable(length, enumerable))); }
return(new DenseVector(DenseVectorStorage <float> .OfIndexedEnumerable(length, enumerable)));
public void ReturnFloatVectorStorage(DenseVectorStorage<float> storage) { lock (_lock) { if (!_floatVectorStoragePool.Contains(storage)) _floatVectorStoragePool.Add(storage); } }
internal DenseVector(DenseVectorStorage<float> storage) : base(storage) { _length = storage.Length; _values = storage.Data; }
/// <summary> /// Create a new dense vector as a copy of the given array. /// This new vector will be independent from the array. /// A new memory block will be allocated for storing the vector. /// </summary> public static DenseVector OfArray(float[] array) { return(new DenseVector(DenseVectorStorage <float> .OfVector(new DenseVectorStorage <float>(array.Length, array)))); }
/// <summary> /// Create a new dense vector with values sampled from the provided random distribution. /// </summary> public static DenseVector CreateRandom(int length, IContinuousDistribution distribution) { return(new DenseVector(DenseVectorStorage <float> .OfInit(length, i => (float)distribution.Sample()))); }
/// <summary> /// Initializes a new instance of the <see cref="DenseVector"/> class. /// </summary> public DenseVector(DenseVectorStorage<Complex> storage) : base(storage) { _length = storage.Length; _values = storage.Data; }
public void Reshape(int num, int channels, int height, int width) { Guard.That(() => num).IsNatural(); Guard.That(() => channels).IsNatural(); Guard.That(() => height).IsNatural(); Guard.That(() => width).IsNatural(); this.Num = num; this.Channels = channels; this.Height = height; this.Width = width; if (this.Count != 0) { switch (this.Location) { case TensorLocation.Cpu: // TODO We can do better here and do not reshape if the size is the same. this._data = DenseVectorStorage<double>.OfValue(this.Count, 0.0f); this._diff = DenseVectorStorage<double>.OfValue(this.Count, 0.0f); break; case TensorLocation.Gpu: throw new NotImplementedException(); } } else { this._data = null; this._diff = null; } }