Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jolyonb committed Jul 3, 2019
1 parent 7e91e7a commit e50781d
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions mitxgraders/matrixsampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class ArraySamplingSet(VariableSamplingSet):
True
"""

schema_config = Schema({
Required('shape'): is_shape_specification(min_dim=1),
Required('norm', default=[1, 5]): NumberRange(),
Expand All @@ -109,12 +108,12 @@ def __init__(self, config=None, **kwargs):

def gen_sample(self):
"""
Generates a random matrix of shape and norm determined by config.
After generation, subclasses can modify the result by shadowing the
apply_symmetry function.
Generates a random matrix of shape and norm determined by config. After
generation, the apply_symmetry and normalize functions are applied to the result.
These functions may be shadowed by a subclass.
After apply_symmetry is called, the result is normalized.
If apply_symmetry or normalize raise the Retry exception, a new sample is
generated, and the procedure starts anew.
"""
# Loop until a good sample is found
loops = 0
Expand All @@ -137,8 +136,8 @@ def gen_sample(self):

# Convert the array to a MathArray and return it
return MathArray(array)
except Retry: # pragma: no cover
pass
except Retry:
continue

raise ValueError('Unable to construct sample for {}'
.format(self.__name__)) # pragma: no cover
Expand Down Expand Up @@ -545,6 +544,13 @@ def __init__(self, config=None, **kwargs):
if self.config['symmetry'] in ['hermitian', 'antihermitian']:
self.config['complex'] = True

# A couple of cases that we can't handle:
if self.config['determinant'] == 0:
if self.config['symmetry'] == 'antisymmetric':
raise ConfigError("Unable to generate zero determinant antisymmetric matrices")
if self.config['traceless']:
raise ConfigError("Unable to generate zero determinant traceless matrices")

def apply_symmetry(self, array):
"""
Applies the required symmetries to the array
Expand Down Expand Up @@ -612,7 +618,7 @@ def make_det_one(self, array):
raise Retry()

# If we get to here, det is real
det = np.real(det)
det = np.real(det) # Get rid of numerical error
if det > 0:
# This is the easy case: Just scale the determinant
return array / np.power(det, 1/self.config['dimension'])
Expand All @@ -630,12 +636,6 @@ def make_det_zero(self, array):
# This should never happen due to floating pointness. But just in case...
return array # pragma: no cover

# A couple of cases that we just can't handle:
if self.config['symmetry'] == 'antisymmetric':
raise ConfigError("Unable to generate zero determinant antisymmetric matrices")
if self.config['traceless']:
raise ConfigError("Unable to generate zero determinant traceless matrices")

# Pick a random number!
index = np.random.randint(self.config['dimension'])

Expand Down

0 comments on commit e50781d

Please sign in to comment.