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

Kronecker Product & Sum #699

Closed
adenchfi opened this issue Mar 3, 2023 · 3 comments · Fixed by #700
Closed

Kronecker Product & Sum #699

adenchfi opened this issue Mar 3, 2023 · 3 comments · Fixed by #700
Labels
idea Proposition of an idea and opening an issue to discuss it

Comments

@adenchfi
Copy link
Contributor

adenchfi commented Mar 3, 2023

Motivation

Implement the Kronecker product (sometimes known as tensor or direct product) for matrices. The equivalent functionality in numpy is numpy.kron.

Additionally, implement the Kronecker sum, A (kron) I_B + I_A (kron) B, which is one way someone would do it when say creating a N-dimensional Laplacian stencil out of 1D stencils.

The numpy.kron convention for ordering matrix elements would be used.

To discuss:

  • Should the user allocate the resulting array beforehand, or just pass in an allocatable array to be allocated?
    • The former allows for a more performant routine (/function), but the latter means the user doesn't have to figure out the final array sizes themselves.

Prior Art

No response

Additional Information

numpy.kron

@adenchfi adenchfi added the idea Proposition of an idea and opening an issue to discuss it label Mar 3, 2023
@jvdp1
Copy link
Member

jvdp1 commented Mar 3, 2023

Thank you @adenchfi for the proposition. I support such an implementation, as it would have been useful for my work a few weeks ago ;)

Re: the resulting array, the default in stdlib is that the resulting array is not allocatable, if the dimensions can be easily derived from the arguments. See for example the diag function.
Even in this case the user doesn't have to figure out the final dimention. E.g. the following should work without an explicit allocation:

integer :: array(5,5)
integer, allocatable :: d(:)
...
d = diag(array)
...

@adenchfi
Copy link
Contributor Author

adenchfi commented Mar 3, 2023

Glad to hear the support @jvdp1. I took your feedback and made an appropriate implementation, using the outer_product as a template. I am working on making a PR now; I am trying to figure out all I need to add for the testing suite.

@tehami02
Copy link

tehami02 commented Mar 6, 2023

@adenchfi @jvdp1 I think it is better for the user to allocate the resulting array beforehand for performance reasons. This allows the function to use memory more efficiently and reduces the overhead associated with dynamic memory allocation.
However, providing the option for the function to allocate the array itself can be useful in situations where the user may not know the final array size or wants to avoid the complexity of calculating it. In such cases, the function can allocate the array and return it to the user. This approach can be more convenient for the user, but may be less efficient.

@jvdp1 jvdp1 closed this as completed in #700 Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea Proposition of an idea and opening an issue to discuss it
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants