Magnetization functions

rfbp.magnetization.arrow(m, x)[source]

Arrow operator of original code

Parameters:
  • x (Mag object) – Input variable
  • y (float) – Input variable
Returns:

m – The result of the operator

Return type:

Mag object

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>> x = np.random.uniform(low=0., high=1.)
>>> m = MagP64(np.random.uniform(low=0., high=1.))
>>>
>>> x = mag.arrow(m, x)
>>> assert isinstance(x, MagP64)
>>>
>>>
>>> y = np.random.uniform(low=0., high=1.)
>>> mag.arrow(x, y)
  ValueError('m must be MagP64 or MagT64')

Notes

Note

The computation of the arrow operator is different from MagP64 and MagT64.

  • In MagP64 the computation is equivalent to

    mtanh(x * arctanh(m)).

  • In MagT64 the computation is equivalent to

    mtanh(x * m)

rfbp.magnetization.auxmix(H, ap, am)[source]

Combine three MagT64 magnetizations

Parameters:
  • H (MagT64 object) – Input variable
  • ap (float) – Input variable
  • am (float) – Input variable
Returns:

m – The result of the mix

Return type:

MagT64 object

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>>
>>> x = np.random.uniform(low=0., high=1.)
>>> y = np.random.uniform(low=0., high=1.)
>>>
>>> mx = mag.auxmix(MagT64(0.), x, y)
>>> assert mx.mag == 0.
>>>
>>> m = MagP64(np.random.uniform(low=0., high=1.))
>>> mag.auxmix(m, y, x)
  ValueError('H must be a MagT64 magnetization type')

Notes

Note

This operation is valid only for MagT64 variables up to now

rfbp.magnetization.bar(m1, m2)[source]

Diff of magnetizations

Parameters:
  • m1 (Mag object) – Input variable
  • m2 (Mag object) – Input variable
Returns:

m – The result of the diff as (m1 - m2)/(1 - m1 * m2) clamped to [-1, 1] if MagP else m1 - m2

Return type:

Mag object

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>>
>>> m = MagP64(np.random.uniform(low=0., high=1.))
>>> n = MagP64(np.random.uniform(low=0., high=1.))
>>>
>>> mx = mag.bar(m, n)
>>> my = mag.bar(n, m)
>>> assert -1 <= mx.mag <= 1.
>>> assert -1 <= my.mag <= 1.
>>> assert np.isclose(abs(mx.mag), abs(my.mag))
rfbp.magnetization.copysign(x, y)[source]

Flip magnetization sign if necessary

Parameters:
  • x (Mag object) – Input variable to check
  • y (float) – Varibale from which copy the sign
Returns:

m – The corrected mag object

Return type:

Mag object

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>> x = np.random.uniform(low=0., high=1.)
>>> m = MagP64(np.random.uniform(low=0., high=1.))
>>>
>>> x = mag.copysign(m, x)
>>> assert x.mag == m.mag
>>>
>>> x = mag.copysign(-m, x)
>>> assert x.mag == m.mag
>>>
>>> x = mag.copysign(m, -x)
>>> assert x.mag == -m.mag
>>>
>>> x = mag.copysign(-m, -x)
>>> assert x.mag == -m.mag
rfbp.magnetization.damp(newx, oldx, l)[source]

Update magnetization

Parameters:
  • newx (Mag object) – Update magnetization value
  • oldx (Mag object) – Old magnetization value
  • l (float) – Scale factor
Returns:

m – The result of the update computed as newx * (1 - l) + oldx * l

Return type:

Mag object

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>>
>>> x = np.random.uniform(low=0., high=1.)
>>> m = MagP64(np.random.uniform(low=0., high=1.))
>>> n = MagP64(np.random.uniform(low=0., high=1.))
>>>
>>> mx = mag.damp(m, n, x)
>>> my = mag.damp(n, m, 1. - x)
>>> assert np.isclose(mx.mag, my.mag)
>>> assert np.isclose(mx.value,m y.value)
>>>
>>> mx = mag.damp(m, n, 0.)
>>> assert np.isclose(mx.mag, m.mag)
rfbp.magnetization.erfmix(H, mp, mm)[source]

