1
$\begingroup$

I am not sure if what I want to do can be done, but I thought to ask anyway.

Note: I already have in hand a workaround, I was just wondering for the power/simplicity that Mathematica may provide.

Problem: Suppose that I have in hand a polynomial in x, y, z and only products between x,, z and y, z occur, e.g.

p = Pi x*z + Zeta[9] x^2*z + 123/7 z + 1/2 x + 5 y + 9/2 y*z + 3 y^5*z + 1/Pi* z^4,

where I put some random constant coefficients in front of every term.

Now suppose, I have a homemade function that only executes "stuff" on numerical/numbers such as Zeta[3] or 19/2. If I apply that function (e.g. myF) to p (myF /@ p) then it doesn't do "the job" because it can't handle the variables x,y,z.

Is there a way when I do myF /@ p to actually get

myF /@ p = myF[Pi] x*z + myF[Zeta[9]] x^2*z + myF[123/7] z + myF[1/2] x + myF[5] y + myF[9/2] y*z + myF[3] y^5*z + myF[1/Pi]* z^4

I know that the build in function N, has attribute NHoldAll and this is how it performs a similar to the problem, job.

Comment: No I will not give you more information (!) about the function myF because I want this to be as generic as it gets. If it has a solution, it is nice to be known and applied to any kind of "home-brewed" function since I can think of a big class of functions that would a enjoy such an attribute.

Thank you!

$\endgroup$
  • $\begingroup$ Have you already seen Distribute[]? $\endgroup$ – J. M.'s technical difficulties May 6 at 9:20
  • $\begingroup$ Would you care to elaborate, possibly with a small snippet? Thank you. $\endgroup$ – hal May 6 at 9:39
  • 2
    $\begingroup$ You did not operate on the number 2 in x^2 in the second term. I suppose you mean to operate only on the numerical coefficients? Might nonnumerical coefficients arise? Are the polynomials always expanded as in the example? I suppose the coefficient zero should be mapped to zero, not myF[0]? $\endgroup$ – Michael E2 May 6 at 12:10
  • $\begingroup$ Further, what about the coeffiicient 1? $\endgroup$ – Michael E2 May 6 at 12:31
  • 1
    $\begingroup$ "I just want the function to be able to 'pick up' the constants in front of the polynomials..." -- And I was taught in school that the constant in front of x^2 was to be treated as a 1. Which way do you want it, x^2 becomes myF[1] x^2 or it remains x^2? $\endgroup$ – Michael E2 May 6 at 19:23
1
$\begingroup$

Perhaps either

Fold[#1.{x, y, z} + #2 &, 
 Function[c, If[c == 0 || c == 1, c, myF[c]], Listable]@
  Reverse@CoefficientArrays[p, {x, y, z}]]

or

FromCoefficientRules[
 Normal[myF /@ Association@CoefficientRules[p, {x, y, z}]],
 {x, y, z}]

[Update: Changed first code to leave 1 (as well as 0) alone; unsure if the comment is saying that's what's desired. Otherwise, I would appreciate an example where the above fails.]

| improve this answer | |
$\endgroup$
  • $\begingroup$ Ok I just tried it and it works. It is a neat solution, thank you :). If you are kind enough, perhaps you could provide me with some clarifications: Why the Reverse? What's its purpose? $\endgroup$ – hal May 7 at 7:51
  • 2
    $\begingroup$ @hal CoefficientArrays[] returns the coefficients as a list of arrays according to the degree, starting with the constant coefficient and ending with highest-degree terms. The polynomial is constructed from the coefficients using a multivariate form of Horner's rule starting from the highest-degree coefficients. (So Reverse puts the highest-degree coefficient first.) $\endgroup$ – Michael E2 May 7 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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