예제 #1
0
        /// <summary>
        /// Finds a perpendicular vector to two given
        /// TODO: So far only implemented for 3D vectors
        /// </summary>
        public static GenTensor <T> VectorCrossProduct(GenTensor <T> a,
                                                       GenTensor <T> b)
        {
            #if ALLOW_EXCEPTIONS
            if (!a.IsVector || !b.IsVector)
            {
                throw new InvalidShapeException($"Both {nameof(a)} and {nameof(b)} should be vectors");
            }
            if (a.Shape[0] != b.Shape[0])
            {
                throw new InvalidShapeException($"Length of {nameof(a)} and {nameof(b)} should be equal");
            }
            if (a.Shape[0] != 3)
            {
                throw new NotImplementedException("Other than vectors of the length of 3 aren't supported for VectorCrossProduct yet");
            }
            #endif
            return(GenTensor <T> .CreateVector(
                       ConstantsAndFunctions <T> .Subtract(
                           ConstantsAndFunctions <T> .Multiply(a[1], b[2]),
                           ConstantsAndFunctions <T> .Multiply(a[2], b[1])),

                       ConstantsAndFunctions <T> .Subtract(
                           ConstantsAndFunctions <T> .Multiply(a[2], b[0]),
                           ConstantsAndFunctions <T> .Multiply(a[0], b[2])),

                       ConstantsAndFunctions <T> .Subtract(
                           ConstantsAndFunctions <T> .Multiply(a[0], b[1]),
                           ConstantsAndFunctions <T> .Multiply(a[1], b[0]))
                       ));
        }
예제 #2
0
        public static GenTensor <T> Concat(GenTensor <T> a, GenTensor <T> b)
        {
            #if ALLOW_EXCEPTIONS
            if (a.Shape.SubShape(1, 0) != b.Shape.SubShape(1, 0))
            {
                throw new InvalidShapeException("Excluding the first dimension, all others should match");
            }
            #endif

            if (a.IsVector)
            {
                var resultingVector = GenTensor <T> .CreateVector(a.Shape.shape[0] + b.Shape.shape[0]);

                for (int i = 0; i < a.Shape.shape[0]; i++)
                {
                    resultingVector.SetValueNoCheck(ConstantsAndFunctions <T> .Forward(a.GetValueNoCheck(i)), i);
                }

                for (int i = 0; i < b.Shape.shape[0]; i++)
                {
                    resultingVector.SetValueNoCheck(ConstantsAndFunctions <T> .Forward(b.GetValueNoCheck(i)), i + a.Shape.shape[0]);
                }

                return(resultingVector);
            }
            else
            {
                var newShape = a.Shape.Copy();
                newShape.shape[0] = a.Shape.shape[0] + b.Shape.shape[0];

                var res = new GenTensor <T>(newShape);
                for (int i = 0; i < a.Shape.shape[0]; i++)
                {
                    res.SetSubtensor(a.GetSubtensor(i), i);
                }

                for (int i = 0; i < b.Shape.shape[0]; i++)
                {
                    res.SetSubtensor(b.GetSubtensor(i), i + a.Shape.shape[0]);
                }

                return(res);
            }
        }