Combine three magnetizations with the erf

Parameters:
  • H (Mag object) – Input variable
  • mp (float) – Input variable
  • mm (float) – Input variable
Returns:

m – The result of the mix

Return type:

Mag object

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>>
>>> x = np.random.uniform(low=0., high=1.)
>>> m = MagP64(np.random.uniform(low=0., high=1.))
>>>
>>> mx = mag.erfmix(MagP64(0.), x, x)
>>> assert np.isclose(mx.mag, 0.)
>>>
>>> mx = mag.erfmix(m, 0., 0.)
>>> assert np.isclose(mx.mag, 0.)

Notes

Note

The computation of the erfmix is different from MagP64 and MagT64.

  • In MagP64 the computation is equivalent to

    H.mag * (erf(mp) - erf(mm)) / (2. + H.mag * (erf(mp) + erf(mm)))

  • In MagT64 the computation is equivalent to

    auxmix(H, atanherf(mp), atanherf(mm))

rfbp.magnetization.exactmix(H, pp, pm)[source]

Combine exactly three magnetizations

Parameters:
  • H (Mag object) – Input variable
  • pp (Mag object) – Input variable
  • pm (Mag object) – Input variable
Returns:

m – The result of the mix

Return type:

Mag object

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>>
>>> x = np.random.uniform(low=0., high=1.)
>>> m = MagP64(np.random.uniform(low=0., high=1.))
>>>
>>> mag.exactmix(m, [x], [m])
  ValueError('Input variables must magnetizations (MagP64 or MagT64)')
>>>
>>> mx = mag.exactmix(MagP64(0.), m, m)
>>> assert np.isclose(x.mag, 0.)
>>>
>>> mx = mag.exactmix(m, m, m)
>>> assert np.isclose(mx.mag, 0.)

Notes

Note

The computation of the exactmix is different from MagP64 and MagT64.

  • In MagP64 the computation is equivalent to

    (pp.mag - pm.mag) * H.mag / (2. + (pp.mag + pm.mag) * H.mag)

  • In MagT64 the computation is equivalent to

    auxmix(H, pp.mag, pm.mag)

rfbp.magnetization.log1pxy(x, y)[source]

Compute the log1p for the combination of the magnetizations

Parameters:
  • x (Mag object) – The input variable
  • y (Mag object) – The input variable
Returns:

res – The result of the operation

Return type:

float

Notes

Note

The computation of the function is different from MagP64 and MagT64.

  • In MagP64 the computation is equivalent to

    np.log((1. + (x.mag * y.mag)) * 0.5)

  • In MagT64 the computation takes care of possible number overflows

rfbp.magnetization.logZ(u0, u)[source]
rfbp.magnetization.logmag2p(x)[source]

Log operation for magnetization objects

Parameters:x (Mag object) – Input variable
Returns:m – The result of the operation
Return type:Mag object

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>>
>>> x = mag.logmag2p(MagP64(0.))
>>> assert np.isclose(x, np.log(.5))
>>>
>>> x = mag.logmag2p(MagT64(-1.))
>>> assert np.isinf(x)
rfbp.magnetization.lr(x)[source]

log1p for magnetizations

Parameters:x (float) – Input variable
Returns:res – value computed as log1p(exp(-2*abs(x)))
Return type:float

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>> x = np.random.uniform(low=0., high=1.)
>>> assert mag.lr(x) >= 0.
rfbp.magnetization.mabs(x)[source]

Abs for magnetization objects

