How to read Symbolic equations from a file in fortran ?

  • Fortran
  • Thread starter haroonjamia
  • Start date
  • Tags
    File Fortran
In summary, haroonjamia is looking for a way to read a symbolic equation from a file in FORTRAN on a Linux platform. The equation is quite long and contains many symbols, making it difficult to manually input into the code without typos. The solution suggested by other experts is to use a parser, which is a program that can read and evaluate mathematical expressions. This would allow haroonjamia to read the equation from the file and use it in their FORTRAN code without any errors. They are looking for a tutorial or example code on how to use a parser for this purpose.
  • #1
haroonjamia
9
0
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.
 
Technology news on Phys.org
  • #2


haroonjamia said:
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.

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.
 
  • #3


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.
http://sourceforge.net/projects/fparser/
 
Last edited:
  • #4


thanks uart.
your reply is quite useful for me, but i am not expert of fortran as you people and i don't know how to use the parser in my program. I have downloaded it from the link provided by you, but really don't know how to use it?
Can you give me an example code using this parser with explicit commands.
 
Last edited:
  • #5


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 couldn't 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:
  • #6


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.
 
  • #7


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 can't 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 can't 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.
 
  • #8


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?
 
  • #9


haroonjamia said:
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 couldn't 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?

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

http://www.codeproject.com/Articles/88435/Simple-Guide-to-Mathematical-Expression-Parsing
 
  • #10


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.
 
  • #11


haroonjamia said:
et 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 can't 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

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:
  • #12


uart said:
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.


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 equation parser.
 
  • #13


gsal said:
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?


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
 
  • #14


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 **

gsal
 
  • #15


I'm still thinking that evaluating the parsed string might take longer than just doing Gaussian elimination.
 
  • #16


Uart:
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.

haroonjamia:
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.
 
  • #17


gsal said:
Uart:
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.

haroonjamia:
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.

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.
 
  • #18
No way of exporting equation in a Fortran format from Matlab?

What about

http://www.mathworks.com/help/toolbox/symbolic/fortran.html

(2 minutes of googling, no, I don't use Matlab.)
 
  • #19


gsal said:
Uart:
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.

haroonjamia:
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.

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 can't 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.
 
  • #20
Borek said:
No way of exporting equation in a Fortran format from Matlab?

What about

http://www.mathworks.com/help/toolbox/symbolic/fortran.html

(2 minutes of googling, no, I don't use Matlab.)



I think it will do my job, i will try it.
Thanks dear
 
  • #21


haroonjamia said:
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 can't be predicted completely numerically. This is the only reason that physisict are fond of using expressions.

The math of the symbolic solution might tell you something interesting, but in general a "professional quality" numerical solution (e.g. from a library like LAPACK, not a Gaussian elimination routine you wrote yourseif after a first course on numerical methods!) will never give you a "worse" answers, and will often give you better ones.

As a trivial example why, consider the 2x2 system
$$\begin{bmatrix} a & b \\ c & d \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} =
\begin{bmatrix} p \\ q \end{bmatrix}$$
The symbolic solution (e.g. by Cramer's rule) is
$$\begin{align} x &= ( dp - bq) / (ad - bc) \\
y &= (-cp + aq) / (ad - bc) \end{align}$$
But if your numbers are badly scaled, for example
$$\begin{bmatrix} 10^{-200} & 10^{-200} \\ -10^{-200} & 10^{-200} \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} =
\begin{bmatrix} 10^{-200} \\ 10^{-200} \end{bmatrix}$$
A numerical method will have no problems calculating an accurate answer, but the symbolic calculation will fail with a zero divide, because 64-bit floating point arithmetic on a PC calculates that ##ad = 10^{-200} \times 10^{-200} = 0##, not ##10^{-400}##.

If similar (but less obvious) things happen when you put numbers into a symbolic solution that is several pages long, you are probably going to have a hard time figuring out what went wrong and what to do about it.
 

1. How do I open a file in fortran?

To open a file in fortran, you can use the OPEN statement followed by the file name, file access mode, and file number. For example, OPEN(unit=1, file='example.txt', status='old') will open a file named "example.txt" for reading.

2. How do I read a specific line from a file in fortran?

To read a specific line from a file in fortran, you can use the READ statement followed by the file number and the line number you want to read. For example, READ(1, 3) will read line 3 from the file with file number 1.

3. How do I read symbolic equations from a file in fortran?

To read symbolic equations from a file in fortran, you can use the READ statement with the FORMAT specifier. You will need to specify the variable names and their corresponding formats in the FORMAT statement. For example, READ(1, '(A2,I3,F5.2)') var1, var2, var3 will read a string, an integer, and a floating-point number from the file with file number 1 and store them in the variables var1, var2, and var3 respectively.

4. How do I handle errors while reading a file in fortran?

To handle errors while reading a file in fortran, you can use the IOSTAT specifier in the READ statement. This will assign a value to a variable indicating the status of the read operation. For example, READ(1,*,IOSTAT=io_stat) var1 will read a value from the file with file number 1 and assign an integer value to io_stat depending on the success or failure of the read operation.

5. How do I close a file in fortran?

To close a file in fortran, you can use the CLOSE statement followed by the file number. For example, CLOSE(1) will close the file with file number 1. It is important to close a file after reading from it to free up system resources and avoid potential errors.

Similar threads

  • Programming and Computer Science
Replies
8
Views
129
  • Programming and Computer Science
Replies
2
Views
913
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
21
Views
524
  • Programming and Computer Science
Replies
4
Views
611
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
347
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top