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
Show file tree
Hide file tree
Changes from 3 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
137 changes: 137 additions & 0 deletions doc/specs/stdlib_linalg.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,140 @@ program demo_outer_product
!A = reshape([3., 6., 9., 4., 8., 12.], [3,2])
end program demo_outer_product
```

## `zeros/ones` - Create an vector or matrix of `integer/real/complex` type, given shape and `0/1` value.

### Status

Experimental

### Class

Pure function.

### Description

`zeros/ones` creates an vector or matrix of `integer/real/complex` type, given shape and `0/1` value.

### Syntax

For vector:
`result = [[stdlib_linalg(module):zeros(interface)]](dim)`
`result = [[stdlib_linalg(module):ones(interface)]](dim)`

For matrix:
`result = [[stdlib_linalg(module):zeros(interface)]](dim1, dim2)`
`result = [[stdlib_linalg(module):ones(interface)]](dim1, dim2)`


### Arguments

`dim/dim1`: Shall be an `integer` type.
This is an `intent(in)` argument.

`dim2`: Shall be an `integer` type.
This is an `intent(in)` argument.

#### Note

Because of `huge(integer :: i) == 2147483647`, the dimensional maximum length of array created by the `zeros/ones` function is `2147483647`.

### Return value

Returns an `vector` or `matrix` of `integer` type, given shape and `0/1` value.

#### Note

If the array that receives the return value of the `zeros/ones` function is of `real/complex` type, conversion from `integer` type to `real/complex` type will occur.

### Example

```fortran
program demo
use stdlib_linalg, only: zeros, ones
implicit none
real, allocatable :: A(:,:)
integer :: iA(2)
compelx :: cA(2)

A = zeros(2,2) !! [0.0,0.0; 0.0,0.0]
A = ones(4,4) !! [1.0,1.0,1.0,1.0; 1.0,1.0,1.0,1.0; 1.0,1.0,1.0,1.0; 1.0,1.0,1.0 1.0]
A = 2.0*ones(2,2) !! [2.0,2.0; 2.0,2.0]

iA = ones(2) !! [1,1]
cA = ones(2) !! [(1.0,0.0),(1.0,0.0)]
cA = (1.0,1.0)*ones(2) !! [(1.0,1.0),(1.0,1.0)]
end program demo
```

## `expand` - Create an vector or matrix of `integer/logical/real/complex/string_type` type, given shape and given `value` value.

### Status

Experimental

### Class

Pure function.

### Description

`expand` creates an vector or matrix of `integer/logical/real/complex/string_type` type, given shape and given `value` value.

### Syntax

For vector:
`result = [[stdlib_linalg(module):expand(interface)]](value, dim)`

For matrix:
`result = [[stdlib_linalg(module):expand(interface)]](value, dim1, dim2)`

### Arguments

`value`: Shall be an `integer/logical/real/complex/string_type` scalar.
This is an `intent(in)` argument.

`dim/dim1`: Shall be an `integer` scalar.
This is an `intent(in)` argument.

`dim2`: Shall be an `integer` scalar.
This is an `intent(in)` argument.

#### Note

Because of `huge(integer :: i) == 2147483647`, the dimensional maximum length of array created by the `expand` function is `2147483647`.

### Return value

Returns an vector or matrix of `integer/logical/real/complex/string_type` type, given shape and given `value` value.

### Example

```fortran
program demo_linalg_expand_1
use stdlib_linalg, only: expand
implicit none
real, allocatable :: A(:,:)

A = expand(0,2,2) !! Same as zeros(2,2)
A = expand(1,2,1) !! Same as ones(4,4)
A = 2.0*expand(1, 2,2) !! 2.00000000 2.00000000 2.00000000 2.00000000
A = expand(1.0, 2) !! 1.00000000 1.00000000

end program demo_linalg_expand_1
```

```fortran
program demo_linalg_expand_2
use stdlib_linalg, only: expand
use stdlib_string_type
implicit none

print *, expand(1, 2) !! [1,1]
print *, expand(1.0, 2) !! [1.0,1.0]
print *, expand((1.0,1.0), 2) !! [(1.0,1.0),(1.0,1.0)]
print *, expand(.false., 2) !! [F,F]
print *, expand(string_type("A"), 2) !! ["A","A"]