Parameters:x (Mag object) – Input variable
Returns:abs – The absolute value of the input
Return type:float

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>> x = np.random.uniform(low=0., high=1.)
>>> assert mag.mabs(MagP64(x)) >= 0.
>>> assert mag.mabs(MagP64(-x)) >= 0.
>>> assert mag.mabs(MagP64(-x)) == mag.mabs(MagP64(x))
>>>
>>> mag.mabs(x)
  ValueError('Incompatible type found. x must be a Mag')
rfbp.magnetization.mcrossentropy(x, y)[source]

Compute the crossentropy score for magnetization objects

Parameters:
  • x (Mag objects) – Input variable
  • y (Mag objects) – Input variable
Returns:

res – The resulting crossentropy score

Return type:

float

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import MagT64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>>
>>> x = mag.mcrossentropy(MagT64(-float('Inf')), MagT64(float('Inf')))
>>> assert np.isinf(x)
>>>
>>> x = mag.mcrossentropy(MagP64(0.), MagP64(0.))
>>> assert np.isclose(x, np.log(2))
>>>
>>> x = mag.mcrossentropy(MagP64(0.), MagP64(1.))
>>> assert np.isnan(x)
>>>
>>> x = mag.mcrossentropy(MagP64(1.), MagP64(1.))
>>> assert np.isnan(x)
>>>
>>> x = mag.mcrossentropy(MagT64(0.), MagT64(0.))
>>> y = mag.mcrossentropy(MagT64(1.), MagT64(0.))
>>> assert np.isclose(x, y)
>>>
>>> x = mag.mcrossentropy(MagT64(float('Inf')), MagT64(float('Inf')))
>>> assert np.isclose(x, 0.)
>>>
>>> x = np.random.uniform(low=0., high=1.)
>>> y = np.random.uniform(low=0., high=1.)
>>> mag.mcrossentropy(x, y)
  ValueError('Both magnetizations must be the same')

Notes

Note

The computation of the mcrossentropy is different from MagP64 and MagT64.

  • In MagP64 the computation is equivalent to

    -x.mag * np.arctanh(y.mag) - np.log1p(- y.mag**2) * .5 + np.log(2)

  • In MagT64 the computation is equivalent to

    -abs(y.mag) * (sign0(y.mag) * x.value - 1.) + lr(y.mag)

rfbp.magnetization.sign0(x)[source]

Sign operation valid also for magnetizations

Parameters:x (float) – Input variable
Returns:res – sign evaluated as 1 - 2*signbit(x)
Return type:float

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>> x = np.random.uniform(low=0., high=1.)
>>> m = MagP64(x)
>>> assert mag.sign0(x) in (-1, 1)
>>> assert mag.sign0(-x) == 1
>>> assert mag.sign0(m) in (-1, 1)
rfbp.magnetization.zero(x)[source]

Set magnetization to zero

Parameters:x (Mag object) – Input variable
Returns:
Return type:None

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>> x = np.random.uniform(low=0., high=1.)
>>> m = MagP64(3.14)
>>> mag.zero(m)
>>>
>>> assert m.mag == 0.
>>> assert m.value == 0.
>>> mag.zero(x)
  ValueError('Incompatible type found. x must be a Mag')
rfbp.magnetization.zeros(x)[source]

Fill array of magnetizations with zeros

Parameters:x (array-like) – Input array or list of Mag objects
Returns:
Return type:None

Example

>>> import numpy as np
>>> from ReplicatedFocusingBeliefPropagation import MagP64
>>> from ReplicatedFocusingBeliefPropagation import magnetization as mag
>>> x = np.random.uniform(low=0., high=1.)
>>> mags = [MagP64(_) for _ in range(10)]
>>> mag.zeros(mags)
>>> assert all((i.mag == 0 for i in mags))
>>>
>>> l = 3.14
>>> mag.zeros(3.14)
  ValueError('zeros takes an iterable object in input')
>>>
>>> l = [MagP64(3.14), x]
>>> mag.zeros(l)
  ValueError('Incompatible type found. x must be an iterable of Mags')