ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_sampleShift::setShifted Interface Reference

Return a sample of shape (nsam), or (ndim, nsam) or (nsam, ndim) that is shifted by the specified input amount along the specified axis dim.
More...

Detailed Description

Return a sample of shape (nsam), or (ndim, nsam) or (nsam, ndim) that is shifted by the specified input amount along the specified axis dim.

Here, ndim stands for the number of dimensions (data attributes) of the input sample and nsam represents the number of data points in the sample.
If the input amount is the negative of the mean of the sample, then the returned sample will have a mean of zero.

Parameters
[in,out]sample: The input/output contiguous array of shape (nsam), (ndim, nsam), or (nsam, ndim) of,
  1. type complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128),
  2. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
containing the sample to be shifted.
[in]dim: The input scalar of type integer of default kind IK, whose value represents the dimension of the input sample containing different nsam observations:
  1. If dim = 1, the input sample is assumed to have the shape (nsam, ndim).
  2. If dim = 2, the input sample is assumed to have the shape (ndim, nsam).
(optional. It must be present if and only if the input arguments the condition rank(sample) > 1 holds.)
[in]amount: The input scalar or the contiguous vector of the same type and kind as sample, representing the amount by which the input sample must be shifted.
  1. If the input rank(sample) = 1, then amount must be a scalar.
  2. If the input rank(sample) = 2, then amount must be a vector of size ndim.
If the sample is to be shifted toward the origin to have a mean of zero, then the input amount corresponds to the negative of the current mean of the sample that is returned by procedures collectively represented by the generic interface getMean.
Note that the size of the input amount must be consistent with the size of the input sample:
  1. If the input argument dim = 1 then, size(amount) == size(sample, 2) == ndim must hold.
  2. If the input argument dim = 2 then, size(amount) == size(sample, 1) == ndim must hold.


Possible calling interfaces