end program demo_linalg_expand_2
```
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_expand.fypp
stdlib_linalg_outer_product.fypp
stdlib_optval.fypp
stdlib_sorting.fypp
Expand Down
8 changes: 7 additions & 1 deletion 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_expand.fypp \
stdlib_linalg_outer_product.fypp \
stdlib_optval.fypp \
stdlib_quadrature.fypp \
Expand Down Expand Up @@ -81,10 +82,15 @@ 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).

stdlib_linalg_diag.o: \
stdlib_linalg.o \
stdlib_kinds.o
stdlib_linalg_expand.o: \
stdlib_linalg.o \
stdlib_kinds.o \
stdlib_string_type.o
stdlib_logger.o: stdlib_ascii.o stdlib_optval.o
stdlib_optval.o: stdlib_kinds.o
stdlib_quadrature.o: stdlib_kinds.o
Expand Down
55 changes: 54 additions & 1 deletion src/stdlib_linalg.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ module stdlib_linalg
!!Provides a support for various linear algebra procedures
!! ([Specification](../page/specs/stdlib_linalg.html))
use stdlib_kinds, only: sp, dp, qp, &
int8, int16, int32, int64
int8, int16, int32, int64, lk, c_bool
use stdlib_string_type, only: string_type
Comment on lines +7 to +8
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)

implicit none
private

public :: diag
public :: eye
public :: trace
public :: outer_product
public :: zeros, ones, expand

interface diag
!! version: experimental
Expand Down Expand Up @@ -80,6 +82,57 @@ module stdlib_linalg
#:endfor
end interface outer_product

!> Version: experimental
!>
!> `ones` creates an vector or matrix of `integer` type and given shape,
!> with a `1` value.
interface ones
pure module function ones_1_default(dim) result(result)
integer, intent(in) :: dim
integer, allocatable :: result(:)
end function ones_1_default
pure module function ones_2_default(dim1, dim2) result(result)
integer, intent(in) :: dim1, dim2
integer, allocatable :: result(:, :)
end function ones_2_default
end interface ones

!> Version: experimental
!>
!> `zeros` creates an vector or matrix of `integer` type and given shape,
!> with a `0` value.
interface zeros
pure module function zeros_1_default(dim) result(result)
integer, intent(in) :: dim
integer, allocatable :: result(:)
end function zeros_1_default
pure module function zeros_2_default(dim1, dim2) result(result)
integer, intent(in) :: dim1, dim2
integer, allocatable :: result(:, :)
end function zeros_2_default
end interface zeros

!> Version: experimental
!>
!> `expand` creates an vector or matrix of `integer/logical/real/complex/string_type` type and given shape,
!> with an `value` value.
interface expand
#:set ALL_KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES + CMPLX_KINDS_TYPES &
& + LOG_KINDS_TYPES + STRING_KINDS_TYPES
#:for k1, t1 in ALL_KINDS_TYPES
pure module function expand_1_${t1[0]}$_${k1}$(value, dim) result(result)
${t1}$, intent(in) :: value
integer, intent(in) :: dim
${t1}$, allocatable :: result(:)
end function expand_1_${t1[0]}$_${k1}$
pure module function expand_2_${t1[0]}$_${k1}$(value, dim1, dim2) result(result)
${t1}$, intent(in) :: value
integer, intent(in) :: dim1, dim2
${t1}$, allocatable :: result(:, :)
end function expand_2_${t1[0]}$_${k1}$
#:endfor
end interface expand

contains

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

contains

!> `ones` creates an vector of `integer` type and `1` value.
pure module function ones_1_default(dim) result(result)
integer, intent(in) :: dim
integer, allocatable :: result(:)

allocate(result(dim))
result = 1

end function ones_1_default

!> `ones` creates a matrix of `integer` type and `1` value.
pure module function ones_2_default(dim1, dim2) result(result)
integer, intent(in) :: dim1, dim2
integer, allocatable :: result(:, :)

allocate(result(dim1, dim2))
result = 1

end function ones_2_default

!> `zeros` creates an vector of `integer` type and `0` value.
pure module function zeros_1_default(dim) result(result)
integer, intent(in) :: dim
integer, allocatable :: result(:)

allocate(result(dim))
result = 0

end function zeros_1_default

!> `zeros` creates a matrix of `integer` type and `0` value.
pure module function zeros_2_default(dim1, dim2) result(result)
integer, intent(in) :: dim1, dim2
integer, allocatable :: result(:, :)

allocate(result(dim1, dim2))
result = 0

end function zeros_2_default

#:for k1, t1 in ALL_KINDS_TYPES
!> `expand` creates an vector of `${t1}$` type and `value` value.
pure module function expand_1_${t1[0]}$_${k1}$(value, dim) result(result)

${t1}$, intent(in) :: value
integer, intent(in) :: dim
${t1}$, allocatable :: result(:)

allocate(result(dim))
result = value

end function expand_1_${t1[0]}$_${k1}$

!> `expand` creates a matrix of `${t1}$` type and `value` value.
pure module function expand_2_${t1[0]}$_${k1}$(value, dim1, dim2) result(result)

${t1}$, intent(in) :: value
integer, intent(in) :: dim1, dim2
${t1}$, allocatable :: result(:, :)

allocate(result(dim1, dim2))
result = value

end function expand_2_${t1[0]}$_${k1}$
#:endfor

end submodule stdlib_linalg_expand
1 change: 1 addition & 0 deletions src/tests/Makefile.manual
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ all test clean:
$(MAKE) -f Makefile.manual --directory=stats $@
$(MAKE) -f Makefile.manual --directory=string $@
$(MAKE) -f Makefile.manual --directory=math $@
$(MAKE) -f Makefile.manual --directory=linalg $@
1 change: 1 addition & 0 deletions src/tests/linalg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ADDTEST(linalg)
ADDTEST(linalg_expand)

4 changes: 4 additions & 0 deletions src/tests/linalg/Makefile.manual
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PROGS_SRC = test_linalg_expand.f90


include ../Makefile.manual.test.mk
Loading