2

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 ?

StarBucK
  • 2,164
  • 1
  • 10
  • 33

2 Answers2

2

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.

John Doty
  • 13,712
  • 1
  • 22
  • 42
1
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.

Coolwater
  • 20,257
  • 3
  • 35
  • 64