2
$\begingroup$

This question already has an answer here:

I need to solve a system of equations. It would be too long to solve it exactly, so I need to approximate by numbers the coefficients.

The problem is the following :

In[51]:= N[1/3*gamma[1], 20]

Out[51]= 0.33333333333333333333 gamma[1.0000000000000000000]

As you can see, the indice 1 in gamma[1] is also approximated. But I don't want the indices to be approximated. (In fact I'm solving a linear system of equations and this behavior can be a little problematic).

How to tell mathematica to only approximate coefficients of the equations but not indices inside brackets ?

$\endgroup$

marked as duplicate by jkuczm, chuy, LCarvalho, march, MarcoB Jul 12 '17 at 22:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
$\begingroup$

Multiply by approximate unity, let it infect your exact coefficients.

1`20 1/3*gamma[1]
(* 0.33333333333333333333 gamma[1] *)

Depending on how complicated your expressions are, you might need to use Expand or other formula manipulation functions to push the approximate constant into the deep corners.

$\endgroup$
1
$\begingroup$
keep[head_] := Not[SameQ[{}, If[System`Private`HasImmediateValueQ[head], 0, {}],
           DownValues[head], UpValues[head], FormatValues[head], Attributes[head],
      Options[head], If[System`Private`HasAnyCodesQ[head], 0, {}], SubValues[head]]]

expr = 1/3*gamma[1];

pos = Position[expr, _?NumericQ];
which = And @@ Extract[expr, Append[#, 0] & /@ #, keep] & /@
           Map[Table[#[[;; i]], {i, 0, Length[#] - 1}] &, pos];
MapAt[N, expr, Pick[pos, which]]

0.33333333 gamma[1]

Probably there are some of the "values" in keep which aren't necessary in your cases. I think Attributes and Options.

$\endgroup$

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