2

I am exporting symbolic expression from Matlab to Fortran. Consider the following example,

>> syms a b c d real;

>> expr=(a+b+c)+(a+b+c)^2+(a+b+c)^3+(a+b+c)^4+(a+b+c)^5+(a+b+c)^6+(a+b+c)^7+(a+b+c)^8+(a+b+c)^9+(a+b+c)^10+(a+b+c)^11+(a+b+c)^12;

>> fortran(expr)

the output from Matlab is:

     t0 = a+b+c+(a+b+c)**2+(a+b+c)**3+(a+b+c)**4+(a+b+c)**5+(a+b+c)**6+
     &(a+b+c)**7+(a+b+c)**8+(a+b+c)**9+(a+b+c)**10+(a+b+c)**11+(a+b+c)**
     &12

This is normal for the so-called "fixed form" of Fortran, where an ampersand appears at the beginning of each line. However, the "free form" or free-format of Fortran, requires ampersands at the end of each line as well, i.e.,

     t0 = a+b+c+(a+b+c)**2+(a+b+c)**3+(a+b+c)**4+(a+b+c)**5+(a+b+c)**6+&
     &(a+b+c)**7+(a+b+c)**8+(a+b+c)**9+(a+b+c)**10+(a+b+c)**11+(a+b+c)**&
     &12

This is obviously annoying when using really big expressions, since one would have either to put manually every ampersand at the end of each line or make a shell script to do it. Is there any output of Matlab that exports the expressions in the latest format that I showed?

uom0
  • 383
  • 2
  • 12
  • I think it does not have to be a bad question, but it is qood to clarify it to avoid the downvotes. I assume you are searching a way how to export free source form Fortran from MATLAB instead of fixed form? It is also good to show how you are doing the export and an example of the code with the ampersands. – Vladimir F Героям слава Mar 29 '17 at 15:16
  • I assume you need free-form (.f90), because the rest of the source is free-form, right? – Vladimir F Героям слава Mar 29 '17 at 15:21
  • Thank you for your answer Vladimir. I try to export the following symbolic line: >> ans =((D33 - 1)^2*(E11^2*+ 2*... By doing fortran(expr), I get, >> ((D33-1.0D0)...*2- &E11*E2..**2*E2 &**2*n..E22**2*E I.e., the ampersands are at the beginning of each line, instead of being in both, at the beginning and at the end. I ignore to which format it belongs to, since I'm not a FORTRAN programmer per-se, but I'm using this to incorporate in a constitutive behaviour in a fem routine... Hope it is clear and would be appreciated if you have any suggestion. Best!. – uom0 Mar 29 '17 at 18:07
  • Please edit the question, it is impossible to see what you mean in the comment. – Vladimir F Героям слава Mar 30 '17 at 15:40
  • Thank you Vladimir. I have already edited the question. – uom0 Apr 03 '17 at 12:52
  • I have just polished it a bit. Please also check if the columns in the two first code block is correct. Isn't the `t0` farther on the first line? – Vladimir F Героям слава Apr 03 '17 at 12:59
  • Yes, thank you. I guess was a copy-paste thing. Looking forward for an answer. Best!. – uom0 Apr 03 '17 at 13:06

1 Answers1

2

the ampersand on the continued line is not needed in freeform. You can fix this with a simple string replacement:

 ee=(a+b+c)+(a+b+c)^2+(a+b+c)^3+(a+b+c)^4+(a+b+c)^5+(a+b+c)^6+(a+b+c)^7+(a+b+c)^8+(a+b+c)^9+(a+b+c)^10+(a+b+c)^11+(a+b+c)^12;
 fstring=fortran(ee)
       t0 = a+b+c+(a+b+c)**2+(a+b+c)**3+(a+b+c)**4+(a+b+c)**5+(a+b+c)**6+
     +(a+b+c)**7+(a+b+c)**8+(a+b+c)**9+(a+b+c)**10+(a+b+c)**11+(a+b+c)**
     +12
 strrep(fstring,[char(10) '     +'],['  &' char(10)])
       t0 = a+b+c+(a+b+c)**2+(a+b+c)**3+(a+b+c)**4+(a+b+c)**5+(a+b+c)**6+  &
(a+b+c)**7+(a+b+c)**8+(a+b+c)**9+(a+b+c)**10+(a+b+c)**11+(a+b+c)**  &
12 

notice my matlab used a '+' for the fixed-form continuation marker. You must use an ampersand at the end for free form.

You could also just get rid of the continuation:

strrep(fstring,[char(10) '     +'],'')
  t0 = a+b+c+(a+b+c)**2+(a+b+c)**3+(a+b+c)**4+(a+b+c)**5+(a+b+c)**6+(a+b+c)**7+(a+b+c)**8+(a+b+c)**9+(a+b+c)**10+(a+b+c)**11+(a+b+c)**12

for a reasonable amount of code I'd do that and then manually insert line breaks as needed. (standard free form has a 130 char/line limit )

agentp
  • 6,849
  • 2
  • 19
  • 37
  • Thank you for your reply and for the suggestion, but in that case, I think is way simpler to do a batch file (MSDOS see http://stackoverflow.com/questions/10021464/batch-file-to-add-characters-to-beginning-and-end-of-each-line-in-txt-file) or a shell script (UNIX, LINUX, MAC see http://stackoverflow.com/questions/12455116/how-to-add-a-character-at-the-end-of-each-line-with-awk) to add such ampersands at the end of each line. I was hoping that Matlab had such an option to do this work directly. Thanks!. – uom0 Apr 05 '17 at 06:51
  • you don't want one at the end of *every* line, only the continued lines. – agentp Apr 05 '17 at 11:59