File MIES_Utilities_Numeric.ipf

utility functions for numerical operations

Functions

variable NewRandomSeed()

Initializes the random number generator with a new seed between (0,1] The time base is assumed to be at least 0.1 microsecond precise, so a new seed is available every 0.1 microsecond.

Usage example for the case that one needs n non reproducible random numbers. Whenever the following code block is executed a new seed is set, resulting in a different series of numbers

Make/D/N=(n) newRandoms
NewRandomSeed() // Initialize random number series with a new seed
newRandoms[] = GetReproducibleRandom() // Get n randoms from the new series

variable GetReproducibleRandom(variable noiseGenMode = defaultValue)

Return a random value in the range (0,1] which can be used as a seed for SetRandomSeed

Return a reproducible random number depending on the RNG seed.

variable GetUniqueInteger()

Return a unique integer.

The returned values can not be used for statistical purposes as the distribution is not uniform anymore.

variable SetBit(variable var, variable bit)

Set the given bit mask in var.

variable ClearBit(variable var, variable bit)

Clear the given bit mask in var.

variable PopCount(uint64 value)

Count the number of ones in value Note: if the argument given is not an unsigned int64 then it will be implicitly converted to an uint64. This mean e.g. for a double precision argument that the maximum number of bits for positive numbers is 53 from 2^53 - 1 and for negative numbers the result is always >= 11.

Parameters:

value – will be truncated to an 64 bit unsigned integer value

std::tuple<variable, variable> MinMax(variable a, variable b)

Return the minimum and maximum of both values.

variable FindNextPower(variable a, variable p)

Find an integer x which is larger than a but the smallest possible power of p.

\( x > a \) where \( x = c^p \) holds and \( x \) is the smallest possible value.

variable FindPreviousPower(variable a, variable p)

Find an integer x which is smaller than a but the largest possible power of p.

\( x < a \) where \( x = c^p \) holds and \( x \) is the largest possible value.

variable GetAlignment(variable val)

Return the alignment of the decimal number (usually a 32bit/64bit pointer)

variable CalculateLCM(variable a, variable b)

Compute the least common multiplier of two variables.

variable RoundNumber(variable val, variable precision)

Round the given number to the given number of decimal digits.

string GenerateRFC4122UUID()

Generate a version 4 UUID according to https://tools.ietf.org/html/rfc4122.

4.4.  Algorithms for Creating a UUID from Truly Random or
      Pseudo-Random Numbers

   The version 4 UUID is meant for generating UUIDs from truly-random or
   pseudo-random numbers.

   The algorithm is as follows:

   o  Set the two most significant bits (bits 6 and 7) of the
      clock_seq_hi_and_reserved to zero and one, respectively.

   o  Set the four most significant bits (bits 12 through 15) of the
      time_hi_and_version field to the 4-bit version number from
      Section 4.1.3.

   o  Set all the other bits to randomly (or pseudo-randomly) chosen
      values.

See Section 4.5 for a discussion on random numbers.

[...]

 In the absence of explicit application or presentation protocol
 specification to the contrary, a UUID is encoded as a 128-bit object,
 as follows:

 The fields are encoded as 16 octets, with the sizes and order of the
 fields defined above, and with each field encoded with the Most
 Significant Byte first (known as network byte order).  Note that the
 field names, particularly for multiplexed fields, follow historical
 practice.

 0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 |                          time_low                             |
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 |       time_mid                |         time_hi_and_version   |
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 |clk_seq_hi_res |  clk_seq_low  |         node (0-1)            |
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 |                         node (2-5)                            |
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

[...]

4.1.3.  Version

   The version number is in the most significant 4 bits of the time
   stamp (bits 4 through 7 of the time_hi_and_version field).

   The following table lists the currently-defined versions for this
   UUID variant.

   Msb0  Msb1  Msb2  Msb3   Version  Description

    0     0     0     1        1     The time-based version
                                     specified in this document.

    0     0     1     0        2     DCE Security version, with
                                     embedded POSIX UIDs.

    0     0     1     1        3     The name-based version
                                     specified in this document
                                     that uses MD5 hashing.

    0     1     0     0        4     The randomly or pseudo-
                                     randomly generated version
                                     specified in this document.

    0     1     0     1        5     The name-based version
                                     specified in this document
                                     that uses SHA-1 hashing.

   The version is more accurately a sub-type; again, we retain the term
   for compatibility.

See also https://www.rfc-editor.org/errata/eid3546 and https://www.rfc-editor.org/errata/eid1957 for some clarifications.

std::tuple<variable, variable> SymmetrizeRangeAroundZero(variable minimum, variable maximum)

Given a range [a, b] this returns a symmetric range around zero including both elements.

variable LimitWithReplace(variable val, variable low, variable high, variable replacement)

Acts like the limit builtin but replaces values outside the valid range instead of clipping them.

variable Base64EncodeSize(variable unencodedSize)

Calculated the size of Base64 encoded data from the unencoded size.

Parameters:

unencodedSize – unencoded size

Returns:

encoded size

variable FindRightMostHighBit(uint64 value)

Find the right most high bit.

Parameters:

value – integer value in the range [0, 2^64]

Returns:

right most high bit or NaN in case nothing could be found

std::tuple<variable, variable> RoundAndDelta(variable val)

Returns the integer result and the difference of it to the original value.

std::tuple<variable, variable> CeilAndDelta(variable val)

Returns the integer result and the difference of it to the original value.

std::tuple<variable, variable> FloorAndDelta(variable val)

Returns the integer result and the difference of it to the original value.

variable IndexAfterDecimation(variable sourceIndex, variable decimationFactor)

Returns the target index closer to zero of a given source index for a decimation in the form target[] = source[round(p * decimationFactor)] Note: For a decimationFactor < 1 a point in source may be decimated to multiple points in target, thus a resulting index in target of sourceIndex + 1 may be equal to the index retrieved for sourceindex. For a decimationFactor > 1 points in source may be skipped on decimation, thus a resulting index in target of sourceIndex + 1 may increase the result by more than 1.

struct Uuid

Helper structure for GenerateRFC4122UUID()

Public Members

uint32 time_low
uint16 time_mid
uint16 time_hi_and_version
uint16 clock_seq
uint16 node0
uint16 node1
uint16 node2