I've want to replace the pattern, say I the arbitrary function $o$ that takes one argument, now, if the two of these function meet in a expression, one with some arugment $B$ and the other with the conjugated argument $B^\dagger$ I want to replace these into one function called $k$ but with the twist, that the argument is now the same letter but lower case symbolically

$$ o(B) o(B^\dagger) \rightarrow k(b) $$ What I thought would work, althought it does not

o[-B] o[-B\[Dagger]] /. o[A__] o[A\[Dagger]__] -> (k[ToExpression@ToLowerCase[ToString[A]]]

sadly this doesn't work. Anyone know the solution?


2 Answers 2


Your problem comes from the fact that MMA considers "A[Dagger]" a single symbol different from "A". To match you need therefore 2 different patterns (note, do not use capital letters, they are reserved for system symbols):

o[-x_] o[-y_]

The second problems comes from ensuring that you have the same symbol as without dagger. You first need to extract the symbol name (a string), get rid of the dagger, store it in tmp for later use, transform it into a symbol and compare with the the first pattern

 o[-x_] o[-y_] /; (Symbol[ tmp = StringTake[ToString[SymbolName[y]], 1]] == x )

The next problem is the creation of a new symbol with a lower case name. As we already have the string with upper case, we simply need "ToLowerCase" to create a new symbol:


Everything together:

o[-B] o[-B\[Dagger]] /.  o[-x_] o[-y_] /;
(Symbol[tmp = StringTake[ToString[SymbolName[y]], 1]] == x ) :>



o[ B] o[Conjugate[B]] /. o[A_ ] o[Conjugate[A_]] -> k [A]  
(* k[B]*)
  • $\begingroup$ But I want the B to be lower case.... $\endgroup$
    – Nitaa a
    Commented Apr 18, 2023 at 11:52
  • $\begingroup$ Replace Rule (->) by RuleDelayed (:>) $\endgroup$
    – Acus
    Commented Apr 18, 2023 at 12:31
  • 1
    $\begingroup$ A dagger is ConjugateTranspose $\endgroup$
    – Bob Hanlon
    Commented Apr 18, 2023 at 13:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.