def _operator_and_matrix(self, build_info, dtype, use_placeholder):
    shape = list(build_info.shape)

    # Either 1 or 2 matrices, depending.
    num_operators = rng.randint(low=1, high=3)
    matrices = [
        linear_operator_test_util.random_positive_definite_matrix(
            shape, dtype, force_well_conditioned=True)
        for _ in range(num_operators)
    ]

    lin_op_matrices = matrices

    if use_placeholder:
      lin_op_matrices = [
          array_ops.placeholder_with_default(
              matrix, shape=None) for matrix in matrices]

    operator = linalg.LinearOperatorComposition(
        [linalg.LinearOperatorFullMatrix(l) for l in lin_op_matrices],
        is_square=True)

    matmul_order_list = list(reversed(matrices))
    mat = matmul_order_list[0]
    for other_mat in matmul_order_list[1:]:
      mat = math_ops.matmul(other_mat, mat)

    return operator, mat
  def operator_and_matrix(self,
                          build_info,
                          dtype,
                          use_placeholder,
                          ensure_self_adjoint_and_pd=False):
    shape = list(build_info.shape)

    if ensure_self_adjoint_and_pd:
      matrix = linear_operator_test_util.random_positive_definite_matrix(
          shape, dtype, force_well_conditioned=True)
    else:
      matrix = linear_operator_test_util.random_tril_matrix(
          shape, dtype, force_well_conditioned=True, remove_upper=True)

    lin_op_matrix = matrix

    if use_placeholder:
      lin_op_matrix = array_ops.placeholder_with_default(matrix, shape=None)

    if ensure_self_adjoint_and_pd:
      operator = LinearOperatorAdjoint(
          linalg.LinearOperatorFullMatrix(
              lin_op_matrix, is_positive_definite=True, is_self_adjoint=True))
    else:
      operator = LinearOperatorAdjoint(
          linalg.LinearOperatorLowerTriangular(lin_op_matrix))

    return operator, linalg.adjoint(matrix)
Example #3
0
    def operator_and_matrix(self, build_info, dtype, use_placeholder):
        shape = list(build_info.shape)

        # Either 1 or 2 matrices, depending.
        num_operators = rng.randint(low=1, high=3)
        matrices = [
            linear_operator_test_util.random_positive_definite_matrix(
                shape, dtype, force_well_conditioned=True)
            for _ in range(num_operators)
        ]

        lin_op_matrices = matrices

        if use_placeholder:
            lin_op_matrices = [
                array_ops.placeholder_with_default(matrix, shape=None)
                for matrix in matrices
            ]

        operator = linalg.LinearOperatorComposition(
            [linalg.LinearOperatorFullMatrix(l) for l in lin_op_matrices],
            is_square=True)

        matmul_order_list = list(reversed(matrices))
        mat = matmul_order_list[0]
        for other_mat in matmul_order_list[1:]:
            mat = math_ops.matmul(other_mat, mat)

        return operator, mat
