Skip to content

Commit 0ce039b

Browse files
committed
Added spec and example program.
1 parent 04caad8 commit 0ce039b

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

doc/specs/stdlib_linalg.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,37 @@ Returns a rank-2 array equal to `u v^T` (where `u, v` are considered column vect
160160
{!example/linalg/example_outer_product.f90!}
161161
```
162162

163+
## `kronecker_product` - Computes the kronecker product of two rank-2 arrays
164+
165+
### Status
166+
167+
Experimental
168+
169+
### Description
170+
171+
Computes the Kronecker product of two rank-2 arrays
172+
173+
### Syntax
174+
175+
`C = [[stdlib_linalg(module):kronecker_product(interface)]](A, B)`
176+
177+
### Arguments
178+
179+
`A`: Shall be a rank-2 array with dimensions M1, N1
180+
181+
`v`: Shall be a rank-2 array with dimensions M2, N2
182+
183+
### Return value
184+
185+
Returns a rank-2 array equal to `A \otimes B`. The shape of the returned array is `[M1*M2, N1*N2]`.
186+
187+
### Example
188+
189+
```fortran
190+
{!example/linalg/example_kronecker_product.f90!}
191+
```
192+
193+
163194
## `cross_product` - Computes the cross product of two vectors
164195

165196
### Status
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
program example_kronecker_product
2+
use stdlib_linalg, only: kronecker_product
3+
implicit none
4+
integer, parameter :: m1=1, n1=2, m2=2, n2=3
5+
real :: A(m1, n1), B(m2,n2)
6+
real, allocatable :: C(:,:)
7+
8+
do j=1, n1
9+
do i=1, m1
10+
A(i,j) = i*j ! A = [1, 2]
11+
end do
12+
end do
13+
14+
do j=1, n2
15+
do i=1, m2 ! B = [ 1, 2, 3 ]
16+
B(i,j) = i*j ! [ 2, 4, 6 ]
17+
end do
18+
end do
19+
20+
C = kronecker_product(A, B)
21+
! C = [ a(1,1) * B(:,:) | a(1,2) * B(:,:) ]
22+
! or in other words,
23+
! C = [ 1.00 2.00 3.00 2.00 4.00 6.00 ]
24+
! [ 2.00 4.00 6.00 4.00 8.00 12.00 ]
25+
end program example_kronecker_product

src/stdlib_linalg_kronecker.fypp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ contains
1919

2020
do n1=1, maxN1
2121
do m1=1, maxM1
22-
! We use the numpy.kron convention for ordering of the matrix elements
22+
! We use the Wikipedia convention for ordering of the matrix elements
23+
! https://en.wikipedia.org/wiki/Kronecker_product
2324
C((m1-1)*maxM2+1:m1*maxM2, (n1-1)*maxN2+1:n1*maxN2) = A(m1, n1) * B(:,:)
2425
end do
2526
end do

0 commit comments

Comments
 (0)