octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #50256] Wrong data type for rand ('twister')


From: Rik
Subject: [Octave-bug-tracker] [bug #50256] Wrong data type for rand ('twister')
Date: Fri, 9 Mar 2018 12:39:42 -0500 (EST)
User-agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0

Follow-up Comment #5, bug #50256 (project octave):

This is relatively easy, replacing declarations of ColumnVector with
uint32NDArray.  See the attached patch.

I'm not completely happy with it.  The first issue is that I used a
reinterpret_cast because I couldn't make the compiler understand that
fortran_vec() should be returning a pointer to uint32 data.


uint32NDArray
octave_rand::get_internal_state (void)
{
  uint32NDArray s (dim_vector (MT_N + 1, 1));

  oct_get_state (reinterpret_cast<uint32_t *> (s.fortran_vec ()));

  return s;
}


The second issue was the set_internal_state routine.  It *should* be simple to
get rid of the OCTAVE_LOCAL_BUFFER and just use s.data() to get a constant
pointer to the data.  But I couldn't make it work.


void
octave_rand::set_internal_state (const uint32NDArray& s)
{
  octave_idx_type len = s.numel ();
  octave_idx_type n = (len < MT_N + 1 ? len : MT_N + 1);

  OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1);

  for (octave_idx_type i = 0; i < n; i++)
    tmp[i] = s.xelem (i);

  if (len == MT_N + 1 && tmp[MT_N] <= MT_N && tmp[MT_N] > 0)
    oct_set_state (tmp);
  else
    oct_init_by_array (tmp, len);
}


If things can be made to work then 4 BIST tests need updating.  One of the
good things is that this brings us much closer to Matlab.  When we were
converting doubles to uint32 we used a subroutine with this header:


-// Guarantee reproducible conversion of negative initialization values to
-// random number algorithm.  Note that Matlab employs slightly different
rules.
-// 1) Seed saturates at 2^32-1 for any value larger than that.
-// 2) NaN, Inf are translated to 2^32-1.
-// 3) -Inf is translated to 0.
-static uint32_t
-double2uint32 (double d)


The rules mentioned above are exactly those enforced by uint32() in Octave
(except for NaN) so comaptibility would be increased.



(file #43500)
    _______________________________________________________

Additional Item Attachment:

File name: rand32.patch                   Size:7 KB


    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?50256>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]