/// <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> /// Creates a new <see cref="VectorProjOperNode"/> projecting <paramref name="a"/> onto <paramref name="b"/>. /// </summary> /// <param name="a">The <see cref="TensorNode"/> to project from.</param> /// <param name="b">The <see cref="TensorNode"/> to project onto.</param> /// <returns>A new <see cref="VectorProjOperNode"/> as <paramref name="a"/> projected onto <paramref name="b"/>.</returns> public static VectorProductOperNode DotProduct(TensorNode a, TensorNode b) { VectorProductOperNode node = new VectorProductOperNode(VectorProductMethod.DOT); node.AddChild(a); node.AddChild(b); return(node); }
/// <inheritdoc/> public override ExpNode Execute(VectorProjOperNode node) { if (node.LeftChild.AreEqualSizeVectors(node.RightChild, out TensorNode a, out TensorNode b)) { VectorProductOperNode adotb = QuickOpers.DotProduct(a, (TensorNode)b.Clone()); BOperNode bdotb = QuickOpers.DotProduct((TensorNode)b.Clone(), (TensorNode)b.Clone()); return(QuickOpers.Multiply(b, adotb, QuickOpers.Reciprical(bdotb)).Execute(this)); } return(HandleError(new CannotVectorProject(this, node))); }
public void DotProduct() { // Parse DefaultParser parser = new DefaultParser(); VectorProductOperNode node = new VectorProductOperNode(VectorProductMethod.DOT); parser.ParseString("<2,1,3>"); node.AddChild(parser.Tree.Root); parser = new DefaultParser(); parser.ParseString("<1,2,3>"); node.AddChild(parser.Tree.Root); // Simplify Simplifier simplifier = new Simplifier(); ExpNode exp_result = node.Execute(simplifier); // Print DefaultPrinter printer = new DefaultPrinter(); string str_result = exp_result.Print(printer); // Compare Assert.IsTrue(str_result == "13"); }
/// <summary> /// Executes operation on a <see cref="VectorProductOperNode"/>. /// </summary> /// <param name="node">The <see cref="VectorProductOperNode"/> to execute operation on.</param> /// <returns>The result of the operation on a <see cref="VectorProductOperNode"/>.</returns> public virtual ExpNode Execute(VectorProductOperNode node) => Execute((BOperNode)node);