/// <summary> /// Applies matrix dot product operation for /// all matrices in tensors /// /// O(N^3) /// </summary> public static GenTensor <T> TensorMatrixMultiply(GenTensor <T> a, GenTensor <T> b) { #if ALLOW_EXCEPTIONS if (a.Shape.Count < 2 || b.Shape.Count < 2) { throw new InvalidShapeException($"Arguments should be at least matrices while their shapes are {a.Shape} and {b.Shape}"); } if (a.Shape.SubShape(0, 2) != b.Shape.SubShape(0, 2)) { throw new InvalidShapeException("Other dimensions of tensors should be equal"); } #endif var oldShape = a.Shape.SubShape(0, 2).ToArray(); var newShape = new int[oldShape.Length + 2]; for (int i = 0; i < oldShape.Length; i++) { newShape[i] = oldShape[i]; } newShape[newShape.Length - 2] = a.Shape[a.Shape.Length - 2]; newShape[newShape.Length - 1] = b.Shape[b.Shape.Length - 1]; var resTensor = new GenTensor <T>(newShape); foreach (var subDimensions in a.IterateOverMatrices()) { var product = MatrixMultiply(a.GetSubtensor(subDimensions), b.GetSubtensor(subDimensions)); resTensor.SetSubtensor(product, subDimensions); } return(resTensor); }
/// <summary> /// Applies scalar product to every vector in a tensor so that /// you will get a one-reduced dimensional tensor /// (e. g. TensorVectorDotProduct([4 x 3 x 2], [4 x 3 x 2]) -> [4 x 3] /// /// O(V) /// </summary> public static GenTensor <T> TensorVectorDotProduct(GenTensor <T> a, GenTensor <T> b) { #if ALLOW_EXCEPTIONS if (a.Shape.SubShape(0, 1) != b.Shape.SubShape(0, 1)) { throw new InvalidShapeException("Other dimensions of tensors should be equal"); } #endif var resTensor = new GenTensor <T>(a.Shape.SubShape(0, 1)); foreach (var index in resTensor.IterateOverElements()) { var scal = VectorDotProduct(a.GetSubtensor(index), b.GetSubtensor(index)); resTensor.SetValueNoCheck(scal, index); } return(resTensor); }
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); } }
/// <summary> /// Calls VectorCrossProduct for every vector in the tensor /// </summary> public static GenTensor <T> TensorVectorCrossProduct(GenTensor <T> a, GenTensor <T> b) { #if ALLOW_EXCEPTIONS if (a.Shape != b.Shape) { throw new InvalidShapeException($"Pre-shapes of {nameof(a)} and {nameof(b)} should be equal"); } #endif var res = new GenTensor <T>(a.Shape); foreach (var index in a.IterateOverVectors()) { res.SetSubtensor( VectorCrossProduct(a.GetSubtensor(index), b.GetSubtensor(index)), index ); } return(res); }
public static GenTensor <T> TensorMatrixDivide(GenTensor <T> a, GenTensor <T> b) { #if ALLOW_EXCEPTIONS InvalidShapeException.NeedTensorSquareMatrix(a); InvalidShapeException.NeedTensorSquareMatrix(b); if (a.Shape != b.Shape) { throw new InvalidShapeException("Should be of the same shape"); } #endif var res = new GenTensor <T>(a.Shape); foreach (var ind in res.IterateOverMatrices()) { res.SetSubtensor( MatrixDivide( a.GetSubtensor(ind), b.GetSubtensor(ind) ), ind); } return(res); }