Var

All fittable values in Nexus are of variable type Var. Although all fit variables can be initialized by a float or int they are converted to a Var object internally.

Warning

Please note that special care has to be taken when assigning Var objects as described below.

class nexus.Var(value=0.0, min=-inf, max=inf, fit=False, id='', equality=None)
Copy()

Copy the Var object.

FitRange(min, max)

Sets the minimum and maximum values for fitting.

Parameters:
  • min (float) – Minimum value.

  • max (float) – Maximum value.

PrintPointer()

Print the pointer of the Var object.

class nexus.FitVariables(*args)

List of Var objects.

Note

When the Var is defined in another object, you can assign a new Var.value like this.

# all of the following assignments are equivalent
site = nx.Hyperfine(magnetic_field = 33) # internally converted to a nexus.Var

site = nx.Hyperfine(magnetic_field = nx.Var(33))

site = nx.Hyperfine(magnetic_field = nx.Var(value = 33))

print(site.magnetic_field)

# assign the Var.value directly
site.magnetic_field.value = 5

print(site.magnetic_field)

# assign the Var via another nexus class (here the hyperfine object).
# stays a Var object as it is the attribute of the class
# RECOMMENDED OPTION
site.magnetic_field = 7

print(site.magnetic_field)

The last option is the recommended way because its easier and clearer syntax.

Warning

Do not try to change a Var.value by setting the Var to a float or int when it is initialized as a python object directly.

my_var = nx.Var(7.4)
print(my_var)

# Don't do this to assign Var.value
my_var = 5  # here, my_var will be of type int
print(my_var)

my_var = nx.Var(7.4)
# Either use
my_var.value = 5
print(my_var)
# or
my_var >> 28
print(my_var)

To define a Var directly is a rare case. It is useful in parameter dependent fitting across different objects.

Examples

Define a Var and change it in various ways.

my_var = nx.Var(value = 5, min = 0, max = 10, fit = True, id = "my variable")

my_var.FitRange(0, 7)

print(my_var)
a = nx.Var(2, id = "my variable")

# reassign a.value to 5
a.value = 5

# use of *= operator
a *= 5  # = 25

# use of / and - operators
a = a / 3 - 1  # = 7.333333

# use of +
a + 2/3  # = 8

print(a)

# operator >> to set a.value to 2/3
a >> 2/3