def build_operator_and_matrix( self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False, diagonals_format='sequence'): shape = list(build_info.shape) # Ensure that diagonal has large enough values. If we generate a # self adjoint PD matrix, then the diagonal will be dominant guaranteeing # positive definitess. diag = linear_operator_test_util.random_sign_uniform( shape[:-1], minval=4., maxval=6., dtype=dtype) # We'll truncate these depending on the format subdiag = linear_operator_test_util.random_sign_uniform( shape[:-1], minval=1., maxval=2., dtype=dtype) if ensure_self_adjoint_and_pd: # Abs on complex64 will result in a float32, so we cast back up. diag = math_ops.cast(math_ops.abs(diag), dtype=dtype) # The first element of subdiag is ignored. We'll add a dummy element # to superdiag to pad it. superdiag = math_ops.conj(subdiag) superdiag = manip_ops.roll(superdiag, shift=-1, axis=-1) else: superdiag = linear_operator_test_util.random_sign_uniform( shape[:-1], minval=1., maxval=2., dtype=dtype) matrix_diagonals = array_ops.stack( [superdiag, diag, subdiag], axis=-2) matrix = gen_array_ops.matrix_diag_v3( matrix_diagonals, k=(-1, 1), num_rows=-1, num_cols=-1, align='LEFT_RIGHT', padding_value=0.) if diagonals_format == 'sequence': diagonals = [superdiag, diag, subdiag] elif diagonals_format == 'compact': diagonals = array_ops.stack([superdiag, diag, subdiag], axis=-2) elif diagonals_format == 'matrix': diagonals = matrix lin_op_diagonals = diagonals if use_placeholder: if diagonals_format == 'sequence': lin_op_diagonals = [array_ops.placeholder_with_default( d, shape=None) for d in lin_op_diagonals] else: lin_op_diagonals = array_ops.placeholder_with_default( lin_op_diagonals, shape=None) operator = linalg_lib.LinearOperatorTridiag( diagonals=lin_op_diagonals, diagonals_format=diagonals_format, is_self_adjoint=True if ensure_self_adjoint_and_pd else None, is_positive_definite=True if ensure_self_adjoint_and_pd else None) return operator, matrix
def _operator_and_matrix( self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = build_info.shape # For this test class, we are creating real spectrums. # We also want the spectrum to have eigenvalues bounded away from zero. # # spectrum is bounded away from zero. spectrum = linear_operator_test_util.random_sign_uniform( shape=self._shape_to_spectrum_shape(shape), minval=1., maxval=2.) if ensure_self_adjoint_and_pd: spectrum = math_ops.abs(spectrum) # If dtype is complex, cast spectrum to complex. The imaginary part will be # zero, so the operator will still be self-adjoint. spectrum = math_ops.cast(spectrum, dtype) lin_op_spectrum = spectrum if use_placeholder: lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant( lin_op_spectrum, is_self_adjoint=True, is_positive_definite=True if ensure_self_adjoint_and_pd else None, input_output_dtype=dtype) mat = self._spectrum_to_circulant_1d(spectrum, shape, dtype=dtype) return operator, mat
def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder): shape = list(shape) assert shape[-1] == shape[-2] batch_shape = shape[:-2] num_rows = shape[-1] # Uniform values that are at least length 1 from the origin. Allows the # operator to be well conditioned. # Shape batch_shape multiplier = linear_operator_test_util.random_sign_uniform( shape=batch_shape, minval=1., maxval=2., dtype=dtype) operator = linalg_lib.LinearOperatorScaledIdentity(num_rows, multiplier) # Nothing to feed since LinearOperatorScaledIdentity takes no Tensor args. if use_placeholder: multiplier_ph = array_ops.placeholder(dtype=dtype) multiplier = multiplier.eval() operator = linalg_lib.LinearOperatorScaledIdentity( num_rows, multiplier_ph) feed_dict = {multiplier_ph: multiplier} else: feed_dict = None multiplier_matrix = array_ops.expand_dims( array_ops.expand_dims(multiplier, -1), -1) mat = multiplier_matrix * linalg_ops.eye( num_rows, batch_shape=batch_shape, dtype=dtype) return operator, mat, feed_dict
def _operator_and_matrix(self, build_info, dtype, use_placeholder): shape = list(build_info.shape) assert shape[-1] == shape[-2] batch_shape = shape[:-2] num_rows = shape[-1] # Uniform values that are at least length 1 from the origin. Allows the # operator to be well conditioned. # Shape batch_shape multiplier = linear_operator_test_util.random_sign_uniform( shape=batch_shape, minval=1., maxval=2., dtype=dtype) # Nothing to feed since LinearOperatorScaledIdentity takes no Tensor args. lin_op_multiplier = multiplier if use_placeholder: lin_op_multiplier = array_ops.placeholder_with_default( multiplier, shape=None) operator = linalg_lib.LinearOperatorScaledIdentity( num_rows, lin_op_multiplier) multiplier_matrix = array_ops.expand_dims( array_ops.expand_dims(multiplier, -1), -1) matrix = multiplier_matrix * linalg_ops.eye( num_rows, batch_shape=batch_shape, dtype=dtype) return operator, matrix
def operator_and_matrix(self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) diag = linear_operator_test_util.random_sign_uniform(shape[:-1], minval=1., maxval=2., dtype=dtype) if ensure_self_adjoint_and_pd: # Abs on complex64 will result in a float32, so we cast back up. diag = math_ops.cast(math_ops.abs(diag), dtype=dtype) lin_op_diag = diag if use_placeholder: lin_op_diag = array_ops.placeholder_with_default(diag, shape=None) operator = linalg.LinearOperatorDiag( lin_op_diag, is_self_adjoint=True if ensure_self_adjoint_and_pd else None, is_positive_definite=True if ensure_self_adjoint_and_pd else None) matrix = array_ops.matrix_diag(diag) return operator, matrix
def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder): shape = list(shape) assert shape[-1] == shape[-2] batch_shape = shape[:-2] num_rows = shape[-1] # Uniform values that are at least length 1 from the origin. Allows the # operator to be well conditioned. # Shape batch_shape multiplier = linear_operator_test_util.random_sign_uniform( shape=batch_shape, minval=1., maxval=2., dtype=dtype) operator = linalg_lib.LinearOperatorScaledIdentity( num_rows, multiplier) # Nothing to feed since LinearOperatorScaledIdentity takes no Tensor args. if use_placeholder: multiplier_ph = array_ops.placeholder(dtype=dtype) multiplier = multiplier.eval() operator = linalg_lib.LinearOperatorScaledIdentity( num_rows, multiplier_ph) feed_dict = {multiplier_ph: multiplier} else: feed_dict = None multiplier_matrix = array_ops.expand_dims( array_ops.expand_dims(multiplier, -1), -1) mat = multiplier_matrix * linalg_ops.eye( num_rows, batch_shape=batch_shape, dtype=dtype) return operator, mat, feed_dict
def _operator_and_matrix(self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = build_info.shape # For this test class, we are creating real spectrums. # We also want the spectrum to have eigenvalues bounded away from zero. # # spectrum is bounded away from zero. spectrum = linear_operator_test_util.random_sign_uniform( shape=self._shape_to_spectrum_shape(shape), minval=1., maxval=2.) if ensure_self_adjoint_and_pd: spectrum = math_ops.abs(spectrum) # If dtype is complex, cast spectrum to complex. The imaginary part will be # zero, so the operator will still be self-adjoint. spectrum = math_ops.cast(spectrum, dtype) lin_op_spectrum = spectrum if use_placeholder: lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant( lin_op_spectrum, is_self_adjoint=True, is_positive_definite=True if ensure_self_adjoint_and_pd else None, input_output_dtype=dtype) mat = self._spectrum_to_circulant_1d(spectrum, shape, dtype=dtype) return operator, mat
def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder): shape = build_info.shape # For this test class, we are creating real spectrums. # We also want the spectrum to have eigenvalues bounded away from zero. # # spectrum is bounded away from zero. spectrum = linear_operator_test_util.random_sign_uniform( shape=self._shape_to_spectrum_shape(shape), minval=1., maxval=2.) # If dtype is complex, cast spectrum to complex. The imaginary part will be # zero, so the operator will still be self-adjoint. spectrum = math_ops.cast(spectrum, dtype) if use_placeholder: spectrum_ph = array_ops.placeholder(dtypes.complex64) # Evaluate here because (i) you cannot feed a tensor, and (ii) # it is random and we want the same value used for both mat and feed_dict. spectrum = spectrum.eval() operator = linalg.LinearOperatorCirculant(spectrum_ph, is_self_adjoint=True, input_output_dtype=dtype) feed_dict = {spectrum_ph: spectrum} else: operator = linalg.LinearOperatorCirculant(spectrum, is_self_adjoint=True, input_output_dtype=dtype) feed_dict = None mat = self._spectrum_to_circulant_1d(spectrum, shape, dtype=dtype) return operator, mat, feed_dict
def _operator_and_matrix(self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): del ensure_self_adjoint_and_pd shape = build_info.shape # Will be well conditioned enough to get accurate solves. spectrum = linear_operator_test_util.random_sign_uniform( shape=self._shape_to_spectrum_shape(shape), dtype=dtype, minval=1., maxval=2.) lin_op_spectrum = spectrum if use_placeholder: lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant2D(lin_op_spectrum, input_output_dtype=dtype) mat = self._spectrum_to_circulant_2d(spectrum, shape, dtype=dtype) return operator, mat
def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder): shape = build_info.shape # For this test class, we are creating real spectrums. # We also want the spectrum to have eigenvalues bounded away from zero. # # spectrum is bounded away from zero. spectrum = linear_operator_test_util.random_sign_uniform( shape=self._shape_to_spectrum_shape(shape), minval=1., maxval=2.) # If dtype is complex, cast spectrum to complex. The imaginary part will be # zero, so the operator will still be self-adjoint. spectrum = math_ops.cast(spectrum, dtype) if use_placeholder: spectrum_ph = array_ops.placeholder(dtypes.complex64) # Evaluate here because (i) you cannot feed a tensor, and (ii) # it is random and we want the same value used for both mat and feed_dict. spectrum = spectrum.eval() operator = linalg.LinearOperatorCirculant( spectrum_ph, is_self_adjoint=True, input_output_dtype=dtype) feed_dict = {spectrum_ph: spectrum} else: operator = linalg.LinearOperatorCirculant( spectrum, is_self_adjoint=True, input_output_dtype=dtype) feed_dict = None mat = self._spectrum_to_circulant_1d(spectrum, shape, dtype=dtype) return operator, mat, feed_dict
def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder): shape = build_info.shape # Will be well conditioned enough to get accurate solves. spectrum = linear_operator_test_util.random_sign_uniform( shape=self._shape_to_spectrum_shape(shape), dtype=dtype, minval=1., maxval=2.) if use_placeholder: spectrum_ph = array_ops.placeholder(dtypes.complex64) # Evaluate here because (i) you cannot feed a tensor, and (ii) # it is random and we want the same value used for both mat and feed_dict. spectrum = spectrum.eval() operator = linalg.LinearOperatorCirculant2D( spectrum_ph, input_output_dtype=dtype) feed_dict = {spectrum_ph: spectrum} else: operator = linalg.LinearOperatorCirculant2D( spectrum, input_output_dtype=dtype) feed_dict = None mat = self._spectrum_to_circulant_2d(spectrum, shape, dtype=dtype) return operator, mat, feed_dict
def operator_and_matrix( self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) reflection_axis = linear_operator_test_util.random_sign_uniform( shape[:-1], minval=1., maxval=2., dtype=dtype) # Make sure unit norm. reflection_axis = reflection_axis / linalg_ops.norm( reflection_axis, axis=-1, keepdims=True) lin_op_reflection_axis = reflection_axis if use_placeholder: lin_op_reflection_axis = array_ops.placeholder_with_default( reflection_axis, shape=None) operator = householder.LinearOperatorHouseholder(lin_op_reflection_axis) mat = reflection_axis[..., array_ops.newaxis] matrix = -2 * linear_operator_util.matmul_with_broadcast( mat, mat, adjoint_b=True) matrix = array_ops.matrix_set_diag( matrix, 1. + array_ops.matrix_diag_part(matrix)) return operator, matrix
def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder): shape = build_info.shape # Will be well conditioned enough to get accurate solves. spectrum = linear_operator_test_util.random_sign_uniform( shape=self._shape_to_spectrum_shape(shape), dtype=dtype, minval=1., maxval=2.) if use_placeholder: spectrum_ph = array_ops.placeholder(dtypes.complex64) # Evaluate here because (i) you cannot feed a tensor, and (ii) # it is random and we want the same value used for both mat and feed_dict. spectrum = spectrum.eval() operator = linalg.LinearOperatorCirculant2D( spectrum_ph, input_output_dtype=dtype) feed_dict = {spectrum_ph: spectrum} else: operator = linalg.LinearOperatorCirculant2D( spectrum, input_output_dtype=dtype) feed_dict = None mat = self._spectrum_to_circulant_2d(spectrum, shape, dtype=dtype) return operator, mat, feed_dict
def _operator_and_matrix(self, build_info, dtype, use_placeholder): shape = list(build_info.shape) diag = linear_operator_test_util.random_sign_uniform( shape[:-1], minval=1., maxval=2., dtype=dtype) lin_op_diag = diag if use_placeholder: lin_op_diag = array_ops.placeholder_with_default(diag, shape=None) operator = linalg.LinearOperatorDiag(lin_op_diag) matrix = array_ops.matrix_diag(diag) return operator, matrix
def _operator_and_matrix(self, build_info, dtype, use_placeholder): shape = list(build_info.shape) diag = linear_operator_test_util.random_sign_uniform( shape[:-1], minval=1., maxval=2., dtype=dtype) lin_op_diag = diag if use_placeholder: lin_op_diag = array_ops.placeholder_with_default(diag, shape=None) operator = linalg.LinearOperatorDiag(lin_op_diag) matrix = array_ops.matrix_diag(diag) return operator, matrix
def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder): diag = linear_operator_test_util.random_sign_uniform( shape[:-1], minval=1., maxval=2., dtype=dtype) if use_placeholder: diag_ph = array_ops.placeholder(dtype=dtype) # Evaluate the diag here because (i) you cannot feed a tensor, and (ii) # diag is random and we want the same value used for both mat and # feed_dict. diag = diag.eval() operator = linalg.LinearOperatorDiag(diag_ph) feed_dict = {diag_ph: diag} else: operator = linalg.LinearOperatorDiag(diag) feed_dict = None mat = array_ops.matrix_diag(diag) return operator, mat, feed_dict
def _operator_and_matrix(self, build_info, dtype, use_placeholder): shape = build_info.shape # Will be well conditioned enough to get accurate solves. spectrum = linear_operator_test_util.random_sign_uniform( shape=self._shape_to_spectrum_shape(shape), dtype=dtype, minval=1., maxval=2.) lin_op_spectrum = spectrum if use_placeholder: lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant2D( lin_op_spectrum, input_output_dtype=dtype) mat = self._spectrum_to_circulant_2d(spectrum, shape, dtype=dtype) return operator, mat
def _operator_and_matrix(self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) assert shape[-1] == shape[-2] batch_shape = shape[:-2] num_rows = shape[-1] # Uniform values that are at least length 1 from the origin. Allows the # operator to be well conditioned. # Shape batch_shape multiplier = linear_operator_test_util.random_sign_uniform( shape=batch_shape, minval=1., maxval=2., dtype=dtype) if ensure_self_adjoint_and_pd: # Abs on complex64 will result in a float32, so we cast back up. multiplier = math_ops.cast(math_ops.abs(multiplier), dtype=dtype) # Nothing to feed since LinearOperatorScaledIdentity takes no Tensor args. lin_op_multiplier = multiplier if use_placeholder: lin_op_multiplier = array_ops.placeholder_with_default(multiplier, shape=None) operator = linalg_lib.LinearOperatorScaledIdentity( num_rows, lin_op_multiplier, is_self_adjoint=True if ensure_self_adjoint_and_pd else None, is_positive_definite=True if ensure_self_adjoint_and_pd else None) multiplier_matrix = array_ops.expand_dims( array_ops.expand_dims(multiplier, -1), -1) matrix = multiplier_matrix * linalg_ops.eye( num_rows, batch_shape=batch_shape, dtype=dtype) return operator, matrix
def _operator_and_matrix( self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) assert shape[-1] == shape[-2] batch_shape = shape[:-2] num_rows = shape[-1] # Uniform values that are at least length 1 from the origin. Allows the # operator to be well conditioned. # Shape batch_shape multiplier = linear_operator_test_util.random_sign_uniform( shape=batch_shape, minval=1., maxval=2., dtype=dtype) if ensure_self_adjoint_and_pd: # Abs on complex64 will result in a float32, so we cast back up. multiplier = math_ops.cast(math_ops.abs(multiplier), dtype=dtype) # Nothing to feed since LinearOperatorScaledIdentity takes no Tensor args. lin_op_multiplier = multiplier if use_placeholder: lin_op_multiplier = array_ops.placeholder_with_default( multiplier, shape=None) operator = linalg_lib.LinearOperatorScaledIdentity( num_rows, lin_op_multiplier, is_self_adjoint=True if ensure_self_adjoint_and_pd else None, is_positive_definite=True if ensure_self_adjoint_and_pd else None) multiplier_matrix = array_ops.expand_dims( array_ops.expand_dims(multiplier, -1), -1) matrix = multiplier_matrix * linalg_ops.eye( num_rows, batch_shape=batch_shape, dtype=dtype) return operator, matrix
def operator_and_matrix(self, shape_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): del ensure_self_adjoint_and_pd shape = shape_info.shape # Will be well conditioned enough to get accurate solves. spectrum = linear_operator_test_util.random_sign_uniform( shape=self._shape_to_spectrum_shape(shape), dtype=dtype, minval=1., maxval=2.) lin_op_spectrum = spectrum if use_placeholder: lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None) operator = linalg.LinearOperatorCirculant2D( lin_op_spectrum, input_output_dtype=dtype) self.assertEqual( operator.parameters, { "input_output_dtype": dtype, "is_non_singular": None, "is_positive_definite": None, "is_self_adjoint": None, "is_square": True, "name": "LinearOperatorCirculant2D", "spectrum": lin_op_spectrum, } ) mat = self._spectrum_to_circulant_2d(spectrum, shape, dtype=dtype) return operator, mat
def operator_and_matrix( self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) diag = linear_operator_test_util.random_sign_uniform( shape[:-1], minval=1., maxval=2., dtype=dtype) if ensure_self_adjoint_and_pd: # Abs on complex64 will result in a float32, so we cast back up. diag = math_ops.cast(math_ops.abs(diag), dtype=dtype) lin_op_diag = diag if use_placeholder: lin_op_diag = array_ops.placeholder_with_default(diag, shape=None) operator = linalg.LinearOperatorDiag( lin_op_diag, is_self_adjoint=True if ensure_self_adjoint_and_pd else None, is_positive_definite=True if ensure_self_adjoint_and_pd else None) matrix = array_ops.matrix_diag(diag) return operator, matrix
def operator_and_matrix( self, build_info, dtype, use_placeholder, ensure_self_adjoint_and_pd=False): shape = list(build_info.shape) reflection_axis = linear_operator_test_util.random_sign_uniform( shape[:-1], minval=1., maxval=2., dtype=dtype) # Make sure unit norm. reflection_axis = reflection_axis / linalg_ops.norm( reflection_axis, axis=-1, keepdims=True) lin_op_reflection_axis = reflection_axis if use_placeholder: lin_op_reflection_axis = array_ops.placeholder_with_default( reflection_axis, shape=None) operator = householder.LinearOperatorHouseholder(lin_op_reflection_axis) mat = reflection_axis[..., array_ops.newaxis] matrix = -2 * math_ops.matmul(mat, mat, adjoint_b=True) matrix = array_ops.matrix_set_diag( matrix, 1. + array_ops.matrix_diag_part(matrix)) return operator, matrix