Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate the Variable class #262

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
68 changes: 43 additions & 25 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
from math import isnan, isinf

import uncertainties.core as uncert_core
from uncertainties.core import ufloat, AffineScalarFunc
from uncertainties.core import ufloat, UFloat, AffineScalarFunc


def get_single_uatom(num_with_uncertainty: UFloat):
error_components = num_with_uncertainty.error_components
if len(error_components) > 1:
raise ValueError("UFloat has more than one error component.")
return next(iter(error_components.keys()))


def power_all_cases(op):
Expand Down Expand Up @@ -32,60 +39,69 @@ def power_all_cases(op):
non_int_larger_than_one = ufloat(3.1, 0.01)
positive_smaller_than_one = ufloat(0.3, 0.01)

negative_uatom = get_single_uatom(negative)
positive_uatom = get_single_uatom(positive)
positive2_uatom = get_single_uatom(positive2)
integer_uatom = get_single_uatom(integer)
one_uatom = get_single_uatom(one)
zero_uatom = get_single_uatom(zero)
zero2_uatom = get_single_uatom(zero2)
non_int_larger_than_one_uatom = get_single_uatom(non_int_larger_than_one)
positive_smaller_than_one_uatom = get_single_uatom(positive_smaller_than_one)
## negative**integer

result = op(negative, integer)
assert not isnan(result.derivatives[negative])
assert isnan(result.derivatives[integer])
assert not isnan(result.error_components[negative_uatom])
assert integer_uatom not in result.error_components

# Limit cases:
result = op(negative, one)
assert result.derivatives[negative] == 1
assert isnan(result.derivatives[one])
assert result.uncertainty.expanded_dict[negative_uatom] == negative.std_dev
assert isnan(result.uncertainty.expanded_dict[one_uatom])

result = op(negative, zero)
assert result.derivatives[negative] == 0
assert isnan(result.derivatives[zero])
assert result.uncertainty.expanded_dict[negative_uatom] == 0
assert isnan(result.uncertainty.expanded_dict[zero_uatom])

## negative**non-integer

## zero**...

result = op(zero, non_int_larger_than_one)
assert isnan(result.derivatives[zero])
assert result.derivatives[non_int_larger_than_one] == 0
assert isnan(result.uncertainty.expanded_dict[zero_uatom])
assert result.uncertainty.expanded_dict[non_int_larger_than_one_uatom] == 0

# Special cases:
result = op(zero, one)
assert result.derivatives[zero] == 1
assert result.derivatives[one] == 0
assert result.uncertainty.expanded_dict[zero_uatom] == zero.std_dev
assert result.uncertainty.expanded_dict[one_uatom] == 0

result = op(zero, 2 * one)
assert result.derivatives[zero] == 0
assert result.derivatives[one] == 0
assert result.uncertainty.expanded_dict[zero_uatom] == 0
assert result.uncertainty.expanded_dict[one_uatom] == 0

result = op(zero, positive_smaller_than_one)
assert isnan(result.derivatives[zero])
assert result.derivatives[positive_smaller_than_one] == 0
assert isnan(result.uncertainty.expanded_dict[zero_uatom])
assert result.uncertainty.expanded_dict[positive_smaller_than_one_uatom] == 0

result = op(zero, zero2)
assert result.derivatives[zero] == 0
assert isnan(result.derivatives[zero2])
assert result.uncertainty.expanded_dict[zero_uatom] == 0
assert isnan(result.uncertainty.expanded_dict[zero2_uatom])

## positive**...: this is a quite regular case where the value and
## the derivatives are all defined.
## the uncertainty.expanded_dict are all defined.

result = op(positive, positive2)
assert not isnan(result.derivatives[positive])
assert not isnan(result.derivatives[positive2])
assert not isnan(result.uncertainty.expanded_dict[positive_uatom])
assert not isnan(result.uncertainty.expanded_dict[positive2_uatom])

result = op(positive, zero)
assert result.derivatives[positive] == 0
assert not isnan(result.derivatives[zero])
assert result.uncertainty.expanded_dict[positive_uatom] == 0
assert not isnan(result.uncertainty.expanded_dict[zero_uatom])

result = op(positive, negative)
assert not isnan(result.derivatives[positive])
assert not isnan(result.derivatives[negative])
assert not isnan(result.uncertainty.expanded_dict[positive_uatom])
assert not isnan(result.uncertainty.expanded_dict[negative_uatom])


def power_special_cases(op):
Expand Down Expand Up @@ -282,7 +298,9 @@ def compare_derivatives(func, numerical_derivatives, num_args_list=None):
if arg_num in integer_arg_nums:
args.append(random.choice(range(-10, 10)))
else:
args.append(uncert_core.Variable(random.random() * 4 - 2, 0))
args.append(
uncert_core.AffineScalarFunc(random.random() * 4 - 2, 0)
)

# 'args', but as scalar values:
args_scalar = [uncert_core.nominal_value(v) for v in args]
Expand Down
4 changes: 0 additions & 4 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ def test_repr():
x = ufloat(3.14159265358979, 0)
assert repr(x) == "3.14159265358979+/-0"

# Tagging:
x = ufloat(3, 1, "length")
assert repr(x) == "< length = 3.0+/-1.0 >"


# The way NaN is formatted with F, E and G depends on the version of Python (NAN for
# Python 2.5+ at least):
Expand Down
Loading