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

Outer product #432

Merged
merged 16 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions doc/specs/stdlib_linalg.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,41 @@ program demo_trace
print *, trace(A) ! 1 + 5 + 9
end program demo_trace
```

## `outer_product` - Computes the outer product of two vectors

### Status

Experimental

### Description

Computes the outer product of two vectors
ghbrown marked this conversation as resolved.
Show resolved Hide resolved

### Syntax

`d = [[stdlib_linalg(module):outer_product(interface)]](u, v)`

### Arguments

`u`: Shall be a rank-1 array

`v`: Shall be a rank-1 array

### Return value

Returns a rank-2 array equal to `u v^T` (where `u, v` are considered column vectors). The shape of the returned array is `[size(u), size(v)]`.

### Example

```fortran
program demo_outer_product
use stdlib_linalg, only: outer_product
implicit none
real, allocatable :: A(:,:), u(:), v(:)
u = [1., 2., 3. ]
v = [3., 4.]
A = outer_product(u,v)
!A = reshape([3., 6., 9., 4., 8., 12.], [3,2])
end program demo_outer_product
```
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(fppFiles
stdlib_io.fypp
stdlib_linalg.fypp
stdlib_linalg_diag.fypp
stdlib_linalg_outer_product.fypp
stdlib_optval.fypp
stdlib_sorting.fypp
stdlib_sorting_ord_sort.fypp
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile.manual
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SRCFYPP =\
stdlib_io.fypp \
stdlib_linalg.fypp \
stdlib_linalg_diag.fypp \
stdlib_linalg_outer_product.fypp \
stdlib_optval.fypp \
stdlib_quadrature.fypp \
stdlib_quadrature_trapz.fypp \
Expand Down Expand Up @@ -128,3 +129,4 @@ stdlib_stats_distribution_PRNG.o: \
stdlib_string_type.o: stdlib_ascii.o stdlib_kinds.o
stdlib_strings.o: stdlib_ascii.o stdlib_string_type.o
stdlib_math.o: stdlib_kinds.o
stdlib_linalg_outer_product.o: stdlib_linalg.o
17 changes: 17 additions & 0 deletions src/stdlib_linalg.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module stdlib_linalg
public :: diag
public :: eye
public :: trace
public :: outer_product

interface diag
!! version: experimental
Expand Down Expand Up @@ -52,6 +53,7 @@ module stdlib_linalg
#:endfor
end interface


! Matrix trace
interface trace
!! version: experimental
Expand All @@ -63,6 +65,21 @@ module stdlib_linalg
#:endfor
end interface


! Outer product (of two vectors)
interface outer_product
!! version: experimental
!!
!! Computes the outer product of two vectors, returning a rank-2 array
!! ([Specification](../page/specs/stdlib_linalg.html#description_3))
#:for k1, t1 in RCI_KINDS_TYPES
pure module function outer_product_${t1[0]}$${k1}$(u, v) result(res)
${t1}$, intent(in) :: u(:), v(:)
${t1}$ :: res(size(u),size(v))
end function outer_product_${t1[0]}$${k1}$
#:endfor
end interface outer_product

contains

function eye(n) result(res)
Expand Down
20 changes: 20 additions & 0 deletions src/stdlib_linalg_outer_product.fypp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#:include "common.fypp"
#:set RCI_KINDS_TYPES = REAL_KINDS_TYPES + CMPLX_KINDS_TYPES + INT_KINDS_TYPES
submodule (stdlib_linalg) stdlib_linalg_outer_product

implicit none

contains

#:for k1, t1 in RCI_KINDS_TYPES
pure module function outer_product_${t1[0]}$${k1}$(u, v) result(res)
${t1}$, intent(in) :: u(:), v(:)
${t1}$ :: res(size(u),size(v))
integer :: col
do col = 1, size(v)
res(:,col) = v(col) * u
end do
end function outer_product_${t1[0]}$${k1}$
#:endfor

end submodule
Loading