Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have an expression as below:

Equations = 2.0799361919940695` x[1] + 3.3534325557330327` x[1]^2 - 
   4.335179297091139` x[1] x[2] + 1.1989715511881491` x[2]^2 - 
   3.766597877399148` x[1] x[3] - 0.33254815073371535` x[2] x[3] + 
   1.9050048836042945` x[3]^2 + 1.1386715715291826` x[1] x[4] + 
   2.802846492104668` x[2] x[4] - 0.6210244597295915` x[3] x[4] + 
   4.943369095158792` x[4]^2

I want to write it in an output file. So I use the below code:

removebracketvar[x_] := 
  StringReplace[
   StringReplace[
    ToString[x], {"[" -> "", "]" -> "", "," -> "", "*^" -> "e", 
     ".*" -> ".0*"}], Whitespace -> ""];
SetDirectory["C:\\folder"];
WriteString["eqfile.txt", 
  removebracketvar[
   ToString[Equations , InputForm, NumberMarks -> False]] ];
Close["eqfile.txt"]

The slight problem with the code for me is that it inserts the floating point numbers up to 16 digits of precision. I just want them to around up to 10 digits of precision. When I use SetPrecision[Equations,10], it weirdly changes x[1] etc. to x[1.0000000], etc.! I want to leave the variables as they are but want to change the floating points to less number of digits after the decimal point. What would be the best way of doing this?

share|improve this question
    
SetPrecision is not meant to be used for display purposes. You should use it only when you actually want to perform calculations at a specific precision. Otherwise, use NumberForm or something similar. –  Oleksandr R. Apr 29 '13 at 18:38
    
@OleksandrR., if I use NumberForm[Equations,6], then it also prints out 'NumberForm' in the front of the expression and '6' at the end of the expression in the output file! –  dbm Apr 29 '13 at 18:52
4  
Here is code I have used for similar purposes. resetPrecision[(a_List | a_Plus), prec_] := Map[resetPrecision[#, prec] &, a] resetPrecision[a_?NumberQ*b_., prec_] := SetPrecision[a, prec]*b resetPrecision[a_, prec_] := a –  Daniel Lichtblau Apr 29 '13 at 20:16
    
@DanielLichtblau, Excellent! That's exactly what I wanted! Thanks a lot! –  dbm Apr 29 '13 at 21:24
1  
@DanielLichtblau - please post this as an answer –  Verbeia Apr 29 '13 at 23:57
add comment

1 Answer

Here is code I have used for similar purposes.

resetPrecision[(a_List | a_Plus), prec_] :=
    Map[resetPrecision[#, prec] &, a]

resetPrecision[a_?NumberQ*b_., prec_] := SetPrecision[a, prec]*b

resetPrecision[a_, prec_] := a

That example:

equations = 
  2.0799361919940695` x[1] + 3.3534325557330327` x[1]^2 - 
   4.335179297091139` x[1] x[2] + 1.1989715511881491` x[2]^2 - 
   3.766597877399148` x[1] x[3] - 0.33254815073371535` x[2] x[3] + 
   1.9050048836042945` x[3]^2 + 1.1386715715291826` x[1] x[4] + 
   2.802846492104668` x[2] x[4] - 0.6210244597295915` x[3] x[4] + 
   4.943369095158792` x[4]^2;

resetPrecision[equations, 10]

(* Out[3]= 2.079936192 x[1] + 3.353432556 x[1]^2 - 4.335179297 x[1] x[2] + 
 1.198971551 x[2]^2 - 3.766597877 x[1] x[3] - 
 0.3325481507 x[2] x[3] + 1.905004884 x[3]^2 + 
 1.138671572 x[1] x[4] + 2.802846492 x[2] x[4] - 
 0.6210244597 x[3] x[4] + 4.943369095 x[4]^2 *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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