1

I have one problem with exportation results from Mathematica. Two matrices A and B have to be exported in special form. These two codes make a list of data exported from Maple. It is important that exported file opened with wordpad looks like column (File attached).

Please, just if you already checked that it is working, write me answer, thank you! You can check your answer comparing with files down.

Codes are here

Matrices A and B with code in Maple and exported file

http://www.2shared.com/file/49wW8Z0-/EXAMPLE_EXPORT_MAPLE_FINAL.html

And also I will present it here to everybody can see easy

Code 1)

A := Matrix(2, 2, {(1, 1) = (455200000000/6133413)*w(1), (1, 2) = -(1792000000000/116534847)*w(1), (2, 1) = (455200000000/6133413)*w(2), (2, 2) = -(1792000000000/116534847)*w(2)})


precision := double: writeto(`Aexport.for`):
for i from 1 to 2 do:for j from 1 to 2 do:
if  A[i,j]<>0  then codegen[fortran]([A00[i,j]=A[i,j]],optimized):
fi:od:od:writeto(terminal):

Code 2)

B := Matrix(2, 2, {(1, 1) = 6436781.609, (1, 2) = 0, (2, 1) = 0, (2, 2) = 3862068.966})

  writeto(Bexport);
    for i to 2 do 
    for j to 2 do 
     printf("%016.15E\n", B[i, j]) 
      end do:
        end do:
         writeto(terminal)
George Mills
  • 159
  • 6
  • It's a unclear what the question is. Do you need code in mathematica to do what this maple code does? – acl Dec 02 '11 at 09:57
  • Yes acl, I need a code in mathematica to this. – George Mills Dec 02 '11 at 10:07
  • [George](http://stackoverflow.com/users/1031298/george-mills), why did you not post this using your normal account[?](http://math.stackexchange.com/q/87073/954) – Simon Dec 02 '11 at 10:23
  • I dont know, my account is not working here, I can not make the post, and I dont know what is a problem with account? – George Mills Dec 02 '11 at 10:25
  • 1
    @user1077093: I noticed that your account (on one of the stackexchange sites) was suspended for a while because of the high volume of poor quality questions you asked. (Something that hasn't improved, based on the poor formatting on [your recent question](http://stackoverflow.com/questions/8354874)). But your account looks fine now - you should try to use it again. – Simon Dec 02 '11 at 10:50

2 Answers2

1

This is a translation of the (B) part only:

matrix = {{6436781.609, 0}, {0, 3862068.966}}

Export["Bexport", Map[FortranForm, N@Flatten[matrix]], "Table"]

Please test it and let me know if it works for you.

Differences compared to the Maple version: the E is written as lowercase and the number of digits that are output is not fixed (but, as you can see, all significant digits are preserved). Will these differences cause problems in your application?

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • @Szabolics It is printed like row, and Maple printed like column when the file is opened with wordpad and this for matrix B, but how to print A also? – George Mills Dec 02 '11 at 10:36
  • @user1077093 I get a column on my system with Mathematica 8. But you could try `Export["Bexport", List/@Map[FortranForm, N@Flatten[matrix]], "Table"]` to enforce a column if you have a different version. I don't see a simple solution for (A) as Mathematica only has builtin functions for translating expressions (not [statements](http://en.wikipedia.org/wiki/Statement_%28programming%29)) to Fortran form. So the details need to be implemented by hand in Mathematica, which takes a lot of time. Maybe someone else will do it. – Szabolcs Dec 02 '11 at 10:56
  • @Szabolcs, it's doable, just not as an automatic process. Can you give me a quick overview of what his Maple code is doing? Specifically what is `codegen` supposed to do? Post it in the chat room, if need be. – rcollyer Dec 02 '11 at 14:03
  • @rcollyer Here is a detail information about function http://www.maplesoft.com/support/help/Maple/view.aspx?path=codegen – George Mills Dec 02 '11 at 16:46
  • I applied it in form and it is printed in wordpad like row, not column, I dont know why matrix = {{6436781.609, 0}, {0, 3862068.966}}; a = Map[FortranForm, N@Flatten[matrix]]; a >> "C:\\test1.txt" – George Mills Dec 02 '11 at 17:00
  • @rcollyer the problem for matrix A is more or less the same. I am not good in programming but maybe you can compare the printed wordpad doc what the maple code did. So Mr. Wizard gave the better code for matrix B. Maybe we can together do something. – George Mills Dec 04 '11 at 12:15
  • @GeorgeMills, I did not get a chance to look at it this weekend. I'm moving in two weeks, and it is unlikely I'll get a chance to look at it until after Christmas. If I get to it before then, I'll definitely post it, but my schedule is a little tight. – rcollyer Dec 05 '11 at 15:34
  • maybe this Mr Wizard code can help b = {{6436781.609, 0}, {0, 3862068.966}} bformatted = NumberForm[ Flatten@b, {16, 15}, NumberFormat -> (Row[{#, "E+", StringTake["00" <> #3, -2]}] &) ]; bstring = StringReplace[ ToString@bformatted, {"{"|"}"|" " -> "", "," -> "\n"} ]; WriteString["Bexport.dat", bstring, "\n"] Close["Bexport.dat"] – George Mills Dec 06 '11 at 03:11
1

I believe this does what you want for matrix B:

b = {{6436781.609, 0}, {0, 3862068.966}}

bformatted = 
  NumberForm[
    Flatten@b,
    {16, 15}, 
    NumberFormat -> (Row[{#, "E+", StringTake["00" <> #3, -2]}] &)
  ];

bstring = 
  StringReplace[
    ToString@bformatted,
    {"{"|"}"|" " -> "", "," -> "\n"}
  ];

WriteString["Bexport.dat", bstring, "\n"]

Close["Bexport.dat"]
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • Thank you Mr. Wizard, I will try. But where the mathematica saves this Bexport.dat? And what about matrix A? Is it possible to make similar or something new is needed? – George Mills Dec 02 '11 at 23:48
  • @George, it is saved to the path shown by evaluating `Directory[]`. You can set this with `SetDirectory`. You could also use a full path name, but be aware that you need to "escape" the backslash like this: `"C:\\Data\\Mathematica\\project1\\Bexport.dat"` – Mr.Wizard Dec 02 '11 at 23:58
  • I didn't respond to Matrix A because I wasn't sure what you are doing there. I'll take another look at it a bit later. – Mr.Wizard Dec 02 '11 at 23:59
  • Wizard you can see the exported matrix A ready for fortran calculating in column form also. Here http://www.2shared.com/file/49wW8Z0-/EXAMPLE_EXPORT_MAPLE_FINAL.html – George Mills Dec 03 '11 at 15:04