Example #1
0
        public static TensorOld <double> Dot(this TensorOld <double> t1, TensorOld <double> t2)
        {
            if (t1.NumberDimensions > 2 || t2.NumberDimensions > 2)
            {
                throw new ProgrammingError("Dot product is not defined for tensors more that dimension 2");
            }

            if (t1.NumberDimensions == 1)
            {
                t1 = t1.Reshape(t1.Len, 1);
            }

            if (t2.NumberDimensions == 1)
            {
                t2.Reshape(1, t2.Len);
            }

            if (t1.ShapeDimensions[1] != t2.ShapeDimensions[0])
            {
                throw new ProgrammingError($"number of colums of the first matrix '{t1.ShapeDimensions[1]}' is not equal to the number of rows of the second: '{t2.ShapeDimensions[0]}'");
            }

            int convolutionSize = t1.ShapeDimensions[1];

            TensorOld <double> result = new TensorOld <double>(new[] { t1.ShapeDimensions[0], t2.ShapeDimensions[1] });

            int numRows = t1.ShapeDimensions[0];
            int numCols = t2.ShapeDimensions[1];

            for (int col = 0; col < numCols; col++)
            {
                result.GetTensorSpan(1, col);
                for (int row = 0; row < numRows; row++)
                {
                    ArraySubCollection <double> iter1 = t1.GetTensorSpan(1, row);
                    ArraySubCollection <double> iter2 = t2.GetTensorSpan(0, 0, col);

                    double cellValue = 0;

                    for (int i = 0; i < convolutionSize; i++)
                    {
                        cellValue += iter1[i] * iter2[i];
                    }
                }
            }

            return(result);
        }