def bias(a, b):
return a**(_np.log(b) / _np.log(0.5))
def gain(a, b):
return _np.where(
a < 0.5,
bias(a * 2, 1 - b) / 2,
1 - bias((1 - a) * 2, 1 - b) / 2
)
op = {
OpCode.Add: lambda a, b: a + b,
OpCode.Subtract: lambda a, b: a - b,
OpCode.Multiply: lambda a, b: a * b,
OpCode.Divide: lambda a, b: a / b,
OpCode.Sin: lambda a, _: _np.sin(a),
OpCode.Cos: lambda a, _: _np.cos(a),
OpCode.Tan: lambda a, _: _np.tan(a),
OpCode.Sqrt: lambda a, _: _np.sqrt(a),
OpCode.Pow: lambda a, b: a ** b,
OpCode.Exp: lambda a, _: _np.exp(a),
OpCode.Log: lambda a, b: _np.log(a) / _np.log(b),
OpCode.Mod: lambda a, b: a % (_np.sign(a) * b),
OpCode.Abs: lambda a, _: _np.abs(a),
OpCode.Sign: lambda a, _: _np.sign(a),
OpCode.Min: lambda a, b: _np.minimum(a, b),
OpCode.Max: lambda a, b: _np.maximum(a, b),
OpCode.Clamp: lambda a, _: _np.clip(a, 0, 1),
OpCode.Ceil: lambda a, _: _np.ceil(a),
OpCode.Floor: lambda a, _: _np.floor(a),
OpCode.Round: lambda a, _: _np.round(a),
OpCode.Step: lambda a, b: (a <= b).astype(_np.float32),
OpCode.Smoothstep: lambda a, _: _np.clip(3 * a**2 - 2 * a**3, 0, 1),
OpCode.Bias: bias,
OpCode.Gain: gain,
}