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 test_assert_positive_definite_fails_for_non_positive_definite(self):
     spectrum = math_ops.cast([[6. + 0j, 4 + 0j], [2j, 3. + 0j]],
                              dtypes.complex64)
     operator = linalg.LinearOperatorCirculant2D(spectrum)
     with self.cached_session():
         with self.assertRaisesOpError("Not positive definite"):
             self.evaluate(operator.assert_positive_definite())
 def test_assert_non_singular_fails_for_singular_operator(self):
     spectrum = math_ops.cast([[0 + 0j, 4 + 0j], [2j + 2, 3. + 0j]],
                              dtypes.complex64)
     operator = linalg.LinearOperatorCirculant2D(spectrum)
     with self.cached_session():
         with self.assertRaisesOpError("Singular operator"):
             self.evaluate(operator.assert_non_singular())
示例#4
0
    def _operator_and_matrix(self, build_info, dtype, use_placeholder):
        shape = build_info.shape
        # For this test class, we are creating Hermitian spectrums.
        # We also want the spectrum to have eigenvalues bounded away from zero.
        #
        # pre_spectrum is bounded away from zero.
        pre_spectrum = linear_operator_test_util.random_uniform(
            shape=self._shape_to_spectrum_shape(shape), minval=1., maxval=2.)
        pre_spectrum_c = _to_complex(pre_spectrum)

        # Real{IFFT[pre_spectrum]}
        #  = IFFT[EvenPartOf[pre_spectrum]]
        # is the IFFT of something that is also bounded away from zero.
        # Therefore, FFT[pre_h] would be a well-conditioned spectrum.
        pre_h = math_ops.ifft2d(pre_spectrum_c)

        # A spectrum is Hermitian iff it is the DFT of a real convolution kernel.
        # So we will make spectrum = FFT[h], for real valued h.
        h = math_ops.real(pre_h)
        h_c = _to_complex(h)

        spectrum = math_ops.fft2d(h_c)

        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):
        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 test_assert_positive_definite_does_not_fail_when_pos_def(self):
     spectrum = math_ops.cast([[6. + 0j, 4 + 0j], [2j + 2, 3. + 0j]],
                              dtypes.complex64)
     operator = linalg.LinearOperatorCirculant2D(spectrum)
     with self.cached_session():
         self.evaluate(
             operator.assert_positive_definite())  # Should not fail
示例#7
0
  def operator_and_matrix(self,
                          shape_info,
                          dtype,
                          use_placeholder,
                          ensure_self_adjoint_and_pd=False):
    shape = shape_info.shape
    # For this test class, we are creating Hermitian spectrums.
    # We also want the spectrum to have eigenvalues bounded away from zero.
    #
    # pre_spectrum is bounded away from zero.
    pre_spectrum = linear_operator_test_util.random_uniform(
        shape=self._shape_to_spectrum_shape(shape),
        dtype=dtype,
        minval=1.,
        maxval=2.)
    pre_spectrum_c = _to_complex(pre_spectrum)

    # Real{IFFT[pre_spectrum]}
    #  = IFFT[EvenPartOf[pre_spectrum]]
    # is the IFFT of something that is also bounded away from zero.
    # Therefore, FFT[pre_h] would be a well-conditioned spectrum.
    pre_h = fft_ops.ifft2d(pre_spectrum_c)

    # A spectrum is Hermitian iff it is the DFT of a real convolution kernel.
    # So we will make spectrum = FFT[h], for real valued h.
    h = math_ops.real(pre_h)
    h_c = _to_complex(h)

    spectrum = fft_ops.fft2d(h_c)

    lin_op_spectrum = spectrum

    if use_placeholder:
      lin_op_spectrum = array_ops.placeholder_with_default(spectrum, shape=None)

    operator = linalg.LinearOperatorCirculant2D(
        lin_op_spectrum,
        is_positive_definite=True if ensure_self_adjoint_and_pd else None,
        is_self_adjoint=True if ensure_self_adjoint_and_pd else None,
        input_output_dtype=dtype)

    self.assertEqual(
        operator.parameters,
        {
            "input_output_dtype": dtype,
            "is_non_singular": None,
            "is_positive_definite": (
                True if ensure_self_adjoint_and_pd else None),
            "is_self_adjoint": (
                True if ensure_self_adjoint_and_pd else None),
            "is_square": True,
            "name": "LinearOperatorCirculant2D",
            "spectrum": lin_op_spectrum,
        })

    mat = self._spectrum_to_circulant_2d(spectrum, shape, dtype=dtype)

    return operator, mat
    def test_real_spectrum_gives_self_adjoint_operator(self):
        with self.cached_session():
            # This is a real and hermitian spectrum.
            spectrum = linear_operator_test_util.random_normal(
                shape=(3, 3), dtype=dtypes.float32)
            operator = linalg.LinearOperatorCirculant2D(spectrum)

            matrix_tensor = operator.to_dense()
            self.assertEqual(matrix_tensor.dtype, dtypes.complex64)
            matrix_h = linalg.adjoint(matrix_tensor)
            matrix, matrix_h = self.evaluate([matrix_tensor, matrix_h])
            self.assertAllClose(matrix, matrix_h, atol=0)
    def _operator_and_mat_and_feed_dict(self, build_info, dtype,
                                        use_placeholder):
        shape = build_info.shape
        # For this test class, we are creating Hermitian spectrums.
        # We also want the spectrum to have eigenvalues bounded away from zero.
        #
        # pre_spectrum is bounded away from zero.
        pre_spectrum = linear_operator_test_util.random_uniform(
            shape=self._shape_to_spectrum_shape(shape), minval=1., maxval=2.)
        pre_spectrum_c = _to_complex(pre_spectrum)

        # Real{IFFT[pre_spectrum]}
        #  = IFFT[EvenPartOf[pre_spectrum]]
        # is the IFFT of something that is also bounded away from zero.
        # Therefore, FFT[pre_h] would be a well-conditioned spectrum.
        pre_h = math_ops.ifft2d(pre_spectrum_c)

        # A spectrum is Hermitian iff it is the DFT of a real convolution kernel.
        # So we will make spectrum = FFT[h], for real valued h.
        h = math_ops.real(pre_h)
        h_c = _to_complex(h)

        spectrum = math_ops.fft2d(h_c)

        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 test_real_spectrum_gives_self_adjoint_operator(self):
        with self.test_session() as sess:
            # This is a real and hermitian spectrum.
            spectrum = linear_operator_test_util.random_normal(
                shape=(3, 3), dtype=dtypes.float32)
            operator = linalg.LinearOperatorCirculant2D(spectrum)

            matrix_tensor = operator.to_dense()
            self.assertEqual(matrix_tensor.dtype,
                             linear_operator_circulant._DTYPE_COMPLEX)
            matrix_h = linalg.adjoint(matrix_tensor)
            matrix, matrix_h = sess.run([matrix_tensor, matrix_h])
            self.assertAllClose(matrix, matrix_h, atol=0)
    def operator_and_matrix(self,
                            shape_info,
                            dtype,
                            use_placeholder,
                            ensure_self_adjoint_and_pd=False):
        shape = shape_info.shape
        spectrum = _spectrum_for_symmetric_circulant(
            spectrum_shape=self._shape_to_spectrum_shape(shape),
            d=2,
            ensure_self_adjoint_and_pd=ensure_self_adjoint_and_pd,
            dtype=dtype)

        lin_op_spectrum = spectrum

        if use_placeholder:
            lin_op_spectrum = array_ops.placeholder_with_default(spectrum,
                                                                 shape=None)

        operator = linalg.LinearOperatorCirculant2D(
            lin_op_spectrum,
            is_positive_definite=True if ensure_self_adjoint_and_pd else None,
            is_self_adjoint=True if ensure_self_adjoint_and_pd else None,
            input_output_dtype=dtype)

        self.assertEqual(
            operator.parameters, {
                "input_output_dtype":
                dtype,
                "is_non_singular":
                None,
                "is_positive_definite":
                (True if ensure_self_adjoint_and_pd else None),
                "is_self_adjoint":
                (True if ensure_self_adjoint_and_pd else None),
                "is_square":
                True,
                "name":
                "LinearOperatorCirculant2D",
                "spectrum":
                lin_op_spectrum,
            })

        mat = self._spectrum_to_circulant_2d(spectrum, shape, dtype=dtype)

        return operator, mat
