Example #1
0
        public void DoubleTensorPower()
        {
            var x1 = DoubleTensor.Range(2f, 15f, 1f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var x2 = x1.Pow(2.0f);

            Assert.AreEqual(x1.Shape.Length, x2.Shape.Length);
            Assert.AreEqual(x1.Shape[0], x2.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Pow(x1[i], 2.0f), x2[i]));
            }

            var x3 = x1.TPow(2.0f);

            Assert.AreEqual(x1.Shape.Length, x3.Shape.Length);
            Assert.AreEqual(x1.Shape[0], x3.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Pow(2.0f, x1[i]), x3[i]));
            }
        }
Example #2
0
        public void DoubleTensorArcTrigonometrics()
        {
            var x1 = DoubleTensor.Range(0.0f, 0.99f, 0.05f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var xsin = x1.Asin();

            Assert.AreEqual(x1.Shape.Length, xsin.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xsin.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Asin(x1[i]), (double)xsin[i]));
            }

            var xcos = x1.Acos();

            Assert.AreEqual(x1.Shape.Length, xcos.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xcos.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Acos(x1[i]), (double)xcos[i]));
            }

            var xtan = x1.Atan();

            Assert.AreEqual(x1.Shape.Length, xtan.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xtan.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Atan(x1[i]), (double)xtan[i]));
            }
        }
Example #3
0
        public void DoubleTensorHyperbolics()
        {
            var x1 = DoubleTensor.Range(2f, 15f, 1f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var xsin = x1.Sinh();

            Assert.AreEqual(x1.Shape.Length, xsin.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xsin.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Sinh(x1[i]), (double)xsin[i]));
            }

            var xcos = x1.Cosh();

            Assert.AreEqual(x1.Shape.Length, xcos.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xcos.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Cosh(x1[i]), (double)xcos[i]));
            }

            var xtan = x1.Tanh();

            Assert.AreEqual(x1.Shape.Length, xtan.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xtan.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Tanh(x1[i]), (double)xtan[i]));
            }
        }
Example #4
0
        public void DoubleTensorTrigonometrics()
        {
            var x1 = DoubleTensor.Range(2f, 15f, 1f);

            Assert.NotNull(x1);
            Assert.True(1 == x1.Shape.Length);

            var xsin = x1.Sin();

            Assert.Equal(x1.Shape.Length, xsin.Shape.Length);
            Assert.Equal(x1.Shape[0], xsin.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.True(BasicTensorAPI.IsApproximatelyEqual(Math.Sin(x1[i]), (double)xsin[i]));
            }

            var xcos = x1.Cos();

            Assert.Equal(x1.Shape.Length, xcos.Shape.Length);
            Assert.Equal(x1.Shape[0], xcos.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.True(BasicTensorAPI.IsApproximatelyEqual(Math.Cos(x1[i]), (double)xcos[i]));
            }

            var xtan = x1.Tan();

            Assert.Equal(x1.Shape.Length, xtan.Shape.Length);
            Assert.Equal(x1.Shape[0], xtan.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.True(BasicTensorAPI.IsApproximatelyEqual(Math.Tan(x1[i]), (double)xtan[i]));
            }
        }
Example #5
0
        public void RangeDouble()
        {
            var x1 = DoubleTensor.Range(0f, 100f, 1f);

            for (int i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.AreEqual(i, x1[i]);
            }
        }
Example #6
0
        public void SignDouble()
        {
            var x1 = DoubleTensor.Range(-15f, 15f, 1f);
            var x2 = x1.Sign();

            for (var i = 0; i < x2.Shape[0]; ++i)
            {
                var sign = (x1[i] < 0) ? -1 : (x1[i] == 0) ? 0 : 1;
                Assert.AreEqual(sign, x2[i]);
            }
        }
Example #7
0
        public void CreateDoubleTensorFromRange()
        {
            var x1 = DoubleTensor.Range(0f, 150f, 5f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            float start = 0f;

            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.AreEqual(start + 5f * i, x1[i]);
            }
        }
Example #8
0
        public void DoubleTensorExp()
        {
            var x1 = DoubleTensor.Range(2f, 15f, 1f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var x2 = x1.Exp();

            Assert.AreEqual(x1.Shape.Length, x2.Shape.Length);
            Assert.AreEqual(x1.Shape[0], x2.Shape[0]);

            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Exp(x1[i]), (double)x2[i]));
            }
        }
Example #9
0
        public void DoubleTensorLog2()
        {
            var x1 = DoubleTensor.Range(2f, 100f, 1f);

            Assert.NotNull(x1);
            Assert.True(1 == x1.Shape.Length);

            var x2 = x1.Log2();

            Assert.Equal(x1.Shape.Length, x2.Shape.Length);
            Assert.Equal(x1.Shape[0], x2.Shape[0]);

            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.True(BasicTensorAPI.IsApproximatelyEqual(Math.Log(x1[i], 2.0), (double)x2[i]));
            }
        }
Example #10
0
        public void DoubleTensorUnaries()
        {
            var x1 = DoubleTensor.Range(2f, 15f, 1f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var xsqrt = x1.Sqrt();

            Assert.AreEqual(x1.Shape.Length, xsqrt.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xsqrt.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Sqrt(x1[i]), (double)xsqrt[i]));
            }

            x1 = DoubleTensor.Range(-15f, 15f, .1f);

            var xceil = x1.Ceil();

            Assert.AreEqual(x1.Shape.Length, xceil.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xceil.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Ceiling(x1[i]), (double)xceil[i]));
            }

            var xfloor = x1.Floor();

            Assert.AreEqual(x1.Shape.Length, xfloor.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xfloor.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Floor(x1[i]), (double)xfloor[i]));
            }

            var xabs = x1.Abs();

            Assert.AreEqual(x1.Shape.Length, xabs.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xabs.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Abs(x1[i]), (double)xabs[i]));
            }

            var xneg = x1.neg();

            Assert.AreEqual(x1.Shape.Length, xneg.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xneg.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(-x1[i], (double)xneg[i]));
            }

#if false
            // DISABLED: libtorch appears to round away from zero for N.5, which means that comparing
            //           against Math.Round() will fail.
            x1 = DoubleTensor.Range(0f, 15f, .1f);

            var xrnd = x1.Round();
            Assert.AreEqual(x1.Shape.Length, xrnd.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xrnd.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Console.WriteLine($"{x1[i]}, {Math.Round(x1[i])}, {(double)xrnd[i]}");
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Round(x1[i]), (double)xrnd[i]));
            }
#endif
        }