-
Notifications
You must be signed in to change notification settings - Fork 2
/
utility_mb.jl
76 lines (67 loc) · 1.56 KB
/
utility_mb.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
mblock(X, listbl)
Make blocks from a matrix.
* `X` : X-data (n, p).
* `listbl` : A vector whose each component defines
the colum numbers defining a block in `X`.
The length of `listbl` is the number of blocks.
The function returns a list (vector) of blocks.
## Examples
```julia
n = 5 ; p = 10
X = rand(n, p)
listbl = [3:4, 1, [6; 8:10]]
Xbl = mblock(X, listbl)
Xbl[1]
Xbl[2]
Xbl[3]
```
"""
function mblock(X, listbl)
Q = eltype(X[1, 1])
nbl = length(listbl)
Xbl = list(Matrix{Q}, nbl)
@inbounds for i = 1:nbl
Xbl[i] = ensure_mat(X[:, listbl[i]])
end
Xbl
end
"""
fblockscal(Xbl, bscales)
fblockscal!(Xbl::Vector, bscales::Vector)
Scale multiblock X-data.
* `Xbl` : List of blocks (vector of matrices) of X-data
Typically, output of function `mblock` from (n, p) data.
* `bscales` : A vector (of length equal to the nb.
of blocks) of the scalars diving the blocks.
## Examples
```julia
n = 5 ; m = 3 ; p = 10
X = rand(n, p)
listbl = [3:4, 1, [6; 8:10]]
Xbl = mblock(X, listbl)
bscales = 10 * ones(3)
zXbl = fblockscal(Xbl, bscales) ;
@head zXbl[3]
@head Xbl[3]
fblockscal!(Xbl, bscales) ;
@head Xbl[3]
```
"""
function fblockscal(Xbl, bscales)
Q = eltype(Xbl[1][1, 1])
nbl = length(Xbl)
zXbl = list(Matrix{Q}, nbl)
@inbounds for k = 1:nbl
zXbl[k] = copy(ensure_mat(Xbl[k]))
end
fblockscal!(zXbl, bscales)
end
function fblockscal!(Xbl::Vector, bscales::Vector)
nbl = length(Xbl)
#Threads not faster
@inbounds for k = 1:nbl
Xbl[k] ./= bscales[k]
end
Xbl
end