Dear Matteo,
You can avoid the sum, keeping the two comparisons as follows:
sign(const mX)
{
return mX .== 0 .? 0 .: mX .> 0 .? 1 .: -1;
}
or a bit more schematically:
sign(const mX)
{
return
(mX .== 0)
.? 0
.: (mX .> 0)
.? 1
.: -1;
}
Jurgen
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
oxoxoxox 8th OxMetrics user conference 2010
oxoxoxox George Washington University, Washington DC
oxoxoxox 18-19 March 2010
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
Dr Jurgen A Doornik
University of Oxford, Nuffield College, Oxford OX1 1NF
tel. UK: +44-1865-278610 fax +44-1865-278621
http://www.doornik.com
http://www.oxmetrics.net
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
On 2010-02-22 22:19, Matteo Pelagatti wrote:
> Thanks for the prompt answer, Jaap.
>
> So, I guess that if I need the 3-value sign function the way I do it is
> the most efficient in Ox (which is actually very quick).
>
> Well, the use is mostly for non-parametric testing.
>
> Best,
>
> Matteo
>
> *Da:* The ox-users list is aimed at all Ox users
> [mailto:[log in to unmask]] *Per conto di *Vries, J.J. de (Jaap)
> *Inviato:* luned́ 22 febbraio 2010 22:28
> *A:* [log in to unmask]
> *Oggetto:* Re: sign function
>
> Dear Matteo,
>
> Your sign function is richer than the sign bit, because it can take
> three values. If you simplify it to
>
> return mX .< 0
>
> you have the sign bit (implementation dependent). No sums or
> subtractions involved, and if the compiler is efficient it just takes
> the sign bit (usually supported by the processor).
>
> Low level solutions would require taking every double separately from
> the matrix, casting it to a binary format, and selecting an individual
> bit. Makes it complicated and heavily platform dependent.
>
> The optimal answer of course depends on the question what you want to do
> with the signs.
>
> Regards,
>
> Jaap
>
> ------------------------------------------------------------------------
>
> *Van:* The ox-users list is aimed at all Ox users namens Matteo Pelagatti
> *Verzonden:* ma 22-2-2010 22:07
> *Aan:* [log in to unmask]
> *Onderwerp:* sign function
>
> Dear Oxers,
>
> when I need the sign function I usually do it this way
>
> sign(const mX)
>
> {
>
> return (mX .> 0) - (mX .< 0);
>
> }
>
> But, to me this does not appear to be very efficient, since many
> comparisons and sums are carried out by the interpreter.
>
> Is there any low level way to do the same? Something that only returns
> the sign bits of every double?
>
> Thanks and best regards to all,
>
> Matteo
>
|