Skip to content

[stdlib_linalg] Add zeros, ones function. #478

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

Closed
wants to merge 5 commits into from
Closed

[stdlib_linalg] Add zeros, ones function. #478

wants to merge 5 commits into from

Conversation

zoziha
Copy link
Contributor

@zoziha zoziha commented Jul 30, 2021

  • add zeros function;
  • add ones function;

add expand function. (the name ex needs to discussed) (aready to expand) (discard!😏)

More routines to do, not this PR

see #476.

`reshape` function is not compatible with `string_type` type in some compilers
@zoziha zoziha mentioned this pull request Jul 31, 2021
2 tasks
@zoziha
Copy link
Contributor Author

zoziha commented Jul 31, 2021

The existing grammar of Fortran cannot reproduce the ones and zeros of Matlab and Python very well. I am looking for ways to adapt the Fortran grammar to write such routines, hoping that the Fortran committee will support features such as generics and look forward to changes.
We can keep such routines in the experimental version, and if we know that Fortran has a better solution (maybe it has to wait for generics or other?), correct them!

@awvwgk awvwgk added the reviewers needed This patch requires extra eyes label Jul 31, 2021
1. automatic array -> allocatable array.
2. the name: `ex` -> `expand`.
3. update stdlib_linalg.md related docs.
@zoziha zoziha changed the title [stdlib_linalg] Add zeros, ones, ex function. [stdlib_linalg] Add zeros, ones, expand function. Aug 3, 2021
@Jim-215-Fisher
Copy link
Contributor

I will review this one.

@zoziha zoziha changed the title [stdlib_linalg] Add zeros, ones, expand function. [stdlib_linalg] Add zeros, ones function. Aug 11, 2021
@@ -81,7 +81,8 @@ stdlib_io.o: \
stdlib_optval.o \
stdlib_kinds.o
stdlib_linalg.o: \
stdlib_kinds.o
stdlib_kinds.o \
stdlib_string_type.o
Copy link
Contributor Author

@zoziha zoziha Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, later I will remove this dependency stdlib_string_type.o (There is no need here, it is a wrong).

Comment on lines +7 to +8
int8, int16, int32, int64, lk, c_bool
use stdlib_string_type, only: string_type
Copy link
Contributor Author

@zoziha zoziha Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto (lk, c_bool, string_type)

@zoziha zoziha mentioned this pull request Aug 18, 2021
3 tasks
@gareth-nx
Copy link
Contributor

Hi @zoziha

Thanks for the pull request. I just started looking at this, and think it might be useful to have more discussion about the use-cases of these routines in the context of Fortran.

To me there are a couple of situations to consider:

  1. We want to initialize an array A to 0's or 1's. In Fortran there are 2 such situations:
    • If the array size is already known then we can just write A=1 or A=0 to achieve results equivalent to zeros and ones. That is simpler than a function call, and should be easier for a compiler to optimize.
    • If the array size is unknown (allocatable), anyway the rank is known (here Fortran differs from matlab/python). In that case the alternative allocate(A(dim), source=0) is only slightly more complex than zeros or ones. But allocate has the advantage of generalising to any rank, and any value.
  2. Using the functions within a right-hand-slide statement (e.g. A = (20.0*ones(2,2) + 5.0), or whatever). In this situation we could simply write A = 20.0 + 5.0. Offhand I can't think of cases where the function call could not be straightforwardly eliminated like this -- what are examples? Another benefit of elimination is that there is less risk of users not noticing that zeros and ones always return integers (as noted in the documentation, it is easy to imagine some users not realising ones(2,2)/2 == 0 -- whereas IMO they are more likely to notice that 1/2 == 0).

This is all I could think of -- and it did make me question whether zeros and ones would be helpful for stdlib -- however I may well be missing important use cases, so would appreciate some discussion.

@zoziha
Copy link
Contributor Author

zoziha commented Aug 29, 2021

Thank you @gareth-nx and everyone who reviewed this PR , I think what you @gareth-nx said makes sense, and I agree to cancel this PR.
My original intention was:

  1. Replace allocate(array(2, 2), source=0) to array = ones(2, 2), which is simpler for stdlib_linalg (only for matrix and vector operations).
    And I may want to adapt matlab.ones and numpy.ones too much, and ignore some differences between statically compiled language and dynamic scripting language.
    I will follow the stdlib workflow and discussions about API more.
real, allocatable :: array(:, :)

array = ones(2, 2)
array = ones(3, 4)
---
allocate(array(2, 2), source=1)
deallocate(array)
allocate(array(3, 4), source=1)

Now it seems that it is not necessary. Fortran itself can handle these well, and zeros/ones have some adaptive shortcomings.

@zoziha zoziha closed this Aug 30, 2021
@awvwgk awvwgk removed the reviewers needed This patch requires extra eyes label Sep 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants