/// <summary>
 /// Create a new sparse matrix as a copy of the given row vectors.
 /// This new matrix will be independent from the vectors.
 /// A new memory block will be allocated for storing the matrix.
 /// </summary>
 public static SparseMatrix OfRowVectors(params Vector<float>[] rows)
 {
     var storage = new VectorStorage<float>[rows.Length];
     for (int i = 0; i < rows.Length; i++)
     {
         storage[i] = rows[i].Storage;
     }
     return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfRowVectors(storage));
 }
 /// <summary>
 /// Create a new dense matrix as a copy of the given row vectors.
 /// This new matrix will be independent from the vectors.
 /// A new memory block will be allocated for storing the matrix.
 /// </summary>
 public static DenseMatrix OfRowVectors(params Vector<double>[] rows)
 {
     var storage = new VectorStorage<double>[rows.Length];
     for (int i = 0; i < rows.Length; i++)
     {
         storage[i] = rows[i].Storage;
     }
     return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowVectors(storage));
 }
 /// <summary>
 /// Create a new sparse matrix as a copy of the given column vectors.
 /// This new matrix will be independent from the vectors.
 /// A new memory block will be allocated for storing the matrix.
 /// </summary>
 public static SparseMatrix OfColumnVectors(params Vector<float>[] columns)
 {
     var storage = new VectorStorage<float>[columns.Length];
     for (int i = 0; i < columns.Length; i++)
     {
         storage[i] = columns[i].Storage;
     }
     return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfColumnVectors(storage));
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the Vector class.
 /// </summary>
 protected Vector(VectorStorage <Complex> storage)
     : base(storage)
 {
 }
Example #5
0
 /// <summary>
 /// Create a new dense matrix as a copy of the given column vectors.
 /// This new matrix will be independent from the vectors.
 /// A new memory block will be allocated for storing the matrix.
 /// </summary>
 public static DenseMatrix OfColumnVectors(params Vector<float>[] columns)
 {
     var storage = new VectorStorage<float>[columns.Length];
     for (int i = 0; i < columns.Length; i++)
     {
         storage[i] = columns[i].Storage;
     }
     return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfColumnVectors(storage));
 }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the Vector class.
 /// </summary>
 protected Vector(VectorStorage <T> storage)
 {
     Storage = storage;
     Count   = storage.Length;
 }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the Vector class.
 /// </summary>
 protected Vector(VectorStorage <float> storage)
     : base(storage)
 {
 }
Example #8
0
 internal Vector(VectorStorage vectorStorage)
 {
     _vectorStorage = vectorStorage;
 }
Example #9
0
 /// <summary>
 /// Initializes a new instance of the Vector class.
 /// </summary>
 protected Vector(VectorStorage <double> storage)
     : base(storage)
 {
 }
Example #10
0
 /// <summary>
 /// Create a new vector straight from an initialized matrix storage instance.
 /// If you have an instance of a discrete storage type instead, use their direct methods instead.
 /// </summary>
 public static Vector <T> WithStorage <T>(VectorStorage <T> storage)
     where T : struct, IEquatable <T>, IFormattable
 {
     return(Vector <T> .Build.OfStorage(storage));
 }
Example #11
0
        public void CouldSerializeVectorStorage()
        {
            var rng   = new Random(42);
            var count = 100_000;
            var arr   = new SmallDecimal[count];

            arr[0] = new SmallDecimal(1000 * 1.0, 4);

            for (int i = 1; i < count; i++)
            {
                arr[i] = arr[i - 1] + new SmallDecimal((double)arr[i - 1] * (0.02 + -0.04 * rng.NextDouble()), 4);
            }
            // arr = Enumerable.Range(0, count).Select(x => new SmallDecimal(1000 + (double)x + (double)Math.Round(0.1 * rng.NextDouble(), 5), precision:3)).ToArray();

            var r = ArrayMemory <SmallDecimal> .Create(arr, 0, arr.Length, externallyOwned : true, pin : true);

            var vs = VectorStorage.Create(r, 0, r.Length);

            var vsT = new VectorStorage <SmallDecimal>(vs);

            var payload = count * Unsafe.SizeOf <double>() + 4;

            foreach (SerializationFormat format in ((SerializationFormat[])Enum.GetValues(typeof(SerializationFormat))).OrderBy(e => e.ToString()))
            {
                var len = BinarySerializer.SizeOf(in vsT, out var rm, format);

                var destination   = BufferPool.Retain(len);
                var destinationDb = new DirectBuffer(destination);

                var len1 = BinarySerializer.Write(in vsT, ref destinationDb, in rm, format);
                Assert.AreEqual(destination.Length, destinationDb.Length);

                Assert.AreEqual(len, len1);

                var flags = destinationDb.Read <VersionAndFlags>(0);
                Assert.AreEqual(format, flags.SerializationFormat);
                var header = destinationDb.Read <DataTypeHeader>(0);
                Assert.AreEqual(TypeEnum.VectorStorage, header.TypeEnum);
                Assert.AreEqual(TypeEnum.SmallDecimal, header.ElementTypeEnum);
                Assert.AreEqual(Unsafe.SizeOf <SmallDecimal>(), header.TypeSize);

                var len2 = BinarySerializer.Read(ref destinationDb, out VectorStorage <SmallDecimal> value);
                Assert.AreEqual(destination.Length, destinationDb.Length);

                Assert.AreEqual(len, len2);
                Assert.AreEqual(vs.Length, value.Storage.Length);


                for (int i = 0; i < count; i++)
                {
                    SmallDecimal left;
                    SmallDecimal right;
                    if ((left = vs.Vec.DangerousGetRef <SmallDecimal>(i)) != (right = value.Storage.DangerousGetRef <SmallDecimal>(i)))
                    {
                        Console.WriteLine("Not equals");
                    }
                }

                Assert.IsTrue(vs.Vec.Slice(0, vs.Length).AsSpan <SmallDecimal>().SequenceEqual(value.Storage.Vec.Slice(0, value.Storage.Length).AsSpan <SmallDecimal>()));

                Console.WriteLine($"{format} len: {len:N0} x{Math.Round((double)payload/len, 2)}");

                destination.Dispose();
                value.Storage.Dispose();
            }

            vs.Dispose();
            Assert.IsTrue(vs.IsDisposed);
        }
Example #12
0
 public ZilVector([NotNull] params ZilObject[] items)
 {
     storage = new VectorStorage(items);
     offset  = 0;
 }
Example #13
0
 ZilVector([NotNull] VectorStorage storage, int offset)
 {
     this.storage = storage;
     this.offset  = offset;
 }
Example #14
0
 public ZilVector()
 {
     storage = new VectorStorage();
     offset  = 0;
 }