Skip to main content
added 50 characters in body
Source Link

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression.). It should also work for several other binary operators. listable, Listable or not  (, althoughalthough I haven't tested it on them).

t[x_, oper_: Times] :=Tr@= Tr @ ((Length[#] - 1) & /@
                       (Extract[x, {Sequence@@Drop[#Sequence @@ Drop[#, -1]}]&] & /@Position[x@ Position[x, oper]))

Usage

t[a b c + d e + f[a b] - 1/f[f[c d]]]

6

6

Under the hood.

Let's examine a b c + d e + f[a b] - 1/f[f[c d]], using Position and Extract and `TreeFormTreeForm.

The tree structure of the expression...

ClearAll[a, b, c, d, e, f]
(x = a b c + d e + f[a b] - 1/f[f[c d]]) // TreeForm

times1

Then the positions of the head, Times.

Position[x, Times]

{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}

{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}  

By dropping the final zero from each position, we obtain the instances of Times, including the arguments.

Extract[x, {Sequence @@ Drop[#, -1]}] & /@ %

{a b c, d e, a b, -(1/f[f[c d]]), c d}

{a b c, d e, a b, -(1/f[f[c d]]), c d}

...the number of items.

Length[%]

5

5

[t[] goes a step further to count the (arguments-1) for each Times, namely, 6.]

...a look at the structure of each of those instances of TimesTimes.

TreeForm /@ %%

times2

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression.) It should also work for several other binary operators. listable or not(, although I haven't tested it on them).

t[x_,oper_: Times]:=Tr@((Length[#]-1)&/@(Extract[x,{Sequence@@Drop[#,-1]}]&/@Position[x,oper]))

Usage

t[a b c + d e + f[a b] - 1/f[f[c d]]]

6


Under the hood.

Let's examine a b c + d e + f[a b] - 1/f[f[c d]], using Position and Extract and `TreeForm.

The tree structure of the expression...

ClearAll[a, b, c, d, e, f]
(x = a b c + d e + f[a b] - 1/f[f[c d]]) // TreeForm

times1

Then the positions of the head, Times.

Position[x, Times]

{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}

By dropping the final zero from each position we obtain the instances of Times including the arguments.

Extract[x, {Sequence @@ Drop[#, -1]}] & /@ %

{a b c, d e, a b, -(1/f[f[c d]]), c d}

...the number of items.

Length[%]

5

[t[] goes a step further to count the (arguments-1) for each Times, namely, 6.]

...a look at the structure of each of those instances of Times.

TreeForm /@ %%

times2

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression). It should also work for several other binary operators, Listable or not  (although I haven't tested it on them).

t[x_, oper_: Times] := Tr @ ((Length[#] - 1) & /@
                       (Extract[x, {Sequence @@ Drop[#, -1]}] & /@ Position[x, oper]))

Usage

t[a b c + d e + f[a b] - 1/f[f[c d]]]
6

Under the hood.

Let's examine a b c + d e + f[a b] - 1/f[f[c d]], using Position and Extract and TreeForm.

The tree structure of the expression...

ClearAll[a, b, c, d, e, f]
(x = a b c + d e + f[a b] - 1/f[f[c d]]) // TreeForm

times1

Then the positions of the head, Times.

Position[x, Times]
{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}  

By dropping the final zero from each position, we obtain the instances of Times, including the arguments.

Extract[x, {Sequence @@ Drop[#, -1]}] & /@ %
{a b c, d e, a b, -(1/f[f[c d]]), c d}

...the number of items.

Length[%]
5

[t[] goes a step further to count the (arguments-1) for each Times, namely, 6.]

...a look at the structure of each of those instances of Times.

TreeForm /@ %%

times2

added 114 characters in body
Source Link
DavidC
  • 16.8k
  • 1
  • 42
  • 94

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression.) It should also work for several other binary operators. listable or not(, although I haven't tested it on them).

t[x_]t[x_,oper_: Times]:=Tr@((Length[#] - 1) & /@ (Extract[x, {Sequence @@ Drop[#Sequence@@Drop[#, -1]}] & ]&/@Position[x, Times]oper]))

Usage

t[a b c + d e + f[a b] - 1/f[f[c d]]]

6


Under the hood.

Let's examine a b c + d e + f[a b] - 1/f[f[c d]], using Position and Extract and `TreeForm.

The tree structure of the expression...

ClearAll[a, b, c, d, e, f]
(x = a b c + d e + f[a b] - 1/f[f[c d]]) // TreeForm

times1

Then the positions of the head, Times.

Position[x, Times]

{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}

By dropping the final zero from each position we obtain the instances of Times including the arguments.

Extract[x, {Sequence @@ Drop[#, -1]}] & /@ %

{a b c, d e, a b, -(1/f[f[c d]]), c d}

...the number of items.

Length[%]

5

[t[] goes a step further to count the (arguments-1) for each Times, namely, 6.]

...a look at the structure of each of those instances of Times.

TreeForm /@ %%

times2

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression.)

t[x_]:=Tr@((Length[#] - 1) & /@ (Extract[x, {Sequence @@ Drop[#, -1]}] & /@Position[x, Times]))

Usage

t[a b c + d e + f[a b] - 1/f[f[c d]]]

6


Under the hood.

Let's examine a b c + d e + f[a b] - 1/f[f[c d]], using Position and Extract and `TreeForm.

The tree structure of the expression...

ClearAll[a, b, c, d, e, f]
(x = a b c + d e + f[a b] - 1/f[f[c d]]) // TreeForm

times1

Then the positions of the head, Times.

Position[x, Times]

{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}

By dropping the final zero from each position we obtain the instances of Times including the arguments.

Extract[x, {Sequence @@ Drop[#, -1]}] & /@ %

{a b c, d e, a b, -(1/f[f[c d]]), c d}

...the number of items.

Length[%]

5

[t[] goes a step further to count the (arguments-1) for each Times, namely, 6.]

...a look at the structure of each of those instances of Times.

TreeForm /@ %%

times2

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression.) It should also work for several other binary operators. listable or not(, although I haven't tested it on them).

t[x_,oper_: Times]:=Tr@((Length[#]-1)&/@(Extract[x,{Sequence@@Drop[#,-1]}]&/@Position[x,oper]))

Usage

t[a b c + d e + f[a b] - 1/f[f[c d]]]

6


Under the hood.

Let's examine a b c + d e + f[a b] - 1/f[f[c d]], using Position and Extract and `TreeForm.

The tree structure of the expression...

ClearAll[a, b, c, d, e, f]
(x = a b c + d e + f[a b] - 1/f[f[c d]]) // TreeForm

times1

Then the positions of the head, Times.

Position[x, Times]

{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}

By dropping the final zero from each position we obtain the instances of Times including the arguments.

Extract[x, {Sequence @@ Drop[#, -1]}] & /@ %

{a b c, d e, a b, -(1/f[f[c d]]), c d}

...the number of items.

Length[%]

5

[t[] goes a step further to count the (arguments-1) for each Times, namely, 6.]

...a look at the structure of each of those instances of Times.

TreeForm /@ %%

times2

t placed at beginning
Source Link
DavidC
  • 16.8k
  • 1
  • 42
  • 94

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression.)

t[x_]:=Tr@((Length[#] - 1) & /@ (Extract[x, {Sequence @@ Drop[#, -1]}] & /@Position[x, Times]))

Usage

t[a b c + d e + f[a b] - 1/f[f[c d]]]

6


Under the hood.

Let's examine a b c + d e + f[a b] - 1/f[f[c d]], using Position and Extract and `TreeForm.

The tree graphstructure of the expression appears first...

ClearAll[a, b, c, d, e, f]
(x = a b c + d e + f[a b] - 1/f[f[c d]]) // TreeForm

times1

Then the positions of the head, Times.

Position[x, Times]

{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}

By dropping the final zero from each position we obtain the instances of Times including the arguments.

Extract[x, {Sequence @@ Drop[#, -1]}] & /@ %

{a b c, d e, a b, -(1/f[f[c d]]), c d}

...the number of items. [

Length[%]

5

[t[] goes a step further to count the (arguments-1)

  for each Times, namely, 6.]

Length[%]

5

...and aa look at the structure of each of those instances of Times.

TreeForm /@ %%

times2

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression.)

t[x_]:=Tr@((Length[#] - 1) & /@ (Extract[x, {Sequence @@ Drop[#, -1]}] & /@Position[x, Times]))

Usage

t[a b c + d e + f[a b] - 1/f[f[c d]]]

6


Under the hood.

Let's examine a b c + d e + f[a b] - 1/f[f[c d]], using Position and Extract and `TreeForm.

The tree graph of the expression appears first.

ClearAll[a, b, c, d, e, f]
(x = a b c + d e + f[a b] - 1/f[f[c d]]) // TreeForm

times1

Then the positions of the head, Times.

Position[x, Times]

{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}

By dropping the final zero from each position we obtain the instances of Times including the arguments.

Extract[x, {Sequence @@ Drop[#, -1]}] & /@ %

{a b c, d e, a b, -(1/f[f[c d]]), c d}

...the number of items. [t[] goes a step further to count the (arguments-1)

  for each Times.]

Length[%]

5

...and a look at the structure of each of those instances of Times.

TreeForm /@ %%

times2

Here's one way to count the number of multiplications in an expression (equal to or greater than the number of Times in the expression.)

t[x_]:=Tr@((Length[#] - 1) & /@ (Extract[x, {Sequence @@ Drop[#, -1]}] & /@Position[x, Times]))

Usage

t[a b c + d e + f[a b] - 1/f[f[c d]]]

6


Under the hood.

Let's examine a b c + d e + f[a b] - 1/f[f[c d]], using Position and Extract and `TreeForm.

The tree structure of the expression...

ClearAll[a, b, c, d, e, f]
(x = a b c + d e + f[a b] - 1/f[f[c d]]) // TreeForm

times1

Then the positions of the head, Times.

Position[x, Times]

{{1, 0}, {2, 0}, {3, 1, 0}, {4, 0}, {4, 2, 1, 1, 1, 0}}

By dropping the final zero from each position we obtain the instances of Times including the arguments.

Extract[x, {Sequence @@ Drop[#, -1]}] & /@ %

{a b c, d e, a b, -(1/f[f[c d]]), c d}

...the number of items.

Length[%]

5

[t[] goes a step further to count the (arguments-1) for each Times, namely, 6.]

...a look at the structure of each of those instances of Times.

TreeForm /@ %%

times2

t placed at beginning
Source Link
DavidC
  • 16.8k
  • 1
  • 42
  • 94
Loading
Source Link
DavidC
  • 16.8k
  • 1
  • 42
  • 94
Loading