Fortran for Symbolic Integration

Hi all, I am newly fresh in learning Fortran for Mathematics purpose, I was learning Julia this one year then read the wikipedia :

It is still used for science and engineering, so I am interested to use Fortran for science. In Julia or Python there is a package called SymPy to calculate symbolic integration. I currently learning this integration by parts in calculus and want to know is Fortran able to compute the solution of this:
Capture d’écran_2023-07-04_20-26-26

I read few topics already and saw this, but have no idea how to use this.

I use GNU compiler GCC 11.2.0, able to execute simple average program from the wikipedia page


Cecil E. Leith called FORTRAN the “mother tongue of scientific computing”, adding that its replacement by any other possible language “may remain a forlorn hope”

3 Likes

I have just discovered that @certik , who is very active on this forum, was at the origin of Sympy, and that he also started the github project on Symengine. So he will surely be able to give you pertinent informations.

2 Likes

Short answer: NO

I contribute to the maxima computer algebra package. I am not aware of any significant general purpose symbolic integration routines in Fortran. Much of the early work in the area - in the period when Fortran was dominant - used lisp.

The wikipedia article List of computer algebra systems will lead you to further information, including the language(s) the systems are written in.

I should add that the recurrence relation for the integral only terminates if α is a positive integer. In the general case you need to evaluate the integral numerically. There are a large number of subroutine libraries for this class of problem, as the algorithm of choice is problem dependent.

See https://fortran-lang.discourse.group/t/numerical-integration-library/ for some clues

1 Like

Yes, I am the original author of SymPy and SymEngine. I wrote the initial Fortran wrappers to SymEngine, but don’t have time currently to make them feature complete. If you, or anybody else, wants to help out, that would be great. I think SymEngine currently cannot do integration, for that you have to use SymPy for now.

3 Likes

Thanks for this. I have heard about Maxima but never try it. There are tons of Scientific Programming Language. Hard to try all of them.

I want to help out the SymEngine, but I am just beginning to learn Fortran. Where do I start? (on SymPy, I already have a year of using it in both Julia and Python)

@FreyaGoddess awesome. You can learn Fortran and then help us figure out a good interface to SymEngine and SymPy. I can see here SymPy · Julia Packages that it’s quite easy to use SymPy from Julia, the same for symengine SymEngine · Julia Packages. Are you hoping to use SymPy and SymEngine in a similar way from Fortran?

I think we should figure it out. Matlab has symbolics too: Symbolic Math Toolbox Documentation, so we can get inspired by some of the API.

The example in symengine.f90 is the best we have so far: symengine.f90/test/test_basic.f90 at master · symengine/symengine.f90 · GitHub.

1 Like

Yes it is very easy to use SymPy in Julia, and I want to know the technical coding behind it, not just few codes, it takes longer to write FORTRAN codes but I think it will be worth more than write less codes.

In Julia, to get an implicit differentiation is like this :

using SymPy
@syms x, y, u()

ex = y/(exp(0.1*x^2))
ex1 = ex(y => u(x))

#  we differentiate both sides in x:
ex2 = diff(ex1, x)

# solve for dy/dx
dydx = diff(u(x), x)
ex3 = solve(ex2, dydx)[1]    # pull out lone answer with [1] indexing

# As this represents an answer in terms of u(x), 
# we replace that term with the original variable:
dydx₁ = ex3(u(x) => y)

If it takes longer for FORTRAN to write the similar code that resulting the same answer, it will be fine by me as long as I can comprehend.

I am on half way learning Calculus from Purcell textbook, and have a Fortran book written by Chapman. I can run a single Fortran file currently like gfortran -o example example.f90, I haven’t arrive at the stage how to link it with other Fortran code, combine with C, use CMake as well. I know I still have to learn and improve more. I used MATLAB, but Julia can easily replaced all that MATLAB can do, thus I uninstall MATLAB.

1 Like

For the subset of SymPy that SymEngine can do, the syntax here https://github.com/symengine/symengine.f90/blob/52f16d89bc4c489135d632a20544fd05475d4508/test/test_basic.f90 is actually not bad, and it’s a great starting point. The type(Basic) is implemented here: https://github.com/symengine/symengine.f90/blob/52f16d89bc4c489135d632a20544fd05475d4508/src/symengine_basic.f90#L9 and I can see that it is using a finalizer to correctly deallocate things, so I think this approach will work and not leak memory, while keeping the syntax quite natural.

We can follow the same approach to wrap SymPy into Fortran, much like Julia does it.

2 Likes

We can prolly also take inspiration from Wolfram Mathematica or Maple. Those are the two commercial suites that I’ve used in the past, thanks to licenses paid by my university back then.