|
| 1 | +""" |
| 2 | +The `apply2list` function applies the function `fun`, the first argument, for |
| 3 | +each element on `list` but avoiding `getfield` calls inside the loop. |
| 4 | +The second argument of the macro is the `PairwiseListMatrix` that will be |
| 5 | +iterated. `fun` should take two arguments; the first is the `list` field of |
| 6 | +the `PairwiseListMatrix`, and the second is the index over that list at a |
| 7 | +given iteration. |
| 8 | +
|
| 9 | +```jldoctest |
| 10 | +julia> using PairwiseListMatrices |
| 11 | +
|
| 12 | +julia> plm = PairwiseListMatrix([1,2,3], false) |
| 13 | +3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}: |
| 14 | + 0 1 2 |
| 15 | + 1 0 3 |
| 16 | + 2 3 0 |
| 17 | +
|
| 18 | +julia> apply2list((list, k) -> println(list[k]), plm) |
| 19 | +1 |
| 20 | +2 |
| 21 | +3 |
| 22 | +
|
| 23 | +``` |
| 24 | +""" |
| 25 | +function apply2list(fun, plm) |
| 26 | + list = plm.list |
| 27 | + for k in 1:length(list) |
| 28 | + fun(list, k) |
| 29 | + end |
| 30 | +end |
| 31 | + |
| 32 | +""" |
| 33 | +The `apply2diag` function applies the function `fun`, the first argument, |
| 34 | +for each element on the `diag` field of a`PairwiseListMatrix{T,false,VT}` |
| 35 | +but avoiding `getfield` calls inside the loop. The second argument of the |
| 36 | +macro is the `PairwiseListMatrix` that will be iterated. `fun` should take |
| 37 | +two arguments; the first is the `diag` field of the `PairwiseListMatrix`, |
| 38 | +and the second is the index over that vector of diagonal elements at a |
| 39 | +given iteration. |
| 40 | +
|
| 41 | +```jldoctest |
| 42 | +julia> using PairwiseListMatrices |
| 43 | +
|
| 44 | +julia> plm = PairwiseListMatrix([1,2,3], false) |
| 45 | +3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}: |
| 46 | + 0 1 2 |
| 47 | + 1 0 3 |
| 48 | + 2 3 0 |
| 49 | +
|
| 50 | +julia>apply2diag((diag, k) -> diag[k] += 10k, plm) |
| 51 | +
|
| 52 | +julia> plm |
| 53 | +3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}: |
| 54 | + 10 1 2 |
| 55 | + 1 20 3 |
| 56 | + 2 3 30 |
| 57 | +
|
| 58 | +``` |
| 59 | +""" |
| 60 | +function apply2diag(fun, plm::PairwiseListMatrix{T,false,VT}) where {T, VT} |
| 61 | + diag = plm.diag |
| 62 | + for k in 1:length(diag) |
| 63 | + fun(diag, k) |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 67 | +""" |
| 68 | +The `apply2upper` function applies the `fun` function over the upper triangular |
| 69 | +part of the `PairwiseListMatrix`. Set `use_diag` to `true` if the diagonal |
| 70 | +needs to be included in the iteration. The function should take four |
| 71 | +arguments, first the `list` and diag fields of the `PairwiseListMatrix`, |
| 72 | +the second is the index `k` over that `list`, and the third and fourth are |
| 73 | +the `i` and `j` indexes for the position `k` in the upper triangular |
| 74 | +part of the matrix. |
| 75 | +
|
| 76 | +```jldoctest |
| 77 | +julia> using PairwiseListMatrices |
| 78 | +
|
| 79 | +julia> plm = PairwiseListMatrix([1,2,3], true) |
| 80 | +2×2 PairwiseListMatrix{Int64,true,Array{Int64,1}}: |
| 81 | + 1 2 |
| 82 | + 2 3 |
| 83 | +
|
| 84 | +julia> mat = zeros(Int, 2, 2) |
| 85 | +2×2 Array{Int64,2}: |
| 86 | + 0 0 |
| 87 | + 0 0 |
| 88 | +
|
| 89 | +julia> apply2upper((list, k, i, j) -> mat[i, j] = list[k], plm; use_diag = true) |
| 90 | +
|
| 91 | +julia> mat |
| 92 | +2×2 Array{Int64,2}: |
| 93 | + 1 2 |
| 94 | + 0 3 |
| 95 | +
|
| 96 | +``` |
| 97 | +""" |
| 98 | +function apply2upper(fun, plm::PairwiseListMatrix{T,false,VT}; use_diag::Bool=false) where {T, VT} |
| 99 | + N = plm.nelements |
| 100 | + if use_diag |
| 101 | + k = 0 |
| 102 | + diag = plm.diag |
| 103 | + list = plm.list |
| 104 | + for i in 1:N |
| 105 | + for j in i:N |
| 106 | + if i != j |
| 107 | + k += 1 |
| 108 | + fun(list, k, i, j) |
| 109 | + else |
| 110 | + let list = diag, k = i |
| 111 | + fun(list, k, i, j) |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + else |
| 117 | + k = 0 |
| 118 | + list = plm.list |
| 119 | + for i in 1:(N-1) |
| 120 | + for j in (i+1):N |
| 121 | + k += 1 |
| 122 | + fun(list, k, i, j) |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | +end |
| 127 | + |
| 128 | +function apply2upper(fun, plm::PairwiseListMatrix{T,true,VT}; use_diag::Bool=false) where {T, VT} |
| 129 | + N = plm.nelements |
| 130 | + if hasdiagonal(plm) |
| 131 | + if use_diag |
| 132 | + k = 0 |
| 133 | + list = plm.list |
| 134 | + for i in 1:N |
| 135 | + for j in i:N |
| 136 | + k += 1 |
| 137 | + fun(list, k, i, j) |
| 138 | + end |
| 139 | + end |
| 140 | + else |
| 141 | + k = 0 |
| 142 | + list = plm.list |
| 143 | + for i in 1:N |
| 144 | + for j in i:N |
| 145 | + k += 1 |
| 146 | + if i != j |
| 147 | + fun(list, k, i, j) |
| 148 | + end |
| 149 | + end |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | +end |
0 commit comments