Example #4
0
    def operator_and_matrix(self,
                            build_info,
                            dtype,
                            use_placeholder,
                            ensure_self_adjoint_and_pd=False):

        # Matrix is always symmetric and positive definite in this class.
        del ensure_self_adjoint_and_pd

        shape = list(build_info.shape)

        matrix = linear_operator_test_util.random_positive_definite_matrix(
            shape, dtype, force_well_conditioned=True)

        lin_op_matrix = matrix

        if use_placeholder:
            lin_op_matrix = array_ops.placeholder_with_default(matrix,
                                                               shape=None)

        operator = linalg.LinearOperatorFullMatrix(lin_op_matrix,
                                                   is_square=True,
                                                   is_self_adjoint=True,
                                                   is_positive_definite=True)

        return operator, matrix
  def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder):
    shape = list(build_info.shape)

    matrix = linear_operator_test_util.random_positive_definite_matrix(
        shape, dtype, force_well_conditioned=True)

    if use_placeholder:
      matrix_ph = array_ops.placeholder(dtype=dtype)
      # Evaluate here because (i) you cannot feed a tensor, and (ii)
      # values are random and we want the same value used for both mat and
      # feed_dict.
      matrix = matrix.eval()
      # is_square is auto-set because of self_adjoint/pd.
      operator = linalg.LinearOperatorFullMatrix(
          matrix_ph, is_self_adjoint=True, is_positive_definite=True)
      feed_dict = {matrix_ph: matrix}
    else:
      operator = linalg.LinearOperatorFullMatrix(
          matrix, is_self_adjoint=True, is_positive_definite=True)
      feed_dict = None

    # Convert back to Tensor.  Needed if use_placeholder, since then we have
    # already evaluated matrix to a numpy array.
    mat = ops.convert_to_tensor(matrix)

    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)

        if ensure_self_adjoint_and_pd:
            matrix = linear_operator_test_util.random_positive_definite_matrix(
                shape, dtype, force_well_conditioned=True)
        else:
            matrix = linear_operator_test_util.random_tril_matrix(
                shape, dtype, force_well_conditioned=True, remove_upper=True)

        lin_op_matrix = matrix

        if use_placeholder:
            lin_op_matrix = array_ops.placeholder_with_default(matrix,
                                                               shape=None)

        if ensure_self_adjoint_and_pd:
            operator = LinearOperatorInversion(
                linalg.LinearOperatorFullMatrix(lin_op_matrix,
                                                is_positive_definite=True,
                                                is_self_adjoint=True))
        else:
            operator = LinearOperatorInversion(
                linalg.LinearOperatorLowerTriangular(lin_op_matrix))

        return operator, linalg.inv(matrix)
  def _operator_and_matrix(self, build_info, dtype, use_placeholder):
    shape = list(build_info.shape)
    expected_factors = build_info.__dict__["factors"]
    matrices = [
        linear_operator_test_util.random_positive_definite_matrix(
            block_shape, dtype, force_well_conditioned=True)
        for block_shape in expected_factors
    ]

    lin_op_matrices = matrices

    if use_placeholder:
      lin_op_matrices = [
          array_ops.placeholder_with_default(m, shape=None) for m in matrices]

    operator = kronecker.LinearOperatorKronecker(
        [linalg.LinearOperatorFullMatrix(
            l, is_square=True) for l in lin_op_matrices])

    matrices = linear_operator_util.broadcast_matrix_batch_dims(matrices)

    kronecker_dense = _kronecker_dense(matrices)

    if not use_placeholder:
      kronecker_dense.set_shape(shape)

    return operator, kronecker_dense
    def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder):
        shape = list(shape)

        matrix = linear_operator_test_util.random_positive_definite_matrix(
            shape, dtype)

        if use_placeholder:
            matrix_ph = array_ops.placeholder(dtype=dtype)
            # Evaluate here because (i) you cannot feed a tensor, and (ii)
            # values are random and we want the same value used for both mat and
            # feed_dict.
            matrix = matrix.eval()
            operator = linalg.LinearOperatorFullMatrix(matrix_ph,
                                                       is_square=True)
            feed_dict = {matrix_ph: matrix}
        else:
            # is_square should be auto-detected here.
            operator = linalg.LinearOperatorFullMatrix(matrix)
            feed_dict = None

        # Convert back to Tensor.  Needed if use_placeholder, since then we have
        # already evaluated matrix to a numpy array.
        mat = ops.convert_to_tensor(matrix)

        return operator, mat, feed_dict
    def _operator_and_mat_and_feed_dict(self, build_info, dtype,
                                        use_placeholder):
        shape = list(build_info.shape)

        matrix = linear_operator_test_util.random_positive_definite_matrix(
            shape, dtype, force_well_conditioned=True)

        if use_placeholder:
            matrix_ph = array_ops.placeholder(dtype=dtype)
            # Evaluate here because (i) you cannot feed a tensor, and (ii)
            # values are random and we want the same value used for both mat and
            # feed_dict.
            matrix = matrix.eval()
            # is_square is auto-set because of self_adjoint/pd.
            operator = linalg.LinearOperatorFullMatrix(
                matrix_ph, is_self_adjoint=True, is_positive_definite=True)
            feed_dict = {matrix_ph: matrix}
        else:
            operator = linalg.LinearOperatorFullMatrix(
                matrix, is_self_adjoint=True, is_positive_definite=True)
            feed_dict = None

        # Convert back to Tensor.  Needed if use_placeholder, since then we have
        # already evaluated matrix to a numpy array.
        mat = ops.convert_to_tensor(matrix)

        return operator, mat, feed_dict
