Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to read Symbolic equations from a file in fortran ?

  1. Aug 29, 2012 #1
    How to read Symbolic equations from a file in fortran ??

    I am working on Linux platform. I have a very big equation about two pages. I want to put that equation in a file.txt and then i want my FORTRAN program to read the equation from file.txt instead of making any function or subroutine.
    Of-course the equation contains many symbols.
    What should be the syntax of the program to read symbolic equation from a file so that i can calculate or use my equation anywhere anytime in the whole program.
    I want to say thanks for repliers and all FORTRAN users.
  2. jcsd
  3. Aug 29, 2012 #2


    User Avatar
    Science Advisor

    Re: How to read Symbolic equations from a file in fortran ??

    Hey haroonjamia and welcome to the forums.

    The thing you will need is an evaluator library or set of routines that will construct the parse tree of your expression and evaluate it.

    Depending on how complicated your expressions are, the parser and evaluator will either be really simple or really complex.

    The simplest way to implement such a scheme is to use a binary tree and turn each expression into a binary tree where each expression can be evaluated.

    If you have complex expressions that are recursive, then you will need something else. If the expressions are not recursive and your total expression is a Directed Acyclic Graph, then you can add a few lines of code to basically insert the trees to make one giant big one.

    The idea is very simple: each node has some kind of operation on it that is a binary operation. The leaves are your actual numeric and variable identitiers.

    If the tree is parsed correctly then all you do is evaluate everything from left to right evaluating every deeper branch first before you evaluate the original branch.

    If you have variable identifiers then you basically just use a table that takes a variable identifier (like "x" or "y") and for that ID you have a numeric value.

    You can add as many kinds of node classes as you want later on to add more complex things instead of basic arithmetic, exponentiation and simple evaluation.

    If you are going to evaluate complex symbolc expressions then you will need something a little more general.
  4. Aug 29, 2012 #3


    User Avatar
    Science Advisor

    Re: How to read Symbolic equations from a file in fortran ??

    What you're doing is called parsing (the program is called a parser). Compilers like fortran parse your source code (and equations) and convert them into executable code. It would generally be much easier write your formula as subroutine and allow the compiler to parse it rather than writing your own (parser).

    To write you're own parser you basically have to read your text file and and examine it character by character, scanning for white space and splitting it into tokens, which is when the real work starts. Chiro gives an overview above. It's a good project if you want to learn some of the basics of compiler design, but probably a lot of work if all you to do is evaluate a formula that Fortran could do anyway.

    Here's one that someone has already written if you want to have a look at it.
    Last edited: Aug 29, 2012
  5. Aug 30, 2012 #4
    Re: How to read Symbolic equations from a file in fortran ??

    thanks uart.
    your reply is quite useful for me, but i am not expert of fortran as you people and i dont know how to use the parser in my program. I have downloaded it from the link provided by you, but really dont know how to use it?
    Can you give me an example code using this parser with explicit commands.
    Last edited: Aug 30, 2012
  6. Aug 30, 2012 #5
    Re: How to read Symbolic equations from a file in fortran ??

    thanks chiro
    What you wrote to me is also useful, i came to know that we can build some library to peform desired operations like reading symbolic equation
    from a file. And yes my equation contain countable keyboard characters including spaces they can be counted, but it is quite difficult for me to do that, really couldnt understand the of how actually i can use parser?
    If you have any idea of any tutorial on web so that i can learn as a beginner about parser and how to use them?
    Last edited: Aug 30, 2012
  7. Aug 30, 2012 #6
    Re: How to read Symbolic equations from a file in fortran ??

    I am also of the opinion that you do not need to bother with a parser...you simply need to write your formulas in fortran...this is very easy...then include the file into your fortran source.

    Try doing what you are trying to do with a small equation first. Can you do that? If not, then, you need to read a basic fortran tutorial.
  8. Aug 31, 2012 #7
    Re: How to read Symbolic equations from a file in fortran ??

    see gasl
    let me tel you what i am doing.
    I have eight simultaneous linear eqns for my research problem in physics. and I have calculated an expression for my desired variable using Matlab, it is a symbolic expression, which fortran cant do. Now I want that expression to be used as an input in actual code I am writing in ForTran. Thing is that it is too long 3 - 4 A4 pages and if I type it in my actual code as you suggest( and offcourse I am able to do that) , it will lead to typos. Since this is my research, the expression containing several thousand characters will not ensure its correctness.
    Therefore without touching the expression, i want that to be read from a file and i heard from you experts that it is possible using parser, which I never heard, my collegus and teacher simply refused, that it cant be done in ForTran. Therefore for me the idea of using 'Parser' will be new and will help me in a way i thought for.
    If there is an example of a code using such parser for such problem, it will be greatly appreciated.
    I am not a ForTran beginner and hope i will be able to understand a bit of this new idea from you people.
  9. Aug 31, 2012 #8
    Re: How to read Symbolic equations from a file in fortran ??

    I understand that the initial symbolic manipulation can't be done by Fortran; but once that part is done with Matlab...it is no longer a symbolic problem, now, it is simply another equation....

    What does it look like?

    Can matlab print it in plain text?

    While I am sure the matlab-to-fortran translation can be done by hand, copy and paste, find and replace...I am starting to think that writing a short "parser" may not be a bad idea, simply because, presently, your equations are wrong (I already know that) and when you realize this, you are going to have to go back to matlab, tweak the equation, re-do the symbolic manipulation and convert it to fortran, again.

    So, I would probably write a very simple, specific "parser", say in python, that does what I would do by hand: Replace square brackets with parenthesis, add fortran continuation line marks here and there, etc.

    Can you post the top section of the first page?
  10. Aug 31, 2012 #9


    User Avatar
    Science Advisor

    Re: How to read Symbolic equations from a file in fortran ??

    This is in another language, but the code should be straight forward and make sense:

  11. Aug 31, 2012 #10


    User Avatar

    Staff: Mentor

    Re: How to read Symbolic equations from a file in fortran ??

    Before writing your own expression (equation) parser, I would first look for one that someone else has already written. A Google search for "Fortran expression parser" turns up some links that may be useful.
  12. Aug 31, 2012 #11


    User Avatar
    Science Advisor

    Re: How to read Symbolic equations from a file in fortran ??

    Ok I can see what you're doing now. You've used a symbolic package to invert the matrix and generate some really big and messy expressions. Yeah I can imagine it's going to be ugly!

    One thing to consider, if these are linear equations then it's not all that difficult or time consuming to simply solve them each time! Basic Gaussian elimination is only O(n^3) and 8^3 is not really that big. There are also algorithms that are only O(n^2) if you're repeating the same equations with just a different column of constants.

    Anyway to test this I just pulled up some of my old Pascal code for Gaussian elimination. I would have written this 10 years ago on a very old compiler. So I tested it (just now) and I had to do 10^5 iterations (8x9 full gaussian elim in double precision) before I could even see it "blink"!

    I measured 10^6 iterations in approximately 3 seconds. So about 3 microseconds per each 8x9 double precision Gaussian elim, and that's on a pretty outdated 2.4 Ghz single core Athlon. Honestly I don't think you are going to parse it any faster than that.
    Last edited: Aug 31, 2012
  13. Sep 3, 2012 #12
    Re: How to read Symbolic equations from a file in fortran ??

    Yes Uart, you got what i am doing.
    See to be more accurate every physicists tries to look for the methods which are more close to the exact. I also thought that i will use some elimination method but it is really good to use expression which is exact and this is not the only job my program do, my program performs integrations of such two functions and find average no(a physical quantity) in self-consistent manner( hope you know) for which it reqires indefinite no of iterations, and in each iterations it does analytic continuiation of the given function. Also, my program calculate other physical quantities which perfoms integration, and time of calculations may vary from several minutes to several days (on a Core-i3 procesor) on slightly changing values of the parameters iinvolved, time of calculation may change to several weeks.

    Any way, i pospond the idea of using eqation parser.
  14. Sep 3, 2012 #13
    Re: How to read Symbolic equations from a file in fortran ??

    Gasl, it look a bit like this
    "X2*X3*X4*X5*Y2*t^2 + X4*g^4*n1d*n1u*n2d*n2u*t^2 + U1*X3*X4*g^2*n1d^2*n2u*t^2 + U2*X2*X5*g^2*n1d*n2d^2*t^2 + U2*X1*X6*g^2*n2d^2*n2u*t^2 + 5.6 + U2*X1*Y2*g^2*n1d*n2d^2*t^2 + U2*X2*Y2*g^2*n1u*n2d^2*t^2 + U2*X1*g^3*n1d*n2d^2*n2u*t^2 + U2*X2*g^3*n1d*n1u*n2d^2*t^2 + U1*X4*g^3*n1d^2*n2d*n2u*t^2 + U2*X5*g^3*n1d*n2d^2*n2u*t^2 + U1*Y2*g^3*n1d^2*n1u*n2u*t^2 + U2*Y2*g^3*n1u*n2d^2*n2u*t^2 + U2*X1*X2*X5*X6*n2d*t^2 - U1*X2*X5*X6*Y2*n1d*t^2 - U1*X3*X4*X6*Y2*n1d*t^2 + U2*g^4*n1d*n1u*n2d^2*n2u*t^2 + X2*X3*X4*X5*g*n1d*t^2"

    And yes it can be printed by Matlab in plain text format.
    Above i have posted is not the top section but few terms in the middle and the expression contain brackets (), division operator(/) multiply(*), square(^) few more keyboard symbols.

    Thank you
  15. Sep 3, 2012 #14
    Re: How to read Symbolic equations from a file in fortran ??

    Well, right now, I am doing some plumbing work, which it is not as trivial as I though it would...and so, I don't quite have the time to code anything at the moment...not that I should...we are here to help you do it.

    In any case, I think your problem is fairly easily do-able...here is what I am thinking.

    I would read your entire equation using whatever language you are thinking of using for parsing it.
    As you parse the original string from beginning to end looking for individual terms (a term is a set of variables/constants being multiplied or divided together and being separated from other terms via parenthesis or '+' or '-' operators)
    For every i-th term
    You issue a fortran statement like: "X(i) = i-th-term", where the i needs to be an actual number, of course.
    And replace the i-th term in the string with "X(i)"
    When you reach the last term in your string, you would have issued a bunch of one-lines in fortran
    And should have a new string of your equation that looks like: "X(1)+X(2)+X(3)+..."
    Then you take your new string and print, say, 120 characters per line, possibly adding fortran continuation markers
    You can automate the rest of the beautification of it or finish it up by hand...the thing is that the typical fortran compiler only allows about 39 continuation lines, so you may have to break your new equation into two equations..but need to be careful where you break...you
    cannot break within a parenthesis...

    Or, you can probably extend the number of allowed continuation lines with another compiler flag...look into it

    I figured:

    If your equation is about 4 pages long, say 80 characters per row and 80 rows...that's 25600 characters.

    If your typical term looks like X2*X3*X4*X5*g*n1d*t^2 and it can be replaced with something like X(nnn)...it goes from about 20 characters long down to 5...that's 75% savings: 25600 characters down to 6400.

    Fortran 90 compiler can easily be flagged to extend statement all the way to the 132nd column...let's just say we want 120 characters per row to allows for continuation mark and indentation: 6400 / 120 = 54 rows...see? You need to break your final equation into two to not go beyond 39 continuation lines...which actually may be extended with another compiler flag...look into it.

    Oh, and of course, you need to replace ^ with **

  16. Sep 3, 2012 #15


    User Avatar
    Science Advisor

    Re: How to read Symbolic equations from a file in fortran ??

    I'm still thinking that evaluating the parsed string might take longer than just doing Gaussian elimination.
  17. Sep 3, 2012 #16
    Re: How to read Symbolic equations from a file in fortran ??

    yeah, me too...but he insists on phycisits practices of using the exact solution...let him go through the exercise, just to find out, at the end, that Guassian elimination yields the same result.

    you see, if I remember correctly gaussian elimination with back substituion is a direct, deterministic method...it is not an approximation, it is not iterative solution truncated at some convergence critarion...it is probably the same thing you did by hand. Take a good look at what you are doing and see if it is really different before you start some unnecessary work.
  18. Sep 3, 2012 #17


    User Avatar
    Science Advisor

    Re: How to read Symbolic equations from a file in fortran ??

    Yes, Gaussian elimination is an exact solution. It may actually result in less rounding errors than the parsed closed form solution (depending on exactly how the closed form solution was calculated). For example a brute force closed form solution using co-factor determinants for the matrix inverse will be O(n x n!) and a lot less efficient than Gaussian elimination.
  19. Sep 3, 2012 #18


    User Avatar

    Staff: Mentor

  20. Sep 4, 2012 #19
    Re: How to read Symbolic equations from a file in fortran ??

    You people are right,
    if you say Gaussian Elimnation is an exact one, okey still i am insisting on using expressions because in some limit to few variables involved, this huge expression will reduce to some physically interpretable expression which cant be predicted completely numerically. This is the only reason that physisict are fond of using expressions.
    Another thing, my equation has this structure
    (Numerator two pages long)/(Denominator two pages long)

    Lastly i am going to do something like seaching '^' and replacing by '**' and similarly i will replace all operators by ForTran like operators and i will use continuiation character and will paste whole equation in my actual code treating it like a function. Hope it will work.
    I am really thankful to all of you for giving me these ideas i will keep them so that i can try them later.
  21. Sep 4, 2012 #20
    Re: How to read Symbolic equations from a file in fortran ??

    I think it will do my job, i will try it.
    Thanks dear
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook