On Fri, 14 Nov 2014, Sven Schreiber wrote:
Hi,
I'm trying to work with matrices converted from series/lists that
contain missings, in order to vectorize and hopefully speed up code.
I'm working around the fact that misszero() doesn't work for matrices by
using the ternary operator to only apply formulas selectively to the
non-missings. (I vaguely remember a discussion about matrices and
misszero(), but don't recall the details anymore. Probably I should go
and dig it up in the archives.)
One thing I'm puzzled about is that I get "domain error" warnings about
the exp() function, even though I only apply it to the non-missings.
Example:
<hansl>
a = exp(NA) # memo: no "domain error" for the scalar case
matrix m = I(4)
matrix m2 = !m ? NA : m # create some "nan" in a matrix
print m2 # look at the nan, they're there!
matrix check = exp(m2) # "domain error" ok, expected
matrix m3 = isnan(m2) ? 0 : exp(m2) # but why here?
</hansl>
After the last line m3 doesn't contain missings/nan, and so the code
works, but then why the warning message? I could imagine that both
branches of the ternary operation are executed and then afterwards the
needed results are collected and combined, but that would seem quite
inefficient computationally, no?
What's the platform and gretl build data? Here's what I'm seeing with
current CVS on Linux:
<hansl>
a = exp(NA)
matrix m = I(4)
matrix m2 = !m ? NA : m
print m2
matrix check = exp(m2)
matrix m3 = isnan(m2) ? 0 : exp(m2)
print m3
</hansl>
<output>
Current session: 2014-11-16 10:44
? run domain.inp
./domain.inp
? a = exp(NA)
Generated scalar a = NA
? matrix m = I(4)
Generated matrix m
? matrix m2 = !m ? NA : m
Generated matrix m2
Warning: generated non-finite values
? print m2
m2 (4 x 4)
1 nan nan nan
nan 1 nan nan
nan nan 1 nan
nan nan nan 1
? matrix check = exp(m2)
Generated matrix check
Warning: generated non-finite values
? matrix m3 = isnan(m2) ? 0 : exp(m2)
Generated matrix m3
? print m3
m3 (4 x 4)
2.7183 0.0000 0.0000 0.0000
0.0000 2.7183 0.0000 0.0000
0.0000 0.0000 2.7183 0.0000
0.0000 0.0000 0.0000 2.7183
Done
</output>
I'm getting the warning "generated non-finite values" for just the two
commands that do generate non-finite values.
However, I found (and fixed) a bug in this neighbourhood: if you
wrapped the script above in a loop, NAs were getting through into
the matrix m2 rather than being converted to NaNs as per policy.
Allin