matr2 = Table[FortranForm[matr[[i, j]]], {i, 4}, {j, 6}];

matr3 = ExportString[
    "matrix(" <> ToString[i] <> "," <> ToString[j] <> ") = " <> 
     ToString[matr2[[i, j]]], {i, Length[matr2]}, {j, 
     Length[matr2[[1]]]}], "Table"]


I have used the above code to Print out the values from a matrix in a Fortran-like form. The problem is I want to paste the output into a fortran source file, so I need the integer values to appear with ".d0" notation. Can anyone help me with this?

  • $\begingroup$ I am not quite sure that I have got your question right. However, if one multiplies all his data (that is, figures, matrices, variables) by 1.0 , all integer numbers automatically transform into reals, while the rest do not change. May be, this will help? $\endgroup$ Nov 12 '14 at 8:49
  • $\begingroup$ Hii Alexei Boulbitch, Thanks for your help. My question is, I want all integers to be transformed into integer.d0 which is Notation for double precision in Fortran. Any help regarding this?? $\endgroup$
    – Amandeep
    Nov 12 '14 at 10:57
  • $\begingroup$ how is this different mathematica.stackexchange.com/questions/65328/… $\endgroup$
    – george2079
    Nov 12 '14 at 12:48
  • $\begingroup$ @george, that solution given by you is not working for me. $\endgroup$
    – Amandeep
    Nov 12 '14 at 12:57
  • 1
    $\begingroup$ @Amandeep I do not understand. Where do you want to get integer.d0 ? Not in Mma, I guess, since there integer.d0 means a scalar product of a vector integer by another vector d0. Do you mean to export data in such a form? $\endgroup$ Nov 12 '14 at 15:43

You can use NumberForm to customize the output. Something like the following should give you what you're after:

d0Form[precision_:10] := NumberForm[#, precision, 
     NumberFormat -> (If[(#3 == ""), Row[{#1, "d0", #3}], 
         Row[{#1, "d", #3}]] &)] &};

Here's a test on a small matrix:

matr = Table[RandomInteger[{0, 10^5}], {i, 2}, {j, 2}];

result = ExportString[
    "matrix(" <> ToString[i] <> "," <> ToString[j] <> ") = " <> 
     ToString@d0Form[matr[[i, j]]], {i, Length[matr]}, {j, 
     Length[matr[[1]]]}], "Table"]]


(* matrix(1,1) = 78467d0
   matrix(1,2) = 51727d0
   matrix(2,1) = 33535d0
   matrix(2,2) = 85963d0 *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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