1
$\begingroup$

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)

$\endgroup$
5
  • 1
    $\begingroup$ x2 = Numerator[x]/N[Denominator[x]] // Expand or x2 = x /. Sqrt[a_] :> N[Sqrt[a]] // Expand $\endgroup$
    – Bob Hanlon
    Commented Jan 9, 2021 at 17:54
  • $\begingroup$ @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 $\endgroup$
    – geom
    Commented Jan 9, 2021 at 18:01
  • $\begingroup$ 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. $\endgroup$
    – Bob Hanlon
    Commented Jan 9, 2021 at 18:12
  • $\begingroup$ Expand is very time consuming. That's what I am trying to avoid $\endgroup$
    – geom
    Commented Jan 9, 2021 at 18:17
  • 2
    $\begingroup$ Then edit your question to include your constraint. $\endgroup$
    – Bob Hanlon
    Commented Jan 9, 2021 at 18:19

4 Answers 4

3
$\begingroup$
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] *)
$\endgroup$
2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ thanks! I will check it $\endgroup$
    – geom
    Commented Jan 10, 2021 at 16:37
1
$\begingroup$

As simple as

Collect[x, _b, N]
$\endgroup$
1
  • $\begingroup$ 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] $\endgroup$
    – Roma Lee
    Commented Jan 11, 2021 at 5:56
0
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$
    – Michael E2
    Commented Jan 10, 2021 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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