call setShifted(sample(:), amount)
call setShifted(sample(:,:), dim, amount(:))
Return a sample of shape (nsam), or (ndim, nsam) or (nsam, ndim) that is shifted by the specified inp...
This module contains classes and procedures for shifting univariate or multivariate samples by arbitr...
Warning
The condition 1 <= dim .and. dim <= rank(sample) must hold for the corresponding input arguments.
The condition size(amount) == size(sample, 3 - dim) .or. rank(sample) /= 2 must hold for the corresponding input arguments.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
The pure procedure(s) documented herein become impure when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1.
By default, these procedures are pure in release build and impure in debug and testing builds.
Remarks
The procedures represented by this generic interface have the same functionality as getShifted.
However, unlike getShifted which are functions and return the shifted data as a new object, setShifted subroutines translate the input data in place, making it potentially more efficient and less memory-consuming than getShifted functions.
See also
getMean
setMean
getShifted
setShifted


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK
4 use pm_sampleMean, only: getMean
5 use pm_sampleShift, only: transHerm
8 use pm_io, only: display_type
9
10 implicit none
11
12 integer(IK) :: dim
13 type(display_type) :: disp
14 disp = display_type(file = "main.out.F90")
15
16 call disp%skip()
17 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
18 call disp%show("!Shift a 1D sample to have a zero mean.")
19 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
20 call disp%skip()
21
22 block
23 use pm_kind, only: TKG => RKS ! All kinds are supported.
24 real(TKG), allocatable :: sample(:)
25 call disp%show("sample = getLinSpace(x1 = 0., x2 = 10., count = 11_IK)")
26 sample = getLinSpace(x1 = 0., x2 = 10., count = 11_IK)
27 call disp%show("sample")
28 call disp%show( sample )
29 call disp%show("getMean(sample)")
30 call disp%show( getMean(sample) )
31 call disp%show("call setShifted(sample, -getMean(sample))")
32 call setShifted(sample, -getMean(sample))
33 call disp%show("sample")
34 call disp%show( sample )
35 call disp%show("getMean(sample)")
36 call disp%show( getMean(sample) )
37 call disp%skip()
38 end block
39
40 block
41 use pm_kind, only: TKG => RKS ! All kinds are supported.
42 complex(TKG), allocatable :: sample(:)
43 call disp%show("sample = cmplx(getLinSpace(x1 = 0., x2 = 10., count = 11_IK), -getLinSpace(x1 = 0., x2 = 10., count = 11_IK), TKG)")
44 sample = cmplx(getLinSpace(x1 = 0., x2 = 10., count = 11_IK), -getLinSpace(x1 = 0., x2 = 10., count = 11_IK), TKG)
45 call disp%show("sample")
46 call disp%show( sample )
47 call disp%show("getMean(sample)")
48 call disp%show( getMean(sample) )
49 call disp%show("call setShifted(sample, -getMean(sample))")
50 call setShifted(sample, -getMean(sample))
51 call disp%show("sample")
52 call disp%show( sample )
53 call disp%show("getMean(sample)")
54 call disp%show( getMean(sample) )
55 call disp%skip()
56 end block
57
58 call disp%skip()
59 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
60 call disp%show("!Shift a 2D sample to have a zero mean along the second dimension.")
61 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
62 call disp%skip()
63
64 block
65 use pm_kind, only: TKG => RKS ! All kinds are supported.
66 real(TKG), allocatable :: sample(:,:)
67 call disp%show("sample = reshape(getLinSpace(x1 = 1._TKG, x2 = 10._TKG, count = 10_IK), [2,5])")
68 sample = reshape(getLinSpace(x1 = 1._TKG, x2 = 10._TKG, count = 10_IK), [2,5])
69 call disp%show("sample")
70 call disp%show( sample )
71 call disp%show("dim = 2")
72 dim = 2
73 call disp%show("getMean(sample, dim)")
74 call disp%show( getMean(sample, dim) )
75 call disp%show("call setShifted(sample, dim, -getMean(sample, dim))")
76 call setShifted(sample, dim, -getMean(sample, dim))
77 call disp%show("sample")
78 call disp%show( sample )
79 call disp%show("getMean(sample, dim)")
80 call disp%show( getMean(sample, dim) )
81 call disp%skip()
82 end block
83
84 block
85 use pm_kind, only: TKG => RKS ! All kinds are supported.
86 complex(TKG), allocatable :: sample(:,:)
87 call disp%show("sample = cmplx(reshape(getLinSpace(x1 = 1._TKG, x2 = 10._TKG, count = 10_IK), [2,5]), -reshape(getLinSpace(x1 = 1._TKG, x2 = 10._TKG, count = 10_IK), [2,5]))")
88 sample = cmplx(reshape(getLinSpace(x1 = 1._TKG, x2 = 10._TKG, count = 10_IK), [2,5]), -reshape(getLinSpace(x1 = 1._TKG, x2 = 10._TKG, count = 10_IK), [2,5]))
89 call disp%show("sample")
90 call disp%show( sample )
91 call disp%show("dim = 2")
92 dim = 2
93 call disp%show("getMean(sample, dim)")
94 call disp%show( getMean(sample, dim) )
95 call disp%show("call setShifted(sample, dim, -getMean(sample, dim))")
96 call setShifted(sample, dim, -getMean(sample, dim))
97 call disp%show("sample")
98 call disp%show( sample )
99 call disp%show("getMean(sample, dim)")
100 call disp%show( getMean(sample, dim) )
101 call disp%skip()
102 end block
103
104end program example
Generate count evenly spaced points over the interval [x1, x2] if x1 < x2, or [x2,...
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
Generate and return the (weighted) mean of an input sample of nsam observations with ndim = 1 or 2 at...
This module contains procedures and generic interfaces for generating arrays with linear or logarithm...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
Definition: pm_kind.F90:567
This module contains classes and procedures for computing the first moment (i.e., the statistical mea...
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3!Shift a 1D sample to have a zero mean.
4!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6sample = getLinSpace(x1 = 0., x2 = 10., count = 11_IK)
7sample
8+0.00000000, +1.00000000, +2.00000000, +3.00000000, +4.00000000, +5.00000000, +6.00000000, +7.00000000, +8.00000000, +9.00000000, +10.0000000
9getMean(sample)
10+5.00000000
11call setShifted(sample, -getMean(sample))
12sample
13-5.00000000, -4.00000000, -3.00000000, -2.00000000, -1.00000000, +0.00000000, +1.00000000, +2.00000000, +3.00000000, +4.00000000, +5.00000000
14getMean(sample)
15+0.00000000
16
17sample = cmplx(getLinSpace(x1 = 0., x2 = 10., count = 11_IK), -getLinSpace(x1 = 0., x2 = 10., count = 11_IK), TKG)
18sample
19(+0.00000000, -0.00000000), (+1.00000000, -1.00000000), (+2.00000000, -2.00000000), (+3.00000000, -3.00000000), (+4.00000000, -4.00000000), (+5.00000000, -5.00000000), (+6.00000000, -6.00000000), (+7.00000000, -7.00000000), (+8.00000000, -8.00000000), (+9.00000000, -9.00000000), (+10.0000000, -10.0000000)
20getMean(sample)
21(+5.00000000, -5.00000000)
22call setShifted(sample, -getMean(sample))
23sample
24(-5.00000000, +5.00000000), (-4.00000000, +4.00000000), (-3.00000000, +3.00000000), (-2.00000000, +2.00000000), (-1.00000000, +1.00000000), (+0.00000000, +0.00000000), (+1.00000000, -1.00000000), (+2.00000000, -2.00000000), (+3.00000000, -3.00000000), (+4.00000000, -4.00000000), (+5.00000000, -5.00000000)
25getMean(sample)
26(+0.00000000, +0.00000000)
27
28
29!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30!Shift a 2D sample to have a zero mean along the second dimension.
31!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32
33sample = reshape(getLinSpace(x1 = 1._TKG, x2 = 10._TKG, count = 10_IK), [2,5])
34sample
35+1.00000000, +3.00000000, +5.00000000, +7.00000000, +9.00000000
36+2.00000000, +4.00000000, +6.00000000, +8.00000000, +10.0000000
37dim = 2
38getMean(sample, dim)
39+5.00000000, +6.00000000
40call setShifted(sample, dim, -getMean(sample, dim))
41sample
42-4.00000000, -2.00000000, +0.00000000, +2.00000000, +4.00000000
43-4.00000000, -2.00000000, +0.00000000, +2.00000000, +4.00000000
44getMean(sample, dim)
45+0.00000000, +0.00000000
46
47sample = cmplx(reshape(getLinSpace(x1 = 1._TKG, x2 = 10._TKG, count = 10_IK), [2,5]), -reshape(getLinSpace(x1 = 1._TKG, x2 = 10._TKG, count = 10_IK), [2,5]))
48sample
49(+1.00000000, -1.00000000), (+3.00000000, -3.00000000), (+5.00000000, -5.00000000), (+7.00000000, -7.00000000), (+9.00000000, -9.00000000)
50(+2.00000000, -2.00000000), (+4.00000000, -4.00000000), (+6.00000000, -6.00000000), (+8.00000000, -8.00000000), (+10.0000000, -10.0000000)
51dim = 2
52getMean(sample, dim)
53(+5.00000000, -5.00000000), (+6.00000000, -6.00000000)
54call setShifted(sample, dim, -getMean(sample, dim))
55sample
56(-4.00000000, +4.00000000), (-2.00000000, +2.00000000), (+0.00000000, +0.00000000), (+2.00000000, -2.00000000), (+4.00000000, -4.00000000)
57(-4.00000000, +4.00000000), (-2.00000000, +2.00000000), (+0.00000000, +0.00000000), (+2.00000000, -2.00000000), (+4.00000000, -4.00000000)
58getMean(sample, dim)
59(+0.00000000, +0.00000000), (+0.00000000, +0.00000000)
60
61
Test:
test_pm_sampleShift
Todo:
Normal Priority: The functionality of this interface can be expanded to include shifting of higher dimensional input sample and whole sample input arrays of arbitrary shape, although the latter is trivial using the Fortran array syntax.


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Amir Shahmoradi, Wednesday 00:01 AM, August 25, 2021, Dallas, TX

Definition at line 1133 of file pm_sampleShift.F90.


The documentation for this interface was generated from the following file: