/// <inheritdoc/> public override ExpNode Execute(VectorProductOperNode node) { // Verify left and right child are two multiplyable vectors. if (node.LeftChild is TensorNode vector1 && vector1.DimensionCount == 1 && node.RightChild is TensorNode vector2 && vector2.DimensionCount == 1 && vector1.SizeIdentity == vector2.SizeIdentity) { int size = vector1.GetDimensionSize(1); switch (node.ProductMethod) { case VectorProductMethod.DOT: ExpNode[] terms = new ExpNode[size]; for (int i = 0; i < size; i++) { terms[i] = QuickOpers.Multiply(vector1.GetChild(i), vector2.GetChild(i)); } return(QuickOpers.Sum(terms).Execute(this)); case VectorProductMethod.CROSS: // TODO: Convert to matrix notation for determinant default: return(node); } } return(HandleError(new CannotMultiplyTensors(this, node))); }
/// <summary> /// Adds tensors. /// </summary> /// <param name="node">The <see cref="AdditionOperNode"/> containing tensors.</param> /// <param name="simplifier">The <see cref="Simplifier"/> calling.</param> /// <returns>The resuling <see cref="ExpNode"/>.</returns> public static ExpNode SumTensors(AdditionOperNode node, Simplifier simplifier) { if (node.GetChild(0) is TensorNode tensorNode) { for (int i = 1; i < node.ChildCount; i++) { if (node.GetChild(i) is TensorNode otherTensorNode) { if (otherTensorNode.SizeIdentity == tensorNode.SizeIdentity) { for (int j = 0; j < tensorNode.ChildCount; j++) { ExpNode addedNode = QuickOpers .Sum(tensorNode.GetChild(j), otherTensorNode.GetChild(j)) .Execute(simplifier); tensorNode.ReplaceChild(addedNode, j); } } else { return(simplifier.HandleError(new CannotAddTensors(simplifier, node, $"Cannot add tensor of shape {otherTensorNode.SizeIdentity} and tensor of shape {tensorNode.SizeIdentity}."))); } } else { return(simplifier.HandleError(new CannotAddTensors(simplifier, node, "Cannot add scalar and tensor."))); } } return(tensorNode); } else { // There is a scalar. // There cannot be any tensors for (int i = 1; i < node.ChildCount; i++) { if (node.GetChild(i) is TensorNode) { return(simplifier.HandleError(new CannotAddTensors(simplifier, node, "Cannot add tensor and scalar."))); } } } return(node); }
/// <summary> /// Adds the exponent of another <see cref="MultiplicativeTerm"/>. /// </summary> /// <param name="other">The other <see cref="MultiplicativeTerm"/>.</param> /// <param name="simplifier">The <see cref="Simplifier"/> to simplify with after adding.</param> public void AddToExponent(MultiplicativeTerm other, Simplifier simplifier) { _exponent = QuickOpers.Sum(_exponent, other._exponent).Execute(simplifier); }