Example #1
0
def convert_to_parameter(value) -> "Parameter":
    """Convert a *numerical* to a fixed parameter or return if already a parameter.

    Args:
        value ():
    """
    if isinstance(value, tf.Variable):
        return value

    # convert to Tensor if not yet
    if not isinstance(value, tf.Tensor):
        if isinstance(value, complex):
            value = ztf.to_complex(value)
        else:
            value = ztf.to_real(value)

    if value.dtype.is_complex:
        value = ComplexParameter("FIXED_autoparam_" + str(get_auto_number()),
                                 value=value)

    else:
        # value = Parameter("FIXED_autoparam_" + str(get_auto_number()), value=value, floating=False)
        independend_params = tf.get_collection("zfit_independent")
        params = get_dependents(tensor=value, candidates=independend_params)
        if params:
            value = ComposedParameter("composite_autoparam_" +
                                      str(get_auto_number()),
                                      tensor=value)
        else:
            value = Parameter("FIXED_autoparam_" + str(get_auto_number()),
                              value=value,
                              floating=False)

    # value.floating = False
    return value
Example #2
0
def convert_to_parameter(value,
                         name=None,
                         prefer_floating=False) -> "ZfitParameter":
    """Convert a *numerical* to a fixed parameter or return if already a parameter.

    Args:
        value ():
    """
    floating = False
    is_python = False
    if name is not None:
        name = str(name)

    if isinstance(
            value,
            ZfitParameter):  # TODO(Mayou36): autoconvert variable. TF 2.0?
        return value
    elif isinstance(value, tf.Variable):
        raise TypeError(
            "Currently, cannot autoconvert tf.Variable to zfit.Parameter.")

    # convert to Tensor if not yet
    if not isinstance(value, tf.Tensor):
        is_python = True
        if isinstance(value, complex):
            value = ztf.to_complex(value)
        else:
            floating = prefer_floating
            value = ztf.to_real(value)

    if not run._enable_parameter_autoconversion:
        return value

    if value.dtype.is_complex:
        if name is None:
            name = "FIXED_complex_autoparam_" + str(get_auto_number())
        value = ComplexParameter(name, value=value, floating=False)

    else:
        # value = Parameter("FIXED_autoparam_" + str(get_auto_number()), value=value, floating=False)
        if is_python:
            params = {}
        else:
            independend_params = tf.get_collection("zfit_independent")
            params = get_dependents_auto(tensor=value,
                                         candidates=independend_params)
        if params:
            if name is None:
                name = "composite_autoparam_" + str(get_auto_number())
            value = ComposedParameter(name, tensor=value)
        else:
            if name is None:
                name = "FIXED_autoparam_" + str(get_auto_number())
            value = Parameter(name, value=value, floating=floating)

    # value.floating = False
    return value
Example #3
0
def poly_complex(*args, real_x=False):
    """Complex polynomial with the last arg being x.

    Args:
        *args (tf.Tensor or equ.): Coefficients of the polynomial
        real_x (bool): If True, x is assumed to be real.

    Returns:
        tf.Tensor:
    """

    args = list(args)
    x = args.pop()
    if real_x is not None:
        pow_func = tf.pow
    else:
        pow_func = ztf.nth_pow
    return tf.add_n(
        [coef * ztf.to_complex(pow_func(x, p)) for p, coef in enumerate(args)])