示例#12
0
  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 test_invalid_rank_raises(self):
     spectrum = array_ops.constant(np.float32(rng.rand(2)))
     with self.assertRaisesRegexp(ValueError,
                                  "must have at least 2 dimensions"):
         linalg.LinearOperatorCirculant2D(spectrum)
 def test_real_spectrum_auto_sets_is_self_adjoint_to_true(self):
     spectrum = [[1., 2.], [3., 4]]
     operator = linalg.LinearOperatorCirculant2D(spectrum)
     self.assertTrue(operator.is_self_adjoint)
 def test_real_spectrum_and_not_self_adjoint_hint_raises(self):
     spectrum = [[1., 2.], [3., 4]]
     with self.assertRaisesRegexp(ValueError, "real.*always.*self-adjoint"):
         linalg.LinearOperatorCirculant2D(spectrum, is_self_adjoint=False)
 def test_assert_non_singular_does_not_fail_for_non_singular_operator(self):
     spectrum = math_ops.cast([[-3j, 4], [2j + 2, 3.]], dtypes.complex64)
     operator = linalg.LinearOperatorCirculant2D(spectrum)
     with self.cached_session():
         operator.assert_non_singular().run()  # Should not fail
 def test_assert_non_singular_fails_for_singular_operator(self):
     spectrum = math_ops.cast([[0, 4], [2j + 2, 3.]], dtypes.complex64)
     operator = linalg.LinearOperatorCirculant2D(spectrum)
     with self.test_session():
         with self.assertRaisesOpError("Singular operator"):
             operator.assert_non_singular().run()
示例#18
0
 def test_invalid_dtype_raises(self):
     spectrum = array_ops.constant(rng.rand(2, 2, 2))
     with self.assertRaisesRegexp(TypeError, "must have dtype"):
         linalg.LinearOperatorCirculant2D(spectrum)
 def test_assert_positive_definite_does_not_fail_when_pos_def(self):
     spectrum = math_ops.cast([[6., 4], [2j + 2, 3.]], dtypes.complex64)
     operator = linalg.LinearOperatorCirculant2D(spectrum)
     with self.test_session():
         operator.assert_positive_definite().run()  # Should not fail
 def test_tape_safe(self):
     spectrum = variables_module.Variable(
         math_ops.cast([[1. + 0j, 1. + 0j], [1. + 1j, 2. + 2j]],
                       dtypes.complex64))
     operator = linalg.LinearOperatorCirculant2D(spectrum)
     self.check_tape_safe(operator)