0
$\begingroup$

I have a .DAT file generated by Fortran in which I have data for $2 \times 2$ matrices. For example, the file takes the form (when I open it in the excel):

enter image description here

I have added a snippet beacause the real variables are too long to type in TeX font. As you can see, each row contains 4 terms, corresponding to an entry in the matrix. Each bracket contains 2 terms because each entry is defined as a complex number; we have a complex matrix at hand.

Now there are about 1500 such entries in the file. Each entry correspond to a distinct time for which the matrix has been calculated. I want to do the following and I am not sure how to do the following steps:

For a specific time, say $t = 0$ (first entry), get the $2 \times 2$ matrix. Do some manipulations on them. The manipulations are of form: multiplying the matrix with 2 other matrices (twice) to get 2 scalar functions (one from each set of multiplication); taking the difference of the 2 scalar functions; and numerically optimizing the difference and get the optimized values of the inputs. I then want to repeat the analysis for all the other entries (matrices) in the file.

How does one go about doing such analysis in Mathematica? I'm clueless on this front. I know how to import one real matrix. I don't know how to import one complex matrix; and how to recursively import matrices from different rows and do the analysis on each matrix seperately.

If I know how to approach the problem, I can tackle it and then post additional questions with codes and related problems.

P.S: Please do suggest any other appropriate tag, if any.


Edit:

Using

Import["FILEPATH", "Table"]

here are the first few entries in the imported list:

{{( 0.70992637972979133     ,  0.0000000000000000     ), 
 ( 0.34768150998932623     ,-0.29148928933886220     ), 
  ( 0.34768150998932623     , 0.29148928933886220     ), 
   ( 0.29007362027020861     ,  0.0000000000000000     )},        {0.71578138101774014     ,  0.0000000000000000     ), (
 0.34162400932308312     ,-0.29395228624439607     ), (
    0.34162400932308312     , 0.29395228624439607     ), (
        0.28421861898225981     ,  0.0000000000000000     )}}

Fullform is:

 List[List["( 0.70992637972979133     ,  0.0000000000000000     )","(   0.347"\[Ellipsis] "0     )",""\[Ellipsis] "","( 0.29007362027020861     ,  0.0000000000000000     )"],\[LeftSkeleton]1500\[RightSkeleton]]
$\endgroup$
  • $\begingroup$ Well, you may try to import it in a real way, then use Map or Apply to create a complex matrix, then in advance uae these two functions and friends to accomplish your goal. And please supply a basic example dataset. $\endgroup$ – Wjx Aug 30 '16 at 0:37
  • $\begingroup$ @Wjx how do I upload a sample .DAT file in my post? I see no such option. $\endgroup$ – Junaid Aftab Aug 30 '16 at 0:38
  • $\begingroup$ copy your import result. $\endgroup$ – Wjx Aug 30 '16 at 0:38
  • $\begingroup$ @Wjx, I am getting extra commas when I copy the imported list. What should I do? $\endgroup$ – Junaid Aftab Aug 30 '16 at 0:57
  • $\begingroup$ @Wjx I manually removed them for a ridiculously small data set, I hope it's usable. $\endgroup$ – Junaid Aftab Aug 30 '16 at 1:00
1
$\begingroup$

You are reading Strings. So please review if Import option settings are correct. There is a whole range of formats for tabular, database and spreadsheet files. If you are sure this is as good as it gets:

input=Import["FILEPATH", "Table"];

I would first split the Strings at the round brackets:

firstpass=StringSplit[input, "(" | ")"];

Then replace the text strings with actual expressions (values). You should now have a list structure with values (check this with FullForm):

secondpass = ToExpression[firstpass];

This may be what you need, i.e. a list of 2x2 matrices; but if dimensionality is still not correct, do this:

thirdpass=Partition[Flatten[secondpass],{2,2}]

This will restructure your flat list into a series of 2x2 matrices. You may need to fiddle this a bit as I don't have the file so could not test it on your data.

So let's assume you get your list of 2x2 matrices and want to manipulate these. First I create a random matrix list with the same format:

fourthpass = RandomReal[{-1, 1}, {1000, 2, 2}];

UPDATE: The rest is speculation, as you do not specifiy what you want to do, however this may be applicable for manipulating several matrices, e.g. matrix multiplication of two subsequent matrices:

FoldList[#1 #2 &, IdentityMatrix[2], fourthpass]//Rest;

or perhaps raising the entire list to the third power

MatrixPower[#,3]&/@fourthpass;

or

myownoptimizationfunction[#]&/@fourthpass;

In general, you do not need loop constructs. The above is more straigtforward and often faster.

$\endgroup$

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.