# Defining UpValue like replacements confined to exprs with specific Head

I'm not seeing a clean method to redefine builtin functions like Cross, Dot, etc. in such a way they only are only applied to expressions that are using some marker-like Head value. Reduced example, let's say I'm using Foo[..] as a head to represents some pair of expression. The pair of expressions inside a Foo might be involved so making explicit patterns doesn't go very far. Basically I'm looking to do something like this non-working example:

Foo /: Cross[v_, v_] := 0;


### update 1

Trivial example is symbolically reducing quaternions over reals. One rule for the product looks like:

realQuat /:
NonCommutativeMultiply[realQuat[L_, l_], realQuat[R_, r_]] :=
realQuat[Cross[L,R] + l R + r L, r l - Dot[L,R]]


So with a pair written as a (bi)vector scalar pair without Cross reduction:

t0 = realQuat[A,a];
t1 = realQuat[A,b];
t0**t1


gives:

realQuat[a A + A b + AxA, a b - A.A]


instead of the Cross term dropping out like this:

realQuat[a A + A b, a b - A.A]


(this is a strawman example)

### attempt using TensorReduce

Starting from the suggesting of using TensorReduce, I tossed together two containers to mark symbols as either elements of R3 or R, basic funcs to add to both and a reduction function. Much cleaner than my previous attempts.

realQuatB = Alternates[]; (* for marking R3 symbols *)
realQuatS = Alternates[]; (* for marking R symbols *)
realQuatDefB[x_] := If[!MemberQ[realQuatB,x],AppendTo[realQuatB,x];];
realQuatDefS[x_] := If[!MemberQ[realQuatS,x],AppendTo[realQuatS,x];];
SetAttributes[realQuatDefB, {Listable}];
SetAttributes[realQuatDefS, {Listable}];

realQuat /: rqReduce[realQuat[Q_,q_]] := realQuat[
TensorReduce[Q, Assumptions->Element[realQuatB,Vectors[3,Reals]] &&
Element[realQuatS,Reals]],
TensorReduce[q, Assumptions->Element[realQuatB,Vectors[3,Reals]] &&
Element[realQuatS,Reals]]];

• You could Block and Unprotect Cross, could you show a small example of input and expected output? – Kuba May 15 '17 at 8:58
• Added an example to help clarify. – MB Reynolds May 15 '17 at 9:59
• It is still not clear what you are asking, and what this has to do with UpValues. What do you expect as the output? Please give a concrete example, complete with example input, actual output (you have both) and desired output (you don't have this). – Szabolcs May 15 '17 at 10:17
• Like this?: mathematica.stackexchange.com/questions/21197/… -- E.g. TensorReduce[Cross[A, A], Assumptions -> A \[Element] Vectors[n]] – Michael E2 May 15 '17 at 10:22
• @ Szabolcs: By "UpValue" like I was intending to say we can use a marker head and upvalue defs to scope when some rules are applied and I'm generally curious if there a simply method to have rules applied at any sub-expression level of a maker head. – MB Reynolds May 16 '17 at 11:55