Commit 352296f
authored
Avoid unneeded array traversals (#108)
## `insupport` for product measures
`insupport` currently has this method:
```julia
@inline function insupport(d::AbstractProductMeasure, x::AbstractArray)
mar = marginals(d)
for (j, mj) in enumerate(mar)
dynamic(insupport(mj, x[j])) || return false
end
return true
end
```
But we can often infer that we're in the support, so we can save the cost of traversing the array. This PR changes this to
```julia
@inline function insupport(d::AbstractProductMeasure, x::AbstractArray)
mar = marginals(d)
# We might get lucky and know statically that everything is inbounds
T = Core.Compiler.return_type(insupport, Tuple{eltype(mar),eltype(x)})
T <: True || all(zip(x, mar)) do (xj, mj)
insupport(mj, xj) == true
end
end
```
## `logdensity_def` for powers of primitive measures
Again, we're currently traversing the array unnecessarily. This PR adds these methods
```julia
logdensity_def(::PowerMeasure{P}, x) where {P<:PrimitiveMeasure} = static(0.0)
# To avoid ambiguities
function logdensity_def(
::PowerMeasure{P,Tuple{Vararg{Base.OneTo{Static.StaticInt{0}},N}}},
x,
) where {P<:PrimitiveMeasure,N}
static(0.0)
end
```
## `istrue`
Lots of code expecting a Bool doesn't work for StaticBools. In these cases, we instead need to check e.g. `p == true`. I've added `istrue(p) = p == true` to make this a little simpler1 parent fe894f7 commit 352296f
File tree
6 files changed
+29
-15
lines changed- src
- combinators
6 files changed
+29
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
93 | 96 | | |
94 | 97 | | |
95 | 98 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
215 | | - | |
216 | | - | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
217 | 219 | | |
218 | | - | |
219 | 220 | | |
220 | 221 | | |
221 | 222 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
74 | | - | |
| 73 | + | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
113 | | - | |
114 | | - | |
| 113 | + | |
| 114 | + | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
125 | | - | |
| 124 | + | |
| 125 | + | |
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
116 | 116 | | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
124 | 124 | | |
125 | | - | |
| 125 | + | |
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
0 commit comments