Skip to main content
deleted 1 character in body
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

I think this is easier to do by working with strings.

First write a function that will expand strings of the form "Power(x,k)" where k is an integer in "xx...x" with k - 1 ""s.

f[x_, k_] :=
  Module[{i = Abs[ToExpression[k]] - 1},
    Nest[StringJoin[#, "*" <> x] &, x, i]]

A couple of tests for f.

f["s", 2] 
"s*s"
f["ab", "-3"]
"ab*ab*ab"

Next write a function that will use f to transform powers and will count the stars in the expression after f has done its transformation.

starCount[expr_] :=
  StringCount[
    StringReplace[
      expr // CForm // ToString, 
      "Power(" ~~ v : WordCharacter .. ~~ "," ~~ k : NumberString ~~ ")" :> f[v, k]],
    "*"]

starCount[a + s^2*b - c/y + o^3 + n*m*u]
6
starCount[1/(b s^3) + 1/t^4]
6

Note: I use CForm to recover the /s that represent division, which the OP apparently wants to preserve as ana distinct operator.

I think this is easier to do by working with strings.

First write a function that will expand strings of the form "Power(x,k)" where k is an integer in "xx...x" with k - 1 ""s.

f[x_, k_] :=
  Module[{i = Abs[ToExpression[k]] - 1},
    Nest[StringJoin[#, "*" <> x] &, x, i]]

A couple of tests for f.

f["s", 2] 
"s*s"
f["ab", "-3"]
"ab*ab*ab"

Next write a function that will use f to transform powers and will count the stars in the expression after f has done its transformation.

starCount[expr_] :=
  StringCount[
    StringReplace[
      expr // CForm // ToString, 
      "Power(" ~~ v : WordCharacter .. ~~ "," ~~ k : NumberString ~~ ")" :> f[v, k]],
    "*"]

starCount[a + s^2*b - c/y + o^3 + n*m*u]
6
starCount[1/(b s^3) + 1/t^4]
6

Note: I use CForm to recover the /s that represent division, which the OP apparently wants to preserve as an distinct operator.

I think this is easier to do by working with strings.

First write a function that will expand strings of the form "Power(x,k)" where k is an integer in "xx...x" with k - 1 ""s.

f[x_, k_] :=
  Module[{i = Abs[ToExpression[k]] - 1},
    Nest[StringJoin[#, "*" <> x] &, x, i]]

A couple of tests for f.

f["s", 2] 
"s*s"
f["ab", "-3"]
"ab*ab*ab"

Next write a function that will use f to transform powers and will count the stars in the expression after f has done its transformation.

starCount[expr_] :=
  StringCount[
    StringReplace[
      expr // CForm // ToString, 
      "Power(" ~~ v : WordCharacter .. ~~ "," ~~ k : NumberString ~~ ")" :> f[v, k]],
    "*"]

starCount[a + s^2*b - c/y + o^3 + n*m*u]
6
starCount[1/(b s^3) + 1/t^4]
6

Note: I use CForm to recover the /s that represent division, which the OP apparently wants to preserve as a distinct operator.

added 131 characters in body
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

I think this is easier to do by working with strings.

First write a function that will expand strings of the form "Power(x,k)" where k is an integer in "xx...x" with k - 1 ""s.

f[x_, k_] :=
  Module[{i = Abs[ToExpression[k]] - 1},
    Nest[StringJoin[#, "*" <> x] &, x, i]]

A couple of tests for f.

f["s", 2] 
"s*s"
f["ab", "-3"]
"ab*ab*ab"

Next write a function that will use f to transform powers and will count the stars in the expression after f has done its transformation.

starCount[expr_] :=
  StringCount[
    StringReplace[
      expr // CForm // ToString, 
      "Power(" ~~ v : WordCharacter .. ~~ "," ~~ k : NumberString ~~ ")" :> f[v, k]],
    "*"]

starCount[a + s^2*b - c/y + o^3 + n*m*u]
6
starCount[1/(b s^3) + 1/t^4]
6

Note: I use CForm to recover the /s that represent division, which the OP apparently wants to preserve as an distinct operator.

I think this is easier to do by working with strings.

First write a function that will expand strings of the form "Power(x,k)" where k is an integer in "xx...x" with k - 1 ""s.

f[x_, k_] :=
  Module[{i = Abs[ToExpression[k]] - 1},
    Nest[StringJoin[#, "*" <> x] &, x, i]]

A couple of tests for f.

f["s", 2] 
"s*s"
f["ab", "-3"]
"ab*ab*ab"

Next write a function that will use f to transform powers and will count the stars in the expression after f has done its transformation.

starCount[expr_] :=
  StringCount[
    StringReplace[
      expr // CForm // ToString, 
      "Power(" ~~ v : WordCharacter .. ~~ "," ~~ k : NumberString ~~ ")" :> f[v, k]],
    "*"]

starCount[a + s^2*b - c/y + o^3 + n*m*u]
6
starCount[1/(b s^3) + 1/t^4]
6

I think this is easier to do by working with strings.

First write a function that will expand strings of the form "Power(x,k)" where k is an integer in "xx...x" with k - 1 ""s.

f[x_, k_] :=
  Module[{i = Abs[ToExpression[k]] - 1},
    Nest[StringJoin[#, "*" <> x] &, x, i]]

A couple of tests for f.

f["s", 2] 
"s*s"
f["ab", "-3"]
"ab*ab*ab"

Next write a function that will use f to transform powers and will count the stars in the expression after f has done its transformation.

starCount[expr_] :=
  StringCount[
    StringReplace[
      expr // CForm // ToString, 
      "Power(" ~~ v : WordCharacter .. ~~ "," ~~ k : NumberString ~~ ")" :> f[v, k]],
    "*"]

starCount[a + s^2*b - c/y + o^3 + n*m*u]
6
starCount[1/(b s^3) + 1/t^4]
6

Note: I use CForm to recover the /s that represent division, which the OP apparently wants to preserve as an distinct operator.

Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

I think this is easier to do by working with strings.

First write a function that will expand strings of the form "Power(x,k)" where k is an integer in "xx...x" with k - 1 ""s.

f[x_, k_] :=
  Module[{i = Abs[ToExpression[k]] - 1},
    Nest[StringJoin[#, "*" <> x] &, x, i]]

A couple of tests for f.

f["s", 2] 
"s*s"
f["ab", "-3"]
"ab*ab*ab"

Next write a function that will use f to transform powers and will count the stars in the expression after f has done its transformation.

starCount[expr_] :=
  StringCount[
    StringReplace[
      expr // CForm // ToString, 
      "Power(" ~~ v : WordCharacter .. ~~ "," ~~ k : NumberString ~~ ")" :> f[v, k]],
    "*"]

starCount[a + s^2*b - c/y + o^3 + n*m*u]
6
starCount[1/(b s^3) + 1/t^4]
6