# numerical values of coefficients

Consider the following output:

x= (Sqrt[3/(2 (1 + Sqrt[11]/4))] b[0, π/3] - (1 + Sqrt[7/3]) b[(2 π)/3, (2 π)/3])/(4 Sqrt[2] 7^(1/4) Sqrt[1 + Sqrt[7]/4])


If i do N[x] then I get rid of the annoying expressions for the coefficients of  b[0, π/3], b[(2 π)/3, (2 π)/3] since they turn to decimal numbers but the same happens to the indices  π/3, (2 π)/3 which I don't want. This is just an example. Suppose you have many terms i.e:

a1 b[0, π/3] + a2 b[0,  π/2] + a3 b[π/12, 3 π/8] + ...


where the coefficients a1, a2,... have square roots, powers etc. How can I turn into decimals only a1, a2,... and not the indices of b

How can I turn into decimal numbers only the ugly coefficients? I am trying to avoid Expand which is time consuming (at least in my case)

• x2 = Numerator[x]/N[Denominator[x]] // Expand or x2 = x /. Sqrt[a_] :> N[Sqrt[a]] // Expand Commented Jan 9, 2021 at 17:54
• @BobHanlon please see the edited question. Also x2 = x /. Sqrt[a_] :> N[Sqrt[a]] //  takes care only of the Sqrt not the power 7^1/4
– geom
Commented Jan 9, 2021 at 18:01
• It works with the revised input. As long as there is any Sqrt with a numeric argument present, the replacement will result in a machine number. Make sure you include the Expand. Commented Jan 9, 2021 at 18:12
• Expand is very time consuming. That's what I am trying to avoid
– geom
Commented Jan 9, 2021 at 18:17
• Then edit your question to include your constraint. Commented Jan 9, 2021 at 18:19

Clear["Global*"]


Without a representative example, I cannot tell how the timing compares with using Expand

x = (Sqrt[3/(2 (1 + Sqrt[11]/4))] b[
0, π/3] - (1 + Sqrt[7/3]) b[(2 π)/3, (2 π)/3])/(4 Sqrt[
2] 7^(1/4) Sqrt[1 + Sqrt[7]/4]);

var = Cases[x, b[__], Infinity];

x2 = Total[Chop[N[CoefficientList[x, var]]] . var]

(* 0.0763535 b[0, π/3] - 0.21311 b[(2 π)/3, (2 π)/3] *)


NHoldAll is designed for this.

ClearAll[b];
SetAttributes[b, NHoldAll];
x = (Sqrt[3/(2 (1 + Sqrt[11]/4))] b[0, π/3] -
(1 + Sqrt[7/3]) b[(2 π)/3, (2 π)/3]) /
(4 Sqrt[2] 7^(1/4) Sqrt[1 + Sqrt[7]/4]);

N[x]
(*
0.0843157 (0.905566 b[0, π/3] -
2.52753 b[(2 π)/3, (2 π)/3])
*)


There are also NHoldFirst and NHoldRest.

• thanks! I will check it
– geom
Commented Jan 10, 2021 at 16:37

As simple as

Collect[x, _b, N]

• I forgot to include the output of the command. It is 0.07635345378337137*b[0, Pi/3] - 0.21311006101665048*b[(2*Pi)/3, (2*Pi)/3] Commented Jan 11, 2021 at 5:56

What worked better than Expand for x consisting of a large number of terms like the ones above is:

rules = {Sqrt[a_] :> N[Sqrt[a]], a_/b_ :> N[a/b] /; Mod[a, π] != 0,
Power[x_, y_] :> N[x^y]};


Sorry I couldn't post a specific example for x as it consists of more than 20 terms added.

• What I usually do if I can't set NHoldAll on the function (e.g. Sin[1]) is something like this: x /. {bb_b :> bb, n_?NumericQ :> N[n]}. Rules like yy_h :> yy at the beginning of the list of rules replaces any expression yy that has a head h by itself. None of the rules that follow will be applied to yy` or to its subexpressions. Commented Jan 10, 2021 at 19:57