Example #10
0
    def operator_and_matrix(self,
                            build_info,
                            dtype,
                            use_placeholder,
                            ensure_self_adjoint_and_pd=False):
        shape = list(build_info.shape)

        # Either 1 or 2 matrices, depending.
        num_operators = rng.randint(low=1, high=3)
        if ensure_self_adjoint_and_pd:
            # The random PD matrices are also symmetric. Here we are computing
            # A @ A ... @ A. Since A is symmetric and PD, so are any powers of it.
            matrices = [
                linear_operator_test_util.random_positive_definite_matrix(
                    shape, dtype, force_well_conditioned=True)
            ] * num_operators
        else:
            matrices = [
                linear_operator_test_util.random_positive_definite_matrix(
                    shape, dtype, force_well_conditioned=True)
                for _ in range(num_operators)
            ]

        lin_op_matrices = matrices

        if use_placeholder:
            lin_op_matrices = [
                array_ops.placeholder_with_default(matrix, shape=None)
                for matrix in matrices
            ]

        operator = linalg.LinearOperatorComposition(
            [linalg.LinearOperatorFullMatrix(l) for l in lin_op_matrices],
            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)

        matmul_order_list = list(reversed(matrices))
        mat = matmul_order_list[0]
        for other_mat in matmul_order_list[1:]:
            mat = math_ops.matmul(other_mat, mat)

        return operator, mat
  def operator_and_matrix(
      self, shape_info, dtype, use_placeholder,
      ensure_self_adjoint_and_pd=False):

    expected_blocks = (
        shape_info.__dict__["blocks"] if "blocks" in shape_info.__dict__
        else [[list(shape_info.shape)]])

    matrices = []
    for i, row_shapes in enumerate(expected_blocks):
      row = []
      for j, block_shape in enumerate(row_shapes):
        if i == j:  # operator is on the diagonal
          row.append(
              linear_operator_test_util.random_positive_definite_matrix(
                  block_shape, dtype, force_well_conditioned=True))
        else:
          row.append(
              linear_operator_test_util.random_normal(block_shape, dtype=dtype))
      matrices.append(row)

    lin_op_matrices = matrices

    if use_placeholder:
      lin_op_matrices = [[
          array_ops.placeholder_with_default(
              matrix, shape=None) for matrix in row] for row in matrices]

    operator = block_lower_triangular.LinearOperatorBlockLowerTriangular(
        [[linalg.LinearOperatorFullMatrix(  # pylint:disable=g-complex-comprehension
            l,
            is_square=True,
            is_self_adjoint=True if ensure_self_adjoint_and_pd else None,
            is_positive_definite=True if ensure_self_adjoint_and_pd else None)
          for l in row] for row in lin_op_matrices])

    # Should be auto-set.
    self.assertTrue(operator.is_square)

    # Broadcast the shapes.
    expected_shape = list(shape_info.shape)
    broadcasted_matrices = linear_operator_util.broadcast_matrix_batch_dims(
        [op for row in matrices for op in row])  # pylint: disable=g-complex-comprehension
    matrices = [broadcasted_matrices[i * (i + 1) // 2:(i + 1) * (i + 2) // 2]
                for i in range(len(matrices))]

    block_lower_triangular_dense = _block_lower_triangular_dense(
        expected_shape, matrices)

    if not use_placeholder:
      block_lower_triangular_dense.set_shape(expected_shape)

    return operator, block_lower_triangular_dense
  def _operator_and_matrix(self, build_info, dtype, use_placeholder):
    shape = list(build_info.shape)

    matrix = linear_operator_test_util.random_positive_definite_matrix(
        shape, dtype)

    lin_op_matrix = matrix

    if use_placeholder:
      lin_op_matrix = array_ops.placeholder_with_default(matrix, shape=None)

    operator = linalg.LinearOperatorFullMatrix(lin_op_matrix, is_square=True)

    return operator, matrix
  def test_tape_safe(self):
    matrices = []
    for _ in range(4):
      matrices.append(variables_module.Variable(
          linear_operator_test_util.random_positive_definite_matrix(
              [2, 2], dtype=dtypes.float32, force_well_conditioned=True)))

    operator = block_diag.LinearOperatorBlockDiag(
        [linalg.LinearOperatorFullMatrix(
            matrix, is_self_adjoint=True,
            is_positive_definite=True) for matrix in matrices],
        is_self_adjoint=True,
        is_positive_definite=True,
    )
    self.check_tape_safe(operator)
Example #14
0
    def _operator_and_matrix(self, build_info, dtype, use_placeholder):
        shape = list(build_info.shape)

        matrix = linear_operator_test_util.random_positive_definite_matrix(
            shape, dtype)

        lin_op_matrix = matrix

        if use_placeholder:
            lin_op_matrix = array_ops.placeholder_with_default(matrix,
                                                               shape=None)

        operator = linalg.LinearOperatorFullMatrix(lin_op_matrix,
                                                   is_square=True)

        return operator, matrix
    def _operator_and_matrix(self,
                             build_info,
                             dtype,
                             use_placeholder,
                             ensure_self_adjoint_and_pd=False):
        shape = list(build_info.shape)
        expected_blocks = (build_info.__dict__["blocks"]
                           if "blocks" in build_info.__dict__ else [shape])
        matrices = [
            linear_operator_test_util.random_positive_definite_matrix(
                block_shape, dtype, force_well_conditioned=True)
            for block_shape in expected_blocks
        ]

        lin_op_matrices = matrices

        if use_placeholder:
            lin_op_matrices = [
                array_ops.placeholder_with_default(matrix, shape=None)
                for matrix in matrices
            ]

        operator = block_diag.LinearOperatorBlockDiag([
            linalg.LinearOperatorFullMatrix(
                l,
                is_square=True,
                is_self_adjoint=True if ensure_self_adjoint_and_pd else None,
                is_positive_definite=True
                if ensure_self_adjoint_and_pd else None)
            for l in lin_op_matrices
        ])

        # Should be auto-set.
        self.assertTrue(operator.is_square)

        # Broadcast the shapes.
        expected_shape = list(build_info.shape)

        matrices = linear_operator_util.broadcast_matrix_batch_dims(matrices)

        block_diag_dense = _block_diag_dense(expected_shape, matrices)

        if not use_placeholder:
            block_diag_dense.set_shape(
                expected_shape[:-2] + [expected_shape[-1], expected_shape[-1]])

        return operator, block_diag_dense
    def _operator_and_mat_and_feed_dict(self, build_info, dtype,
                                        use_placeholder):
        shape = list(build_info.shape)
        expected_blocks = (build_info.__dict__["blocks"]
                           if "blocks" in build_info.__dict__ else [shape])
        matrices = [
            linear_operator_test_util.random_positive_definite_matrix(
                block_shape, dtype, force_well_conditioned=True)
            for block_shape in expected_blocks
        ]

        if use_placeholder:
            matrices_ph = [
                array_ops.placeholder(dtype=dtype) for _ in expected_blocks
            ]
            # Evaluate here because (i) you cannot feed a tensor, and (ii)
            # values are random and we want the same value used for both mat and
            # feed_dict.
            matrices = self.evaluate(matrices)
            operator = block_diag.LinearOperatorBlockDiag([
                linalg.LinearOperatorFullMatrix(m_ph, is_square=True)
                for m_ph in matrices_ph
            ],
                                                          is_square=True)
            feed_dict = {m_ph: m for (m_ph, m) in zip(matrices_ph, matrices)}
        else:
            operator = block_diag.LinearOperatorBlockDiag([
                linalg.LinearOperatorFullMatrix(m, is_square=True)
                for m in matrices
            ])
            feed_dict = None
            # Should be auto-set.
            self.assertTrue(operator.is_square)

        # Broadcast the shapes.
        expected_shape = list(build_info.shape)

        matrices = linear_operator_util.broadcast_matrix_batch_dims(matrices)

        block_diag_dense = _block_diag_dense(expected_shape, matrices)

        if not use_placeholder:
            block_diag_dense.set_shape(
                expected_shape[:-2] + [expected_shape[-1], expected_shape[-1]])

        return operator, block_diag_dense, feed_dict
  def test_convert_variables_to_tensors(self):
    matrices = []
    for _ in range(3):
      matrices.append(variables_module.Variable(
          linear_operator_test_util.random_positive_definite_matrix(
              [3, 3], dtype=dtypes.float32, force_well_conditioned=True)))

    operator = block_diag.LinearOperatorBlockDiag(
        [linalg.LinearOperatorFullMatrix(
            matrix, is_self_adjoint=True,
            is_positive_definite=True) for matrix in matrices],
        is_self_adjoint=True,
        is_positive_definite=True,
    )
    with self.cached_session() as sess:
      sess.run([x.initializer for x in operator.variables])
      self.check_convert_variables_to_tensors(operator)
  def _operator_and_mat_and_feed_dict(self, build_info, dtype, use_placeholder):
    shape = list(build_info.shape)
    expected_blocks = (
        build_info.__dict__["blocks"] if "blocks" in build_info.__dict__
        else [shape])
    matrices = [
        linear_operator_test_util.random_positive_definite_matrix(
            block_shape, dtype, force_well_conditioned=True)
        for block_shape in expected_blocks
    ]

    if use_placeholder:
      matrices_ph = [
          array_ops.placeholder(dtype=dtype) for _ in expected_blocks
      ]
      # Evaluate here because (i) you cannot feed a tensor, and (ii)
      # values are random and we want the same value used for both mat and
      # feed_dict.
      matrices = self.evaluate(matrices)
      operator = block_diag.LinearOperatorBlockDiag(
          [linalg.LinearOperatorFullMatrix(
              m_ph, is_square=True) for m_ph in matrices_ph],
          is_square=True)
      feed_dict = {m_ph: m for (m_ph, m) in zip(matrices_ph, matrices)}
    else:
      operator = block_diag.LinearOperatorBlockDiag(
          [linalg.LinearOperatorFullMatrix(
              m, is_square=True) for m in matrices])
      feed_dict = None
      # Should be auto-set.
      self.assertTrue(operator.is_square)

    # Broadcast the shapes.
    expected_shape = list(build_info.shape)

    matrices = linear_operator_util.broadcast_matrix_batch_dims(matrices)

    block_diag_dense = _block_diag_dense(expected_shape, matrices)

    if not use_placeholder:
      block_diag_dense.set_shape(
          expected_shape[:-2] + [expected_shape[-1], expected_shape[-1]])

    return operator, block_diag_dense, feed_dict
  def operator_and_matrix(
      self, shape_info, dtype, use_placeholder,
      ensure_self_adjoint_and_pd=False):
    shape = list(shape_info.shape)
    expected_blocks = (
        shape_info.__dict__["blocks"] if "blocks" in shape_info.__dict__
        else [shape])
    matrices = [
        linear_operator_test_util.random_positive_definite_matrix(
            block_shape, dtype, force_well_conditioned=True)
        for block_shape in expected_blocks
    ]

    lin_op_matrices = matrices

    if use_placeholder:
      lin_op_matrices = [
          array_ops.placeholder_with_default(
              matrix, shape=None) for matrix in matrices]

    operator = block_diag.LinearOperatorBlockDiag(
        [linalg.LinearOperatorFullMatrix(
            l,
            is_square=True,
            is_self_adjoint=True if ensure_self_adjoint_and_pd else None,
            is_positive_definite=True if ensure_self_adjoint_and_pd else None)
         for l in lin_op_matrices])

    # Should be auto-set.
    self.assertTrue(operator.is_square)

    # Broadcast the shapes.
    expected_shape = list(shape_info.shape)

    matrices = linear_operator_util.broadcast_matrix_batch_dims(matrices)

    block_diag_dense = _block_diag_dense(expected_shape, matrices)

    if not use_placeholder:
      block_diag_dense.set_shape(
          expected_shape[:-2] + [expected_shape[-1], expected_shape[-1]])

    return operator, block_diag_dense
Example #20
0
    def _operator_and_mat_and_feed_dict(self, build_info, dtype,
                                        use_placeholder):
        sess = ops.get_default_session()
        shape = list(build_info.shape)

        # Either 1 or 2 matrices, depending.
        num_operators = rng.randint(low=1, high=3)
        matrices = [
            linear_operator_test_util.random_positive_definite_matrix(
                shape, dtype, force_well_conditioned=True)
            for _ in range(num_operators)
        ]

        if use_placeholder:
            matrices_ph = [
                array_ops.placeholder(dtype=dtype)
                for _ in range(num_operators)
            ]
            # Evaluate here because (i) you cannot feed a tensor, and (ii)
            # values are random and we want the same value used for both mat and
            # feed_dict.
            matrices = sess.run(matrices)
            operator = linalg.LinearOperatorComposition([
                linalg.LinearOperatorFullMatrix(m_ph) for m_ph in matrices_ph
            ],
                                                        is_square=True)
            feed_dict = {m_ph: m for (m_ph, m) in zip(matrices_ph, matrices)}
        else:
            operator = linalg.LinearOperatorComposition(
                [linalg.LinearOperatorFullMatrix(m) for m in matrices])
            feed_dict = None
            # Should be auto-set.
            self.assertTrue(operator.is_square)

        # Convert back to Tensor.  Needed if use_placeholder, since then we have
        # already evaluated each matrix to a numpy array.
        matmul_order_list = list(reversed(matrices))
        mat = ops.convert_to_tensor(matmul_order_list[0])
        for other_mat in matmul_order_list[1:]:
            mat = math_ops.matmul(other_mat, mat)

        return operator, mat, feed_dict
Example #21
0
  def _operator_and_matrix(
      self, build_info, dtype, use_placeholder,
      ensure_self_adjoint_and_pd=False):
    shape = list(build_info.shape)

    matrix = linear_operator_test_util.random_positive_definite_matrix(
        shape, dtype)

    lin_op_matrix = matrix

    if use_placeholder:
      lin_op_matrix = array_ops.placeholder_with_default(matrix, shape=None)

    # Set the hints to none to test non-symmetric PD code paths.
    operator = linalg.LinearOperatorFullMatrix(
        lin_op_matrix,
        is_square=True,
        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 = list(build_info.shape)

    matrix = linear_operator_test_util.random_positive_definite_matrix(
        shape, dtype)

    lin_op_matrix = matrix

    if use_placeholder:
      lin_op_matrix = array_ops.placeholder_with_default(matrix, shape=None)

    # Set the hints to none to test non-symmetric PD code paths.
    operator = linalg.LinearOperatorFullMatrix(
        lin_op_matrix,
        is_square=True,
        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
Example #23
0
    def operator_and_matrix(self,
                            build_info,
                            dtype,
                            use_placeholder,
                            ensure_self_adjoint_and_pd=False):
        # Kronecker products constructed below will be from symmetric
        # positive-definite matrices.
        del ensure_self_adjoint_and_pd
        shape = list(build_info.shape)
        expected_factors = build_info.__dict__["factors"]
        matrices = [
            linear_operator_test_util.random_positive_definite_matrix(
                block_shape, dtype, force_well_conditioned=True)
            for block_shape in expected_factors
        ]

        lin_op_matrices = matrices

        if use_placeholder:
            lin_op_matrices = [
                array_ops.placeholder_with_default(m, shape=None)
                for m in matrices
            ]

        operator = kronecker.LinearOperatorKronecker([
            linalg.LinearOperatorFullMatrix(l,
                                            is_square=True,
                                            is_self_adjoint=True,
                                            is_positive_definite=True)
            for l in lin_op_matrices
        ])

        matrices = linear_operator_util.broadcast_matrix_batch_dims(matrices)

        kronecker_dense = _kronecker_dense(matrices)

        if not use_placeholder:
            kronecker_dense.set_shape(shape)

        return operator, kronecker_dense
  def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder):
    sess = ops.get_default_session()
    shape = list(shape)

    # Either 1 or 2 matrices, depending.
    num_operators = rng.randint(low=1, high=3)
    matrices = [
        linear_operator_test_util.random_positive_definite_matrix(
            shape, dtype, force_well_conditioned=True)
        for _ in range(num_operators)
    ]

    if use_placeholder:
      matrices_ph = [
          array_ops.placeholder(dtype=dtype) for _ in range(num_operators)
      ]
      # Evaluate here because (i) you cannot feed a tensor, and (ii)
      # values are random and we want the same value used for both mat and
      # feed_dict.
      matrices = sess.run(matrices)
      operator = linalg.LinearOperatorComposition(
          [linalg.LinearOperatorFullMatrix(m_ph) for m_ph in matrices_ph],
          is_square=True)
      feed_dict = {m_ph: m for (m_ph, m) in zip(matrices_ph, matrices)}
    else:
      operator = linalg.LinearOperatorComposition(
          [linalg.LinearOperatorFullMatrix(m) for m in matrices])
      feed_dict = None
      # Should be auto-set.
      self.assertTrue(operator.is_square)

    # Convert back to Tensor.  Needed if use_placeholder, since then we have
    # already evaluated each matrix to a numpy array.
    matmul_order_list = list(reversed(matrices))
    mat = ops.convert_to_tensor(matmul_order_list[0])
    for other_mat in matmul_order_list[1:]:
      mat = math_ops.matmul(other_mat, mat)

    return operator, mat, feed_dict
  def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder):
    shape = list(shape)

    matrix = linear_operator_test_util.random_positive_definite_matrix(shape,
                                                                       dtype)

    if use_placeholder:
      matrix_ph = array_ops.placeholder(dtype=dtype)
      # Evaluate here because (i) you cannot feed a tensor, and (ii)
      # values are random and we want the same value used for both mat and
      # feed_dict.
      matrix = matrix.eval()
      operator = linalg.LinearOperatorFullMatrix(matrix_ph, is_square=True)
      feed_dict = {matrix_ph: matrix}
    else:
      # is_square should be auto-detected here.
      operator = linalg.LinearOperatorFullMatrix(matrix)
      feed_dict = None

    # Convert back to Tensor.  Needed if use_placeholder, since then we have
    # already evaluated matrix to a numpy array.
    mat = ops.convert_to_tensor(matrix)

    return operator, mat, feed_dict
  def _operator_and_matrix(
      self, build_info, dtype, use_placeholder,
      ensure_self_adjoint_and_pd=False):
    # Kronecker products constructed below will be from symmetric
    # positive-definite matrices.
    del ensure_self_adjoint_and_pd
    shape = list(build_info.shape)
    expected_factors = build_info.__dict__["factors"]
    matrices = [
        linear_operator_test_util.random_positive_definite_matrix(
            block_shape, dtype, force_well_conditioned=True)
        for block_shape in expected_factors
    ]

    lin_op_matrices = matrices

    if use_placeholder:
      lin_op_matrices = [
          array_ops.placeholder_with_default(m, shape=None) for m in matrices]

    operator = kronecker.LinearOperatorKronecker(
        [linalg.LinearOperatorFullMatrix(
            l,
            is_square=True,
            is_self_adjoint=True,
            is_positive_definite=True)
         for l in lin_op_matrices])

    matrices = linear_operator_util.broadcast_matrix_batch_dims(matrices)

    kronecker_dense = _kronecker_dense(matrices)

    if not use_placeholder:
      kronecker_dense.set_shape(shape)

    return operator, kronecker_dense
  def _operator_and_matrix(
      self, build_info, dtype, use_placeholder,
      ensure_self_adjoint_and_pd=False):

    # Matrix is always symmetric and positive definite in this class.
    del ensure_self_adjoint_and_pd

    shape = list(build_info.shape)

    matrix = linear_operator_test_util.random_positive_definite_matrix(
        shape, dtype, force_well_conditioned=True)

    lin_op_matrix = matrix

    if use_placeholder:
      lin_op_matrix = array_ops.placeholder_with_default(matrix, shape=None)

    operator = linalg.LinearOperatorFullMatrix(
        lin_op_matrix,
        is_square=True,
        is_self_adjoint=True,
        is_positive_definite=True)

    return operator, matrix