Message Boards Message Boards

0
|
1800 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Unexpected behavior from Distribute[ ]?

Posted 2 years ago

When using the Distribute function, there is something strange happens.

I started with an example case:

Distribute[f[g[a, b, c], g[a, b, c]], g]

The result looks fine

g[f[a, a], f[a, b], f[a, c], f[b, a], f[b, b], f[b, c], f[c, a], 
 f[c, b], f[c, c]]

However, when I replace f and g with real functions below:

Distribute[Times[List[a, b, c], List[a, b, c]], List]

The result is totally different:

{{a^2, b^2, c^2}}

But if I use a rule and replace the function in the first case like this:

Distribute[f[g[a, b, c], g[a, b, c]], g] /. {f -> Times, g -> List}

I will get the expected correct result:

{a^2, a b, a c, a b, b^2, b c, a c, b c, c^2}

Please anyone tell me if I have some misunderstanding of Distribute or this is a bug.

Thanks

POSTED BY: Fan Yang
2 Replies

Times is evaluated first before Distribute so you are evaluating

Distribute[List[a, b, c] List[a, b, c], List]

which gives

{{a^2, b^2, c^2}}

To get what you want you need to keep Times unevaluated.

Distribute[Unevaluated@Times[List[a, b, c], List[a, b, c]], List]

which gives

{a^2, a b, a c, a b, b^2, b c, a c, b c, c^2}
POSTED BY: Martijn Froeling
Posted 2 years ago

This explains my issue with Distribute[] perfectly. Thank you for your answer!

POSTED BY: Fan Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract