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];


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]] &&
  TensorReduce[q, Assumptions->Element[realQuatB,Vectors[3,Reals]] &&
  • 1
    $\begingroup$ You could Block and Unprotect Cross, could you show a small example of input and expected output? $\endgroup$
    – Kuba
    Commented May 15, 2017 at 8:58
  • $\begingroup$ Added an example to help clarify. $\endgroup$ Commented May 15, 2017 at 9:59
  • $\begingroup$ 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). $\endgroup$
    – Szabolcs
    Commented May 15, 2017 at 10:17
  • $\begingroup$ Like this?: mathematica.stackexchange.com/questions/21197/… -- E.g. TensorReduce[Cross[A, A], Assumptions -> A \[Element] Vectors[n]] $\endgroup$
    – Michael E2
    Commented May 15, 2017 at 10:22
  • $\begingroup$ @ 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. $\endgroup$ Commented May 16, 2017 at 11:55


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.