# Path: jaxopt/_src/tree_util.py
# def tree_add_scalar_mul(tree_x, scalar, tree_y):
#   """Compute tree_x + scalar * tree_y."""
#   return tree_multimap(lambda x, y: x + scalar * y, tree_x, tree_y)


from typing import Any
from typing import Callable
from typing import Optional
from jaxopt._src.tree_util import tree_add_scalar_mul
import jax
import jax.numpy as jnp
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Linear system solvers."""




def _materialize_array(matvec, shape, dtype=None):
  """Materializes the matrix A used in matvec(x) = Ax."""
  x = jnp.zeros(shape, dtype)
  return jax.jacfwd(matvec)(x)


def _make_ridge_matvec(matvec: Callable, ridge: float = 0.0):
  def ridge_matvec(v: Any) -> Any:

# Path: jaxopt/_src/tree_util.py
# def tree_add_scalar_mul(tree_x, scalar, tree_y):
#   """Compute tree_x + scalar * tree_y."""
#   return tree_multimap(lambda x, y: x + scalar * y, tree_x, tree_y)


# Path: jaxopt/_src/linear_solve.py
from typing import Any
from typing import Callable
from typing import Optional
from jaxopt._src.tree_util import tree_add_scalar_mul
import jax
import jax.numpy as jnp

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Linear system solvers."""




def _materialize_array(matvec, shape, dtype=None):
  """Materializes the matrix A used in matvec(x) = Ax."""
  x = jnp.zeros(shape, dtype)
  return jax.jacfwd(matvec)(x)


def _make_ridge_matvec(matvec: Callable, ridge: float = 0.0):
  def ridge_matvec(v: Any) -> Any:

    return tree_add_scalar_mul(matvec(v), ridge, v)