Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an N by N (N>3) matrix generated from fortran. I would like to find determinant of that matrix using mathematica? I could do this for a 3 by 3 matrix. I can also import the fortran output file to mathematica but how to write down the data in the prescribed format : Det[{{a1, a2, a3}, {a4, a5, a6}, {a7, a8, a9}}] for large N? Writing the data one by one is a tedious job.Kindly help.

fortran output of a 5 by 5 matrix:

row1(0.600336   -0.169615    0.054516   -0.003712   -0.038923)
row2(0.637602    0.363638   -0.069266    0.006823    0.035388)
row3(-0.032774    0.876752    0.226435   -0.020353   -0.017496)
row4(-0.024340   -0.144815    0.950590    0.182733   -0.007234)
row5(0.024211    0.030092   -0.161703    0.966905    0.173151)
share|improve this question
Please add a sample Fortran output to your question. – Rolf Mertig Mar 28 '13 at 8:34
Sir, I have added a fortran output. – S.D Mar 28 '13 at 10:57
I edited your "fortran output" just a bit further, to make sure it was formatted properly, rather than wrapped. Are you saying that you have a text file in exactly that form, with possibly more rows for a larger matrix? – Mark McClure Mar 28 '13 at 11:08
do yuo have the option to fix the fortran. It would be easier if you got rid of the row labels and parenthesis.. You should change your question subject by the way this really has little to do with determinants – george2079 Mar 28 '13 at 11:50
it would actually be a bit of a challenge to coax fortran to generate that output (no lead spaces..) – george2079 Mar 28 '13 at 12:25

Suppose you call your file out.f, then a quick way could be:

     "Text"], "row"~~Shortest[__]~~"("~~
      Shortest[r__]~~")" :> r], "Table"]]


share|improve this answer

Am I positive in assuming that the output above is what you have in a text file when switching to Mathematica? Then first read the file line by line. Here output.txt is in the directory of Mathematica notebook that you saved.

s = Import[NotebookDirectory[] <> "output.txt", "Lines"]

This will then take the value parts of strings, split them into elements and convert them to numbers.

A = ToExpression[StringSplit /@ (StringTake[#, {6, -2}] & /@ s)]

Then calculate Det[A].

share|improve this answer
StringTake wouldn't work for row12 for example with two digits. This would work: StringTrim[#, {"row" ~~ DigitCharacter .. ~~ "(", ")"}] & /@ s – BoLe Mar 28 '13 at 13:23

regexp version..

Det@ (First@ImportString[StringReplace[ #, {
    RegularExpression["row\\d*\\(|\\)"] -> ""}] , "Table"] & /@
     Import["badlyformatteddata.txt", "Lines"])



or this which will strip anything before/after the parenthesis.

Det@ (First@ImportString[StringReplace[ #, {
       RegularExpression["^.*\\(|\\).*$"] -> ""}] , "Table"] & /@
          Import["badlyformatteddata.txt", "Lines"])
share|improve this answer

Your Answer


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.