2018-12-12 08:44:07 -05:00
|
|
|
# Copyright 2018 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.
|
|
|
|
|
2018-12-11 12:44:02 -05:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
|
Add Cholesky, QR, and Triangular solve implementations.
* Adds lax.{cholesky,triangular_solve,qr}. Adds a JVP for Cholesky.
* Adds a transpose rule for add_p, needed by the Cholesky JVP.
* Adds np.linalg.{cholesky,qr,dot,matmul,trace}.
* Adds scipy.linalg.{cholesky,qr,solve_triangular,tril,triu}.
Pair programmed with mattjj.
2018-12-13 13:03:08 -05:00
|
|
|
|
2018-12-11 12:44:02 -05:00
|
|
|
import numpy as onp
|
|
|
|
|
Add Cholesky, QR, and Triangular solve implementations.
* Adds lax.{cholesky,triangular_solve,qr}. Adds a JVP for Cholesky.
* Adds a transpose rule for add_p, needed by the Cholesky JVP.
* Adds np.linalg.{cholesky,qr,dot,matmul,trace}.
* Adds scipy.linalg.{cholesky,qr,solve_triangular,tril,triu}.
Pair programmed with mattjj.
2018-12-13 13:03:08 -05:00
|
|
|
from .. import lax_linalg
|
2018-12-11 12:44:02 -05:00
|
|
|
from .lax_numpy import _not_implemented
|
Add Cholesky, QR, and Triangular solve implementations.
* Adds lax.{cholesky,triangular_solve,qr}. Adds a JVP for Cholesky.
* Adds a transpose rule for add_p, needed by the Cholesky JVP.
* Adds np.linalg.{cholesky,qr,dot,matmul,trace}.
* Adds scipy.linalg.{cholesky,qr,solve_triangular,tril,triu}.
Pair programmed with mattjj.
2018-12-13 13:03:08 -05:00
|
|
|
from .lax_numpy import _wraps
|
|
|
|
from . import lax_numpy as np
|
|
|
|
from ..util import get_module_functions
|
|
|
|
|
|
|
|
|
|
|
|
dot = np.dot
|
|
|
|
matmul = np.matmul
|
|
|
|
trace = np.trace
|
|
|
|
|
2018-12-13 19:28:05 -05:00
|
|
|
_T = lambda x: np.swapaxes(x, -1, -2)
|
Add Cholesky, QR, and Triangular solve implementations.
* Adds lax.{cholesky,triangular_solve,qr}. Adds a JVP for Cholesky.
* Adds a transpose rule for add_p, needed by the Cholesky JVP.
* Adds np.linalg.{cholesky,qr,dot,matmul,trace}.
* Adds scipy.linalg.{cholesky,qr,solve_triangular,tril,triu}.
Pair programmed with mattjj.
2018-12-13 13:03:08 -05:00
|
|
|
|
|
|
|
@_wraps(onp.linalg.cholesky)
|
|
|
|
def cholesky(a):
|
|
|
|
return lax_linalg.cholesky(a)
|
|
|
|
|
2018-12-13 19:28:05 -05:00
|
|
|
@_wraps(onp.linalg.inv)
|
|
|
|
def inv(a):
|
|
|
|
if np.ndim(a) < 2 or a.shape[-1] != a.shape[-2]:
|
2018-12-13 21:02:24 -05:00
|
|
|
raise ValueError("Argument to inv must have shape [..., n, n], got {}."
|
|
|
|
.format(np.shape(a)))
|
|
|
|
q, r = qr(a)
|
2018-12-13 19:28:05 -05:00
|
|
|
return lax_linalg.triangular_solve(r, _T(q), lower=False, left_side=True)
|
|
|
|
|
Add Cholesky, QR, and Triangular solve implementations.
* Adds lax.{cholesky,triangular_solve,qr}. Adds a JVP for Cholesky.
* Adds a transpose rule for add_p, needed by the Cholesky JVP.
* Adds np.linalg.{cholesky,qr,dot,matmul,trace}.
* Adds scipy.linalg.{cholesky,qr,solve_triangular,tril,triu}.
Pair programmed with mattjj.
2018-12-13 13:03:08 -05:00
|
|
|
|
|
|
|
@_wraps(onp.linalg.qr)
|
|
|
|
def qr(a, mode="reduced"):
|
|
|
|
if mode in ("reduced", "r", "full"):
|
|
|
|
full_matrices = False
|
|
|
|
elif mode == "complete":
|
|
|
|
full_matrices = True
|
|
|
|
else:
|
|
|
|
raise ValueError("Unsupported QR decomposition mode '{}'".format(mode))
|
|
|
|
q, r = lax_linalg.qr(a, full_matrices)
|
|
|
|
if mode == "r":
|
|
|
|
return r
|
|
|
|
return q, r
|
2018-12-11 12:44:02 -05:00
|
|
|
|
2018-12-13 11:52:41 -08:00
|
|
|
for func in get_module_functions(onp.linalg):
|
2018-12-11 12:44:02 -05:00
|
|
|
if func.__name__ not in globals():
|
|
|
|
globals()[func.__name__] = _not_implemented(func)
|