コード例 #1
0
 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));
 }
コード例 #2
0
 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));
 }
コード例 #3
0
 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));
 }
コード例 #4
0
 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));
 }
コード例 #5
0
 public void ReturnComplex32VectorStorage(DenseVectorStorage<Complex32> storage)
 {
     lock (_lock)
     {
         if (!_complex32VectorStoragePool.Contains(storage))
             _complex32VectorStoragePool.Add(storage);
     }
 }
コード例 #6
0
ファイル: DenseVector.cs プロジェクト: hrapa/mathnet-numerics
 internal DenseVector(DenseVectorStorage<Complex32> storage)
     : base(storage)
 {
     _length = storage.Length;
     _values = storage.Data;
 }
コード例 #7
0
 /// <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)));
 }
コード例 #8
0
 /// <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)));
 }
コード例 #9
0
 /// <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));
 }
コード例 #10
0
 /// <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)));
 }
コード例 #11
0
 /// <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)));
 }
コード例 #12
0
 /// <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)));
 }
コード例 #13
0
 /// <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);
 }
コード例 #14
0
 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));
 }
コード例 #15
0
 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));
 }
コード例 #16
0
 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));
 }
コード例 #17
0
 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));
 }
コード例 #18
0
ファイル: Tensor.cs プロジェクト: redknightlois/cudalearn
 public void ShareDiff(Tensor other)
 {
     switch (this.Location)
     {
         case TensorLocation.Cpu:
             this._diff = other._diff;
             break;
         case TensorLocation.Gpu:
             throw new NotImplementedException();
     }
 }
コード例 #19
0
 public DenseVector(int length, Complex32 value)
     : this(DenseVectorStorage <Complex32> .OfInit(length, i => value))
 {
 }
コード例 #20
0
 /// <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))));
 }
コード例 #21
0
 public DenseVector(Vector <Complex32> other)
     : this(DenseVectorStorage <Complex32> .OfVector(other.Storage))
 {
 }
コード例 #22
0
 /// <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)));
 }
コード例 #23
0
 public DenseVector(IEnumerable <Complex32> other)
     : this(DenseVectorStorage <Complex32> .OfEnumerable(other))
 {
 }
コード例 #24
0
        /// <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);
        }
コード例 #25
0
 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)."));
 }
コード例 #26
0
 /// <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;
 }
コード例 #27
0
 return(new DenseVector(DenseVectorStorage <Complex32> .OfIndexedEnumerable(length, enumerable)));
コード例 #28
0
 /// <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))));
 }
コード例 #29
0
 /// <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()))));
 }
コード例 #30
0
 /// <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)));
 }
コード例 #31
0
 return(new DenseVector(DenseVectorStorage <float> .OfIndexedEnumerable(length, enumerable)));
コード例 #32
0
        public void ReturnFloatVectorStorage(DenseVectorStorage<float> storage)
        {
            lock (_lock)
            {
                if (!_floatVectorStoragePool.Contains(storage))
                    _floatVectorStoragePool.Add(storage);

            }
        }
コード例 #33
0
ファイル: DenseVector.cs プロジェクト: hrapa/mathnet-numerics
 internal DenseVector(DenseVectorStorage<float> storage)
     : base(storage)
 {
     _length = storage.Length;
     _values = storage.Data;
 }
コード例 #34
0
 /// <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))));
 }
コード例 #35
0
 /// <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())));
 }
コード例 #36
0
 /// <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;
 }
コード例 #37
0
ファイル: Tensor.cs プロジェクト: redknightlois/cudalearn
        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;
            }
        }