# Leibniz rule with unknown orders of imput

I'm trying to find a simple way to define a bilinear and Leibniz functional/map "$$B(\cdot,\cdot)$$", that eats two functions $$f(x),f(y)$$ or products theirof and produces the following, $$B(f(x),f(y))=f(x**y-y**x) \,,$$

where "$$**$$" is a non-commutative product that's un-important for my question.

The problem I have is making the map $$B(\cdot,\cdot)$$ obey the Leibniz rule $$B(f(x_1)f(x_2)...f(x_n),f(y))=\sum_if(x_1)...f(x_{i-1})B(f(x_i),f(y))f(x_{i+1})..f(x_n) \,.$$

I have read this question and tried the following code to implement just for Leibniz rule

B[f[x_], f[y_]] := f[x ** y - y ** x]
B[f[x_]*f[y_], f[z_]] := B[f[x], f[z]]*f[y] + f[x]*B[f[y], f[z]]


But the above code doesn't seem to understand how to handle power imputs of $$f(x)$$ with the same argument $$x$$, e.g. $$f(x)^2$$, and higher order products like $$f(x)f(y)f(z)$$. For example if I evaluate

B[f[a]^2, f[e]]
B[f[a]*f[b]*f[c], f[e]]


Mathematica doesn't seem to know what to do.

I guess it's because I only told Mathematica how to handle two imputs in one slot of $$B(\cdot,\cdot)$$. But since I am going to be manipulating higher order products of $$f(x)$$, how should I define the map $$B$$ without making one definition for each order of product/power of the imput function?

PS: The problem also comes up when I try to implement Bilinearity for the map $$B$$.

B[f[x_],f[y_]]:=f[x**y-y**x]; B[x_Times,y_]:=Sum[ReplacePart[x,{i}:>B[x[[i]],y]],{i,Length[x]}]; B[Power[x_,y_],z_]:=y*Power[x,y-1]*B[x,z];
• @JDING x_Times is a pattern which represents an expression with head Times. Similarly, Power[x_, y_] is pattern represents an expression with head Power. For more detail, you may check the document for Pattern and Blank. Mathematically, x^2 can be identified with x*x. However, these are two different expressions in Mathematica. The former is of head Power, while the latter is of head Times (Mathematica would automatically replace x*x by x^2, but not vice versa). That's why we need the third line. Apr 9 '20 at 7:57