Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linspace and logspace #17

Closed
certik opened this issue Dec 19, 2019 · 11 comments
Closed

linspace and logspace #17

certik opened this issue Dec 19, 2019 · 11 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@certik
Copy link
Member

certik commented Dec 19, 2019

The linspace API is implemented here: https://github.com/certik/fortran-utils/blob/b43bd24cd421509a5bc6d3b9c3eeae8ce856ed88/src/mesh.f90#L157

Matlab's linspace.

The logspace is similar, but I don't have it implemented yet -- historically I have used a function called meshexp, which is more general --- it allows you to change the gradation of the mesh, which the Matlab's logspace does not allow. The NumPy's logspace allows to set different base which allows to change gradation. So I think my meshexp can be implemented using NumPy's logspace. NumPy also has geomspace where you can specify the end points directly (just like in my meshexp) but it does not allow to change gradation. So I think there is room for meshexp, perhaps we should change the name somehow to be consistent with the other functions.

@ivan-pi
Copy link
Member

ivan-pi commented Dec 19, 2019

Personally, for linspace I like the extended API of the numpy linspace; ported to Fortran this is:

function linspace(start,end,num,endpoint,step) result(samples)

I have created a gist with my linspace version here.

@ivan-pi
Copy link
Member

ivan-pi commented Dec 19, 2019

A minor downside of mirroring the numpy API, is that since the step is an intent(out) argument, meaning the function can not have the pure attribute.

@certik
Copy link
Member Author

certik commented Dec 19, 2019

Have you ever used the step argument? I've never used it.

@milancurcic
Copy link
Member

Along the lines of "Let's use pure Fortran whenever we can" from #20, I suggest we aim for pure implementations whenever we can.

If we have an intent(out) parameter here, I'd argue that linspace should be a subroutine (a function shouldn't modify state elsewhere), which I'm not in favor of.

@ivan-pi
Copy link
Member

ivan-pi commented Dec 19, 2019

I have used the step argument before. A simple use case is setting up a simple finite difference method:

x = linspace(0.0_dp,1.0_dp,101,step=dx) 
! call linspace(0.0_dp,1.0_dp,101,x,dx) ! subroutine version

! ... assemble the tridiagonal matrix and rhs, dx appears in the matrix entries ... 
! ... solve system for field  u  using Thomas algorithm or gttrf and gttrs from LAPACK ...

call print_file("result.txt",x,u)

But I can agree with the pure argument of @milancurcic . If the step size is added to the function prototype, then linspace should be a subroutine.

In the end, the user can always recover the step size as follows

x = linspace(0.0_dp,1.0_dp,11)
dx = x(2)-x(1)

which I suppose is easier to remember and more intuitive than a subroutine version...

@certik
Copy link
Member Author

certik commented Dec 19, 2019

Indeed, I feel the second option:

x = linspace(0.0_dp, 1.0_dp, 11)
dx = x(2)-x(1)

is perhaps even clearer what it is doing, and it allows us to stay "pure". Matlab's linspace does not have this dx either.

@milancurcic
Copy link
Member

How would this module be called? stdlib_numerical and stdlib_experimental_numerical?

meshgrid() from #18 would belong here as well.

@certik
Copy link
Member Author

certik commented Dec 22, 2019

In fortran-utils I call it a mesh.f90. NumPy has this directly in the numpy namespace. Matlab also seems to have this directly in the built-in namespace. For us, here are some options:

  • stdlib_mesh.f90
  • stdlib_grid.f90
  • stdlib_numerical.f90
  • stdlib_space.f90
  • stdlib_mesh_utilities.f90

I feel "numerical" is too general. I would prefer something more concrete. I like "mesh" or "grid" so far the most.

jvdp1 pushed a commit to jvdp1/stdlib that referenced this issue Feb 4, 2021
@milancurcic milancurcic added enhancement New feature or request good first issue Good for newcomers labels Mar 11, 2021
@aman-godara

This comment has been minimized.

@awvwgk
Copy link
Member

awvwgk commented Sep 18, 2021

This has been implemented.

@awvwgk awvwgk closed this as completed Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

5 participants